You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by nc...@apache.org on 2017/07/13 19:14:19 UTC

[01/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Repository: ambari
Updated Branches:
  refs/heads/branch-feature-AMBARI-21450 88d93b924 -> 48f7fb22a


http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java
deleted file mode 100644
index 2590d1c..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog250Test.java
+++ /dev/null
@@ -1,2131 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
-import com.google.gson.Gson;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-import com.google.gson.JsonPrimitive;
-import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.Provider;
-
-import junit.framework.Assert;
-import junit.framework.AssertionFailedError;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.actionmanager.ActionManager;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.controller.AmbariManagementControllerImpl;
-import org.apache.ambari.server.controller.KerberosHelper;
-import org.apache.ambari.server.controller.MaintenanceStateHelper;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
-import org.apache.ambari.server.orm.dao.ArtifactDAO;
-import org.apache.ambari.server.orm.dao.PermissionDAO;
-import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
-import org.apache.ambari.server.orm.dao.RoleAuthorizationDAO;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.ArtifactEntity;
-import org.apache.ambari.server.orm.entities.PermissionEntity;
-import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
-import org.apache.ambari.server.orm.entities.RoleAuthorizationEntity;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.Service;
-import org.apache.ambari.server.state.kerberos.AbstractKerberosDescriptorContainer;
-import org.apache.ambari.server.state.kerberos.KerberosComponentDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptorFactory;
-import org.apache.ambari.server.state.kerberos.KerberosIdentityDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosKeytabDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosPrincipalDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosServiceDescriptor;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.easymock.Capture;
-import org.easymock.CaptureType;
-import org.easymock.EasyMock;
-import org.easymock.EasyMockRunner;
-import org.easymock.EasyMockSupport;
-import org.easymock.Mock;
-import org.easymock.MockType;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import javax.persistence.EntityManager;
-
-import java.io.File;
-import java.lang.reflect.Method;
-import java.net.URL;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-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 static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.anyString;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.createMockBuilder;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.newCapture;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
-import static org.junit.Assert.assertTrue;
-
-/**
- * {@link UpgradeCatalog250} unit tests.
- */
-@RunWith(EasyMockRunner.class)
-public class UpgradeCatalog250Test {
-
-  //  private Injector injector;
-  @Mock(type = MockType.STRICT)
-  private Provider<EntityManager> entityManagerProvider;
-
-  @Mock(type = MockType.NICE)
-  private EntityManager entityManager;
-
-  @Mock(type = MockType.NICE)
-  private DBAccessor dbAccessor;
-
-  @Mock(type = MockType.NICE)
-  private Configuration configuration;
-
-  @Mock(type = MockType.NICE)
-  private Connection connection;
-
-  @Mock(type = MockType.NICE)
-  private Statement statement;
-
-  @Mock(type = MockType.NICE)
-  private ResultSet resultSet;
-
-  @Mock(type = MockType.NICE)
-  private OsFamily osFamily;
-
-  @Mock(type = MockType.NICE)
-  private KerberosHelper kerberosHelper;
-
-  @Mock(type = MockType.NICE)
-  private ActionManager actionManager;
-
-  @Mock(type = MockType.NICE)
-  private Config config;
-
-  @Mock(type = MockType.STRICT)
-  private Service service;
-
-  @Mock(type = MockType.NICE)
-  private Clusters clusters;
-
-  @Mock(type = MockType.NICE)
-  private Cluster cluster;
-
-  @Mock(type = MockType.NICE)
-  private Injector injector;
-
-  private UpgradeCatalog250 upgradeCatalog250;
-
-  @Before
-  public void init() {
-    reset(entityManagerProvider, injector);
-
-    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
-
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(kerberosHelper).anyTimes();
-
-    replay(entityManagerProvider, injector);
-
-    upgradeCatalog250 = new UpgradeCatalog250(injector);
-  }
-
-  @After
-  public void tearDown() {
-  }
-
-  @Test
-  public void testExecuteDDLUpdates() throws Exception {
-    // !!! setup capture for host_version
-    dbAccessor.addUniqueConstraint("host_version", "UQ_host_repo", "repo_version_id", "host_id");
-
-    Capture<DBAccessor.DBColumnInfo> groupGroupType = newCapture();
-    dbAccessor.addColumn(eq(UpgradeCatalog250.GROUPS_TABLE), capture(groupGroupType));
-    dbAccessor.addUniqueConstraint("groups", "UNQ_groups_0", "group_name", "group_type");
-
-    expectLastCall().once();
-
-    // !!! setup capture for servicecomponent_version
-    Capture<List<DBAccessor.DBColumnInfo>> capturedComponentVersionColumns = newCapture();
-
-    dbAccessor.createTable(eq(UpgradeCatalog250.COMPONENT_VERSION_TABLE), capture(capturedComponentVersionColumns),
-        eq((String[]) null));
-
-    dbAccessor.addPKConstraint(eq(UpgradeCatalog250.COMPONENT_VERSION_TABLE),
-        eq(UpgradeCatalog250.COMPONENT_VERSION_PK), eq("id"));
-    dbAccessor.addFKConstraint(eq(UpgradeCatalog250.COMPONENT_VERSION_TABLE),
-        eq(UpgradeCatalog250.COMPONENT_VERSION_FK_COMPONENT), eq("component_id"),
-        eq(UpgradeCatalog250.COMPONENT_TABLE), eq("id"), eq(false));
-    dbAccessor.addFKConstraint(eq(UpgradeCatalog250.COMPONENT_VERSION_TABLE),
-        eq(UpgradeCatalog250.COMPONENT_VERSION_FK_REPO_VERSION), eq("repo_version_id"),
-        eq("repo_version"), eq("repo_version_id"), eq(false));
-
-    // servicedesiredstate table
-    Capture<DBAccessor.DBColumnInfo> capturedCredentialStoreEnabledCol = newCapture();
-    dbAccessor.addColumn(eq(UpgradeCatalog250.SERVICE_DESIRED_STATE_TABLE), capture(capturedCredentialStoreEnabledCol));
-
-    expect(dbAccessor.getConnection()).andReturn(connection).anyTimes();
-    expect(connection.createStatement()).andReturn(statement).anyTimes();
-    expect(statement.executeQuery(anyObject(String.class))).andReturn(resultSet).anyTimes();
-    expect(configuration.getDatabaseType()).andReturn(Configuration.DatabaseType.POSTGRES).anyTimes();
-
-    replay(dbAccessor, configuration, connection, statement, resultSet);
-
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(OsFamily.class).toInstance(osFamily);
-        binder.bind(EntityManager.class).toInstance(entityManager);
-        binder.bind(Configuration.class).toInstance(configuration);
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    UpgradeCatalog250 upgradeCatalog250 = injector.getInstance(UpgradeCatalog250.class);
-    upgradeCatalog250.executeDDLUpdates();
-
-    DBAccessor.DBColumnInfo capturedGroupTypeColumn = groupGroupType.getValue();
-    Assert.assertNotNull(capturedGroupTypeColumn);
-    Assert.assertEquals(UpgradeCatalog250.GROUP_TYPE_COL, capturedGroupTypeColumn.getName());
-    Assert.assertEquals(String.class, capturedGroupTypeColumn.getType());
-    Assert.assertEquals(null, capturedGroupTypeColumn.getLength());
-    Assert.assertEquals("LOCAL", capturedGroupTypeColumn.getDefaultValue());
-    Assert.assertEquals(false, capturedGroupTypeColumn.isNullable());
-
-    verify(dbAccessor);
-
-    // !!! check the captured for host_version
-    // (no checks)
-
-    // !!! check the captured for servicecomponent_version
-    Map<String, DBAccessor.DBColumnInfo> expected = new HashMap<>();
-    expected.put("id", new DBAccessor.DBColumnInfo("id", Long.class, null, null, false));
-    expected.put("component_id", new DBAccessor.DBColumnInfo("component_id", Long.class, null, null, false));
-    expected.put("repo_version_id", new DBAccessor.DBColumnInfo("repo_version_id", Long.class, null, null, false));
-    expected.put("state", new DBAccessor.DBColumnInfo("state", String.class, 32, null, false));
-    expected.put("user_name", new DBAccessor.DBColumnInfo("user_name", String.class, 255, null, false));
-
-    List<DBAccessor.DBColumnInfo> captured = capturedComponentVersionColumns.getValue();
-    Assert.assertEquals(5, captured.size());
-
-    for (DBAccessor.DBColumnInfo column : captured) {
-      DBAccessor.DBColumnInfo expectedColumn = expected.remove(column.getName());
-
-      Assert.assertNotNull(expectedColumn);
-      Assert.assertEquals(expectedColumn.getDefaultValue(), column.getDefaultValue());
-      Assert.assertEquals(expectedColumn.getName(), column.getName());
-      Assert.assertEquals(expectedColumn.getLength(), column.getLength());
-      Assert.assertEquals(expectedColumn.getType(), column.getType());
-      Assert.assertEquals(expectedColumn.getClass(), column.getClass());
-    }
-
-    // did we get them all?
-    Assert.assertEquals(0, expected.size());
-
-    // Verify if credential_store_enabled columns
-    // were added to servicedesiredstate table
-
-    DBAccessor.DBColumnInfo capturedCredentialStoreEnabledColValues = capturedCredentialStoreEnabledCol.getValue();
-    Assert.assertNotNull(capturedCredentialStoreEnabledColValues);
-
-    Assert.assertEquals(UpgradeCatalog250.CREDENTIAL_STORE_ENABLED_COL, capturedCredentialStoreEnabledColValues.getName());
-    Assert.assertEquals(null, capturedCredentialStoreEnabledColValues.getLength());
-    Assert.assertEquals(Short.class, capturedCredentialStoreEnabledColValues.getType());
-    Assert.assertEquals(0, capturedCredentialStoreEnabledColValues.getDefaultValue());
-    Assert.assertEquals(false, capturedCredentialStoreEnabledColValues.isNullable());
-  }
-
-  @Test
-  public void testExecuteDMLUpdates() throws Exception {
-    Method updateAmsConfigs = UpgradeCatalog250.class.getDeclaredMethod("updateAMSConfigs");
-    Method updateHadoopEnvConfigs = UpgradeCatalog250.class.getDeclaredMethod("updateHadoopEnvConfigs");
-    Method updateKafkaConfigs = UpgradeCatalog250.class.getDeclaredMethod("updateKafkaConfigs");
-    Method updateTablesForZeppelinViewRemoval = UpgradeCatalog250.class.getDeclaredMethod("unInstallAllZeppelinViews");
-    Method updateZeppelinConfigs = UpgradeCatalog250.class.getDeclaredMethod("updateZeppelinConfigs");
-    Method updateAtlasConfigs = UpgradeCatalog250.class.getDeclaredMethod("updateAtlasConfigs");
-    Method addNewConfigurationsFromXml = AbstractUpgradeCatalog.class.getDeclaredMethod("addNewConfigurationsFromXml");
-    Method updateHIVEInteractiveConfigs = UpgradeCatalog250.class.getDeclaredMethod("updateHIVEInteractiveConfigs");
-    Method updateLogSearchConfigs = UpgradeCatalog250.class.getDeclaredMethod("updateLogSearchConfigs");
-    Method updateLogSearchAlert = UpgradeCatalog250.class.getDeclaredMethod("updateLogSearchAlert");
-    Method updateAmbariInfraConfigs = UpgradeCatalog250.class.getDeclaredMethod("updateAmbariInfraConfigs");
-    Method updateRangerUrlConfigs = UpgradeCatalog250.class.getDeclaredMethod("updateRangerUrlConfigs");
-    Method addManageServiceAutoStartPermissions = UpgradeCatalog250.class.getDeclaredMethod("addManageServiceAutoStartPermissions");
-    Method addManageAlertNotificationsPermissions = UpgradeCatalog250.class.getDeclaredMethod("addManageAlertNotificationsPermissions");
-    Method updateTezHistoryUrlBase = UpgradeCatalog250.class.getDeclaredMethod("updateTezHistoryUrlBase");
-    Method updateYarnSite = UpgradeCatalog250.class.getDeclaredMethod("updateYarnSite");
-    Method updateAlerts = UpgradeCatalog250.class.getDeclaredMethod("updateStormAlerts");
-    Method removeAlertDuplicates = UpgradeCatalog250.class.getDeclaredMethod("removeAlertDuplicates");
-    Method updateKerberosDescriptorArtifacts = AbstractUpgradeCatalog.class.getDeclaredMethod("updateKerberosDescriptorArtifacts");
-    Method fixHBaseMasterCPUUtilizationAlertDefinition = UpgradeCatalog250.class.getDeclaredMethod("fixHBaseMasterCPUUtilizationAlertDefinition");
-
-    UpgradeCatalog250 upgradeCatalog250 = createMockBuilder(UpgradeCatalog250.class)
-        .addMockedMethod(updateAmsConfigs)
-        .addMockedMethod(updateHadoopEnvConfigs)
-        .addMockedMethod(updateKafkaConfigs)
-        .addMockedMethod(updateHIVEInteractiveConfigs)
-        .addMockedMethod(updateTablesForZeppelinViewRemoval)
-        .addMockedMethod(updateZeppelinConfigs)
-        .addMockedMethod(updateAtlasConfigs)
-        .addMockedMethod(updateLogSearchConfigs)
-        .addMockedMethod(updateAmbariInfraConfigs)
-        .addMockedMethod(addNewConfigurationsFromXml)
-        .addMockedMethod(updateRangerUrlConfigs)
-        .addMockedMethod(addManageServiceAutoStartPermissions)
-        .addMockedMethod(addManageAlertNotificationsPermissions)
-        .addMockedMethod(updateYarnSite)
-        .addMockedMethod(updateAlerts)
-        .addMockedMethod(updateLogSearchAlert)
-        .addMockedMethod(removeAlertDuplicates)
-        .addMockedMethod(updateKerberosDescriptorArtifacts)
-        .addMockedMethod(fixHBaseMasterCPUUtilizationAlertDefinition)
-        .addMockedMethod(updateTezHistoryUrlBase)
-        .createMock();
-
-
-    upgradeCatalog250.updateAMSConfigs();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateHadoopEnvConfigs();
-    expectLastCall().once();
-
-    upgradeCatalog250.addNewConfigurationsFromXml();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateKafkaConfigs();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateHIVEInteractiveConfigs();
-    expectLastCall().once();
-
-    upgradeCatalog250.unInstallAllZeppelinViews();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateZeppelinConfigs();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateAtlasConfigs();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateLogSearchConfigs();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateAmbariInfraConfigs();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateRangerUrlConfigs();
-    expectLastCall().once();
-
-    upgradeCatalog250.addManageServiceAutoStartPermissions();
-    expectLastCall().once();
-
-    upgradeCatalog250.addManageAlertNotificationsPermissions();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateTezHistoryUrlBase();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateYarnSite();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateStormAlerts();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateLogSearchAlert();
-    expectLastCall().once();
-
-    upgradeCatalog250.removeAlertDuplicates();
-    expectLastCall().once();
-
-    upgradeCatalog250.updateKerberosDescriptorArtifacts();
-    expectLastCall().once();
-
-    upgradeCatalog250.fixHBaseMasterCPUUtilizationAlertDefinition();
-    expectLastCall().once();
-
-    replay(upgradeCatalog250);
-
-    upgradeCatalog250.executeDMLUpdates();
-
-    verify(upgradeCatalog250);
-  }
-
-  @Test
-  public void testUpdateAlerts_StormUIWebAlert() {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class);
-    final AlertDefinitionEntity stormWebUIAlertMock = easyMockSupport.createNiceMock(AlertDefinitionEntity.class);
-
-    final Injector mockInjector = createInjector(mockAmbariManagementController, mockClusters, mockAlertDefinitionDAO);
-    long clusterId = 1;
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getClusterId()).andReturn(clusterId).anyTimes();
-    expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("storm_webui")))
-        .andReturn(stormWebUIAlertMock).atLeastOnce();
-    expect(stormWebUIAlertMock.getSource()).andReturn("{\"uri\": {\n" +
-        "            \"http\": \"{{storm-site/ui.port}}\",\n" +
-        "            \"kerberos_keytab\": \"{{storm-env/storm_ui_keytab}}\",\n" +
-        "            \"kerberos_principal\": \"{{storm-env/storm_ui_principal_name}}\",\n" +
-        "            \"connection_timeout\": 5.0\n" +
-        "          } }");
-
-    stormWebUIAlertMock.setSource("{\"uri\":{\"http\":\"{{storm-site/ui.port}}\",\"kerberos_keytab\":\"{{storm-env/storm_ui_keytab}}\",\"kerberos_principal\":\"{{storm-env/storm_ui_principal_name}}\",\"connection_timeout\":5.0,\"https\":\"{{storm-site/ui.https.port}}\",\"https_property\":\"{{storm-site/ui.https.keystore.type}}\",\"https_property_value\":\"jks\"}}");
-
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog250.class).updateStormAlerts();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateAlerts_StormUIPortAlert() {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class);
-    final AlertDefinitionEntity stormUIPortAlertMock = easyMockSupport.createNiceMock(AlertDefinitionEntity.class);
-
-    final Injector mockInjector = createInjector(mockAmbariManagementController, mockClusters, mockAlertDefinitionDAO);
-    long clusterId = 1;
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getClusterId()).andReturn(clusterId).anyTimes();
-    expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("storm_server_process")))
-        .andReturn(stormUIPortAlertMock).atLeastOnce();
-
-    mockAlertDefinitionDAO.remove(stormUIPortAlertMock);
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-
-    mockInjector.getInstance(UpgradeCatalog250.class).updateStormAlerts();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateAlerts_LogSearchUIWebAlert() {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class);
-    final AlertDefinitionEntity logSearchWebUIAlertMock = easyMockSupport.createNiceMock(AlertDefinitionEntity.class);
-
-    final Injector mockInjector = createInjector(mockAmbariManagementController, mockClusters, mockAlertDefinitionDAO);
-    long clusterId = 1;
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getClusterId()).andReturn(clusterId).anyTimes();
-    expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("logsearch_ui")))
-      .andReturn(logSearchWebUIAlertMock).atLeastOnce();
-    expect(logSearchWebUIAlertMock.getSource()).andReturn("{\"uri\": {\n" +
-      "            \"http\": \"{{logsearch-env/logsearch_ui_port}}\",\n" +
-      "            \"https\": \"{{logsearch-env/logsearch_ui_port}}\"\n" +
-      "          } }");
-
-    logSearchWebUIAlertMock.setSource("{\"uri\":{\"http\":\"{{logsearch-env/logsearch_ui_port}}\",\"https\":\"{{logsearch-env/logsearch_ui_port}}\",\"https_property\":\"{{logsearch-env/logsearch_ui_protocol}}\",\"https_property_value\":\"https\"}}");
-
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog250.class).updateLogSearchAlert();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testFixHBaseMasterCPUUtilizationAlertDefinition() {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class);
-    final AlertDefinitionEntity hbaseMasterCPUAlertMock = easyMockSupport.createNiceMock(AlertDefinitionEntity.class);
-
-    String brokenSource = "{\"uri\":{\"http\":\"{{hbase-site/hbase.master.info.port}}\",\"kerberos_keytab\":\"{{hbase-site/hbase.security.authentication.spnego.kerberos.principal}}\",\"kerberos_principal\":\"{{hbase-site/hbase.security.authentication.spnego.kerberos.keytab}}\",\"default_port\":60010,\"connection_timeout\":5.0},\"jmx\":{\"property_list\":[\"java.lang:type\\u003dOperatingSystem/SystemCpuLoad\",\"java.lang:type\\u003dOperatingSystem/AvailableProcessors\"],\"value\":\"{0} * 100\"},\"type\":\"METRIC\",\"reporting\":{\"ok\":{\"text\":\"{1} CPU, load {0:.1%}\"},\"warning\":{\"text\":\"{1} CPU, load {0:.1%}\",\"value\":200.0},\"critical\":{\"text\":\"{1} CPU, load {0:.1%}\",\"value\":250.0},\"units\":\"%\",\"type\":\"PERCENT\"}}";
-
-    Capture<String> capturedFixedSource = newCapture();
-
-    final Injector mockInjector = createInjector(mockAmbariManagementController, mockClusters, mockAlertDefinitionDAO);
-    long clusterId = 1;
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(Collections.singletonMap("normal", mockClusterExpected)).atLeastOnce();
-    expect(mockClusterExpected.getClusterId()).andReturn(clusterId).anyTimes();
-    expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("hbase_master_cpu"))).andReturn(hbaseMasterCPUAlertMock).atLeastOnce();
-    expect(hbaseMasterCPUAlertMock.getDefinitionName()).andReturn("hbase_master_cpu").once();
-    expect(hbaseMasterCPUAlertMock.getSource()).andReturn(brokenSource).once();
-
-    hbaseMasterCPUAlertMock.setSource(capture(capturedFixedSource));
-    expectLastCall().once();
-
-    hbaseMasterCPUAlertMock.setHash(anyString());
-    expectLastCall().once();
-
-    expect(mockAlertDefinitionDAO.merge(hbaseMasterCPUAlertMock)).andReturn(hbaseMasterCPUAlertMock).once();
-
-    easyMockSupport.replayAll();
-
-    mockInjector.getInstance(UpgradeCatalog250.class).fixHBaseMasterCPUUtilizationAlertDefinition();
-    easyMockSupport.verifyAll();
-
-    String fixedSource = capturedFixedSource.getValue();
-    Assert.assertNotNull(fixedSource);
-
-    JsonObject sourceJson = new JsonParser().parse(fixedSource).getAsJsonObject();
-    Assert.assertNotNull(sourceJson);
-
-    JsonObject uriJson = sourceJson.get("uri").getAsJsonObject();
-    Assert.assertNotNull(uriJson);
-
-    JsonPrimitive primitive;
-    primitive = uriJson.getAsJsonPrimitive("kerberos_keytab");
-    Assert.assertTrue(primitive.isString());
-    Assert.assertEquals("{{hbase-site/hbase.security.authentication.spnego.kerberos.keytab}}", primitive.getAsString());
-
-    primitive = uriJson.getAsJsonPrimitive("kerberos_principal");
-    Assert.assertTrue(primitive.isString());
-    Assert.assertEquals("{{hbase-site/hbase.security.authentication.spnego.kerberos.principal}}", primitive.getAsString());
-  }
-
-  @Test
-  public void testFixHBaseMasterCPUUtilizationAlertDefinitionMissingKerberosInfo() {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class);
-    final AlertDefinitionEntity hbaseMasterCPUAlertMock = easyMockSupport.createNiceMock(AlertDefinitionEntity.class);
-
-    String brokenSource = "{\"uri\":{\"http\":\"{{hbase-site/hbase.master.info.port}}\",\"default_port\":60010,\"connection_timeout\":5.0},\"jmx\":{\"property_list\":[\"java.lang:type\\u003dOperatingSystem/SystemCpuLoad\",\"java.lang:type\\u003dOperatingSystem/AvailableProcessors\"],\"value\":\"{0} * 100\"},\"type\":\"METRIC\",\"reporting\":{\"ok\":{\"text\":\"{1} CPU, load {0:.1%}\"},\"warning\":{\"text\":\"{1} CPU, load {0:.1%}\",\"value\":200.0},\"critical\":{\"text\":\"{1} CPU, load {0:.1%}\",\"value\":250.0},\"units\":\"%\",\"type\":\"PERCENT\"}}";
-
-    Capture<String> capturedFixedSource = newCapture();
-
-    final Injector mockInjector = createInjector(mockAmbariManagementController, mockClusters, mockAlertDefinitionDAO);
-    long clusterId = 1;
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(Collections.singletonMap("normal", mockClusterExpected)).atLeastOnce();
-    expect(mockClusterExpected.getClusterId()).andReturn(clusterId).anyTimes();
-    expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("hbase_master_cpu"))).andReturn(hbaseMasterCPUAlertMock).atLeastOnce();
-    expect(hbaseMasterCPUAlertMock.getDefinitionName()).andReturn("hbase_master_cpu").once();
-    expect(hbaseMasterCPUAlertMock.getSource()).andReturn(brokenSource).once();
-
-    expect(mockAlertDefinitionDAO.merge(hbaseMasterCPUAlertMock)).andReturn(hbaseMasterCPUAlertMock).anyTimes();
-
-    easyMockSupport.replayAll();
-
-    mockInjector.getInstance(UpgradeCatalog250.class).fixHBaseMasterCPUUtilizationAlertDefinition();
-    easyMockSupport.verifyAll();
-
-    Assert.assertFalse(capturedFixedSource.hasCaptured());
-  }
-
-  @Test
-  public void testUpdateYarnSite() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    final String propertyToRemove = "yarn.nodemanager.linux-container-executor.cgroups.mount-path";
-    final AmbariManagementController ambariManagementController = createNiceMock(AmbariManagementController.class);
-    Config mockYarnEnv = easyMockSupport.createNiceMock(Config.class);
-    Config mockYarnSite = easyMockSupport.createNiceMock(Config.class);
-
-    HashMap<String, String> yarnEnv = new HashMap<String, String>() {{
-      put("yarn_cgroups_enabled", "false");
-    }};
-
-    HashMap<String, String> yarnSite = new HashMap<String, String>() {{
-      put(propertyToRemove, "");
-    }};
-
-    reset(clusters, cluster, injector);
-
-    expect(injector.getInstance(AmbariManagementController.class)).andReturn(ambariManagementController).atLeastOnce();
-    expect(ambariManagementController.getClusters()).andReturn(clusters).atLeastOnce();
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("yarn-env")).andReturn(mockYarnEnv).atLeastOnce();
-    expect(mockYarnEnv.getProperties()).andReturn(yarnEnv).anyTimes();
-    expect(cluster.getDesiredConfigByType("yarn-site")).andReturn(mockYarnSite).atLeastOnce();
-    expect(mockYarnSite.getProperties()).andReturn(yarnSite).anyTimes();
-
-    replay(clusters, cluster, injector, ambariManagementController, mockYarnEnv, mockYarnSite);
-
-    UpgradeCatalog250 upgradeCatalog250 = createMockBuilder(UpgradeCatalog250.class)
-        .addMockedMethod("removeConfigurationPropertiesFromCluster")
-        .withConstructor(injector)
-        .createNiceMock();
-
-    Capture<HashSet<String>> removeConfigName = EasyMock.newCapture();
-
-    upgradeCatalog250.removeConfigurationPropertiesFromCluster(anyObject(Cluster.class), eq("yarn-site"), capture(removeConfigName));
-    EasyMock.expectLastCall();
-
-    replay(upgradeCatalog250);
-
-    upgradeCatalog250.updateYarnSite();
-
-    easyMockSupport.verifyAll();
-
-    Set<String> updatedProperties = removeConfigName.getValue();
-    assertTrue(updatedProperties.contains(propertyToRemove));
-
-    reset(injector);
-  }
-
-  @Test
-  public void testUpdateYarnSiteWithEnabledCGroups() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    final String propertyToRemove = "yarn.nodemanager.linux-container-executor.cgroups.mount-path";
-    final AmbariManagementController ambariManagementController = createNiceMock(AmbariManagementController.class);
-    Config mockYarnEnv = easyMockSupport.createNiceMock(Config.class);
-    Config mockYarnSite = easyMockSupport.createNiceMock(Config.class);
-
-    HashMap<String, String> yarnEnv = new HashMap<String, String>() {{
-      put("yarn_cgroups_enabled", "true");
-    }};
-
-    HashMap<String, String> yarnSite = new HashMap<String, String>() {{
-      put(propertyToRemove, "");
-    }};
-
-    reset(clusters, cluster, injector);
-
-    expect(injector.getInstance(AmbariManagementController.class)).andReturn(ambariManagementController).atLeastOnce();
-    expect(ambariManagementController.getClusters()).andReturn(clusters).atLeastOnce();
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("yarn-env")).andReturn(mockYarnEnv).atLeastOnce();
-    expect(mockYarnEnv.getProperties()).andReturn(yarnEnv).anyTimes();
-    expect(cluster.getDesiredConfigByType("yarn-site")).andReturn(mockYarnSite).atLeastOnce();
-    expect(mockYarnSite.getProperties()).andReturn(yarnSite).anyTimes();
-
-    replay(clusters, cluster, injector, ambariManagementController, mockYarnEnv, mockYarnSite);
-
-    UpgradeCatalog250 upgradeCatalog250 = createMockBuilder(UpgradeCatalog250.class)
-        .addMockedMethod("removeConfigurationPropertiesFromCluster")
-        .withConstructor(injector)
-        .createNiceMock();
-
-    Capture<HashSet<String>> removeConfigName = EasyMock.newCapture();
-
-    upgradeCatalog250.removeConfigurationPropertiesFromCluster(anyObject(Cluster.class), eq("yarn-site"), capture(removeConfigName));
-    EasyMock.expectLastCall().andThrow(new AssertionFailedError()).anyTimes();
-
-    replay(upgradeCatalog250);
-
-    upgradeCatalog250.updateYarnSite();
-
-    reset(injector);
-  }
-
-  @Test
-  public void testAmsEnvUpdateConfigs() throws Exception {
-
-    Map<String, String> oldPropertiesAmsEnv = new HashMap<String, String>() {
-      {
-        put("content", "\n" +
-            "# AMS Collector heapsize\n" +
-            "export AMS_COLLECTOR_HEAPSIZE={{metrics_collector_heapsize}}\n" +
-            "\n" +
-            "# HBase normalizer enabled\n" +
-            "export AMS_HBASE_NORMALIZER_ENABLED={{ams_hbase_normalizer_enabled}}\n" +
-            "\n" +
-            "# HBase compaction policy enabled\n" +
-            "export HBASE_FIFO_COMPACTION_POLICY_ENABLED={{ams_hbase_fifo_compaction_policy_enabled}}\n" +
-            "\n" +
-            "# HBase Tables Initialization check enabled\n" +
-            "export AMS_HBASE_INIT_CHECK_ENABLED={{ams_hbase_init_check_enabled}}\n");
-      }
-    };
-    Map<String, String> newPropertiesAmsEnv = new HashMap<String, String>() {
-      {
-        put("content", "\n" +
-            "# AMS Collector heapsize\n" +
-            "export AMS_COLLECTOR_HEAPSIZE={{metrics_collector_heapsize}}\n" +
-            "\n" +
-            "# HBase Tables Initialization check enabled\n" +
-            "export AMS_HBASE_INIT_CHECK_ENABLED={{ams_hbase_init_check_enabled}}\n");
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Config mockAmsEnv = easyMockSupport.createNiceMock(Config.class);
-
-    reset(clusters, cluster);
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("ams-env")).andReturn(mockAmsEnv).atLeastOnce();
-    expect(mockAmsEnv.getProperties()).andReturn(oldPropertiesAmsEnv).anyTimes();
-
-    replay(clusters, mockAmsEnv, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-        .addMockedMethod("createConfiguration")
-        .addMockedMethod("getClusters", new Class[]{})
-        .addMockedMethod("createConfig")
-        .withConstructor(actionManager, clusters, injector)
-        .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map<String, String>> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-        EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog250(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesAmsEnv, updatedProperties).areEqual());
-  }
-
-  @Test
-  public void testAmsGrafanaIniUpdateConfigs() throws Exception {
-
-    Map<String, String> oldProperties = new HashMap<String, String>() {
-      {
-        put("content", "[security]\n" +
-          "# default admin user, created on startup\n" +
-          "admin_user = {{ams_grafana_admin_user}}\n" +
-          "\n" +
-          "# default admin password, can be changed before first start of grafana,  or in profile settings\n" +
-          "admin_password = {{ams_grafana_admin_pwd}}\n" +
-          "\n" +
-          "# used for signing\n" +
-          ";secret_key = SW2YcwTIb9zpOOhoPsMm\n" +
-          "\n" +
-          "# Auto-login remember days\n" +
-          ";login_remember_days = 7\n" +
-          ";cookie_username = grafana_user\n" +
-          ";cookie_remember_name = grafana_remember\n" +
-          "\n" +
-          "# disable gravatar profile images\n" +
-          ";disable_gravatar = false\n" +
-          "\n" +
-          "# data source proxy whitelist (ip_or_domain:port seperated by spaces)\n" +
-          ";data_source_proxy_whitelist =\n");
-      }
-    };
-    Map<String, String> newProperties = new HashMap<String, String>() {
-      {
-        put("content", "[security]\n" +
-          "# default admin user, created on startup\n" +
-          "admin_user = {{ams_grafana_admin_user}}\n" +
-          "\n" +
-          "# default admin password, can be changed before first start of grafana,  or in profile settings\n" +
-          ";admin_password =\n" +
-          "\n" +
-          "# used for signing\n" +
-          ";secret_key = SW2YcwTIb9zpOOhoPsMm\n" +
-          "\n" +
-          "# Auto-login remember days\n" +
-          ";login_remember_days = 7\n" +
-          ";cookie_username = grafana_user\n" +
-          ";cookie_remember_name = grafana_remember\n" +
-          "\n" +
-          "# disable gravatar profile images\n" +
-          ";disable_gravatar = false\n" +
-          "\n" +
-          "# data source proxy whitelist (ip_or_domain:port seperated by spaces)\n" +
-          ";data_source_proxy_whitelist =\n");
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Config mockAmsGrafanaIni = easyMockSupport.createNiceMock(Config.class);
-
-    reset(clusters, cluster);
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("ams-grafana-ini")).andReturn(mockAmsGrafanaIni).atLeastOnce();
-    expect(mockAmsGrafanaIni.getProperties()).andReturn(oldProperties).anyTimes();
-
-    replay(clusters, mockAmsGrafanaIni, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-      .addMockedMethod("createConfiguration")
-      .addMockedMethod("getClusters", new Class[]{})
-      .addMockedMethod("createConfig")
-      .withConstructor(actionManager, clusters, injector)
-      .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map<String, String>> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-      EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog250(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newProperties, updatedProperties).areEqual());
-  }
-
-  @Test
-  public void testAmsHbaseSiteUpdateConfigs() throws Exception {
-
-    Map<String, String> newProperties = new HashMap<String, String>() {
-      {
-        put("hbase.rootdir", "/user/ams/hbase");
-      }
-    };
-
-    Map<String, String> oldProperties = new HashMap<String, String>() {
-      {
-        put("hbase.rootdir", "hdfs://namenodehost.domain.com:8020/user/ams/hbase");
-      }
-    };
-
-    testAmsHbaseRootDir(oldProperties, newProperties);
-
-    oldProperties = new HashMap<String, String>() {
-      {
-        put("hbase.rootdir", "hdfs://nameservice/user/ams/hbase");
-      }
-    };
-
-    testAmsHbaseRootDir(oldProperties, newProperties);
-
-  }
-
-  private void testAmsHbaseRootDir(Map<String, String> oldProperties, Map<String, String> newProperties) throws AmbariException {
-    Map<String, String> amsSite = new HashMap<String, String>() {
-      {
-        put("timeline.metrics.service.operation.mode", "distributed");
-        put("timeline.metrics.hbase.fifo.compaction.enabled", "true");
-      }
-    };
-
-    Map<String, String> newAmsSite = new HashMap<String, String>() {
-      {
-        put("timeline.metrics.service.operation.mode", "distributed");
-      }
-    };
-
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Config mockAmsHbaseSite = easyMockSupport.createNiceMock(Config.class);
-    Config mockAmsSite = easyMockSupport.createNiceMock(Config.class);
-
-    reset(clusters, cluster);
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-
-    expect(cluster.getDesiredConfigByType("ams-site")).andReturn(mockAmsSite).atLeastOnce();
-    expect(mockAmsSite.getProperties()).andReturn(amsSite).anyTimes();
-    expect(cluster.getDesiredConfigByType("ams-hbase-site")).andReturn(mockAmsHbaseSite).atLeastOnce();
-    expect(mockAmsHbaseSite.getProperties()).andReturn(oldProperties).anyTimes();
-
-    replay(clusters, mockAmsHbaseSite, mockAmsSite, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-        .addMockedMethod("createConfiguration")
-        .addMockedMethod("getClusters", new Class[]{})
-        .addMockedMethod("createConfig")
-        .withConstructor(actionManager, clusters, injector)
-        .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map<String, String>> propertiesCapture = EasyMock.newCapture(CaptureType.ALL);
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-        EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).times(2);
-
-    replay(controller, injector2);
-    new UpgradeCatalog250(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    assertTrue(propertiesCapture.getValues().size() == 2);
-
-    Map<String, String> updatedProperties = propertiesCapture.getValues().get(0);
-    assertTrue(Maps.difference(newAmsSite, updatedProperties).areEqual());
-
-    updatedProperties = propertiesCapture.getValues().get(1);
-    assertTrue(Maps.difference(newProperties, updatedProperties).areEqual());
-  }
-
-  @Test
-  public void testKafkaUpdateConfigs() throws Exception {
-
-    Map<String, String> oldProperties = new HashMap<String, String>() {
-      {
-        put("kafka.timeline.metrics.host", "{{metric_collector_host}}");
-        put("kafka.timeline.metrics.port", "{{metric_collector_port}}");
-      }
-    };
-    Map<String, String> newProperties = new HashMap<String, String>() {
-      {
-        put("kafka.timeline.metrics.port", "{{metric_collector_port}}");
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    Config mockKafkaBroker = easyMockSupport.createNiceMock(Config.class);
-
-    reset(clusters, cluster);
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("kafka-broker")).andReturn(mockKafkaBroker).atLeastOnce();
-    expect(mockKafkaBroker.getProperties()).andReturn(oldProperties).anyTimes();
-
-    replay(clusters, mockKafkaBroker, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-        .addMockedMethod("createConfiguration")
-        .addMockedMethod("getClusters", new Class[]{})
-        .addMockedMethod("createConfig")
-        .withConstructor(actionManager, clusters, injector)
-        .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map<String, String>> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-        EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog250(injector2).updateKafkaConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newProperties, updatedProperties).areEqual());
-  }
-
-  @Test
-  public void testAmsLog4jUpdateConfigs() throws Exception {
-    reset(clusters, cluster);
-    expect(clusters.getClusters()).andReturn(ImmutableMap.of("normal", cluster)).once();
-
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-        .addMockedMethod("createConfiguration")
-        .addMockedMethod("getClusters", new Class[]{})
-        .addMockedMethod("createConfig")
-        .withConstructor(actionManager, clusters, injector)
-        .createNiceMock();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-
-    Map<String, String> oldAmsLog4j = ImmutableMap.of(
-        "content",
-        "#\n" +
-            "# Licensed to the Apache Software Foundation (ASF) under one\n" +
-            "# or more contributor license agreements.  See the NOTICE file\n" +
-            "# distributed with this work for additional information\n" +
-            "# regarding copyright ownership.  The ASF licenses this file\n" +
-            "# to you under the Apache License, Version 2.0 (the\n" +
-            "# \"License\"); you may not use this file except in compliance\n" +
-            "# with the License.  You may obtain a copy of the License at\n" +
-            "#\n" +
-            "#     http://www.apache.org/licenses/LICENSE-2.0\n" +
-            "#\n" +
-            "# Unless required by applicable law or agreed to in writing, software\n" +
-            "# distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
-            "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
-            "# See the License for the specific language governing permissions and\n" +
-            "# limitations under the License.\n" +
-            "#\n" +
-            "\n" +
-            "# Define some default values that can be overridden by system properties\n" +
-            "ams.log.dir=.\n" +
-            "ams.log.file=ambari-metrics-collector.log\n" +
-            "\n" +
-            "# Root logger option\n" +
-            "log4j.rootLogger=INFO,file\n" +
-            "\n" +
-            "# Direct log messages to a log file\n" +
-            "log4j.appender.file=org.apache.log4j.RollingFileAppender\n" +
-            "log4j.appender.file.File=${ams.log.dir}/${ams.log.file}\n" +
-            "log4j.appender.file.MaxFileSize=10MB\n" +
-            "log4j.appender.file.MaxBackupIndex=12\n" +
-            "log4j.appender.file.layout=org.apache.log4j.PatternLayout\n" +
-            "log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %p %c: %m%n");
-
-    Map<String, String> expectedAmsLog4j = new HashMap<>();
-    expectedAmsLog4j.put("content", "#\n" +
-        "# Licensed to the Apache Software Foundation (ASF) under one\n" +
-        "# or more contributor license agreements.  See the NOTICE file\n" +
-        "# distributed with this work for additional information\n" +
-        "# regarding copyright ownership.  The ASF licenses this file\n" +
-        "# to you under the Apache License, Version 2.0 (the\n" +
-        "# \"License\"); you may not use this file except in compliance\n" +
-        "# with the License.  You may obtain a copy of the License at\n" +
-        "#\n" +
-        "#     http://www.apache.org/licenses/LICENSE-2.0\n" +
-        "#\n" +
-        "# Unless required by applicable law or agreed to in writing, software\n" +
-        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
-        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
-        "# See the License for the specific language governing permissions and\n" +
-        "# limitations under the License.\n" +
-        "#\n" +
-        "\n" +
-        "# Define some default values that can be overridden by system properties\n" +
-        "ams.log.dir=.\n" +
-        "ams.log.file=ambari-metrics-collector.log\n" +
-        "\n" +
-        "# Root logger option\n" +
-        "log4j.rootLogger=INFO,file\n" +
-        "\n" +
-        "# Direct log messages to a log file\n" +
-        "log4j.appender.file=org.apache.log4j.RollingFileAppender\n" +
-        "log4j.appender.file.File=${ams.log.dir}/${ams.log.file}\n" +
-        "log4j.appender.file.MaxFileSize={{ams_log_max_backup_size}}MB\n" +
-        "log4j.appender.file.MaxBackupIndex={{ams_log_number_of_backup_files}}\n" +
-        "log4j.appender.file.layout=org.apache.log4j.PatternLayout\n" +
-        "log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %p %c: %m%n");
-    expectedAmsLog4j.put("ams_log_max_backup_size", "10");
-    expectedAmsLog4j.put("ams_log_number_of_backup_files", "12");
-
-
-    Config mockAmsLog4j = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("ams-log4j")).andReturn(mockAmsLog4j).atLeastOnce();
-    expect(mockAmsLog4j.getProperties()).andReturn(oldAmsLog4j).anyTimes();
-    Capture<Map<String, String>> AmsLog4jCapture = EasyMock.newCapture();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(AmsLog4jCapture), anyString(),
-        anyObject(Map.class))).andReturn(config).once();
-
-    Map<String, String> oldAmsHbaseLog4j = ImmutableMap.of(
-        "content", "# Licensed to the Apache Software Foundation (ASF) under one\n" +
-            "# or more contributor license agreements.  See the NOTICE file\n" +
-            "# distributed with this work for additional information\n" +
-            "# regarding copyright ownership.  The ASF licenses this file\n" +
-            "# to you under the Apache License, Version 2.0 (the\n" +
-            "# \"License\"); you may not use this file except in compliance\n" +
-            "# with the License.  You may obtain a copy of the License at\n" +
-            "#\n" +
-            "#     http://www.apache.org/licenses/LICENSE-2.0\n" +
-            "#\n" +
-            "# Unless required by applicable law or agreed to in writing, software\n" +
-            "# distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
-            "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
-            "# See the License for the specific language governing permissions and\n" +
-            "# limitations under the License.\n" +
-            "\n" +
-            "\n" +
-            "# Define some default values that can be overridden by system properties\n" +
-            "hbase.root.logger=INFO,console\n" +
-            "hbase.security.logger=INFO,console\n" +
-            "hbase.log.dir=.\n" +
-            "hbase.log.file=hbase.log\n" +
-            "\n" +
-            "# Define the root logger to the system property \"hbase.root.logger\".\n" +
-            "log4j.rootLogger=${hbase.root.logger}\n" +
-            "\n" +
-            "# Logging Threshold\n" +
-            "log4j.threshold=ALL\n" +
-            "\n" +
-            "#\n" +
-            "# Daily Rolling File Appender\n" +
-            "#\n" +
-            "log4j.appender.DRFA=org.apache.log4j.DailyRollingFileAppender\n" +
-            "log4j.appender.DRFA.File=${hbase.log.dir}/${hbase.log.file}\n" +
-            "\n" +
-            "# Rollver at midnight\n" +
-            "log4j.appender.DRFA.DatePattern=.yyyy-MM-dd\n" +
-            "\n" +
-            "# 30-day backup\n" +
-            "#log4j.appender.DRFA.MaxBackupIndex=30\n" +
-            "log4j.appender.DRFA.layout=org.apache.log4j.PatternLayout\n" +
-            "\n" +
-            "# Pattern format: Date LogLevel LoggerName LogMessage\n" +
-            "log4j.appender.DRFA.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c{2}: %m%n\n" +
-            "\n" +
-            "# Rolling File Appender properties\n" +
-            "hbase.log.maxfilesize=256MB\n" +
-            "hbase.log.maxbackupindex=20\n" +
-            "\n" +
-            "# Rolling File Appender\n" +
-            "log4j.appender.RFA=org.apache.log4j.RollingFileAppender\n" +
-            "log4j.appender.RFA.File=${hbase.log.dir}/${hbase.log.file}\n" +
-            "\n" +
-            "log4j.appender.RFA.MaxFileSize=${hbase.log.maxfilesize}\n" +
-            "log4j.appender.RFA.MaxBackupIndex=${hbase.log.maxbackupindex}\n" +
-            "\n" +
-            "log4j.appender.RFA.layout=org.apache.log4j.PatternLayout\n" +
-            "log4j.appender.RFA.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c{2}: %m%n\n" +
-            "\n" +
-            "#\n" +
-            "# Security audit appender\n" +
-            "#\n" +
-            "hbase.security.log.file=SecurityAuth.audit\n" +
-            "hbase.security.log.maxfilesize=256MB\n" +
-            "hbase.security.log.maxbackupindex=20\n" +
-            "log4j.appender.RFAS=org.apache.log4j.RollingFileAppender\n" +
-            "log4j.appender.RFAS.File=${hbase.log.dir}/${hbase.security.log.file}\n" +
-            "log4j.appender.RFAS.MaxFileSize=${hbase.security.log.maxfilesize}\n" +
-            "log4j.appender.RFAS.MaxBackupIndex=${hbase.security.log.maxbackupindex}\n" +
-            "log4j.appender.RFAS.layout=org.apache.log4j.PatternLayout\n" +
-            "log4j.appender.RFAS.layout.ConversionPattern=%d{ISO8601} %p %c: %m%n\n" +
-            "log4j.category.SecurityLogger=${hbase.security.logger}\n" +
-            "log4j.additivity.SecurityLogger=false\n" +
-            "#log4j.logger.SecurityLogger.org.apache.hadoop.hbase.security.access.AccessController=TRACE\n" +
-            "\n" +
-            "#\n" +
-            "# Null Appender\n" +
-            "#\n" +
-            "log4j.appender.NullAppender=org.apache.log4j.varia.NullAppender\n" +
-            "\n" +
-            "#\n" +
-            "# console\n" +
-            "# Add \"console\" to rootlogger above if you want to use this\n" +
-            "#\n" +
-            "log4j.appender.console=org.apache.log4j.ConsoleAppender\n" +
-            "log4j.appender.console.target=System.err\n" +
-            "log4j.appender.console.layout=org.apache.log4j.PatternLayout\n" +
-            "log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c{2}: %m%n\n" +
-            "\n" +
-            "# Custom Logging levels\n" +
-            "\n" +
-            "log4j.logger.org.apache.zookeeper=INFO\n" +
-            "#log4j.logger.org.apache.hadoop.fs.FSNamesystem=DEBUG\n" +
-            "log4j.logger.org.apache.hadoop.hbase=INFO\n" +
-            "# Make these two classes INFO-level. Make them DEBUG to see more zk debug.\n" +
-            "log4j.logger.org.apache.hadoop.hbase.zookeeper.ZKUtil=INFO\n" +
-            "log4j.logger.org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher=INFO\n" +
-            "#log4j.logger.org.apache.hadoop.dfs=DEBUG\n" +
-            "# Set this class to log INFO only otherwise its OTT\n" +
-            "# Enable this to get detailed connection error/retry logging.\n" +
-            "# log4j.logger.org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation=TRACE\n" +
-            "\n" +
-            "\n" +
-            "# Uncomment this line to enable tracing on _every_ RPC call (this can be a lot of output)\n" +
-            "#log4j.logger.org.apache.hadoop.ipc.HBaseServer.trace=DEBUG\n" +
-            "\n" +
-            "# Uncomment the below if you want to remove logging of client region caching'\n" +
-            "# and scan of .META. messages\n" +
-            "# log4j.logger.org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation=INFO\n" +
-            "# log4j.logger.org.apache.hadoop.hbase.client.MetaScanner=INFO\n");
-
-    Map<String, String> expectedAmsHbaseLog4j = new HashMap<String, String>();
-    expectedAmsHbaseLog4j.put("content", "# Licensed to the Apache Software Foundation (ASF) under one\n" +
-        "# or more contributor license agreements.  See the NOTICE file\n" +
-        "# distributed with this work for additional information\n" +
-        "# regarding copyright ownership.  The ASF licenses this file\n" +
-        "# to you under the Apache License, Version 2.0 (the\n" +
-        "# \"License\"); you may not use this file except in compliance\n" +
-        "# with the License.  You may obtain a copy of the License at\n" +
-        "#\n" +
-        "#     http://www.apache.org/licenses/LICENSE-2.0\n" +
-        "#\n" +
-        "# Unless required by applicable law or agreed to in writing, software\n" +
-        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
-        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
-        "# See the License for the specific language governing permissions and\n" +
-        "# limitations under the License.\n" +
-        "\n" +
-        "\n" +
-        "# Define some default values that can be overridden by system properties\n" +
-        "hbase.root.logger=INFO,console\n" +
-        "hbase.security.logger=INFO,console\n" +
-        "hbase.log.dir=.\n" +
-        "hbase.log.file=hbase.log\n" +
-        "\n" +
-        "# Define the root logger to the system property \"hbase.root.logger\".\n" +
-        "log4j.rootLogger=${hbase.root.logger}\n" +
-        "\n" +
-        "# Logging Threshold\n" +
-        "log4j.threshold=ALL\n" +
-        "\n" +
-        "#\n" +
-        "# Daily Rolling File Appender\n" +
-        "#\n" +
-        "log4j.appender.DRFA=org.apache.log4j.DailyRollingFileAppender\n" +
-        "log4j.appender.DRFA.File=${hbase.log.dir}/${hbase.log.file}\n" +
-        "\n" +
-        "# Rollver at midnight\n" +
-        "log4j.appender.DRFA.DatePattern=.yyyy-MM-dd\n" +
-        "\n" +
-        "# 30-day backup\n" +
-        "#log4j.appender.DRFA.MaxBackupIndex=30\n" +
-        "log4j.appender.DRFA.layout=org.apache.log4j.PatternLayout\n" +
-        "\n" +
-        "# Pattern format: Date LogLevel LoggerName LogMessage\n" +
-        "log4j.appender.DRFA.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c{2}: %m%n\n" +
-        "\n" +
-        "# Rolling File Appender properties\n" +
-        "hbase.log.maxfilesize={{ams_hbase_log_maxfilesize}}MB\n" +
-        "hbase.log.maxbackupindex={{ams_hbase_log_maxbackupindex}}\n" +
-        "\n" +
-        "# Rolling File Appender\n" +
-        "log4j.appender.RFA=org.apache.log4j.RollingFileAppender\n" +
-        "log4j.appender.RFA.File=${hbase.log.dir}/${hbase.log.file}\n" +
-        "\n" +
-        "log4j.appender.RFA.MaxFileSize=${hbase.log.maxfilesize}\n" +
-        "log4j.appender.RFA.MaxBackupIndex=${hbase.log.maxbackupindex}\n" +
-        "\n" +
-        "log4j.appender.RFA.layout=org.apache.log4j.PatternLayout\n" +
-        "log4j.appender.RFA.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c{2}: %m%n\n" +
-        "\n" +
-        "#\n" +
-        "# Security audit appender\n" +
-        "#\n" +
-        "hbase.security.log.file=SecurityAuth.audit\n" +
-        "hbase.security.log.maxfilesize={{ams_hbase_security_log_maxfilesize}}MB\n" +
-        "hbase.security.log.maxbackupindex={{ams_hbase_security_log_maxbackupindex}}\n" +
-        "log4j.appender.RFAS=org.apache.log4j.RollingFileAppender\n" +
-        "log4j.appender.RFAS.File=${hbase.log.dir}/${hbase.security.log.file}\n" +
-        "log4j.appender.RFAS.MaxFileSize=${hbase.security.log.maxfilesize}\n" +
-        "log4j.appender.RFAS.MaxBackupIndex=${hbase.security.log.maxbackupindex}\n" +
-        "log4j.appender.RFAS.layout=org.apache.log4j.PatternLayout\n" +
-        "log4j.appender.RFAS.layout.ConversionPattern=%d{ISO8601} %p %c: %m%n\n" +
-        "log4j.category.SecurityLogger=${hbase.security.logger}\n" +
-        "log4j.additivity.SecurityLogger=false\n" +
-        "#log4j.logger.SecurityLogger.org.apache.hadoop.hbase.security.access.AccessController=TRACE\n" +
-        "\n" +
-        "#\n" +
-        "# Null Appender\n" +
-        "#\n" +
-        "log4j.appender.NullAppender=org.apache.log4j.varia.NullAppender\n" +
-        "\n" +
-        "#\n" +
-        "# console\n" +
-        "# Add \"console\" to rootlogger above if you want to use this\n" +
-        "#\n" +
-        "log4j.appender.console=org.apache.log4j.ConsoleAppender\n" +
-        "log4j.appender.console.target=System.err\n" +
-        "log4j.appender.console.layout=org.apache.log4j.PatternLayout\n" +
-        "log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c{2}: %m%n\n" +
-        "\n" +
-        "# Custom Logging levels\n" +
-        "\n" +
-        "log4j.logger.org.apache.zookeeper=INFO\n" +
-        "#log4j.logger.org.apache.hadoop.fs.FSNamesystem=DEBUG\n" +
-        "log4j.logger.org.apache.hadoop.hbase=INFO\n" +
-        "# Make these two classes INFO-level. Make them DEBUG to see more zk debug.\n" +
-        "log4j.logger.org.apache.hadoop.hbase.zookeeper.ZKUtil=INFO\n" +
-        "log4j.logger.org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher=INFO\n" +
-        "#log4j.logger.org.apache.hadoop.dfs=DEBUG\n" +
-        "# Set this class to log INFO only otherwise its OTT\n" +
-        "# Enable this to get detailed connection error/retry logging.\n" +
-        "# log4j.logger.org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation=TRACE\n" +
-        "\n" +
-        "\n" +
-        "# Uncomment this line to enable tracing on _every_ RPC call (this can be a lot of output)\n" +
-        "#log4j.logger.org.apache.hadoop.ipc.HBaseServer.trace=DEBUG\n" +
-        "\n" +
-        "# Uncomment the below if you want to remove logging of client region caching'\n" +
-        "# and scan of .META. messages\n" +
-        "# log4j.logger.org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation=INFO\n" +
-        "# log4j.logger.org.apache.hadoop.hbase.client.MetaScanner=INFO\n");
-    expectedAmsHbaseLog4j.put("ams_hbase_log_maxfilesize", "256");
-    expectedAmsHbaseLog4j.put("ams_hbase_log_maxbackupindex", "20");
-    expectedAmsHbaseLog4j.put("ams_hbase_security_log_maxfilesize", "256");
-    expectedAmsHbaseLog4j.put("ams_hbase_security_log_maxbackupindex", "20");
-
-    Config mockAmsHbaseLog4j = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("ams-hbase-log4j")).andReturn(mockAmsHbaseLog4j).atLeastOnce();
-    expect(mockAmsHbaseLog4j.getProperties()).andReturn(oldAmsHbaseLog4j).anyTimes();
-    Capture<Map<String, String>> AmsHbaseLog4jCapture = EasyMock.newCapture();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(AmsHbaseLog4jCapture), anyString(),
-        anyObject(Map.class))).andReturn(config).once();
-
-    replay(clusters, cluster);
-    replay(controller, injector2);
-    replay(mockAmsLog4j, mockAmsHbaseLog4j);
-    new UpgradeCatalog250(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedAmsLog4jProperties = AmsLog4jCapture.getValue();
-    assertTrue(Maps.difference(expectedAmsLog4j, updatedAmsLog4jProperties).areEqual());
-
-    Map<String, String> updatedAmsHbaseLog4jProperties = AmsHbaseLog4jCapture.getValue();
-    assertTrue(Maps.difference(expectedAmsHbaseLog4j, updatedAmsHbaseLog4jProperties).areEqual());
-
-  }
-
-  @Test
-  public void testLogSearchUpdateConfigs() throws Exception {
-    reset(clusters, cluster);
-    expect(clusters.getClusters()).andReturn(ImmutableMap.of("normal", cluster)).once();
-
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-        .addMockedMethod("createConfiguration")
-        .addMockedMethod("getClusters", new Class[]{})
-        .addMockedMethod("createConfig")
-        .withConstructor(actionManager, clusters, injector)
-        .createNiceMock();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-
-    Map<String, String> oldLogSearchProperties = ImmutableMap.of(
-        "logsearch.external.auth.enabled", "true",
-        "logsearch.external.auth.host_url", "host_url",
-        "logsearch.external.auth.login_url", "login_url");
-
-    Map<String, String> expectedLogSearchProperties = ImmutableMap.of(
-        "logsearch.auth.external_auth.enabled", "true",
-        "logsearch.auth.external_auth.host_url", "host_url",
-        "logsearch.auth.external_auth.login_url", "login_url");
-
-    Config mockLogSearchProperties = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("logsearch-properties")).andReturn(mockLogSearchProperties).atLeastOnce();
-    expect(mockLogSearchProperties.getProperties()).andReturn(oldLogSearchProperties).anyTimes();
-    Capture<Map<String, String>> logSearchPropertiesCapture = EasyMock.newCapture();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(logSearchPropertiesCapture), anyString(),
-        EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
-
-    Map<String, String> oldLogFeederEnv = ImmutableMap.of(
-        "content", "infra_solr_ssl_enabled");
-
-    Map<String, String> expectedLogFeederEnv = ImmutableMap.of(
-        "content", "logfeeder_use_ssl");
-
-    Config mockLogFeederEnv = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("logfeeder-env")).andReturn(mockLogFeederEnv).atLeastOnce();
-    expect(mockLogFeederEnv.getProperties()).andReturn(oldLogFeederEnv).anyTimes();
-    Capture<Map<String, String>> logFeederEnvCapture = EasyMock.newCapture();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(logFeederEnvCapture), anyString(),
-        EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
-
-    Map<String, String> oldLogSearchEnv = new HashMap<>();
-    oldLogSearchEnv.put("logsearch_solr_audit_logs_use_ranger", "false");
-    oldLogSearchEnv.put("logsearch_solr_audit_logs_zk_node", "zk_node");
-    oldLogSearchEnv.put("logsearch_solr_audit_logs_zk_quorum", "zk_quorum");
-    oldLogSearchEnv.put("logsearch_ui_protocol", "http");
-    oldLogSearchEnv.put("logsearch_truststore_location", "/etc/security/serverKeys/logsearch.trustStore.jks");
-    oldLogSearchEnv.put("logsearch_keystore_location", "/etc/security/serverKeys/logsearch.keyStore.jks");
-    oldLogSearchEnv.put("content", "infra_solr_ssl_enabled or logsearch_ui_protocol == 'https'");
-
-    Map<String, String> expectedLogSearchEnv = ImmutableMap.of(
-        "logsearch_ui_protocol", "http",
-        "logsearch_truststore_location", "/etc/ambari-logsearch-portal/conf/keys/logsearch.jks",
-        "logsearch_keystore_location", "/etc/ambari-logsearch-portal/conf/keys/logsearch.jks",
-        "content", "logsearch_use_ssl");
-
-    Config mockLogSearchEnv = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("logsearch-env")).andReturn(mockLogSearchEnv).atLeastOnce();
-    expect(mockLogSearchEnv.getProperties()).andReturn(oldLogSearchEnv).anyTimes();
-    Capture<Map<String, String>> logSearchEnvCapture = EasyMock.newCapture();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(logSearchEnvCapture), anyString(),
-        EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
-
-    Map<String, String> oldLogFeederLog4j = ImmutableMap.of(
-        "content",
-        "    <appender name=\"rolling_file\" class=\"org.apache.log4j.RollingFileAppender\">\n" +
-            "    <param name=\"file\" value=\"{{logfeeder_log_dir}}/logfeeder.log\"/>\n" +
-            "    <param name=\"append\" value=\"true\"/>\n" +
-            "    <param name=\"maxFileSize\" value=\"11MB\"/>\n" +
-            "    <param name=\"maxBackupIndex\" value=\"12\"/>\n" +
-            "    <layout class=\"org.apache.log4j.PatternLayout\">\n" +
-            "      <param name=\"ConversionPattern\" value=\"%d [%t] %-5p %C{6} (%F:%L) - %m%n\"/>\n" +
-            "    </layout>\n" +
-            "  </appender>\n" +
-            "\n" +
-            "  <appender name=\"rolling_file_json\"\n" +
-            "    class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
-            "    <param name=\"file\" value=\"{{logfeeder_log_dir}}/logsearch-logfeeder.json\" />\n" +
-            "    <param name=\"append\" value=\"true\" />\n" +
-            "    <param name=\"maxFileSize\" value=\"13MB\" />\n" +
-            "    <param name=\"maxBackupIndex\" value=\"14\" />\n" +
-            "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\" />\n" +
-            "  </appender>");
-
-    Map<String, String> expectedLogFeederLog4j = ImmutableMap.of(
-        "content",
-        "    <appender name=\"rolling_file\" class=\"org.apache.log4j.RollingFileAppender\">\n" +
-            "    <param name=\"file\" value=\"{{logfeeder_log_dir}}/logfeeder.log\"/>\n" +
-            "    <param name=\"append\" value=\"true\"/>\n" +
-            "    <param name=\"maxFileSize\" value=\"{{logfeeder_log_maxfilesize}}MB\"/>\n" +
-            "    <param name=\"maxBackupIndex\" value=\"{{logfeeder_log_maxbackupindex}}\"/>\n" +
-            "    <layout class=\"org.apache.log4j.PatternLayout\">\n" +
-            "      <param name=\"ConversionPattern\" value=\"%d [%t] %-5p %C{6} (%F:%L) - %m%n\"/>\n" +
-            "    </layout>\n" +
-            "  </appender>\n" +
-            "\n" +
-            "  <appender name=\"rolling_file_json\"\n" +
-            "    class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
-            "    <param name=\"file\" value=\"{{logfeeder_log_dir}}/logsearch-logfeeder.json\" />\n" +
-            "    <param name=\"append\" value=\"true\" />\n" +
-            "    <param name=\"maxFileSize\" value=\"{{logfeeder_json_log_maxfilesize}}MB\" />\n" +
-            "    <param name=\"maxBackupIndex\" value=\"{{logfeeder_json_log_maxbackupindex}}\" />\n" +
-            "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\" />\n" +
-            "  </appender>",
-        "logfeeder_log_maxfilesize", "11",
-        "logfeeder_log_maxbackupindex", "12",
-        "logfeeder_json_log_maxfilesize", "13",
-        "logfeeder_json_log_maxbackupindex", "14");
-
-    Config mockLogFeederLog4j = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("logfeeder-log4j")).andReturn(mockLogFeederLog4j).atLeastOnce();
-    expect(mockLogFeederLog4j.getProperties()).andReturn(oldLogFeederLog4j).anyTimes();
-    Capture<Map<String, String>> logFeederLog4jCapture = EasyMock.newCapture();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(logFeederLog4jCapture), anyString(),
-        EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
-
-    Map<String, String> oldLogSearchLog4j = ImmutableMap.of(
-        "content",
-        "  <appender name=\"rolling_file\" class=\"org.apache.log4j.RollingFileAppender\">\n" +
-            "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch.err\" />\n" +
-            "    <param name=\"Threshold\" value=\"info\" />\n" +
-            "    <param name=\"append\" value=\"true\" />\n" +
-            "    <param name=\"maxFileSize\" value=\"11MB\" />\n" +
-            "    <param name=\"maxBackupIndex\" value=\"12\" />\n" +
-            "    <layout class=\"org.apache.log4j.PatternLayout\">\n" +
-            "      <param name=\"ConversionPattern\" value=\"%d [%t] %-5p %C{6} (%F:%L) - %m%n\" />\n" +
-            "    </layout>\n" +
-            "  </appender>\n" +
-            "\n" +
-            "  <appender name=\"rolling_file_json\" class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
-            "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch.json\"/>\n" +
-            "    <param name=\"append\" value=\"true\"/>\n" +
-            "    <param name=\"maxFileSize\" value=\"13MB\"/>\n" +
-            "    <param name=\"maxBackupIndex\" value=\"14\"/>\n" +
-            "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\"/>\n" +
-            "  </appender>\n" +
-            "\n" +
-            "  <appender name=\"audit_rolling_file_json\" class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
-            "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch-audit.json\"/>\n" +
-            "    <param name=\"append\" value=\"true\"/>\n" +
-            "    <param name=\"maxFileSize\" value=\"15MB\"/>\n" +
-            "    <param name=\"maxBackupIndex\" value=\"16\"/>\n" +
-            "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\"/>\n" +
-            "  </appender>\n" +
-            "\n" +
-            "  <appender name=\"performance_analyzer_json\" class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
-            "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch-performance.json\"/>\n" +
-            "    <param name=\"Threshold\" value=\"info\"/>\n" +
-            "    <param name=\"append\" value=\"true\"/>\n" +
-            "    <param name=\"maxFileSize\" value=\"17MB\"/>\n" +
-            "    <param name=\"maxBackupIndex\" value=\"18\"/>\n" +
-            "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\"/>\n" +
-            "  </appender>\n" +
-            "\n" +
-            "  <logger name=\"org.apache.ambari.logsearch.audit\" additivity=\"true\">\n" +
-            "     <appender-ref ref=\"audit_rolling_file_json\"/>\n" +
-            "  </logger>\n" +
-            "\n" +
-            "  <logger name=\"org.apache.ambari.logsearch.performance\" additivity=\"false\">\n" +
-            "    <appender-ref ref=\"performance_analyzer_json\"/>\n" +
-            "  </logger>\n" +
-            "\n" +
-            "  <category name=\"org.apache.ambari.logsearch\" additivity=\"false\">\n" +
-            "    <priority value=\"warn\"/>\n" +
-            "    <appender-ref ref=\"rolling_file_json\"/>\n" +
-            "  </category>");
-
-    Map<String, String> expectedLogSearchLog4j = new HashMap<>();
-    expectedLogSearchLog4j.put("content",
-        "  <appender name=\"rolling_file\" class=\"org.apache.log4j.RollingFileAppender\">\n" +
-            "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch.log\" />\n" +
-            "    <param name=\"Threshold\" value=\"info\" />\n" +
-            "    <param name=\"append\" value=\"true\" />\n" +
-            "    <param name=\"maxFileSize\" value=\"{{logsearch_log_maxfilesize}}MB\" />\n" +
-            "    <param name=\"maxBackupIndex\" value=\"{{logsearch_log_maxbackupindex}}\" />\n" +
-            "    <layout class=\"org.apache.log4j.PatternLayout\">\n" +
-            "      <param name=\"ConversionPattern\" value=\"%d [%t] %-5p %C{6} (%F:%L) - %m%n\" />\n" +
-            "    </layout>\n" +
-            "  </appender>\n" +
-            "\n" +
-            "  <appender name=\"rolling_file_json\" class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
-            "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch.json\"/>\n" +
-            "    <param name=\"append\" value=\"true\"/>\n" +
-            "    <param name=\"maxFileSize\" value=\"{{logsearch_json_log_maxfilesize}}MB\"/>\n" +
-            "    <param name=\"maxBackupIndex\" value=\"{{logsearch_json_log_maxbackupindex}}\"/>\n" +
-            "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\"/>\n" +
-            "  </appender>\n" +
-            "\n" +
-            "  <appender name=\"audit_rolling_file_json\" class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
-            "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch-audit.json\"/>\n" +
-            "    <param name=\"append\" value=\"true\"/>\n" +
-            "    <param name=\"maxFileSize\" value=\"{{logsearch_audit_log_maxfilesize}}MB\"/>\n" +
-            "    <param name=\"maxBackupIndex\" value=\"{{logsearch_audit_log_maxbackupindex}}\"/>\n" +
-            "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\"/>\n" +
-            "  </appender>\n" +
-            "\n" +
-            "  <appender name=\"performance_analyzer_json\" class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
-            "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch-performance.json\"/>\n" +
-            "    <param name=\"Threshold\" value=\"info\"/>\n" +
-            "    <param name=\"append\" value=\"true\"/>\n" +
-            "    <param name=\"maxFileSize\" value=\"{{logsearch_perf_log_maxfilesize}}MB\"/>\n" +
-            "    <param name=\"maxBackupIndex\" value=\"{{logsearch_perf_log_maxbackupindex}}\"/>\n" +
-            "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\"/>\n" +
-            "  </appender>\n" +
-            "\n" +
-            "  <logger name=\"org.apache.ambari.logsearch.audit\" additivity=\"true\">\n" +
-            "     <appender-ref ref=\"audit_rolling_file_json\"/>\n" +
-            "  </logger>\n" +
-            "\n" +
-            "  <logger name=\"org.apache.ambari.logsearch.performance\" additivity=\"false\">\n" +
-            "    <appender-ref ref=\"performance_analyzer_json\"/>\n" +
-            "  </logger>\n" +
-            "\n" +
-            "  <category name=\"org.apache.ambari.logsearch\" additivity=\"false\">\n" +
-            "    <priority value=\"info\"/>\n" +
-            "    <appender-ref ref=\"rolling_file_json\"/>\n" +
-            "  </category>");
-
-    expectedLogSearchLog4j.put("logsearch_log_maxfilesize", "11");
-    expectedLogSearchLog4j.put("logsearch_log_maxbackupindex", "12");
-    expectedLogSearchLog4j.put("logsearch_json_log_maxfilesize", "13");
-    expectedLogSearchLog4j.put("logsearch_json_log_maxbackupindex", "14");
-    expectedLogSearchLog4j.put("logsearch_audit_log_maxfilesize", "15");
-    expectedLogSearchLog4j.put("logsearch_audit_log_maxbackupindex", "16");
-    expectedLogSearchLog4j.put("logsearch_perf_log_maxfilesize", "17");
-    expectedLogSearchLog4j.put("logsearch_perf_log_maxbackupindex", "18");
-
-    Config mockLogSearchLog4j = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("logsearch-log4j")).andReturn(mockLogSearchLog4j).atLeastOnce();
-    expect(mockLogSearchLog4j.getProperties()).andReturn(oldLogSearchLog4j).anyTimes();
-    Capture<Map<String, String>> logSearchLog4jCapture = EasyMock.newCapture();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(logSearchLog4jCapture), anyString(),
-        EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
-
-    replay(clusters, cluster);
-    replay(controller, injector2);
-    replay(mockLogSearchProperties, mockLogFeederEnv, mockLogSearchEnv, mockLogFeederLog4j, mockLogSearchLog4j);
-    new UpgradeCatalog250(injector2).updateLogSearchConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedLogSearchProperties = logSearchPropertiesCapture.getValue();
-    assertTrue(Maps.difference(expectedLogSearchProperties, updatedLogSearchProperties).areEqual());
-
-    Map<String, String> updatedLogFeederEnv = logFeederEnvCapture.getValue();
-    assertTrue(Maps.difference(expectedLogFeederEnv, updatedLogFeederEnv).areEqual());
-
-    Map<String, String> updatedLogSearchEnv = logSearchEnvCapture.getValue();
-    assertTrue(Maps.difference(expectedLogSearchEnv, updatedLogSearchEnv).areEqual());
-
-    Map<String, String> updatedLogFeederLog4j = logFeederLog4jCapture.getValue();
-    assertTrue(Maps.difference(expectedLogFeederLog4j, updatedLogFeederLog4j).areEqual());
-
-    Map<String, String> updatedLogSearchLog4j = logSearchLog4jCapture.getValue();
-    assertTrue(Maps.difference(expectedLogSearchLog4j, updatedLogSearchLog4j).areEqual());
-  }
-
-  @Test
-  public void testAmbariInfraUpdateConfigs() throws Exception {
-    reset(clusters, cluster);
-    expect(clusters.getClusters()).andReturn(ImmutableMap.of("normal", cluster)).once();
-
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-        .addMockedMethod("createConfiguration")
-        .addMockedMethod("getClusters", new Class[]{})
-        .addMockedMethod("createConfig")
-        .withConstructor(actionManager, clusters, injector)
-        .createNiceMock();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-
-    Map<String, String> oldInfraSolrEnv = ImmutableMap.of(
-        "content", "SOLR_SSL_TRUST_STORE={{infra_solr_keystore_location}}\n" +
-            "SOLR_SSL_TRUST_STORE_PASSWORD={{infra_solr_keystore_password}}\n" +
-            "SOLR_KERB_NAME_RULES={{infra_solr_kerberos_name_rules}}\n" +
-            "SOLR_AUTHENTICATION_OPTS=\" -DauthenticationPlugin=org.apache.solr.security.KerberosPlugin -Djava.security.auth.login.config=$SOLR_JAAS_FILE -Dsolr.kerberos.principal=${SOLR_KERB_PRINCIPAL} -Dsolr.kerberos.keytab=${SOLR_KERB_KEYTAB} -Dsolr.kerberos.cookie.domain=${SOLR_HOST} -Dsolr.kerberos.name.rules=${SOLR_KERB_NAME_RULES}\"");
-
-    Map<String, String> expectedInfraSolrEnv = ImmutableMap.of(
-        "content", "SOLR_SSL_TRUST_STORE={{infra_solr_truststore_location}}\n" +
-            "SOLR_SSL_TRUST_STORE_PASSWORD={{infra_solr_truststore_password}}\n" +
-            "SOLR_KERB_NAME_RULES=\"{{infra_solr_kerberos_name_rules}}\"\n" +
-            "SOLR_AUTHENTICATION_OPTS=\" -DauthenticationPlugin=org.apache.solr.security.KerberosPlugin -Djava.security.auth.login.config=$SOLR_JAAS_FILE -Dsolr.kerberos.principal=${SOLR_KERB_PRINCIPAL} -Dsolr.kerberos.keytab=${SOLR_KERB_KEYTAB} -Dsolr.kerberos.cookie.domain=${SOLR_HOST}\"");
-
-    Config mockInfraSolrEnv = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("infra-solr-env")).andReturn(mockInfraSolrEnv).atLeastOnce();
-    expect(mockInfraSolrEnv.getProperties()).andReturn(oldInfraSolrEnv).anyTimes();
-    Capture<Map<String, String>> infraSolrEnvCapture = EasyMock.newCapture();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(infraSolrEnvCapture), anyString(),
-        EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
-
-    Map<String, String> oldInfraSolrLog4j = ImmutableMap.of(
-        "content", "log4j.appender.file.MaxFileSize=15MB\n" +
-            "log4j.appender.file.MaxBackupIndex=5\n");
-
-    Map<String, String> expectedInfraSolrLog4j = ImmutableMap.of(
-        "content", "log4j.appender.file.MaxFileSize={{infra_log_maxfilesize}}MB\n" +
-            "log4j.appender.file.MaxBackupIndex={{infra_log_maxbackupindex}}\n",
-        "infra_log_maxfilesize", "15",
-        "infra_log_maxbackupindex", "5");
-
-    Config mockInfraSolrLog4j = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("infra-solr-log4j")).andReturn(mockInfraSolrLog4j).atLeastOnce();
-    expect(mockInfraSolrLog4j.getProperties()).andReturn(oldInfraSolrLog4j).anyTimes();
-    Capture<Map<String, String>> infraSolrLog4jCapture = EasyMock.newCapture();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(infraSolrLog4jCapture), anyString(),
-        EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
-
-    Map<String, String> oldInfraSolrClientLog4j = ImmutableMap.of(
-        "content", "log4j.appender.file.File\u003d{{infra_client_log|default(\u0027/var/log/ambari-infra-solr-client/solr-client.log\u0027)}}\n" +
-            "log4j.appender.file.MaxFileSize=55MB\n" +
-            "log4j.appender.file.MaxBackupIndex=10\n");
-
-    Map<String, String> expectedInfraSolrClientLog4j = ImmutableMap.of(
-        "content", "log4j.appender.file.File\u003d{{solr_client_log|default(\u0027/var/log/ambari-infra-solr-client/solr-client.log\u0027)}}\n" +
-            "log4j.appender.file.MaxFileSize={{solr_client_log_maxfilesize}}MB\n" +
-            "log4j.appender.file.MaxBackupIndex={{solr_client_log_maxbackupindex}}\n",
-        "infra_client_log_maxfilesize", "55",
-        "infra_client_log_maxbackupindex", "10");
-
-    Config mockInfraSolrClientLog4j = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("infra-solr-client-log4j")).andReturn(mockInfraSolrClientLog4j).atLeastOnce();
-    expect(mockInfraSolrClientLog4j.getProperties()).andReturn(oldInfraSolrClientLog4j).anyTimes();
-    Capture<Map<String, String>> infraSolrClientLog4jCapture = EasyMock.newCapture();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(infraSolrClientLog4jCapture), anyString(),
-        EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
-
-    replay(clusters, cluster);
-    replay(controller, injector2);
-    replay(mockInfraSolrEnv, mockInfraSolrLog4j, mockInfraSolrClientLog4j);
-    new UpgradeCatalog250(injector2).updateAmbariInfraConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedInfraSolrEnv = infraSolrEnvCapture.getValue();
-    assertTrue(Maps.difference(expectedInfraSolrEnv, updatedInfraSolrEnv).areEqual());
-
-    Map<String, String> updatedInfraSolrLog4j = infraSolrLog4jCapture.getValue();
-    assertTrue(Maps.difference(expectedInfraSolrLog4j, updatedInfraSolrLog4j).areEqual());
-
-    Map<String, String> updatedInfraSolrClientLog4j = infraSolrClientLog4jCapture.getValue();
-    assertTrue(Maps.difference(expectedInfraSolrClientLog4j, updatedInfraSolrClientLog4j).areEqual());
-  }
-
-  @Test
-  public void testUpdateHiveConfigs() throws Exception {
-    reset(clusters, cluster);
-    expect(clusters.getClusters()).andReturn(ImmutableMap.of("normal", cluster)).anyTimes();
-
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-        .addMockedMethod("createConfiguration")
-        .addMockedMethod("getClusters", new Cla

<TRUNCATED>

[18/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog240.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog240.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog240.java
deleted file mode 100644
index cf0c37d..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog240.java
+++ /dev/null
@@ -1,3020 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import com.google.common.collect.Lists;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-import com.google.gson.JsonPrimitive;
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import com.google.inject.persist.Transactional;
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.agent.RecoveryConfigHelper;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
-import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
-import org.apache.ambari.server.orm.dao.ArtifactDAO;
-import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.PermissionDAO;
-import org.apache.ambari.server.orm.dao.PrincipalDAO;
-import org.apache.ambari.server.orm.dao.PrincipalTypeDAO;
-import org.apache.ambari.server.orm.dao.PrivilegeDAO;
-import org.apache.ambari.server.orm.dao.RemoteAmbariClusterDAO;
-import org.apache.ambari.server.orm.dao.RequestScheduleDAO;
-import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
-import org.apache.ambari.server.orm.dao.RoleAuthorizationDAO;
-import org.apache.ambari.server.orm.dao.UserDAO;
-import org.apache.ambari.server.orm.dao.ViewInstanceDAO;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.ArtifactEntity;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.PermissionEntity;
-import org.apache.ambari.server.orm.entities.PrincipalEntity;
-import org.apache.ambari.server.orm.entities.PrincipalTypeEntity;
-import org.apache.ambari.server.orm.entities.PrivilegeEntity;
-import org.apache.ambari.server.orm.entities.RemoteAmbariClusterEntity;
-import org.apache.ambari.server.orm.entities.RequestScheduleEntity;
-import org.apache.ambari.server.orm.entities.ResourceEntity;
-import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
-import org.apache.ambari.server.orm.entities.RoleAuthorizationEntity;
-import org.apache.ambari.server.orm.entities.UserEntity;
-import org.apache.ambari.server.orm.entities.ViewEntityEntity;
-import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
-import org.apache.ambari.server.security.authorization.ResourceType;
-import org.apache.ambari.server.security.authorization.User;
-import org.apache.ambari.server.security.authorization.Users;
-import org.apache.ambari.server.state.AlertFirmness;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.ConfigHelper;
-import org.apache.ambari.server.state.PropertyInfo;
-import org.apache.ambari.server.state.RepositoryType;
-import org.apache.ambari.server.state.SecurityType;
-import org.apache.ambari.server.state.ServiceInfo;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.StackInfo;
-import org.apache.ambari.server.state.State;
-import org.apache.ambari.server.state.kerberos.KerberosComponentDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptorFactory;
-import org.apache.ambari.server.state.kerberos.KerberosIdentityDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosKeytabDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosPrincipalDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosServiceDescriptor;
-import org.apache.ambari.server.view.DefaultMasker;
-import org.apache.ambari.view.ClusterType;
-import org.apache.ambari.view.MaskException;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.jdbc.support.JdbcUtils;
-import org.apache.commons.lang.StringUtils;
-import javax.persistence.EntityManager;
-import javax.persistence.Query;
-import javax.persistence.TypedQuery;
-import java.sql.Clob;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.UUID;
-import java.util.concurrent.atomic.AtomicLong;
-
-/**
- * Upgrade catalog for version 2.4.0.
- */
-public class UpgradeCatalog240 extends AbstractUpgradeCatalog {
-
-  protected static final String ADMIN_PERMISSION_TABLE = "adminpermission";
-  protected static final String PRINCIPAL_ID_COL = "principal_id";
-  protected static final String ALERT_DEFINITION_TABLE = "alert_definition";
-  protected static final String ALERT_TARGET_TABLE = "alert_target";
-  protected static final String ALERT_TARGET_ENABLED_COLUMN = "is_enabled";
-  protected static final String ALERT_CURRENT_TABLE = "alert_current";
-  protected static final String ALERT_CURRENT_OCCURRENCES_COLUMN = "occurrences";
-  protected static final String ALERT_CURRENT_FIRMNESS_COLUMN = "firmness";
-  protected static final String HELP_URL_COLUMN = "help_url";
-  protected static final String REPEAT_TOLERANCE_COLUMN = "repeat_tolerance";
-  protected static final String REPEAT_TOLERANCE_ENABLED_COLUMN = "repeat_tolerance_enabled";
-  protected static final String PERMISSION_ID_COL = "permission_name";
-  protected static final String SORT_ORDER_COL = "sort_order";
-  protected static final String REPO_VERSION_TABLE = "repo_version";
-  protected static final String HOST_ROLE_COMMAND_TABLE = "host_role_command";
-  protected static final String SERVICE_COMPONENT_DS_TABLE = "servicecomponentdesiredstate";
-  protected static final String HOST_COMPONENT_DS_TABLE = "hostcomponentdesiredstate";
-  protected static final String HOST_COMPONENT_STATE_TABLE = "hostcomponentstate";
-  protected static final String SERVICE_COMPONENT_HISTORY_TABLE = "servicecomponent_history";
-  protected static final String UPGRADE_TABLE = "upgrade";
-  protected static final String STACK_TABLE = "stack";
-  protected static final String CLUSTER_TABLE = "clusters";
-  protected static final String CLUSTER_UPGRADE_ID_COLUMN = "upgrade_id";
-  protected static final String YARN_ENV_CONFIG = "yarn-env";
-  protected static final String CAPACITY_SCHEDULER_CONFIG = "capacity-scheduler";
-  protected static final String WEBHCAT_SITE_CONFIG = "webhcat-site";
-  protected static final String TEZ_SITE_CONFIG = "tez-site";
-  protected static final String MAPRED_SITE_CONFIG = "mapred-site";
-  public static final String DESIRED_VERSION_COLUMN_NAME = "desired_version";
-  public static final String BLUEPRINT_SETTING_TABLE = "blueprint_setting";
-  public static final String BLUEPRINT_NAME_COL = "blueprint_name";
-  public static final String SETTING_NAME_COL = "setting_name";
-  public static final String SETTING_DATA_COL = "setting_data";
-  public static final String ID = "id";
-  public static final String BLUEPRINT_TABLE = "blueprint";
-  public static final String VIEWINSTANCE_TABLE = "viewinstance";
-  public static final String SHORT_URL_COLUMN = "short_url";
-  public static final String CLUSTER_HANDLE_COLUMN = "cluster_handle";
-  public static final String REQUESTSCHEDULE_TABLE = "requestschedule";
-  public static final String AUTHENTICATED_USER_ID_COLUMN = "authenticated_user_id";
-  protected static final String CLUSTER_VERSION_TABLE = "cluster_version";
-  protected static final String HOST_VERSION_TABLE = "host_version";
-  protected static final String TOPOLOGY_REQUEST_TABLE = "topology_request";
-  protected static final String PROVISION_ACTION_COL = "provision_action";
-  protected static final String PHOENIX_QUERY_SERVER_PRINCIPAL_KEY = "phoenix.queryserver.kerberos.principal";
-  protected static final String PHOENIX_QUERY_SERVER_KEYTAB_KEY = "phoenix.queryserver.keytab.file";
-  protected static final String DEFAULT_CONFIG_VERSION = "version1";
-  protected static final String SLIDER_SERVICE_NAME = "SLIDER";
-
-  private static final String OOZIE_ENV_CONFIG = "oozie-env";
-  private static final String SLIDER_CLIENT_CONFIG = "slider-client";
-  private static final String HIVE_ENV_CONFIG = "hive-env";
-  private static final String AMS_SITE = "ams-site";
-  public static final String TIMELINE_METRICS_SINK_COLLECTION_PERIOD = "timeline.metrics.sink.collection.period";
-  public static final String ONE_DIR_PER_PARITION_PROPERTY = "one_dir_per_partition";
-  public static final String VIEWURL_TABLE = "viewurl";
-  public static final String URL_ID_COLUMN = "url_id";
-  private static final String PRINCIPAL_TYPE_TABLE = "adminprincipaltype";
-  private static final String PRINCIPAL_TABLE = "adminprincipal";
-  protected static final String HBASE_SITE_CONFIG = "hbase-site";
-  protected static final String HBASE_SPNEGO_PRINCIPAL_KEY = "hbase.security.authentication.spnego.kerberos.principal";
-  protected static final String HBASE_SPNEGO_KEYTAB_KEY = "hbase.security.authentication.spnego.kerberos.keytab";
-  protected static final String EXTENSION_TABLE = "extension";
-  protected static final String EXTENSION_ID_COLUMN = "extension_id";
-  protected static final String EXTENSION_LINK_TABLE = "extensionlink";
-  protected static final String EXTENSION_LINK_ID_COLUMN = "link_id";
-
-  private static final Map<String, Integer> ROLE_ORDER;
-  private static final String AMS_HBASE_SITE = "ams-hbase-site";
-  private static final String HBASE_RPC_TIMEOUT_PROPERTY = "hbase.rpc.timeout";
-  private static final String AMS_HBASE_SITE_NORMALIZER_ENABLED_PROPERTY = "hbase.normalizer.enabled";
-  public static final String PRECISION_TABLE_TTL_PROPERTY = "timeline.metrics.host.aggregator.ttl";
-  public static final String CLUSTER_SECOND_TABLE_TTL_PROPERTY = "timeline.metrics.cluster.aggregator.second.ttl";
-
-  static {
-    // Manually create role order since there really isn't any mechanism for this
-    ROLE_ORDER = new HashMap<String, Integer>();
-    ROLE_ORDER.put("AMBARI.ADMINISTRATOR", 1);
-    ROLE_ORDER.put("CLUSTER.ADMINISTRATOR", 2);
-    ROLE_ORDER.put("CLUSTER.OPERATOR", 3);
-    ROLE_ORDER.put("SERVICE.ADMINISTRATOR", 4);
-    ROLE_ORDER.put("SERVICE.OPERATOR", 5);
-    ROLE_ORDER.put("CLUSTER.USER", 6);
-  }
-
-  @Inject
-  UserDAO userDAO;
-
-  @Inject
-  PermissionDAO permissionDAO;
-
-  @Inject
-  PrivilegeDAO privilegeDAO;
-
-  @Inject
-  ResourceTypeDAO resourceTypeDAO;
-
-  @Inject
-  ClusterDAO clusterDAO;
-
-  @Inject
-  PrincipalTypeDAO principalTypeDAO;
-
-  @Inject
-  PrincipalDAO principalDAO;
-
-  @Inject
-  RequestScheduleDAO requestScheduleDAO;
-
-  @Inject
-  Users users;
-
-  @Inject
-  Configuration config;
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog240.class);
-
-  private static final String SETTING_TABLE = "setting";
-
-  protected static final String SERVICE_COMPONENT_DESIRED_STATE_TABLE = "servicecomponentdesiredstate";
-  protected static final String RECOVERY_ENABLED_COL = "recovery_enabled";
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   *
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog240(Injector injector) {
-    super(injector);
-    injector.injectMembers(this);
-  }
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.4.0";
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getSourceVersion() {
-    return "2.3.0";
-  }
-
-  public static final String CLUSTER_TYPE_COLUMN = "cluster_type";
-  public static final String REMOTE_AMBARI_CLUSTER_TABLE = "remoteambaricluster";
-  public static final String REMOTE_AMBARI_CLUSTER_SERVICE_TABLE = "remoteambariclusterservice";
-
-  public static final String CLUSTER_ID = "cluster_id";
-  public static final String SERVICE_NAME = "service_name";
-  public static final String CLUSTER_NAME = "name";
-
-
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-    updateAdminPermissionTable();
-    updateServiceComponentDesiredStateTable();
-    createExtensionTable();
-    createExtensionLinkTable();
-    createSettingTable();
-    updateRepoVersionTableDDL();
-    updateServiceComponentDesiredStateTableDDL();
-    createServiceComponentHistoryTable();
-    updateClusterTableDDL();
-    updateAlertDefinitionTable();
-    updateAlertCurrentTable();
-    updateAlertTargetTable();
-    createBlueprintSettingTable();
-    updateHostRoleCommandTableDDL();
-    createViewUrlTableDDL();
-    updateViewInstanceEntityTable();
-    createRemoteClusterTable();
-    updateViewInstanceTable();
-    updateRequestScheduleEntityTable();
-    updateTopologyRequestTable();
-  }
-
-  private void createRemoteClusterTable() throws SQLException {
-
-    List<DBColumnInfo> columns = new ArrayList<>();
-    LOG.info("Creating {} table", REMOTE_AMBARI_CLUSTER_TABLE);
-    columns.add(new DBColumnInfo(CLUSTER_ID, Long.class, null, null, false));
-    columns.add(new DBColumnInfo(CLUSTER_NAME, String.class, 255, null, false));
-    columns.add(new DBColumnInfo("url", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("username", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("password", String.class, 255, null, false));
-    dbAccessor.createTable(REMOTE_AMBARI_CLUSTER_TABLE, columns, CLUSTER_ID);
-    dbAccessor.addUniqueConstraint(REMOTE_AMBARI_CLUSTER_TABLE , "UQ_remote_ambari_cluster" , CLUSTER_NAME);
-    addSequence("remote_cluster_id_seq", 1L, false);
-
-    List<DBColumnInfo> remoteClusterServiceColumns = new ArrayList<>();
-    LOG.info("Creating {} table", REMOTE_AMBARI_CLUSTER_SERVICE_TABLE);
-    remoteClusterServiceColumns.add(new DBColumnInfo(ID, Long.class, null, null, false));
-    remoteClusterServiceColumns.add(new DBColumnInfo(SERVICE_NAME, String.class, 255, null, false));
-    remoteClusterServiceColumns.add(new DBColumnInfo(CLUSTER_ID, Long.class, null, null, false));
-    dbAccessor.createTable(REMOTE_AMBARI_CLUSTER_SERVICE_TABLE, remoteClusterServiceColumns, ID);
-    dbAccessor.addFKConstraint(REMOTE_AMBARI_CLUSTER_SERVICE_TABLE, "FK_remote_ambari_cluster_id",
-      CLUSTER_ID, REMOTE_AMBARI_CLUSTER_TABLE, CLUSTER_ID, false);
-    addSequence("remote_cluster_service_id_seq", 1L, false);
-
-  }
-
-  private void createViewUrlTableDDL() throws SQLException {
-    List<DBColumnInfo> columns = new ArrayList<>();
-
-    //  Add setting table
-    LOG.info("Creating " + VIEWURL_TABLE + " table");
-
-    columns.add(new DBColumnInfo(URL_ID_COLUMN, Long.class, null, null, false));
-    columns.add(new DBColumnInfo("url_name", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("url_suffix", String.class, 255, null, false));
-    dbAccessor.createTable(VIEWURL_TABLE, columns, URL_ID_COLUMN);
-    addSequence("viewurl_id_seq", 1L, false);
-  }
-
-  private void updateViewInstanceEntityTable() throws SQLException {
-    dbAccessor.addColumn(VIEWINSTANCE_TABLE,
-      new DBColumnInfo(SHORT_URL_COLUMN, Long.class, null, null, true));
-    dbAccessor.addFKConstraint(VIEWINSTANCE_TABLE, "FK_instance_url_id",
-      SHORT_URL_COLUMN, VIEWURL_TABLE, URL_ID_COLUMN, false);
-    dbAccessor.addColumn(VIEWINSTANCE_TABLE,
-      new DBColumnInfo(CLUSTER_TYPE_COLUMN, String.class, 100, ClusterType.LOCAL_AMBARI.name(), false));
-  }
-
-  private void updateRequestScheduleEntityTable() throws SQLException {
-    dbAccessor.addColumn(REQUESTSCHEDULE_TABLE,
-      new DBColumnInfo(AUTHENTICATED_USER_ID_COLUMN, Integer.class, null, null, true));
-  }
-
-  private void updateClusterTableDDL() throws SQLException {
-    dbAccessor.addColumn(CLUSTER_TABLE, new DBColumnInfo(CLUSTER_UPGRADE_ID_COLUMN, Long.class, null, null, true));
-
-    dbAccessor.addFKConstraint(CLUSTER_TABLE, "FK_clusters_upgrade_id",
-      CLUSTER_UPGRADE_ID_COLUMN, UPGRADE_TABLE, "upgrade_id", false);
-  }
-
-  @Override
-  protected void executePreDMLUpdates() throws AmbariException, SQLException {
-    //To change body of implemented methods use File | Settings | File Templates.
-  }
-
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    addNewConfigurationsFromXml();
-    updateAlerts();
-    setRoleSortOrder();
-    addSettingPermission();
-    addViewOperationalLogsPermission();
-    addManageUserPersistedDataPermission();
-    allowClusterOperatorToManageCredentials();
-    updateHDFSConfigs();
-    updateHIVEConfigs();
-    updateAMSConfigs();
-    updateClusterEnv();
-    updateSequenceForView();
-    updateHostRoleCommandTableDML();
-    updateKerberosConfigs();
-    updateYarnEnv();
-    updatePhoenixConfigs();
-    updateSparkConfigs();
-    updateHBaseConfigs();
-    updateFalconConfigs();
-    updateKerberosDescriptorArtifacts();
-    removeHiveOozieDBConnectionConfigs();
-    updateClustersAndHostsVersionStateTableDML();
-    removeStandardDeviationAlerts();
-    removeAtlasMetaserverAlert();
-    updateClusterInheritedPermissionsConfig();
-    consolidateUserRoles();
-    createRolePrincipals();
-    updateHDFSWidgetDefinition();
-    updateTezViewProperty();
-    upgradeCapSchedulerView();
-    fixAuthorizationDescriptions();
-    removeAuthorizations();
-    addConnectionTimeoutParamForWebAndMetricAlerts();
-    addSliderClientConfig();
-    updateRequestScheduleEntityUserIds();
-    updateRecoveryConfigurationDML();
-    updatePigSmokeTestEntityClass();
-    updateRangerHbasePluginProperties();
-    adjustHiveJobTimestamps();
-  }
-
-  /**
-   * Populates authenticated_user_id field by correct user id calculated from user name
-   * @throws SQLException
-   */
-  protected void updateRequestScheduleEntityUserIds() throws SQLException {
-    List<RequestScheduleEntity> requestScheduleEntities = requestScheduleDAO.findAll();
-    for (RequestScheduleEntity requestScheduleEntity : requestScheduleEntities) {
-      String createdUserName = requestScheduleEntity.getCreateUser();
-
-      if (createdUserName != null) {
-        User user = users.getUserIfUnique(createdUserName);
-
-        if (user != null && StringUtils.equals(user.getUserName(), createdUserName)) {
-          requestScheduleEntity.setAuthenticatedUserId(user.getUserId());
-          requestScheduleDAO.merge(requestScheduleEntity);
-        }
-      }
-    }
-  }
-
-  protected void updateClusterInheritedPermissionsConfig() throws SQLException {
-    insertClusterInheritedPrincipal("ALL.CLUSTER.ADMINISTRATOR");
-    insertClusterInheritedPrincipal("ALL.CLUSTER.OPERATOR");
-    insertClusterInheritedPrincipal("ALL.CLUSTER.USER");
-    insertClusterInheritedPrincipal("ALL.SERVICE.ADMINISTRATOR");
-    insertClusterInheritedPrincipal("ALL.SERVICE.OPERATIOR");
-  }
-
-  private void insertClusterInheritedPrincipal(String name) {
-    PrincipalTypeEntity principalTypeEntity = new PrincipalTypeEntity();
-    principalTypeEntity.setName(name);
-    principalTypeEntity = principalTypeDAO.merge(principalTypeEntity);
-
-    PrincipalEntity principalEntity = new PrincipalEntity();
-    principalEntity.setPrincipalType(principalTypeEntity);
-    principalDAO.create(principalEntity);
-  }
-  private static final String NAME_PREFIX = "DS_";
-
-  private String getEntityName(ViewEntityEntity entity) {
-    String className = entity.getClassName();
-    String[] parts = className.split("\\.");
-    String simpleClassName = parts[parts.length - 1];
-
-    if (entity.getViewInstance().alterNames()) {
-      return NAME_PREFIX + simpleClassName + "_" + entity.getId();
-    }
-    return simpleClassName + entity.getId();
-  }
-
-  /**
-   * get all entries of viewentity
-   * find all the table names by parsing class_name
-   * create all the sequence names by appending _id_seq
-   * query each dynamic table to find the max of id
-   * insert into ambari_sequence name and counter for each item
-   */
-  protected void updateSequenceForView() {
-    LOG.info("updateSequenceForView called.");
-    EntityManager entityManager = getEntityManagerProvider().get();
-    TypedQuery<ViewEntityEntity> viewEntityQuery = entityManager.createQuery("SELECT vee FROM ViewEntityEntity vee", ViewEntityEntity.class);
-    List<ViewEntityEntity> viewEntities = viewEntityQuery.getResultList();
-    LOG.info("Received view Entities : {}, length : {}", viewEntities, viewEntities.size());
-
-    // as the id fields are string in these entities we will have to get all ids and convert to int and find max.
-    String selectIdsFormat = "select %s from %s";
-    String insertQuery = "insert into ambari_sequences values ('%s',%d)";
-    for (ViewEntityEntity viewEntity : viewEntities) {
-      LOG.info("Working with viewEntity : {} : {} ", viewEntity, viewEntity.getViewName() + viewEntity.getViewInstance());
-      String tableName = getEntityName(viewEntity);
-      String seqName = tableName.toLowerCase() + "_id_seq";
-      try {
-        entityManager.getTransaction().begin();
-        String selectIdsQueryString = String.format(selectIdsFormat, NAME_PREFIX + viewEntity.getIdProperty(), tableName).toLowerCase();
-        LOG.info("executing max query string {}", selectIdsQueryString);
-        Query selectIdsQuery = entityManager.createNativeQuery(selectIdsQueryString);
-        List<String> ids = selectIdsQuery.getResultList();
-        LOG.info("Received ids : {}", ids);
-        int maxId = 0;
-        if (null != ids && ids.size() != 0) {
-          for (String id : ids) {
-            try {
-              Integer intId = Integer.parseInt(id);
-              maxId = Math.max(intId, maxId);
-            } catch (NumberFormatException e) {
-              LOG.error("the id was non integer : id : {}. So ignoring.", id);
-            }
-          }
-        }
-
-        String insertQueryString = String.format(insertQuery, seqName, maxId).toLowerCase();
-        LOG.info("Executing insert query : {}", insertQueryString);
-        Query insertQ = entityManager.createNativeQuery(insertQueryString);
-        int rowsChanged = insertQ.executeUpdate();
-        entityManager.getTransaction().commit();
-        LOG.info("executing insert resulted in {} row changes.", rowsChanged);
-      } catch (Exception e) { // when the entity table is not yet created or other exception.
-        entityManager.getTransaction().rollback();
-        LOG.info("Error (can be ignored) {}", e.getMessage());
-        LOG.debug("Exception occured while updating : {}",viewEntity.getViewName() + viewEntity.getViewInstance(), e);
-      }
-    }
-  }
-
-
-  /**
-   * get all entries of viewentity
-   * find all the table names by parsing class_name
-   * update jobimpls creation timestamp * 1000
-   */
-  protected void adjustHiveJobTimestamps() {
-    LOG.info("updateSequenceForView called.");
-    EntityManager entityManager = getEntityManagerProvider().get();
-    TypedQuery<ViewEntityEntity> viewEntityQuery = entityManager.createQuery("SELECT vee FROM ViewEntityEntity vee where vee.className = 'org.apache.ambari.view.hive.resources.jobs.viewJobs.JobImpl'", ViewEntityEntity.class);
-    List<ViewEntityEntity> viewEntities = viewEntityQuery.getResultList();
-    LOG.info("Received JobImpl view Entities : {}, length : {}", viewEntities, viewEntities.size());
-
-    String selectIdsFormat = "update %s set ds_datesubmitted = ds_datesubmitted * 1000";
-    for (ViewEntityEntity viewEntity : viewEntities) {
-      LOG.info("Working with JobImpl viewEntity : {} : {} ", viewEntity, viewEntity.getViewName() + ":" + viewEntity.getViewInstanceName() + ":" + viewEntity.getClassName());
-      String tableName = getEntityName(viewEntity);
-      try {
-        entityManager.getTransaction().begin();
-        String updatesQueryString = String.format(selectIdsFormat, tableName).toLowerCase();
-        LOG.info("executing update query string for jobimpl {}", updatesQueryString);
-        Query updateQuery = entityManager.createNativeQuery(updatesQueryString);
-        int rowsChanged = updateQuery.executeUpdate();
-        entityManager.getTransaction().commit();
-        LOG.info("executing update on jobimpl resulted in {} row changes.", rowsChanged);
-      } catch (Exception e) { // when the entity table is not yet created or other exception.
-        entityManager.getTransaction().rollback();
-        LOG.info("Error (can be ignored) {}", e.getMessage());
-        LOG.debug("Exception occured while updating : {}",viewEntity.getViewName() + viewEntity.getViewInstance(), e);
-      }
-    }
-  }
-
-  private void createExtensionTable() throws SQLException {
-    List<DBColumnInfo> columns = new ArrayList<>();
-
-    // Add extension table
-    LOG.info("Creating " + EXTENSION_TABLE + " table");
-
-    columns.add(new DBColumnInfo(EXTENSION_ID_COLUMN, Long.class, null, null, false));
-    columns.add(new DBColumnInfo("extension_name", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("extension_version", String.class, 255, null, false));
-    dbAccessor.createTable(EXTENSION_TABLE, columns, EXTENSION_ID_COLUMN);
-
-    // create UNIQUE constraint, ensuring column order matches SQL files
-    String[] uniqueColumns = new String[] { "extension_name", "extension_version" };
-    dbAccessor.addUniqueConstraint(EXTENSION_TABLE, "UQ_extension", uniqueColumns);
-
-    addSequence("extension_id_seq", 0L, false);
-  }
-
-  private void createExtensionLinkTable() throws SQLException {
-    List<DBColumnInfo> columns = new ArrayList<>();
-
-    // Add extension link table
-    LOG.info("Creating " + EXTENSION_LINK_TABLE + " table");
-
-    columns.add(new DBColumnInfo(EXTENSION_LINK_ID_COLUMN, Long.class, null, null, false));
-    columns.add(new DBColumnInfo("stack_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo(EXTENSION_ID_COLUMN, Long.class, null, null, false));
-    dbAccessor.createTable(EXTENSION_LINK_TABLE, columns, EXTENSION_LINK_ID_COLUMN);
-
-    // create UNIQUE constraint, ensuring column order matches SQL files
-    String[] uniqueColumns = new String[] { "stack_id", EXTENSION_ID_COLUMN };
-    dbAccessor.addUniqueConstraint(EXTENSION_LINK_TABLE, "UQ_extension_link", uniqueColumns);
-
-    dbAccessor.addFKConstraint(EXTENSION_LINK_TABLE, "FK_extensionlink_extension_id",
-      EXTENSION_ID_COLUMN, EXTENSION_TABLE, EXTENSION_ID_COLUMN, false);
-
-    dbAccessor.addFKConstraint(EXTENSION_LINK_TABLE, "FK_extensionlink_stack_id",
-      "stack_id", STACK_TABLE, "stack_id", false);
-
-    addSequence("link_id_seq", 0L, false);
-  }
-
-  private void createSettingTable() throws SQLException {
-    List<DBColumnInfo> columns = new ArrayList<>();
-
-    //  Add setting table
-    LOG.info("Creating " + SETTING_TABLE + " table");
-
-    columns.add(new DBColumnInfo(ID, Long.class, null, null, false));
-    columns.add(new DBColumnInfo("name", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("setting_type", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("content", String.class, 3000, null, false));
-    columns.add(new DBColumnInfo("updated_by", String.class, 255, "_db", false));
-    columns.add(new DBColumnInfo("update_timestamp", Long.class, null, null, false));
-    dbAccessor.createTable(SETTING_TABLE, columns, ID);
-    addSequence("setting_id_seq", 0L, false);
-  }
-
-  protected void addSettingPermission() throws SQLException {
-    addRoleAuthorization("AMBARI.MANAGE_SETTINGS", "Manage settings", Collections.singleton("AMBARI.ADMINISTRATOR:AMBARI"));
-  }
-
-  protected void addViewOperationalLogsPermission() throws SQLException {
-    Collection<String> roles = Arrays.asList(
-        "AMBARI.ADMINISTRATOR:AMBARI",
-        "CLUSTER.ADMINISTRATOR:CLUSTER",
-        "CLUSTER.OPERATOR:CLUSTER",
-        "SERVICE.ADMINISTRATOR:CLUSTER");
-
-    addRoleAuthorization("SERVICE.VIEW_OPERATIONAL_LOGS", "View service operational logs", roles);
-  }
-
-  /**
-   * Add 'MANAGE_USER_PERSISTED_DATA' permissions for CLUSTER.ADMINISTRATOR, SERVICE.OPERATOR, SERVICE.ADMINISTRATOR,
-   * CLUSTER.OPERATOR, AMBARI.ADMINISTRATOR.
-   *
-   */
-  protected void addManageUserPersistedDataPermission() throws SQLException {
-    Collection<String> roles = Arrays.asList(
-        "AMBARI.ADMINISTRATOR:AMBARI",
-        "CLUSTER.ADMINISTRATOR:CLUSTER",
-        "CLUSTER.OPERATOR:CLUSTER",
-        "SERVICE.ADMINISTRATOR:CLUSTER",
-        "SERVICE.OPERATOR:CLUSTER",
-        "CLUSTER.USER:CLUSTER");
-
-    addRoleAuthorization("CLUSTER.MANAGE_USER_PERSISTED_DATA", "Manage cluster-level user persisted data", roles);
-  }
-
-  /**
-   * Adds <code>CLUSTER.MANAGE_CREDENTIALS</code> to the set of authorizations a <code>CLUSTER.OPERATOR</code> can perform.
-   *
-   * @throws SQLException
-   */
-  protected void allowClusterOperatorToManageCredentials() throws SQLException {
-    addAuthorizationToRole("CLUSTER.OPERATOR", "CLUSTER", "CLUSTER.MANAGE_CREDENTIAL");
-  }
-
-  protected void removeHiveOozieDBConnectionConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(ambariManagementController.getClusters());
-
-    for (final Cluster cluster : clusterMap.values()) {
-      Config oozieEnv = cluster.getDesiredConfigByType(OOZIE_ENV_CONFIG);
-      if(oozieEnv != null) {
-        Map<String, String> oozieEnvProperties = oozieEnv.getProperties();
-        Set<String> removePropertiesSet = new HashSet<>();
-        if (oozieEnvProperties.containsKey("oozie_derby_database")) {
-          LOG.info("Removing property oozie_derby_database from " + OOZIE_ENV_CONFIG);
-          removePropertiesSet.add("oozie_derby_database");
-        }
-        if (oozieEnvProperties.containsKey("oozie_hostname")) {
-          LOG.info("Removing property oozie_hostname from " + OOZIE_ENV_CONFIG);
-          removePropertiesSet.add("oozie_hostname");
-        }
-        if (!removePropertiesSet.isEmpty()) {
-          removeConfigurationPropertiesFromCluster(cluster, OOZIE_ENV_CONFIG, removePropertiesSet);
-        }
-      }
-
-      Config hiveEnv = cluster.getDesiredConfigByType(HIVE_ENV_CONFIG);
-      if(hiveEnv != null) {
-        Map<String, String> hiveEnvProperties = hiveEnv.getProperties();
-        if (hiveEnvProperties.containsKey("hive_hostname")) {
-          LOG.info("Removing property hive_hostname from " + HIVE_ENV_CONFIG);
-          removeConfigurationPropertiesFromCluster(cluster, HIVE_ENV_CONFIG, Collections.singleton("hive_hostname"));
-        }
-      }
-    }
-  }
-
-  protected void addSliderClientConfig() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-    ConfigHelper configHelper = ambariManagementController.getConfigHelper();
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
-
-    for (final Cluster cluster : clusterMap.values()) {
-      Set<String> installedServices = cluster.getServices().keySet();
-      if (installedServices.contains(SLIDER_SERVICE_NAME)) {
-        Config sliderClientConfig = cluster.getDesiredConfigByType(SLIDER_CLIENT_CONFIG);
-        if (sliderClientConfig == null) {
-          configHelper.createConfigType(cluster, ambariManagementController, SLIDER_CLIENT_CONFIG,
-                  new HashMap<String, String>(), AUTHENTICATED_USER_NAME, "");
-        }
-      }
-    }
-  }
-
-  protected void updateAlerts() {
-    // map of alert_name -> property_name -> visibility_value
-    final Map<String, String> hdfsVisibilityMap = new HashMap<String, String>(){{
-      put("mergeHaMetrics", "HIDDEN");
-      put("appId", "HIDDEN");
-      put("metricName", "HIDDEN");
-    }};
-    final Map<String, String> defaultKeytabVisibilityMap = new HashMap<String, String>(){{
-      put("default.smoke.principal", "HIDDEN");
-      put("default.smoke.keytab", "HIDDEN");
-    }};
-
-    final Map<String, String> percentParameterMap = new HashMap<String, String>(){{
-      put("units", "%");
-      put("type", "PERCENT");
-    }};
-
-    Map<String, Map<String, String>> visibilityMap = new HashMap<String, Map<String, String>>(){{
-      put("hive_webhcat_server_status", new HashMap<String, String>(){{
-        put("default.smoke.user", "HIDDEN");
-      }});
-      put("hive_metastore_process", defaultKeytabVisibilityMap);
-      put("hive_server_process", defaultKeytabVisibilityMap);
-      put("zookeeper_server_process", new HashMap<String, String>(){{
-        put("socket.command", "HIDDEN");
-        put("socket.command.response", "HIDDEN");
-      }});
-    }};
-
-    Map<String, Map<String, String>> reportingPercentMap = new HashMap<String, Map<String, String>>(){{
-      put("hawq_segment_process_percent", percentParameterMap);
-      put("mapreduce_history_server_cpu", percentParameterMap);
-      put("yarn_nodemanager_webui_percent", percentParameterMap);
-      put("yarn_resourcemanager_cpu", percentParameterMap);
-      put("datanode_process_percent", percentParameterMap);
-      put("datanode_storage_percent", percentParameterMap);
-      put("journalnode_process_percent", percentParameterMap);
-      put("namenode_cpu", percentParameterMap);
-      put("namenode_hdfs_capacity_utilization", percentParameterMap);
-      put("datanode_storage", percentParameterMap);
-      put("datanode_heap_usage", percentParameterMap);
-      put("storm_supervisor_process_percent", percentParameterMap);
-      put("hbase_regionserver_process_percent", percentParameterMap);
-      put("hbase_master_cpu", percentParameterMap);
-      put("zookeeper_server_process_percent", percentParameterMap);
-      put("metrics_monitor_process_percent", percentParameterMap);
-      put("ams_metrics_collector_hbase_master_cpu", percentParameterMap);
-    }};
-
-    Map<String, Map<String, Integer>> reportingMultiplierMap = new HashMap<String, Map<String, Integer>>(){{
-      put("hawq_segment_process_percent", new HashMap<String, Integer>() {{
-        put("warning", 100);
-        put("critical", 100);
-      }});
-      put("yarn_nodemanager_webui_percent", new HashMap<String, Integer>() {{
-        put("warning", 100);
-        put("critical", 100);
-      }});
-      put("datanode_process_percent", new HashMap<String, Integer>() {{
-        put("warning", 100);
-        put("critical", 100);
-      }});
-      put("datanode_storage_percent", new HashMap<String, Integer>() {{
-        put("warning", 100);
-        put("critical", 100);
-      }});
-      put("journalnode_process_percent", new HashMap<String, Integer>() {{
-        put("warning", 100);
-        put("critical", 100);
-      }});
-      put("storm_supervisor_process_percent", new HashMap<String, Integer>() {{
-        put("warning", 100);
-        put("critical", 100);
-      }});
-      put("hbase_regionserver_process_percent", new HashMap<String, Integer>() {{
-        put("warning", 100);
-        put("critical", 100);
-      }});
-      put("zookeeper_server_process_percent", new HashMap<String, Integer>() {{
-        put("warning", 100);
-        put("critical", 100);
-      }});
-      put("metrics_monitor_process_percent", new HashMap<String, Integer>() {{
-        put("warning", 100);
-        put("critical", 100);
-      }});
-    }};
-
-    Map<String, Map<String, Integer>> scriptAlertMultiplierMap = new HashMap<String, Map<String, Integer>>(){{
-      put("ambari_agent_disk_usage", new HashMap<String, Integer>() {{
-        put("percent.used.space.warning.threshold", 100);
-        put("percent.free.space.critical.threshold", 100);
-      }});
-      put("namenode_last_checkpoint", new HashMap<String, Integer>() {{
-        put("checkpoint.time.warning.threshold", 100);
-        put("checkpoint.time.critical.threshold", 100);
-      }});
-    }};
-
-    String newNameservicePropertyValue = "{{hdfs-site/dfs.internal.nameservices}}";
-    final Set<String> alertNamesForNameserviceUpdate = new HashSet<String>() {{
-      add("namenode_webui");
-      add("namenode_hdfs_blocks_health");
-      add("namenode_hdfs_pending_deletion_blocks");
-      add("namenode_rpc_latency");
-      add("namenode_directory_status");
-      add("datanode_health_summary");
-      add("namenode_cpu");
-      add("namenode_hdfs_capacity_utilization");
-    }};
-
-    // list of alerts that need to get property updates
-    Set<String> alertNamesForPropertyUpdates = new HashSet<String>() {{
-      add("hawq_segment_process_percent");
-      add("mapreduce_history_server_cpu");
-      add("yarn_nodemanager_webui_percent");
-      add("yarn_resourcemanager_cpu");
-      add("datanode_process_percent");
-      add("datanode_storage_percent");
-      add("journalnode_process_percent");
-      add("namenode_cpu");
-      add("namenode_hdfs_capacity_utilization");
-      add("datanode_storage");
-      add("datanode_heap_usage");
-      add("storm_supervisor_process_percent");
-      add("hbase_regionserver_process_percent");
-      add("hbase_master_cpu");
-      add("zookeeper_server_process_percent");
-      add("metrics_monitor_process_percent");
-      add("ams_metrics_collector_hbase_master_cpu");
-      add("ambari_agent_disk_usage");
-      add("namenode_last_checkpoint");
-      addAll(alertNamesForNameserviceUpdate);
-    }};
-
-    LOG.info("Updating alert definitions.");
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
-    for (final Cluster cluster : clusterMap.values()) {
-      long clusterID = cluster.getClusterId();
-
-      // here goes alerts that need get new properties
-      final AlertDefinitionEntity namenodeLastCheckpointAlertDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "namenode_last_checkpoint");
-      final AlertDefinitionEntity namenodeHAHealthAlertDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "namenode_ha_health");
-      final AlertDefinitionEntity nodemanagerHealthAlertDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "yarn_nodemanager_health");
-      final AlertDefinitionEntity nodemanagerHealthSummaryAlertDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "nodemanager_health_summary");
-      final AlertDefinitionEntity hiveMetastoreProcessAlertDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "hive_metastore_process");
-      final AlertDefinitionEntity hiveServerProcessAlertDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "hive_server_process");
-      final AlertDefinitionEntity hiveWebhcatServerStatusAlertDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "hive_webhcat_server_status");
-      final AlertDefinitionEntity flumeAgentStatusAlertDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "flume_agent_status");
-      final AlertDefinitionEntity zookeeperServerProcessAlertDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "zookeeper_server_process");
-
-      Map<AlertDefinitionEntity, List<String>> alertDefinitionParams = new HashMap<>();
-      checkedPutToMap(alertDefinitionParams, namenodeLastCheckpointAlertDefinitionEntity,
-              Lists.newArrayList("connection.timeout", "checkpoint.time.warning.threshold",
-                "checkpoint.time.critical.threshold", "checkpoint.txns.multiplier.warning.threshold",
-                "checkpoint.txns.multiplier.critical.threshold"));
-      checkedPutToMap(alertDefinitionParams, namenodeHAHealthAlertDefinitionEntity,
-        Lists.newArrayList("connection.timeout"));
-      checkedPutToMap(alertDefinitionParams, nodemanagerHealthAlertDefinitionEntity,
-              Lists.newArrayList("connection.timeout"));
-      checkedPutToMap(alertDefinitionParams, nodemanagerHealthSummaryAlertDefinitionEntity,
-              Lists.newArrayList("connection.timeout"));
-      checkedPutToMap(alertDefinitionParams, hiveMetastoreProcessAlertDefinitionEntity,
-              Lists.newArrayList("default.smoke.user", "default.smoke.principal", "default.smoke.keytab"));
-      checkedPutToMap(alertDefinitionParams, hiveServerProcessAlertDefinitionEntity,
-              Lists.newArrayList("default.smoke.user", "default.smoke.principal", "default.smoke.keytab"));
-      checkedPutToMap(alertDefinitionParams, hiveWebhcatServerStatusAlertDefinitionEntity,
-              Lists.newArrayList("default.smoke.user", "connection.timeout"));
-      checkedPutToMap(alertDefinitionParams, flumeAgentStatusAlertDefinitionEntity,
-              Lists.newArrayList("run.directory"));
-      checkedPutToMap(alertDefinitionParams, zookeeperServerProcessAlertDefinitionEntity,
-              Lists.newArrayList("socket.command", "socket.command.response"));
-
-
-      Map<Long, AlertDefinitionEntity> definitionsForPropertyUpdates = new HashMap<>();
-
-      // adding new properties
-      for (Map.Entry<AlertDefinitionEntity, List<String>> entry : alertDefinitionParams.entrySet()){
-        AlertDefinitionEntity alertDefinition = entry.getKey();
-        String source = alertDefinition.getSource();
-        alertDefinition.setSource(addParam(source, entry.getValue()));
-        definitionsForPropertyUpdates.put(alertDefinition.getDefinitionId(), alertDefinition);
-      }
-
-      // here goes alerts that need update for existing properties
-      for (String name : alertNamesForPropertyUpdates) {
-        AlertDefinitionEntity alertDefinition = alertDefinitionDAO.findByName(clusterID, name);
-        if (alertDefinition != null && !definitionsForPropertyUpdates.containsKey(alertDefinition.getDefinitionId())) {
-          definitionsForPropertyUpdates.put(alertDefinition.getDefinitionId(), alertDefinition);
-        }
-      }
-
-      // updating old and new properties, best way to use map like visibilityMap.
-      for (AlertDefinitionEntity alertDefinition : definitionsForPropertyUpdates.values()) {
-        // here goes property updates
-        if (visibilityMap.containsKey(alertDefinition.getDefinitionName())) {
-          for (Map.Entry<String, String> entry : visibilityMap.get(alertDefinition.getDefinitionName()).entrySet()){
-            String paramName = entry.getKey();
-            String visibilityValue = entry.getValue();
-            String source = alertDefinition.getSource();
-            alertDefinition.setSource(addParamOption(source, paramName, "visibility", visibilityValue));
-          }
-        }
-        // update percent script alerts param values from 0.x to 0.x * 100 values
-        if (scriptAlertMultiplierMap.containsKey(alertDefinition.getDefinitionName())) {
-          for (Map.Entry<String, Integer> entry : scriptAlertMultiplierMap.get(alertDefinition.getDefinitionName()).entrySet()){
-            String paramName = entry.getKey();
-            Integer multiplier = entry.getValue();
-            String source = alertDefinition.getSource();
-            Float oldValue = getParamFloatValue(source, paramName);
-            if (oldValue == null) {
-              alertDefinition.setSource(addParam(source, Arrays.asList(paramName)));
-            } else {
-              Integer newValue = Math.round(oldValue * multiplier);
-              alertDefinition.setSource(setParamIntegerValue(source, paramName, newValue));
-            }
-          }
-        }
-
-        // update reporting alerts(aggregate and metrics) values from 0.x to 0.x * 100 values
-        if (reportingMultiplierMap.containsKey(alertDefinition.getDefinitionName())) {
-          for (Map.Entry<String, Integer> entry : reportingMultiplierMap.get(alertDefinition.getDefinitionName()).entrySet()){
-            String reportingName = entry.getKey();
-            Integer multiplier = entry.getValue();
-            String source = alertDefinition.getSource();
-            Float oldValue = getReportingFloatValue(source, reportingName);
-            Integer newValue = Math.round(oldValue * multiplier);
-            alertDefinition.setSource(setReportingIntegerValue(source, reportingName, newValue));
-          }
-        }
-
-        if (reportingPercentMap.containsKey(alertDefinition.getDefinitionName())) {
-          for (Map.Entry<String, String> entry : reportingPercentMap.get(alertDefinition.getDefinitionName()).entrySet()){
-            String paramName = entry.getKey();
-            String paramValue = entry.getValue();
-            String source = alertDefinition.getSource();
-            alertDefinition.setSource(addReportingOption(source, paramName, paramValue));
-          }
-        }
-
-        if (alertNamesForNameserviceUpdate.contains(alertDefinition.getDefinitionName())) {
-          String source = alertDefinition.getSource();
-          alertDefinition.setSource(setNameservice(source, newNameservicePropertyValue));
-        }
-        // regeneration of hash and writing modified alerts to database, must go after all modifications finished
-        alertDefinition.setHash(UUID.randomUUID().toString());
-        alertDefinitionDAO.merge(alertDefinition);
-      }
-      //update Atlas alert
-      final AlertDefinitionEntity atlasMetadataServerWebUI = alertDefinitionDAO.findByName(
-              clusterID, "metadata_server_webui");
-      if (atlasMetadataServerWebUI != null) {
-        String source = atlasMetadataServerWebUI.getSource();
-        JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-
-        JsonObject uriJson = sourceJson.get("uri").getAsJsonObject();
-        uriJson.remove("kerberos_keytab");
-        uriJson.remove("kerberos_principal");
-        uriJson.addProperty("kerberos_keytab", "{{cluster-env/smokeuser_keytab}}");
-        uriJson.addProperty("kerberos_principal", "{{cluster-env/smokeuser_principal_name}}");
-
-        atlasMetadataServerWebUI.setSource(sourceJson.toString());
-
-        atlasMetadataServerWebUI.setHash(UUID.randomUUID().toString());
-        alertDefinitionDAO.merge(atlasMetadataServerWebUI);
-      }
-
-      AlertDefinitionEntity stormRestApiAlertDefinition = alertDefinitionDAO.findByName(clusterID, "storm_rest_api");
-      if (stormRestApiAlertDefinition != null) {
-        LOG.info("Removing alert : storm_rest_api");
-        alertDefinitionDAO.remove(stormRestApiAlertDefinition);
-      }
-    }
-  }
-
-  protected String setNameservice(String source, String paramValue) {
-    final String nameservicePropertyName = "nameservice";
-    JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-    JsonObject highAvailability = sourceJson.getAsJsonObject("uri").getAsJsonObject("high_availability");
-    if (highAvailability.has(nameservicePropertyName)) {
-      highAvailability.addProperty(nameservicePropertyName, paramValue);
-    }
-    return sourceJson.toString();
-  }
-
-  /*
-  * Simple put method with check for key is not null
-  * */
-  private void checkedPutToMap(Map<AlertDefinitionEntity, List<String>> alertDefinitionParams, AlertDefinitionEntity alertDefinitionEntity,
-                               List<String> params) {
-    if (alertDefinitionEntity != null) {
-      alertDefinitionParams.put(alertDefinitionEntity, params);
-    }
-  }
-
-  /**
-   * Add option to script parameter.
-   * @param source json string of script source
-   * @param paramName parameter name
-   * @param optionName option name
-   * @param optionValue option value
-   * @return modified source
-   */
-  protected String addParamOption(String source, String paramName, String optionName, String optionValue){
-    JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-    JsonArray parametersJson = sourceJson.getAsJsonArray("parameters");
-    if(parametersJson != null && !parametersJson.isJsonNull()) {
-      for(JsonElement param : parametersJson) {
-        if(param.isJsonObject()) {
-          JsonObject paramObject = param.getAsJsonObject();
-          if(paramObject.has("name") && paramObject.get("name").getAsString().equals(paramName)){
-            paramObject.add(optionName, new JsonPrimitive(optionValue));
-          }
-        }
-      }
-    }
-    return sourceJson.toString();
-  }
-
-  /**
-   * Returns param value as float.
-   * @param source source of script alert
-   * @param paramName param name
-   * @return param value as float
-   */
-  protected Float getParamFloatValue(String source, String paramName){
-    JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-    JsonArray parametersJson = sourceJson.getAsJsonArray("parameters");
-    if(parametersJson != null && !parametersJson.isJsonNull()) {
-      for(JsonElement param : parametersJson) {
-        if(param.isJsonObject()) {
-          JsonObject paramObject = param.getAsJsonObject();
-          if(paramObject.has("name") && paramObject.get("name").getAsString().equals(paramName)){
-            if(paramObject.has("value")) {
-              return paramObject.get("value").getAsFloat();
-            }
-          }
-        }
-      }
-    }
-    return null;
-  }
-
-  /**
-   * Set integer param value.
-   * @param source source of script alert
-   * @param paramName param name
-   * @param value new param value
-   * @return modified source
-   */
-  protected String setParamIntegerValue(String source, String paramName, Integer value){
-    JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-    JsonArray parametersJson = sourceJson.getAsJsonArray("parameters");
-    if(parametersJson != null && !parametersJson.isJsonNull()) {
-      for(JsonElement param : parametersJson) {
-        if(param.isJsonObject()) {
-          JsonObject paramObject = param.getAsJsonObject();
-          if(paramObject.has("name") && paramObject.get("name").getAsString().equals(paramName)){
-            paramObject.add("value", new JsonPrimitive(value));
-          }
-        }
-      }
-    }
-    return sourceJson.toString();
-  }
-
-  /**
-   * Returns reporting value as float.
-   * @param source source of aggregate or metric alert
-   * @param reportingName reporting name, must be "warning" or "critical"
-   * @return reporting value as float
-   */
-  protected Float getReportingFloatValue(String source, String reportingName){
-    JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-    return sourceJson.getAsJsonObject("reporting").getAsJsonObject(reportingName).get("value").getAsFloat();
-  }
-
-  /**
-   * Set integer value of reporting.
-   * @param source source of aggregate or metric alert
-   * @param reportingName reporting name, must be "warning" or "critical"
-   * @param value new value
-   * @return modified source
-   */
-  protected String setReportingIntegerValue(String source, String reportingName, Integer value){
-    JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-    sourceJson.getAsJsonObject("reporting").getAsJsonObject(reportingName).add("value", new JsonPrimitive(value));
-    return sourceJson.toString();
-  }
-
-  /**
-   * Add option to reporting
-   * @param source source of aggregate or metric alert
-   * @param optionName option name
-   * @param value option value
-   * @return modified source
-   */
-  protected String addReportingOption(String source, String optionName, String value){
-    JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-    sourceJson.getAsJsonObject("reporting").add(optionName, new JsonPrimitive(value));
-    return sourceJson.toString();
-  }
-
-  protected String addParam(String source, List<String> params) {
-    JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-    JsonArray parametersJson = sourceJson.getAsJsonArray("parameters");
-
-    boolean parameterExists = parametersJson != null && !parametersJson.isJsonNull();
-
-    if (parameterExists) {
-      Iterator<JsonElement> jsonElementIterator = parametersJson.iterator();
-      while (jsonElementIterator.hasNext()) {
-        JsonElement element = jsonElementIterator.next();
-        JsonElement name = element.getAsJsonObject().get("name");
-        if (name != null && !name.isJsonNull() && params.contains(name.getAsString())) {
-          params.remove(name.getAsString());
-        }
-      }
-      if (params.size() == 0) {
-        return sourceJson.toString();
-      }
-    }
-
-    List<JsonObject> paramsToAdd = new ArrayList<>();
-
-    if (params.contains("connection.timeout")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("connection.timeout"));
-      param.add("display_name", new JsonPrimitive("Connection Timeout"));
-      param.add("value", new JsonPrimitive(5.0));
-      param.add("type", new JsonPrimitive("NUMERIC"));
-      param.add("description", new JsonPrimitive("The maximum time before this alert is considered to be CRITICAL"));
-      param.add("units", new JsonPrimitive("seconds"));
-      param.add("threshold", new JsonPrimitive("CRITICAL"));
-
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("checkpoint.time.warning.threshold")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("checkpoint.time.warning.threshold"));
-      param.add("display_name", new JsonPrimitive("Checkpoint Warning"));
-      param.add("value", new JsonPrimitive(2.0));
-      param.add("type", new JsonPrimitive("PERCENT"));
-      param.add("description", new JsonPrimitive("The percentage of the last checkpoint time greater than the interval in order to trigger a warning alert."));
-      param.add("units", new JsonPrimitive("%"));
-      param.add("threshold", new JsonPrimitive("WARNING"));
-
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("checkpoint.time.critical.threshold")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("checkpoint.time.critical.threshold"));
-      param.add("display_name", new JsonPrimitive("Checkpoint Critical"));
-      param.add("value", new JsonPrimitive(4.0));
-      param.add("type", new JsonPrimitive("PERCENT"));
-      param.add("description", new JsonPrimitive("The percentage of the last checkpoint time greater than the interval in order to trigger a critical alert."));
-      param.add("units", new JsonPrimitive("%"));
-      param.add("threshold", new JsonPrimitive("CRITICAL"));
-
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("checkpoint.txns.multiplier.warning.threshold")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("checkpoint.txns.multiplier.warning.threshold"));
-      param.add("display_name", new JsonPrimitive("Uncommitted transactions Warning"));
-      param.add("value", new JsonPrimitive(2.0));
-      param.add("type", new JsonPrimitive("NUMERIC"));
-      param.add("description", new JsonPrimitive("The multiplier to use against dfs.namenode.checkpoint.period compared to the difference between last transaction id and most recent transaction id beyond which to trigger a warning alert."));
-      param.add("threshold", new JsonPrimitive("WARNING"));
-
-      paramsToAdd.add(param);
-    }
-    if (params.contains("checkpoint.txns.multiplier.critical.threshold")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("checkpoint.txns.multiplier.critical.threshold"));
-      param.add("display_name", new JsonPrimitive("Uncommitted transactions Critical"));
-      param.add("value", new JsonPrimitive(4.0));
-      param.add("type", new JsonPrimitive("NUMERIC"));
-      param.add("description", new JsonPrimitive("The multiplier to use against dfs.namenode.checkpoint.period compared to the difference between last transaction id and most recent transaction id beyond which to trigger a critical alert."));
-      param.add("threshold", new JsonPrimitive("CRITICAL"));
-
-      paramsToAdd.add(param);
-    }
-    if (params.contains("default.smoke.user")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("default.smoke.user"));
-      param.add("display_name", new JsonPrimitive("Default Smoke User"));
-      param.add("value", new JsonPrimitive("ambari-qa"));
-      param.add("type", new JsonPrimitive("STRING"));
-      param.add("description", new JsonPrimitive("The user that will run the Hive commands if not specified in cluster-env/smokeuser"));
-
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("default.smoke.principal")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("default.smoke.principal"));
-      param.add("display_name", new JsonPrimitive("Default Smoke Principal"));
-      param.add("value", new JsonPrimitive("ambari-qa@EXAMPLE.COM"));
-      param.add("type", new JsonPrimitive("STRING"));
-      param.add("description", new JsonPrimitive("The principal to use when retrieving the kerberos ticket if not specified in cluster-env/smokeuser_principal_name"));
-
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("default.smoke.keytab")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("default.smoke.keytab"));
-      param.add("display_name", new JsonPrimitive("Default Smoke Keytab"));
-      param.add("value", new JsonPrimitive("/etc/security/keytabs/smokeuser.headless.keytab"));
-      param.add("type", new JsonPrimitive("STRING"));
-      param.add("description", new JsonPrimitive("The keytab to use when retrieving the kerberos ticket if not specified in cluster-env/smokeuser_keytab"));
-
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("run.directory")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("run.directory"));
-      param.add("display_name", new JsonPrimitive("Run Directory"));
-      param.add("value", new JsonPrimitive("/var/run/flume"));
-      param.add("type", new JsonPrimitive("STRING"));
-      param.add("description", new JsonPrimitive("The directory where flume agent processes will place their PID files."));
-
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("minimum.free.space")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("minimum.free.space"));
-      param.add("display_name", new JsonPrimitive("Minimum Free Space"));
-      param.add("value", new JsonPrimitive("5000000000"));
-      param.add("type", new JsonPrimitive("NUMERIC"));
-      param.add("description", new JsonPrimitive("The overall amount of free disk space left before an alert is triggered."));
-      param.add("units", new JsonPrimitive("bytes"));
-      param.add("threshold", new JsonPrimitive("WARNING"));
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("percent.used.space.warning.threshold")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("percent.used.space.warning.threshold"));
-      param.add("display_name", new JsonPrimitive("Warning"));
-      param.add("value", new JsonPrimitive("50"));
-      param.add("type", new JsonPrimitive("PERCENT"));
-      param.add("description", new JsonPrimitive("The percent of disk space consumed before a warning is triggered."));
-      param.add("units", new JsonPrimitive("%"));
-      param.add("threshold", new JsonPrimitive("WARNING"));
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("percent.free.space.critical.threshold")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("percent.free.space.critical.threshold"));
-      param.add("display_name", new JsonPrimitive("Critical"));
-      param.add("value", new JsonPrimitive("80"));
-      param.add("type", new JsonPrimitive("PERCENT"));
-      param.add("description", new JsonPrimitive("The percent of disk space consumed before a critical alert is triggered."));
-      param.add("units", new JsonPrimitive("%"));
-      param.add("threshold", new JsonPrimitive("CRITICAL"));
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("request.by.status.warning.threshold")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("request.by.status.warning.threshold"));
-      param.add("display_name", new JsonPrimitive("Warning Request Time"));
-      param.add("value", new JsonPrimitive("3000"));
-      param.add("type", new JsonPrimitive("NUMERIC"));
-      param.add("description", new JsonPrimitive("The time to find requests in progress before a warning alert is triggered."));
-      param.add("units", new JsonPrimitive("ms"));
-      param.add("threshold", new JsonPrimitive("WARNING"));
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("request.by.status.critical.threshold")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("request.by.status.critical.threshold"));
-      param.add("display_name", new JsonPrimitive("Critical Request Time"));
-      param.add("value", new JsonPrimitive("5000"));
-      param.add("type", new JsonPrimitive("NUMERIC"));
-      param.add("description", new JsonPrimitive("The time to find requests in progress before a critical alert is triggered."));
-      param.add("units", new JsonPrimitive("ms"));
-      param.add("threshold", new JsonPrimitive("CRITICAL"));
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("task.status.aggregation.warning.threshold")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("task.status.aggregation.warning.threshold"));
-      param.add("display_name", new JsonPrimitive("Warning Process Time"));
-      param.add("value", new JsonPrimitive("3000"));
-      param.add("type", new JsonPrimitive("NUMERIC"));
-      param.add("description", new JsonPrimitive("The time to calculate a request's status from its tasks before a warning alert is triggered."));
-      param.add("units", new JsonPrimitive("ms"));
-      param.add("threshold", new JsonPrimitive("WARNING"));
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("task.status.aggregation.critical.threshold")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("task.status.aggregation.critical.threshold"));
-      param.add("display_name", new JsonPrimitive("Critical Process Time"));
-      param.add("value", new JsonPrimitive("5000"));
-      param.add("type", new JsonPrimitive("NUMERIC"));
-      param.add("description", new JsonPrimitive("The time to calculate a request's status from its tasks before a critical alert is triggered."));
-      param.add("units", new JsonPrimitive("ms"));
-      param.add("threshold", new JsonPrimitive("CRITICAL"));
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("rest.api.cluster.warning.threshold")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("rest.api.cluster.warning.threshold"));
-      param.add("display_name", new JsonPrimitive("Warning Response Time"));
-      param.add("value", new JsonPrimitive("5000"));
-      param.add("type", new JsonPrimitive("NUMERIC"));
-      param.add("description", new JsonPrimitive("The time to get a cluster via the REST API before a warning alert is triggered."));
-      param.add("units", new JsonPrimitive("ms"));
-      param.add("threshold", new JsonPrimitive("WARNING"));
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("rest.api.cluster.critical.threshold")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("rest.api.cluster.critical.threshold"));
-      param.add("display_name", new JsonPrimitive("Critical Response Time"));
-      param.add("value", new JsonPrimitive("7000"));
-      param.add("type", new JsonPrimitive("NUMERIC"));
-      param.add("description", new JsonPrimitive("The time to get a cluster via the REST API before a critical alert is triggered."));
-      param.add("units", new JsonPrimitive("ms"));
-      param.add("threshold", new JsonPrimitive("CRITICAL"));
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("socket.command")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("socket.command"));
-      param.add("display_name", new JsonPrimitive("Socket Command"));
-      param.add("value", new JsonPrimitive("ruok"));
-      param.add("type", new JsonPrimitive("STRING"));
-      param.add("description", new JsonPrimitive("A socket command which queries ZooKeeper to respond with its state. The expected response is imok."));
-      paramsToAdd.add(param);
-
-    }
-    if (params.contains("socket.command.response")) {
-      JsonObject param = new JsonObject();
-      param.add("name", new JsonPrimitive("socket.command.response"));
-      param.add("display_name", new JsonPrimitive("Expected Response"));
-      param.add("value", new JsonPrimitive("imok"));
-      param.add("type", new JsonPrimitive("STRING"));
-      param.add("description", new JsonPrimitive("The expected response to the socket command."));
-      paramsToAdd.add(param);
-
-    }
-
-
-
-    if (!parameterExists) {
-      parametersJson = new JsonArray();
-      for (JsonObject param : paramsToAdd) {
-        parametersJson.add(param);
-      }
-      sourceJson.add("parameters", parametersJson);
-    } else {
-      for (JsonObject param : paramsToAdd) {
-        parametersJson.add(param);
-      }
-      sourceJson.remove("parameters");
-      sourceJson.add("parameters", parametersJson);
-    }
-
-    return sourceJson.toString();
-  }
-
-  protected void updateAdminPermissionTable() throws SQLException {
-    // Add the sort_order column to the adminpermission table
-    dbAccessor.addColumn(ADMIN_PERMISSION_TABLE,
-        new DBColumnInfo(SORT_ORDER_COL, Short.class, null, 1, false));
-
-    // Add the principal_id column to the adminpermission table
-    //   Note: This is set to nullable here, but will be altered once the column has been set
-    //         properly during the DML update phase.
-    dbAccessor.addColumn(ADMIN_PERMISSION_TABLE,
-        new DBColumnInfo(PRINCIPAL_ID_COL, Long.class, null, null, true));
-  }
-
-  protected void updateTopologyRequestTable() throws SQLException {
-    // Add the sort_order column to the adminpermission table
-    dbAccessor.addColumn(TOPOLOGY_REQUEST_TABLE,
-      new DBColumnInfo(PROVISION_ACTION_COL, String.class, 255, null, true));
-  }
-
-  /**
-   * Updates the {@value #ALERT_DEFINITION_TABLE} in the following ways:
-   * <ul>
-   * <li>Craetes the {@value #HELP_URL_COLUMN} column</li>
-   * <li>Craetes the {@value #REPEAT_TOLERANCE_COLUMN} column</li>
-   * <li>Craetes the {@value #REPEAT_TOLERANCE_ENABLED_COLUMN} column</li>
-   * </ul>
-   *
-   * @throws SQLException
-   */
-  protected void updateAlertDefinitionTable() throws SQLException {
-    dbAccessor.addColumn(ALERT_DEFINITION_TABLE,
-        new DBColumnInfo(HELP_URL_COLUMN, String.class, 512, null, true));
-
-    dbAccessor.addColumn(ALERT_DEFINITION_TABLE,
-        new DBColumnInfo(REPEAT_TOLERANCE_COLUMN, Integer.class, null, 1, false));
-
-    dbAccessor.addColumn(ALERT_DEFINITION_TABLE,
-        new DBColumnInfo(REPEAT_TOLERANCE_ENABLED_COLUMN, Short.class, null, 0, false));
-  }
-
-  /**
-   * Updates the {@value #ALERT_CURRENT_TABLE} in the following ways:
-   * <ul>
-   * <li>Creates the {@value #ALERT_CURRENT_OCCURRENCES_COLUMN} column</li>
-   * <li>Creates the {@value #ALERT_CURRENT_FIRMNESS_COLUMN} column</li>
-   * </ul>
-   *
-   * @throws SQLException
-   */
-  protected void updateAlertCurrentTable() throws SQLException {
-    dbAccessor.addColumn(ALERT_CURRENT_TABLE,
-      new DBColumnInfo(ALERT_CURRENT_OCCURRENCES_COLUMN, Long.class, null, 1, false));
-
-    dbAccessor.addColumn(ALERT_CURRENT_TABLE, new DBColumnInfo(ALERT_CURRENT_FIRMNESS_COLUMN,
-      String.class, 255, AlertFirmness.HARD.name(), false));
-  }
-
-  /**
-   * Updates the {@value #ALERT_TARGET_TABLE} in the following ways:
-   * <ul>
-   * <li>Creates the {@value #ALERT_TARGET_ENABLED_COLUMN} column</li>
-   * </ul>
-   *
-   * @throws SQLException
-   */
-  protected void updateAlertTargetTable() throws SQLException {
-    dbAccessor.addColumn(ALERT_TARGET_TABLE,
-      new DBColumnInfo(ALERT_TARGET_ENABLED_COLUMN, Short.class, null, 1, false));
-  }
-
-  protected void setRoleSortOrder() throws SQLException {
-    String updateStatement = "UPDATE " + ADMIN_PERMISSION_TABLE + " SET " + SORT_ORDER_COL + "=%d WHERE " + PERMISSION_ID_COL + "='%s'";
-
-    LOG.info("Setting permission labels");
-    dbAccessor.executeUpdate(String.format(updateStatement,
-        1, PermissionEntity.AMBARI_ADMINISTRATOR_PERMISSION_NAME));
-    dbAccessor.executeUpdate(String.format(updateStatement,
-        2, PermissionEntity.CLUSTER_ADMINISTRATOR_PERMISSION_NAME));
-    dbAccessor.executeUpdate(String.format(updateStatement,
-      3, PermissionEntity.CLUSTER_OPERATOR_PERMISSION_NAME));
-    dbAccessor.executeUpdate(String.format(updateStatement,
-      4, PermissionEntity.SERVICE_ADMINISTRATOR_PERMISSION_NAME));
-    dbAccessor.executeUpdate(String.format(updateStatement,
-      5, PermissionEntity.SERVICE_OPERATOR_PERMISSION_NAME));
-    dbAccessor.executeUpdate(String.format(updateStatement,
-      6, PermissionEntity.CLUSTER_USER_PERMISSION_NAME));
-    dbAccessor.executeUpdate(String.format(updateStatement,
-      7, PermissionEntity.VIEW_USER_PERMISSION_NAME));
-  }
-
-  /**
-   * Create and update records to create the role-based principals.
-   * <p>
-   * This includes creating the new "ROLE" principal type, a principal for each role, and finally
-   * updating the princial_id column for the role.
-   */
-  void createRolePrincipals() throws SQLException {
-    // Create Role Principal Type
-    PrincipalTypeEntity rolePrincipalType = new PrincipalTypeEntity();
-    rolePrincipalType.setName("ROLE");
-
-    // creates the new record and returns an entity with the id set.
-    rolePrincipalType = principalTypeDAO.merge(rolePrincipalType);
-
-    // Get the roles (adminpermissions) and create a principal for each.... set the role's principal_id
-    // value as we go...
-    List<PermissionEntity> roleEntities = permissionDAO.findAll();
-
-    for (PermissionEntity roleEntity : roleEntities) {
-      PrincipalEntity principalEntity = new PrincipalEntity();
-      principalEntity.setPrincipalType(rolePrincipalType);
-
-      roleEntity.setPrincipal(principalDAO.merge(principalEntity));
-
-      permissionDAO.merge(roleEntity);
-    }
-
-    // Fix the adminpermission.principal_id column to be non-nullable:
-    dbAccessor.alterColumn(ADMIN_PERMISSION_TABLE,
-        new DBColumnInfo(PRINCIPAL_ID_COL, Long.class, null, null, false));
-  }
-
-  /**
-   * Makes the following changes to the {@value #REPO_VERSION_TABLE} table:
-   * <ul>
-   * <li>repo_type VARCHAR(255) DEFAULT 'STANDARD' NOT NULL</li>
-   * <li>version_url VARCHAR(1024)</li>
-   * <li>version_xml MEDIUMTEXT</li>
-   * <li>version_xsd VARCHAR(512)</li>
-   * <li>parent_id BIGINT</li>
-   * </ul>
-   *
-   * @throws SQLException
-   */
-  private void updateRepoVersionTableDDL() throws SQLException {
-    DBColumnInfo repoTypeColumn = new DBColumnInfo("repo_type", String.class, 255, RepositoryType.STANDARD.name(), false);
-    DBColumnInfo versionUrlColumn = new DBColumnInfo("version_url", String.class, 1024, null, true);
-    DBColumnInfo versionXmlColumn = new DBColumnInfo("version_xml", Clob.class, null, null, true);
-    DBColumnInfo versionXsdColumn = new DBColumnInfo("version_xsd", String.class, 512, null, true);
-    DBColumnInfo parentIdColumn = new DBColumnInfo("parent_id", Long.class, null, null, true);
-
-    dbAccessor.addColumn(REPO_VERSION_TABLE, repoTypeColumn);
-    dbAccessor.addColumn(REPO_VERSION_TABLE, versionUrlColumn);
-    dbAccessor.addColumn(REPO_VERSION_TABLE, versionXmlColumn);
-    dbAccessor.addColumn(REPO_VERSION_TABLE, versionXsdColumn);
-    dbAccessor.addColumn(REPO_VERSION_TABLE, parentIdColumn);
-  }
-
-  /**
-   * Makes the following changes to the {@value #SERVICE_COMPONENT_DS_TABLE} table,
-   * but only if the table doesn't have it's new PK set.
-   * <ul>
-   * <li>id BIGINT NOT NULL</li>
-   * <li>Drops FKs on {@value #HOST_COMPONENT_DS_TABLE} and {@value #HOST_COMPONENT_STATE_TABLE}</li>
-   * <li>Populates ID in {@value #SERVICE_COMPONENT_DS_TABLE}</li>
-   * <li>Creates {@code UNIQUE} constraint on {@value #HOST_COMPONENT_DS_TABLE}</li>
-   * <li>Adds FKs on {@value #HOST_COMPONENT_DS_TABLE} and {@value #HOST_COMPONENT_STATE_TABLE}</li>
-   * <li>Adds new sequence value of {@code servicecomponentdesiredstate_id_seq}</li>
-   * </ul>
-   *
-   * @throws SQLException
-   */
-  void updateServiceComponentDesiredStateTableDDL() throws SQLException {
-    if (dbAccessor.tableHasPrimaryKey(SERVICE_COMPONENT_DS_TABLE, ID)) {
-      LOG.info("Skipping {} table Primary Key modifications since the new {} column already exists",
-          SERVICE_COMPONENT_DS_TABLE, ID);
-
-      return;
-    }
-
-    // drop FKs to SCDS in both HCDS and HCS tables
-    // These are the expected constraint names
-    dbAccessor.dropFKConstraint(HOST_COMPONENT_DS_TABLE, "hstcmpnntdesiredstatecmpnntnme");
-    dbAccessor.dropFKConstraint(HOST_COMPONENT_STATE_TABLE, "hstcomponentstatecomponentname");
-    // These are the old (pre Ambari 1.5) constraint names, however still found on some installations
-    dbAccessor.dropFKConstraint(HOST_COMPONENT_DS_TABLE, "FK_hostcomponentdesiredstate_component_name");
-    dbAccessor.dropFKConstraint(HOST_COMPONENT_STATE_TABLE, "FK_hostcomponentstate_component_name");
-
-    // remove existing compound PK
-    dbAccessor.dropPKConstraint(SERVICE_COMPONENT_DS_TABLE, "servicecomponentdesiredstate_pkey");
-
-    // add new PK column to SCDS, making it nullable for now
-    DBColumnInfo idColumn = new DBColumnInfo(ID, Long.class, null, null, true);
-    dbAccessor.addColumn(SERVICE_COMPONENT_DS_TABLE, idColumn);
-
-    // populate SCDS id column
-    AtomicLong scdsIdCounter = new AtomicLong(1);
-    Statement statement = null;
-    ResultSet resultSet = null;
-    try {
-      statement = dbAccessor.getConnection().createStatement();
-      if (statement != null) {
-        String selectSQL = String.format("SELECT cluster_id, service_name, component_name FROM %s",
-            SERVICE_COMPONENT_DS_TABLE);
-
-        resultSet = statement.executeQuery(selectSQL);
-        while (null != resultSet && resultSet.next()) {
-          final Long clusterId = resultSet.getLong("cluster_id");
-          final String serviceName = resultSet.getString("service_name");
-          final String componentName = resultSet.getString("component_name");
-
-          String updateSQL = String.format(
-              "UPDATE %s SET %s = %d WHERE cluster_id = %d AND service_name = '%s' AND component_name = '%s'",
-              SERVICE_COMPONENT_DS_TABLE, ID, scdsIdCounter.getAndIncrement(), clusterId,
-              serviceName, componentName);
-
-          dbAccessor.executeQuery(updateSQL);
-        }
-      }
-    } finally {
-      JdbcUtils.closeResultSet(resultSet);
-      JdbcUtils.closeStatement(statement);
-    }
-
-    // make the column NON NULL now
-    dbAccessor.alterColumn(SERVICE_COMPONENT_DS_TABLE,
-        new DBColumnInfo(ID, Long.class, null, null, false));
-
-    // create a new PK, matching the name of the constraint found in SQL
-    dbAccessor.addPKConstraint(SERVICE_COMPONENT_DS_TABLE, "pk_sc_desiredstate", ID);
-
-    // create UNIQUE constraint, ensuring column order matches SQL files
-    String[] uniqueColumns = new String[] { "component_name", "service_name", "cluster_id" };
-    dbAccessor.addUniqueConstraint(SERVICE_COMPONENT_DS_TABLE, "UQ_scdesiredstate_name",
-        uniqueColumns);
-
-    // add FKs back to SCDS in both HCDS and HCS tables
-    dbAccessor.addFKConstraint(HOST_COMPONENT_DS_TABLE, "hstcmpnntdesiredstatecmpnntnme",
-        uniqueColumns, SERVICE_COMPONENT_DS_TABLE, uniqueColumns, false);
-
-    dbAccessor.addFKConstraint(HOST_COMPONENT_STATE_TABLE, "hstcomponentstatecomponentname",
-        uniqueColumns, SERVICE_COMPONENT_DS_TABLE, uniqueColumns, false);
-
-    // Add sequence for SCDS id
-    addSequence("servicecomponentdesiredstate_id_seq", scdsIdCounter.get(), false);
-  }
-
-  /**
-   * Makes the following changes to the {@value #SERVICE_COMPONENT_HISTORY_TABLE} table:
-   * <ul>
-   * <li>id BIGINT NOT NULL</li>
-   * <li>component_id BIGINT NOT NULL</li>
-   * <li>upgrade_id BIGINT NOT NULL</li>
-   * <li>from_stack_id BIGINT NOT NULL</li>
-   * <li>to_stack_id BIGINT NOT NULL</li>
-   * <li>CONSTRAINT PK_sc_history PRIMARY KEY (id)</li>
-   * <li>CONSTRAINT FK_sc_history_component_id FOREIGN KEY (component_id) REFERENCES servicecomponentdesiredstate (id)</li>
-   * <li>CONSTRAINT FK_sc_history_upgrade_id FOREIGN KEY (upgrade_id) REFERENCES upgrade (upgrade_id)</li>
-   * <li>CONSTRAINT FK_sc_history_from_stack_id FOREIGN KEY (from_stack_id) REFERENCES stack (stack_id)</li>
-   * <li>CONSTRAINT FK_sc_history_to_stack_id FOREIGN KEY (to_stack_id) REFERENCES stack (stack_id)</li>
-   * <li>Creates the {@code servicecomponent_history_id_seq}</li>
-   * </ul>
-   *
-   * @throws SQLException
-   */
-  private void createServiceComponentHistoryTable() throws SQLException {
-    List<DBColumnInfo> columns = new ArrayList<>();
-    columns.add(new DBColumnInfo(ID, Long.class, null, null, false));
-    columns.add(new DBColumnInfo("component_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("upgrade_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("from_stack_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("to_stack_id", Long.class, null, null, false));
-    dbAccessor.createTable(SERVICE_COMPONENT_HISTORY_TABLE, columns, (String[]) null);
-
-    dbAccessor.addPKConstraint(SERVICE_COMPONENT_HISTORY_TABLE, "PK_sc_history", ID);
-
-    dbAccessor.addFKConstraint(SERVICE_COMPONENT_HISTORY_TABLE, "FK_sc_history_component_id",
-        "component_id", SERVICE_COMPONENT_DS_TABLE, "id", false);
-
-    dbAccessor.addFKConstraint(SERVICE_COMPONENT_HISTORY_TABLE, "FK_sc_history_upgrade_id",
-        "upgrade_id", UPGRADE_TABLE, "upgrade_id", false);
-
-    dbAccessor.addFKConstraint(SERVICE_COMPONENT_HISTORY_TABLE, "FK_sc_history_from_stack_id",
-        "from_stack_id", STACK_TABLE, "stack_id", false);
-
-    dbAccessor.addFKConstraint(SERVICE_COMPONENT_HISTORY_TABLE, "FK_sc_history_to_stack_id",
-      "to_stack_id", STACK_TABLE, "stack_id", false);
-
-    addSequence("servicecomponent_history_id_seq", 0L, false);
-  }
-
-  /**
-   * Alter servicecomponentdesiredstate table to add recovery_enabled column.
-   * @throws SQLException
-   */
-  private void updateServiceComponentDesiredStateTable() throws SQLException {
-    // ALTER TABLE servicecomponentdesiredstate ADD COLUMN
-    // recovery_enabled SMALLINT DEFAULT 0 NOT NULL
-    dbAccessor.addColumn(SERVICE_COMPONENT_DESIRED_STATE_TABLE,
-      new DBColumnInfo(RECOVERY_ENABLED_COL, Short.class, null, 0, false));
-
-    dbAccessor.addColumn(SERVICE_COMPONENT_DESIRED_STATE_TABLE,
-      new DBColumnInfo(DESIRED_VERSION_COLUMN_NAME, String.class, 255, State.UNKNOWN.toString(), false));
-  }
-
-  /**
-   * Alter host_role_command table to add original_start_time, which is needed because the start_time column now
-   * allows overriding the value in ActionScheduler.java
-   * @throws SQLException
-   */
-  private void updateHostRoleCommandTableDDL() throws SQLException {
-    final String columnName = "original_start_time";
-    DBColumnInfo originalStartTimeColumn = new DBColumnInfo(columnName, Long.class, null, -1L, true);
-    dbAccessor.addColumn(HOST_ROLE_COMMAND_TABLE, originalStartTimeColumn);
-  }
-
-  /**
-   * Alter host_role_command table to update original_start_time with values and make it non-nullable
-   * @throws SQLException
-   */
-  protected void updateHostRoleCommandTableDML() throws SQLException {
-    final String columnName = "original_start_time";
-    dbAccessor.executeQuery("UPDATE " + HOST_ROLE_COMMAND_TABLE + " SET original_start_time = start_time", false);
-    dbAccessor.executeQuery("UPDATE " + HOST_ROLE_COMMAND_TABLE + " SET original_start_time=-1 WHERE original_start_time IS NULL");
-    dbAccessor.setColumnNullable(HOST_ROLE_COMMAND_TABLE, columnName, false);
-  }
-
-  /**
-   * Puts each item in the specified list inside single quotes and
-   * returns a comma separated value for use in a SQL query.
-   * @param list
-   * @return
-   */
-  private String sqlStringListFromArrayList(List<String> list) {
-    List sqlList = new ArrayList<String>(list.size());
-
-    for (String item : list) {
-      sqlList.add(String.format("'%s'", item.trim()));
-    }
-
-    return StringUtils.join(sqlList, ',');
-  }
-
-  /**
-   * Update clusterconfig table for config type 'cluster-env' with the
-   * recovery attributes.
-   *
-   * @throws AmbariException
-   */
-  private void updateRecoveryClusterEnvConfig() throws AmbariException {
-    Map<String, String> propertyMap = new HashMap<>();
-
-    if (StringUtils.isNotEmpty(config.getNodeRecoveryType())) {
-      propertyMap.put(RecoveryConfigHelper.RECOVERY_ENABLED_KEY, "true");
-      propertyMap.put(RecoveryConfigHelper.RECOVERY_TYPE_KEY, config.getNodeRecoveryType());
-    }
-    else {
-      propertyMap.put(RecoveryConfigHelper.RECOVERY_ENABLED_KEY, "false");
-    }
-
-    if (StringUtils.isNotEmpty(config.getNodeRecoveryLifetimeMaxCount())) {
-      propertyMap.put(RecoveryConfigHelper.RECOVERY_LIFETIME_MAX_COUNT_KEY, config.getNodeRecoveryLifetimeMaxCount());
-    }
-
-    if (StringUtils.isNotEmpty(config.getNodeRecoveryMaxCount())) {
-      propertyMap.put(RecoveryConfigHelper.RECOVERY_MAX_COUNT_KEY, config.getNodeRecoveryMaxCount());
-    }
-
-    if (StringUtils.isNotEmpty(config.getNodeRecoveryRetryGap())) {
-      propertyMap.put(RecoveryConfigHelper.RECOVERY_RETRY_GAP_KEY, config.getNodeRecoveryRetryGap());
-    }
-
-    if (StringUtils.isNotEmpty(config.getNodeRecoveryWindowInMin())) {
-      propertyMap.put(RecoveryConfigHelper.RECOVERY_WINDOW_IN_MIN_KEY, config.getNodeRecoveryWindowInMin());
-    }
-
-    AmbariManagementController ambariManagementController = injector.getInstance(
-            AmbariManagementController.class);
-
-    Clusters clusters = ambariManagementController.getClusters();
-
-    // for each cluster, update/create the cluster-env config type in clusterconfig
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
-    for (final Cluster cluster : clusterMap.values()) {
-      updateConfigurationPropertiesForCluster(cluster, ConfigHelper.CLUSTER_ENV, propertyMap,
-              true /* update if exists */, true /* create new config type */);
-    }
-  }
-
-  /**
-   * Alter servicecomponentdesiredstate table to update recovery_enabled to 1
-   * for the components that have been marked for auto start in ambari.properties
-   * @throws SQLException
-   */
-  private void updateRecoveryComponents() throws SQLException {
-
-    /*
-     * Whether specific components are enabled/disabled for recovery. Being enabled takes
-     * precedence over being disabled. When specific components are enabled then only
-     * those components are enabled. When specific components are disabled then all of
-     * the other components are enabled.
-     */
-    String enabledComponents = config.getRecoveryEnabledComponents();
-    String disabledComponents = config.getRecoveryDisabledComponents();
-    String query;
-
-    if (StringUtils.isEmpty(enabledComponents)) {
-      if (StringUtils.isEmpty(disabledComponents)) {
-        // disable all components
-        query = String.format("UPDATE %s SET recovery_enabled = 0", SERVICE_COMPONENT_DESIRED_STATE_TABLE);
-      }
-      else {
-        // enable (1 - disabledComponents)
-        List<String> disabledComponentsList = Arrays.asList(disabledComponents.split(","));
-        String components = sqlStringListFromArrayList(disabledComponentsList);
-        query = String.format("UPDATE %s SET recovery_enabled = 1 WHERE component_name NOT IN (%s)",
-                SERVICE_COMPONENT_DESIRED_STATE_TABLE, components);
-      }
-    }
-    else {
-      // enable the specified components
-      List<String> enabledComponentsList = Arrays.asList(enabledComponents.split(","));
-      String components = sqlStringListFromArrayList(enabledComponentsList);
-      query = String.format("UPDATE %s SET recovery_enabled = 1 WHERE component_name IN (%s)",
-              SERVICE_COMPONENT_DESIRED_STATE_TABLE, components);
-    }
-
-    dbAccessor.executeQuery(query);
-  }
-
-
-  /**
-   * Update clusterconfig table and servicecomponentdesiredstate table with the
-   * recovery attributes and componenents to be recovered.
-   *
-   * @throws SQLException
-     */
-  @Transactional
-  protected void updateRecoveryConfigurationDML() throws SQLException, AmbariException {
-    updateRecoveryClusterEnvConfig();
-    updateRecoveryComponents();
-  }
-
-  /**
-   * Update Clusters and Hosts Version State from UPGRADING, UPGRADE_FAILED to INSTALLED
-   * and UPGRADED to CURRENT if repo_version_id from cluster_version equals repo_version_id of Clusters and Hosts Version State
-   *
-   * @throws SQLException
-   */
-
-  @Transactional
-  protected void updateClustersAndHostsVersionStateTableDML() throws SQLException, AmbariException {
-
-    dbAccessor.executeQuery("UPDATE " + HOST_VERSION_TABLE + " SET state = 'INSTALLED' WHERE state IN ('UPGRADING', 'UPGRADE_FAILED', 'UPGRADED')");
-    dbAccessor.executeQuery("UPDATE " + CLUSTER_VERSION_TABLE + " SET state = 'INSTALLED' WHERE state IN ('UPGRADING', 'UPGRADE_FAILED', 'UPGRADED')");
-
-    Statement statement = null;
-    ResultSet resultSet = null;
-    try {
-      statement = dbAccessor.getConnection().createStatement();
-      if (statement != null) {
-        String selectSQL = String.format("SELECT repo_version_id, cluster_id FROM %s WHERE state = 'CURRENT'",
-                CLUSTER_VERSION_TABLE);
-
-        resultSet = statement.executeQuery(selectSQL);
-        Set<Long> clusterIds = new HashSet<>();
-        while (null != resultSet && resultSet.next()) {
-          Long clusterId = resultSet.getLong("cluster_id");
-          if (clusterIds.contains(clusterId)) {
-            throw new AmbariException(String.format("Database is in a bad state. Cluster %s contains multiple CURRENT version", clusterId));
-          }
-          clusterIds.add(clusterId);
-          Long repoVersionId = resultSet.getLong("repo_version_id");
-
-          String updateHostVersionSQL = String.format(
-                  "UPDATE %s SET state = 'CURRENT' WHERE repo

<TRUNCATED>

[37/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
AMBARI-21450. Initial cherry-picking for feature branch (ncole)


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

Branch: refs/heads/branch-feature-AMBARI-21450
Commit: 48f7fb22ae2ea61d7b05fa24703ccff2941bf8ff
Parents: 88d93b9
Author: Nate Cole <nc...@hortonworks.com>
Authored: Thu Jul 13 15:13:50 2017 -0400
Committer: Nate Cole <nc...@hortonworks.com>
Committed: Thu Jul 13 15:13:50 2017 -0400

----------------------------------------------------------------------
 .../apache/ambari/annotations/Experimental.java |    2 +-
 .../ambari/server/StateRecoveryManager.java     |   39 +-
 .../actionmanager/ExecutionCommandWrapper.java  |   78 +-
 .../ExecutionCommandWrapperFactory.java         |    2 +-
 .../ambari/server/agent/CommandRepository.java  |  184 ++
 .../ambari/server/agent/ExecutionCommand.java   |  135 +-
 .../ambari/server/agent/HeartbeatProcessor.java |   14 +-
 .../alerts/ComponentVersionAlertRunnable.java   |   57 +-
 .../server/api/services/AmbariMetaInfo.java     |  166 +-
 .../request/AddUpgradeRequestAuditEvent.java    |   10 +-
 .../eventcreator/UpgradeEventCreator.java       |    2 +-
 .../server/checks/AbstractCheckDescriptor.java  |    9 +-
 .../checks/DatabaseConsistencyCheckHelper.java  |    2 -
 .../HardcodedStackVersionPropertiesCheck.java   |   79 +-
 .../server/checks/InstallPackagesCheck.java     |   20 +-
 .../server/checks/PreviousUpgradeCompleted.java |   12 +-
 .../YarnTimelineServerStatePreservingCheck.java |    7 +-
 .../controller/ActionExecutionContext.java      |   53 +-
 .../controller/AmbariActionExecutionHelper.java |  108 +-
 .../AmbariCustomCommandExecutionHelper.java     |  422 ++-
 .../AmbariManagementControllerImpl.java         |  165 +-
 .../ambari/server/controller/HostRequest.java   |   67 +-
 .../ambari/server/controller/HostResponse.java  |  267 +-
 .../server/controller/KerberosHelperImpl.java   |  124 +-
 .../server/controller/PrereqCheckRequest.java   |   27 +-
 .../RootServiceComponentResponse.java           |   51 +-
 .../RootServiceHostComponentResponse.java       |   62 +-
 .../controller/RootServiceResponseFactory.java  |   95 +-
 .../ServiceComponentHostResponse.java           |   41 +-
 .../controller/ServiceComponentResponse.java    |   74 +-
 .../server/controller/ServiceRequest.java       |   46 +-
 .../server/controller/ServiceResponse.java      |   78 +-
 .../AbstractControllerResourceProvider.java     |    2 +-
 .../ClusterStackVersionResourceProvider.java    |  710 +---
 .../internal/ComponentResourceProvider.java     |    9 +-
 .../internal/HostComponentResourceProvider.java |   81 +-
 .../internal/HostResourceProvider.java          |  253 +-
 .../HostStackVersionResourceProvider.java       |   78 +-
 .../controller/internal/HostStatusHelper.java   |    9 +-
 .../PreUpgradeCheckResourceProvider.java        |   27 +-
 .../RepositoryVersionResourceProvider.java      |  100 +-
 ...ootServiceHostComponentResourceProvider.java |   36 +-
 .../internal/ScaleClusterRequest.java           |    8 +-
 .../internal/ServiceResourceProvider.java       |  256 +-
 .../internal/UpgradeResourceProvider.java       | 1198 ++-----
 .../controller/utilities/PropertyHelper.java    |   29 +-
 .../DistributeRepositoriesActionListener.java   |   21 +-
 .../upgrade/HostVersionOutOfSyncListener.java   |  101 +-
 .../listeners/upgrade/StackVersionListener.java |  101 +-
 .../ambari/server/orm/dao/ClusterDAO.java       |  173 +-
 .../server/orm/dao/ClusterVersionDAO.java       |  233 --
 .../server/orm/dao/HostComponentStateDAO.java   |   39 +-
 .../ambari/server/orm/dao/HostVersionDAO.java   |  189 +-
 .../ambari/server/orm/dao/ServiceConfigDAO.java |   19 +-
 .../server/orm/dao/ServiceDesiredStateDAO.java  |   20 +-
 .../apache/ambari/server/orm/dao/StackDAO.java  |   16 +-
 .../orm/entities/ClusterConfigEntity.java       |  129 +-
 .../server/orm/entities/ClusterEntity.java      |   17 -
 .../orm/entities/ClusterVersionEntity.java      |  217 --
 .../server/orm/entities/RepositoryEntity.java   |   11 +-
 .../orm/entities/RepositoryVersionEntity.java   |   11 -
 .../ServiceComponentDesiredStateEntity.java     |  152 +-
 .../entities/ServiceComponentVersionEntity.java |   28 +-
 .../orm/entities/ServiceDesiredStateEntity.java |   87 +-
 .../entities/ServiceDesiredStateEntityPK.java   |    5 +-
 .../server/orm/entities/UpgradeEntity.java      |  199 +-
 .../orm/entities/UpgradeHistoryEntity.java      |  233 ++
 .../PrepareDisableKerberosServerAction.java     |   12 +-
 .../UpdateKerberosConfigsServerAction.java      |   31 +-
 .../upgrades/AbstractUpgradeServerAction.java   |   59 +
 .../upgrades/ComponentVersionCheckAction.java   |   34 +-
 .../serveraction/upgrades/ConfigureAction.java  |    2 +-
 .../upgrades/FinalizeUpgradeAction.java         |  614 ++--
 .../upgrades/UpdateDesiredStackAction.java      |  163 +-
 .../ambari/server/stack/MasterHostResolver.java |   85 +-
 .../apache/ambari/server/stack/RepoUtil.java    |   11 +-
 .../stack/UpdateActiveRepoVersionOnStartup.java |   50 +-
 .../org/apache/ambari/server/state/Cluster.java |  210 +-
 .../ambari/server/state/ConfigHelper.java       |  284 +-
 .../server/state/RepositoryVersionState.java    |   85 +-
 .../org/apache/ambari/server/state/Service.java |   23 +-
 .../ambari/server/state/ServiceComponent.java   |   29 +-
 .../server/state/ServiceComponentHost.java      |   40 +-
 .../server/state/ServiceComponentImpl.java      |  270 +-
 .../ambari/server/state/ServiceFactory.java     |   27 +-
 .../apache/ambari/server/state/ServiceImpl.java |  195 +-
 .../ambari/server/state/UpgradeContext.java     | 1011 ++++--
 .../server/state/UpgradeContextFactory.java     |   25 +-
 .../ambari/server/state/UpgradeHelper.java      |  381 ++-
 .../server/state/cluster/ClusterImpl.java       | 1369 ++------
 .../server/state/cluster/ClustersImpl.java      |   34 +-
 .../ambari/server/state/host/HostImpl.java      |   16 +-
 .../state/repository/VersionDefinitionXml.java  |   26 +-
 .../services/RetryUpgradeActionService.java     |   24 +-
 .../state/stack/upgrade/ClusterGrouping.java    |   12 +-
 .../server/state/stack/upgrade/Direction.java   |   11 +-
 .../server/state/stack/upgrade/ExecuteTask.java |    2 +-
 .../server/state/stack/upgrade/Grouping.java    |   18 +-
 .../state/stack/upgrade/HostOrderGrouping.java  |   19 +-
 .../stack/upgrade/RepositoryVersionHelper.java  |  140 +-
 .../state/stack/upgrade/StageWrapper.java       |    8 +-
 .../stack/upgrade/StageWrapperBuilder.java      |    2 +-
 .../ambari/server/state/stack/upgrade/Task.java |    2 +-
 .../server/state/stack/upgrade/TaskWrapper.java |   10 +-
 .../state/stack/upgrade/TaskWrapperBuilder.java |    8 +-
 .../svccomphost/ServiceComponentHostImpl.java   |  216 +-
 .../ServiceComponentHostSummary.java            |  112 +-
 .../server/upgrade/AbstractUpgradeCatalog.java  |   30 +-
 .../server/upgrade/SchemaUpgradeHelper.java     |   15 +-
 .../server/upgrade/StackUpgradeHelper.java      |  172 -
 .../ambari/server/upgrade/StackUpgradeUtil.java |  197 --
 .../server/upgrade/UpgradeCatalog200.java       |  611 ----
 .../server/upgrade/UpgradeCatalog210.java       | 1761 ----------
 .../server/upgrade/UpgradeCatalog211.java       |  295 --
 .../server/upgrade/UpgradeCatalog212.java       |  404 ---
 .../server/upgrade/UpgradeCatalog2121.java      |  194 --
 .../server/upgrade/UpgradeCatalog220.java       | 1409 --------
 .../server/upgrade/UpgradeCatalog221.java       |  448 ---
 .../server/upgrade/UpgradeCatalog222.java       |  751 -----
 .../server/upgrade/UpgradeCatalog230.java       |  402 ---
 .../server/upgrade/UpgradeCatalog240.java       | 3020 ------------------
 .../server/upgrade/UpgradeCatalog2402.java      |  207 --
 .../server/upgrade/UpgradeCatalog242.java       |  272 --
 .../server/upgrade/UpgradeCatalog250.java       | 1344 --------
 .../apache/ambari/server/utils/StageUtils.java  |   64 +-
 .../main/python/ambari_server/serverUpgrade.py  |  140 -
 .../src/main/resources/META-INF/persistence.xml |    1 -
 .../ambari/server/StateRecoveryManagerTest.java |  134 +-
 .../ExecutionCommandWrapperTest.java            |   39 +-
 .../ComponentVersionAlertRunnableTest.java      |   36 +-
 .../creator/UpgradeEventCreatorTest.java        |    4 +-
 ...ardcodedStackVersionPropertiesCheckTest.java |   10 +-
 .../server/checks/InstallPackagesCheckTest.java |   32 +-
 .../checks/PreviousUpgradeCompletedTest.java    |   15 +-
 ...nTimelineServerStatePreservingCheckTest.java |   23 +-
 .../AmbariCustomCommandExecutionHelperTest.java |  174 +-
 .../RootServiceResponseFactoryTest.java         |   11 +-
 ...ClusterStackVersionResourceProviderTest.java |  548 +---
 .../internal/HostResourceProviderTest.java      |  184 +-
 .../HostStackVersionResourceProviderTest.java   |   14 +-
 .../PreUpgradeCheckResourceProviderTest.java    |   22 +-
 .../RepositoryVersionResourceProviderTest.java  |  100 +-
 .../internal/ServiceResourceProviderTest.java   |  157 +-
 .../internal/UpgradeResourceProviderTest.java   |  532 +--
 .../HostVersionOutOfSyncListenerTest.java       |  178 +-
 .../upgrade/StackVersionListenerTest.java       |  129 +-
 .../orm/dao/HostComponentStateDAOTest.java      |    4 +-
 .../server/orm/dao/HostVersionDAOTest.java      |   38 +-
 .../server/orm/dao/ServiceConfigDAOTest.java    |  419 +--
 .../ComponentVersionCheckActionTest.java        |  223 +-
 .../ambari/server/stack/RepoUtilTest.java       |   10 +-
 .../UpdateActiveRepoVersionOnStartupTest.java   |   88 +-
 .../ambari/server/state/UpgradeHelperTest.java  |  648 ++--
 .../state/repository/VersionDefinitionTest.java |    2 +-
 .../services/RetryUpgradeActionServiceTest.java |   46 +-
 .../upgrade/RepositoryVersionHelperTest.java    |   31 +-
 .../stack/upgrade/StageWrapperBuilderTest.java  |   12 +-
 .../server/upgrade/StackUpgradeUtilTest.java    |  145 -
 .../server/upgrade/UpgradeCatalog200Test.java   |  922 ------
 .../server/upgrade/UpgradeCatalog210Test.java   | 1345 --------
 .../server/upgrade/UpgradeCatalog211Test.java   |  444 ---
 .../server/upgrade/UpgradeCatalog2121Test.java  |  164 -
 .../server/upgrade/UpgradeCatalog212Test.java   |  545 ----
 .../server/upgrade/UpgradeCatalog220Test.java   | 1633 ----------
 .../server/upgrade/UpgradeCatalog221Test.java   |  614 ----
 .../server/upgrade/UpgradeCatalog222Test.java   | 1125 -------
 .../server/upgrade/UpgradeCatalog230Test.java   |  313 --
 .../server/upgrade/UpgradeCatalog240Test.java   | 2586 ---------------
 .../server/upgrade/UpgradeCatalog242Test.java   |  448 ---
 .../server/upgrade/UpgradeCatalog250Test.java   | 2131 ------------
 170 files changed, 7859 insertions(+), 33121 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/annotations/Experimental.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/annotations/Experimental.java b/ambari-server/src/main/java/org/apache/ambari/annotations/Experimental.java
index 8282095..5495933 100644
--- a/ambari-server/src/main/java/org/apache/ambari/annotations/Experimental.java
+++ b/ambari-server/src/main/java/org/apache/ambari/annotations/Experimental.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/StateRecoveryManager.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/StateRecoveryManager.java b/ambari-server/src/main/java/org/apache/ambari/server/StateRecoveryManager.java
index aa6f053..03990a2 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/StateRecoveryManager.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/StateRecoveryManager.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -18,16 +18,17 @@
 
 package org.apache.ambari.server;
 
-import com.google.inject.Inject;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
+import java.util.List;
+
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
+import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
+import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
 import org.apache.ambari.server.state.RepositoryVersionState;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.List;
+import com.google.inject.Inject;
 
 /**
  * Is executed on server start.
@@ -35,20 +36,18 @@ import java.util.List;
  */
 public class StateRecoveryManager {
 
-  private static Logger LOG = LoggerFactory.getLogger(StateRecoveryManager.class);
+  private static final Logger LOG = LoggerFactory.getLogger(StateRecoveryManager.class);
 
   @Inject
   private HostVersionDAO hostVersionDAO;
 
   @Inject
-  private ClusterVersionDAO clusterVersionDAO;
-
+  private ServiceComponentDesiredStateDAO serviceComponentDAO;
 
   public void doWork() {
     checkHostAndClusterVersions();
   }
 
-
   void checkHostAndClusterVersions() {
     List<HostVersionEntity> hostVersions = hostVersionDAO.findAll();
     for (HostVersionEntity hostVersion : hostVersions) {
@@ -65,18 +64,20 @@ public class StateRecoveryManager {
       }
     }
 
-    List<ClusterVersionEntity> clusterVersions = clusterVersionDAO.findAll();
-    for (ClusterVersionEntity clusterVersion : clusterVersions) {
-      if (clusterVersion.getState().equals(RepositoryVersionState.INSTALLING)) {
-        clusterVersion.setState(RepositoryVersionState.INSTALL_FAILED);
+    List<ServiceComponentDesiredStateEntity> components = serviceComponentDAO.findAll();
+    for (ServiceComponentDesiredStateEntity component : components) {
+      if (RepositoryVersionState.INSTALLING == component.getRepositoryState()) {
+        component.setRepositoryState(RepositoryVersionState.INSTALL_FAILED);
+        serviceComponentDAO.merge(component);
         String msg = String.format(
-                "Recovered state of cluster version %s for cluster %s from %s to %s",
-                clusterVersion.getRepositoryVersion().getDisplayName(),
-                clusterVersion.getClusterEntity().getClusterName(),
-                RepositoryVersionState.INSTALLING,
-                RepositoryVersionState.INSTALL_FAILED);
+            "Recovered state of cluster %s of component %s/%s for version %s from %s to %s",
+            component.getClusterId(),
+            component.getServiceName(),
+            component.getComponentName(),
+            component.getDesiredRepositoryVersion().getDisplayName(),
+            RepositoryVersionState.INSTALLING,
+            RepositoryVersionState.INSTALL_FAILED);
         LOG.warn(msg);
-        clusterVersionDAO.merge(clusterVersion);
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapper.java b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapper.java
index 28946e7..efd609a 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapper.java
@@ -17,29 +17,25 @@
  */
 package org.apache.ambari.server.actionmanager;
 
-import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.HOOKS_FOLDER;
-import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.SERVICE_PACKAGE_FOLDER;
-
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Set;
 import java.util.TreeMap;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.ClusterNotFoundException;
+import org.apache.ambari.server.ServiceNotFoundException;
 import org.apache.ambari.server.agent.AgentCommand.AgentCommandType;
 import org.apache.ambari.server.agent.ExecutionCommand;
 import org.apache.ambari.server.agent.ExecutionCommand.KeyNames;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ConfigHelper;
 import org.apache.ambari.server.state.DesiredConfig;
-import org.apache.ambari.server.state.ServiceInfo;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.StackInfo;
+import org.apache.ambari.server.state.Service;
+import org.apache.ambari.server.state.ServiceComponent;
+import org.apache.commons.collections.MapUtils;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -67,12 +63,6 @@ public class ExecutionCommandWrapper {
   @Inject
   private Gson gson;
 
-  /**
-   * Used for injecting hooks and common-services into the command.
-   */
-  @Inject
-  private AmbariMetaInfo ambariMetaInfo;
-
   @AssistedInject
   public ExecutionCommandWrapper(@Assisted String jsonExecutionCommand) {
     this.jsonExecutionCommand = jsonExecutionCommand;
@@ -95,7 +85,6 @@ public class ExecutionCommandWrapper {
    * long as it has been instructed to set updated ones at execution time.
    *
    * @return
-   * @see ExecutionCommand#setForceRefreshConfigTagsBeforeExecution(Set)
    */
   public ExecutionCommand getExecutionCommand() {
     if (executionCommand != null) {
@@ -155,7 +144,8 @@ public class ExecutionCommandWrapper {
       // now that the tags have been updated (if necessary), fetch the
       // configurations
       Map<String, Map<String, String>> configurationTags = executionCommand.getConfigurationTags();
-      if (null != configurationTags && !configurationTags.isEmpty()) {
+
+      if (MapUtils.isNotEmpty(configurationTags)) {
         Map<String, Map<String, String>> configProperties = configHelper
             .getEffectiveConfigProperties(cluster, configurationTags);
 
@@ -196,34 +186,44 @@ public class ExecutionCommandWrapper {
         }
       }
 
-      Map<String,String> commandParams = executionCommand.getCommandParams();
+      // set the repository version for the component this command is for -
+      // always use the current desired version
+      try {
+        RepositoryVersionEntity repositoryVersion = null;
+        String serviceName = executionCommand.getServiceName();
+        if (!StringUtils.isEmpty(serviceName)) {
+          Service service = cluster.getService(serviceName);
+          if (null != service) {
+            repositoryVersion = service.getDesiredRepositoryVersion();
+          }
 
-      ClusterVersionEntity effectiveClusterVersion = cluster.getEffectiveClusterVersion();
-      if (null != effectiveClusterVersion) {
-        commandParams.put(KeyNames.VERSION,
-            effectiveClusterVersion.getRepositoryVersion().getVersion());
-      }
+          String componentName = executionCommand.getComponentName();
+          if (!StringUtils.isEmpty(componentName)) {
+            ServiceComponent serviceComponent = service.getServiceComponent(
+                executionCommand.getComponentName());
 
-      // add the stack and common-services folders to the command, but only if
-      // they don't exist - they may have been put on here with specific values
-      // ahead of time
-      StackId stackId = cluster.getDesiredStackVersion();
-      StackInfo stackInfo = ambariMetaInfo.getStack(stackId.getStackName(),
-          stackId.getStackVersion());
+            if (null != serviceComponent) {
+              repositoryVersion = serviceComponent.getDesiredRepositoryVersion();
+            }
+          }
+        }
 
-      if (!commandParams.containsKey(HOOKS_FOLDER)) {
-        commandParams.put(HOOKS_FOLDER, stackInfo.getStackHooksFolder());
+        if (null != repositoryVersion) {
+          executionCommand.getCommandParams().put(KeyNames.VERSION, repositoryVersion.getVersion());
+          executionCommand.getHostLevelParams().put(KeyNames.CURRENT_VERSION, repositoryVersion.getVersion());
+        }
+      } catch (ServiceNotFoundException serviceNotFoundException) {
+        // it's possible that there are commands specified for a service where
+        // the service doesn't exist yet
+        LOG.warn(
+            "The service {} is not installed in the cluster. No repository version will be sent for this command.",
+            executionCommand.getServiceName());
       }
 
-      if (!commandParams.containsKey(SERVICE_PACKAGE_FOLDER)) {
-        String serviceName = executionCommand.getServiceName();
-        if (!StringUtils.isEmpty(serviceName)) {
-          ServiceInfo serviceInfo = ambariMetaInfo.getService(stackId.getStackName(),
-              stackId.getStackVersion(), serviceName);
+      // set the desired versions of versionable components.  This is safe even during an upgrade because
+      // we are "loading-late": components that have not yet upgraded in an EU will have the correct versions.
+      executionCommand.setComponentVersions(cluster);
 
-          commandParams.put(SERVICE_PACKAGE_FOLDER, serviceInfo.getServicePackageFolder());
-        }
-      }
     } catch (ClusterNotFoundException cnfe) {
       // it's possible that there are commands without clusters; in such cases,
       // just return the de-serialized command and don't try to read configs

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapperFactory.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapperFactory.java b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapperFactory.java
index 29ee709..02d1b23 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapperFactory.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapperFactory.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/agent/CommandRepository.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/agent/CommandRepository.java b/ambari-server/src/main/java/org/apache/ambari/server/agent/CommandRepository.java
new file mode 100644
index 0000000..3d96122
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/agent/CommandRepository.java
@@ -0,0 +1,184 @@
+/**
+ * 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.agent;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.ambari.server.orm.entities.RepositoryEntity;
+import org.apache.ambari.server.state.RepositoryInfo;
+import org.apache.commons.lang.builder.ToStringBuilder;
+
+import com.google.gson.annotations.SerializedName;
+
+/**
+ * Wraps the information required to create repositories from a command.  This was added
+ * as a top level command object.
+ */
+public class CommandRepository {
+
+  @SerializedName("repositories")
+  private List<Repository> m_repositories = new ArrayList<>();
+
+  @SerializedName("repoVersion")
+  private String m_repoVersion;
+
+  @SerializedName("repoVersionId")
+  private long m_repoVersionId;
+
+  @SerializedName("stackName")
+  private String m_stackName;
+
+  /**
+   * @param version the repo version
+   */
+  public void setRepositoryVersion(String version) {
+    m_repoVersion = version;
+  }
+
+  /**
+   * @param id the repository id
+   */
+  public void setRepositoryVersionId(long id) {
+    m_repoVersionId = id;
+  }
+
+  /**
+   * @param name the stack name
+   */
+  public void setStackName(String name) {
+    m_stackName = name;
+  }
+
+  /**
+   * @param repositories the repositories if sourced from the stack instead of the repo_version.
+   */
+  public void setRepositories(Collection<RepositoryInfo> repositories) {
+    m_repositories = new ArrayList<>();
+
+    for (RepositoryInfo info : repositories) {
+      m_repositories.add(new Repository(info));
+    }
+  }
+
+  /**
+   * @param osType        the OS type for the repositories
+   * @param repositories  the repository entities that should be processed into a file
+   */
+  public void setRepositories(String osType, Collection<RepositoryEntity> repositories) {
+    m_repositories = new ArrayList<>();
+
+    for (RepositoryEntity entity : repositories) {
+      m_repositories.add(new Repository(osType, entity));
+    }
+  }
+
+  /**
+   * @return the repositories that the command should process into a file.
+   */
+  public Collection<Repository> getRepositories() {
+    return m_repositories;
+  }
+
+  /**
+   * Sets a uniqueness on the repo ids.
+   *
+   * @param suffix  the repo id suffix
+   */
+  public void setUniqueSuffix(String suffix) {
+    for (Repository repo : m_repositories) {
+      repo.m_repoId = repo.m_repoId + suffix;
+    }
+  }
+
+  /**
+   * Minimal information required to generate repo files on the agent.  These are copies
+   * of the repository objects from repo versions that can be changed for URL overrides, etc.
+   */
+  public static class Repository {
+
+    @SerializedName("baseUrl")
+    private String m_baseUrl;
+
+    @SerializedName("repoId")
+    private String m_repoId;
+
+    /**
+     * The name should not change.  Ubuntu requires that it match exactly as the repo was built.
+     */
+    @SerializedName("repoName")
+    private final String m_repoName;
+
+    @SerializedName("mirrorsList")
+    private String m_mirrorsList;
+
+    private transient String m_osType;
+
+    private Repository(RepositoryInfo info) {
+      m_baseUrl = info.getBaseUrl();
+      m_osType = info.getOsType();
+      m_repoId = info.getRepoId();
+      m_repoName = info.getRepoName();
+      m_mirrorsList = info.getMirrorsList();
+    }
+
+    private Repository(String osType, RepositoryEntity entity) {
+      m_baseUrl = entity.getBaseUrl();
+      m_repoId = entity.getRepositoryId();
+      m_repoName = entity.getName();
+      m_mirrorsList = entity.getMirrorsList();
+      m_osType = osType;
+    }
+
+    public void setBaseUrl(String url) {
+      m_baseUrl = url;
+    }
+
+    public String getOsType() {
+      return m_osType;
+    }
+
+    public String getRepoId() {
+      return m_repoId;
+    }
+
+    public String getRepoName() {
+      return m_repoName;
+    }
+
+
+    public String getBaseUrl() {
+      return m_baseUrl;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+      return new ToStringBuilder(null)
+          .append("os", m_osType)
+          .append("name", m_repoName)
+          .append("id", m_repoId)
+          .append("baseUrl", m_baseUrl)
+          .toString();
+    }
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/agent/ExecutionCommand.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/agent/ExecutionCommand.java b/ambari-server/src/main/java/org/apache/ambari/server/agent/ExecutionCommand.java
index 32fb37b..441c047 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/agent/ExecutionCommand.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/agent/ExecutionCommand.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -24,11 +24,16 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.ambari.annotations.Experimental;
+import org.apache.ambari.annotations.ExperimentalFeature;
+import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.RoleCommand;
-import org.apache.ambari.server.state.ServiceInfo;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Service;
+import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.utils.StageUtils;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.gson.annotations.SerializedName;
 
@@ -39,7 +44,7 @@ import com.google.gson.annotations.SerializedName;
  */
 public class ExecutionCommand extends AgentCommand {
 
-  private static Log LOG = LogFactory.getLog(ExecutionCommand.class);
+  private static final Logger LOG = LoggerFactory.getLogger(ExecutionCommand.class);
 
   public ExecutionCommand() {
     super(AgentCommandType.EXECUTION_COMMAND);
@@ -67,7 +72,7 @@ public class ExecutionCommand extends AgentCommand {
   private String role;
 
   @SerializedName("hostLevelParams")
-  private Map<String, String> hostLevelParams = new HashMap<String, String>();
+  private Map<String, String> hostLevelParams = new HashMap<>();
 
   @SerializedName("roleParams")
   private Map<String, String> roleParams = null;
@@ -76,8 +81,7 @@ public class ExecutionCommand extends AgentCommand {
   private RoleCommand roleCommand;
 
   @SerializedName("clusterHostInfo")
-  private Map<String, Set<String>> clusterHostInfo =
-      new HashMap<String, Set<String>>();
+  private Map<String, Set<String>> clusterHostInfo = new HashMap<>();
 
   @SerializedName("configurations")
   private Map<String, Map<String, String>> configurations;
@@ -92,7 +96,7 @@ public class ExecutionCommand extends AgentCommand {
   private boolean forceRefreshConfigTagsBeforeExecution = false;
 
   @SerializedName("commandParams")
-  private Map<String, String> commandParams = new HashMap<String, String>();
+  private Map<String, String> commandParams = new HashMap<>();
 
   @SerializedName("serviceName")
   private String serviceName;
@@ -104,13 +108,10 @@ public class ExecutionCommand extends AgentCommand {
   private String componentName;
 
   @SerializedName("kerberosCommandParams")
-  private List<Map<String, String>> kerberosCommandParams = new ArrayList<Map<String, String>>();
+  private List<Map<String, String>> kerberosCommandParams = new ArrayList<>();
 
   @SerializedName("localComponents")
-  private Set<String> localComponents = new HashSet<String>();
-
-  @SerializedName("availableServices")
-  private Map<String, String> availableServices = new HashMap<>();
+  private Set<String> localComponents = new HashSet<>();
 
   /**
    * "true" or "false" indicating whether this
@@ -144,12 +145,23 @@ public class ExecutionCommand extends AgentCommand {
   @SerializedName("configuration_credentials")
   private Map<String, Map<String, String>> configurationCredentials;
 
+
+  /**
+   * Provides information regarding the content of repositories.  This structure replaces
+   * the deprecated use of {@link KeyNames#REPO_INFO}
+   */
+  @SerializedName("repositoryFile")
+  private CommandRepository commandRepository;
+
+  @SerializedName("componentVersionMap")
+  private Map<String, Map<String, String>> componentVersionMap = new HashMap<>();
+
   public void setConfigurationCredentials(Map<String, Map<String, String>> configurationCredentials) {
     this.configurationCredentials = configurationCredentials;
   }
 
   public Map<String, Map<String, String>> getConfigurationCredentials() {
-    return this.configurationCredentials;
+    return configurationCredentials;
   }
 
   public String getCommandId() {
@@ -291,18 +303,6 @@ public class ExecutionCommand extends AgentCommand {
     this.localComponents = localComponents;
   }
 
-  public Map<String, String> getAvailableServices() {
-    return availableServices;
-  }
-
-  public void setAvailableServicesFromServiceInfoMap(Map<String, ServiceInfo> serviceInfoMap) {
-    Map<String, String> serviceVersionMap = new HashMap<>();
-    for (Map.Entry<String, ServiceInfo> entry : serviceInfoMap.entrySet()) {
-      serviceVersionMap.put(entry.getKey(), entry.getValue().getVersion());
-    }
-    availableServices = serviceVersionMap;
-  }
-
   public Map<String, Map<String, Map<String, String>>> getConfigurationAttributes() {
     return configurationAttributes;
   }
@@ -395,10 +395,24 @@ public class ExecutionCommand extends AgentCommand {
   }
 
   /**
+   * @return the repository file that is to be written.
+   */
+  public CommandRepository getRepositoryFile() {
+    return commandRepository;
+  }
+
+  /**
+   * @param repository  the command repository instance.
+   */
+  public void setRepositoryFile(CommandRepository repository) {
+    commandRepository = repository;
+  }
+
+  /**
    * Contains key name strings. These strings are used inside maps
    * incapsulated inside command.
    */
-  public static interface KeyNames {
+  public interface KeyNames {
     String COMMAND_TIMEOUT = "command_timeout";
     String SCRIPT = "script";
     String SCRIPT_TYPE = "script_type";
@@ -421,6 +435,11 @@ public class ExecutionCommand extends AgentCommand {
     String ORACLE_JDBC_URL = "oracle_jdbc_url";
     String DB_DRIVER_FILENAME = "db_driver_filename";
     String CLIENTS_TO_UPDATE_CONFIGS = "clientsToUpdateConfigs";
+    /**
+     * Keep for backward compatibility.
+     */
+    @Deprecated
+    @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES)
     String REPO_INFO = "repo_info";
     String DB_NAME = "db_name";
     String GLOBAL = "global";
@@ -433,7 +452,6 @@ public class ExecutionCommand extends AgentCommand {
     String GROUP_LIST = "group_list";
     String USER_GROUPS = "user_groups";
     String NOT_MANAGED_HDFS_PATH_LIST = "not_managed_hdfs_path_list";
-    String VERSION = "version";
     String REFRESH_TOPOLOGY = "refresh_topology";
     String HOST_SYS_PREPPED = "host_sys_prepped";
     String MAX_DURATION_OF_RETRIES = "max_duration_for_retries";
@@ -454,6 +472,8 @@ public class ExecutionCommand extends AgentCommand {
     /**
      * The key indicating that the package_version string is available
      */
+    @Deprecated
+    @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES)
     String PACKAGE_VERSION = "package_version";
 
     /**
@@ -468,6 +488,63 @@ public class ExecutionCommand extends AgentCommand {
      * The agent will return this value back in its response so the repository
      * can be looked up and possibly have its version updated.
      */
+    @Deprecated
+    @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES)
     String REPO_VERSION_ID = "repository_version_id";
+
+    /**
+     * The version of the component to send down with the command. Normally,
+     * this is simply the repository version of the component. However, during
+     * ugprades, this value may change depending on the progress of the upgrade
+     * and the type/direction.
+     */
+    @Experimental(
+        feature = ExperimentalFeature.PATCH_UPGRADES,
+        comment = "Change this to reflect the component version")
+    String VERSION = "version";
+
+    /**
+     * Put on hostLevelParams to indicate the version that the component should
+     * be.
+     */
+    @Deprecated
+    @Experimental(
+        feature = ExperimentalFeature.PATCH_UPGRADES,
+        comment = "This should be replaced by a map of all service component versions")
+    String CURRENT_VERSION = "current_version";
+  }
+
+  /**
+   * @return
+   */
+  public Map<String, Map<String, String>> getComponentVersionMap() {
+    return componentVersionMap;
+  }
+
+  /**
+   * Used to set a map of {service -> { component -> version}}.  This is necessary when performing
+   * an upgrade to correct build paths of required binaries.
+   * @param cluster the cluster from which to build the map
+   */
+  public void setComponentVersions(Cluster cluster) throws AmbariException {
+    Map<String, Map<String, String>> componentVersionMap = new HashMap<>();
+
+    for (Service service : cluster.getServices().values()) {
+      Map<String, String> componentMap = new HashMap<>();
+
+      boolean shouldSet = false;
+      for (ServiceComponent component : service.getServiceComponents().values()) {
+        if (component.isVersionAdvertised()) {
+          shouldSet = true;
+          componentMap.put(component.getName(), component.getDesiredVersion());
+        }
+      }
+
+      if (shouldSet) {
+        componentVersionMap.put(service.getName(), componentMap);
+      }
+    }
+
+    this.componentVersionMap = componentVersionMap;
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatProcessor.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatProcessor.java b/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatProcessor.java
index 222f201..6fc3bdd 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatProcessor.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatProcessor.java
@@ -293,13 +293,13 @@ public class HeartbeatProcessor extends AbstractService{
         int slaveCount = 0;
         int slavesRunning = 0;
 
-        StackId stackId;
         Cluster cluster = clusterFsm.getCluster(clusterName);
-        stackId = cluster.getDesiredStackVersion();
 
 
         List<ServiceComponentHost> scHosts = cluster.getServiceComponentHosts(heartbeat.getHostname());
         for (ServiceComponentHost scHost : scHosts) {
+          StackId stackId = scHost.getDesiredStackId();
+
           ComponentInfo componentInfo =
               ambariMetaInfo.getComponent(stackId.getStackName(),
                   stackId.getStackVersion(), scHost.getServiceName(),
@@ -356,7 +356,7 @@ public class HeartbeatProcessor extends AbstractService{
     List<CommandReport> reports = heartbeat.getReports();
 
     // Cache HostRoleCommand entities because we will need them few times
-    List<Long> taskIds = new ArrayList<Long>();
+    List<Long> taskIds = new ArrayList<>();
     for (CommandReport report : reports) {
       taskIds.add(report.getTaskId());
     }
@@ -491,9 +491,7 @@ public class HeartbeatProcessor extends AbstractService{
             }
 
             // Updating stack version, if needed (this is not actually for express/rolling upgrades!)
-            if (scHost.getState().equals(org.apache.ambari.server.state.State.UPGRADING)) {
-              scHost.setStackVersion(scHost.getDesiredStackVersion());
-            } else if ((report.getRoleCommand().equals(RoleCommand.START.toString()) ||
+            if ((report.getRoleCommand().equals(RoleCommand.START.toString()) ||
                 (report.getRoleCommand().equals(RoleCommand.CUSTOM_COMMAND.toString()) &&
                     ("START".equals(report.getCustomCommand()) ||
                         "RESTART".equals(report.getCustomCommand()))))
@@ -615,10 +613,6 @@ public class HeartbeatProcessor extends AbstractService{
                 }
               }
 
-              if (null != status.getStackVersion() && !status.getStackVersion().isEmpty()) {
-                scHost.setStackVersion(gson.fromJson(status.getStackVersion(), StackId.class));
-              }
-
               if (null != status.getConfigTags()) {
                 scHost.updateActualConfigs(status.getConfigTags());
               }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/alerts/ComponentVersionAlertRunnable.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/alerts/ComponentVersionAlertRunnable.java b/ambari-server/src/main/java/org/apache/ambari/server/alerts/ComponentVersionAlertRunnable.java
index 9dc10ea..45cc973 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/alerts/ComponentVersionAlertRunnable.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/alerts/ComponentVersionAlertRunnable.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -28,7 +28,6 @@ import java.util.TreeMap;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.state.Alert;
@@ -36,9 +35,11 @@ import org.apache.ambari.server.state.AlertState;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.ComponentInfo;
 import org.apache.ambari.server.state.Host;
+import org.apache.ambari.server.state.Service;
+import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentHost;
 import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.State;
+import org.apache.ambari.server.state.stack.upgrade.Direction;
 import org.apache.commons.lang.StringUtils;
 
 import com.google.inject.Inject;
@@ -63,7 +64,7 @@ public class ComponentVersionAlertRunnable extends AlertRunnable {
   /**
    * The message for the alert when there is an upgrade in progress.
    */
-  private static final String UPGRADE_IN_PROGRESS_MSG = "This alert will be suspended while the upgrade to {0} is in progress.";
+  private static final String UPGRADE_IN_PROGRESS_MSG = "This alert will be suspended while the {0} is in progress.";
 
   /**
    * The unknown component error message.
@@ -75,17 +76,6 @@ public class ComponentVersionAlertRunnable extends AlertRunnable {
    */
   private static final String MISMATCHED_VERSIONS_MSG = "The following components are reporting unexpected versions: ";
 
-  /**
-   * The message when there is no CURRENT cluster version, but the cluster is
-   * still being setup.
-   */
-  private static final String CLUSTER_PROVISIONING_MSG = "The cluster is currently being provisioned. This alert will be skipped.";
-
-  /**
-   * The message when there is no CURRENT cluster version.
-   */
-  private static final String CLUSTER_OUT_OF_SYNC_MSG = "The cluster's CURRENT version could not be determined.";
-
   @Inject
   private AmbariMetaInfo m_metaInfo;
 
@@ -102,13 +92,12 @@ public class ComponentVersionAlertRunnable extends AlertRunnable {
    * {@inheritDoc}
    */
   @Override
-  List<Alert> execute(Cluster cluster, AlertDefinitionEntity myDefinition) {
+  List<Alert> execute(Cluster cluster, AlertDefinitionEntity myDefinition) throws AmbariException {
     // if there is an upgrade in progress, then skip running this alert
     UpgradeEntity upgrade = cluster.getUpgradeInProgress();
     if (null != upgrade) {
-      RepositoryVersionEntity repositoryVersion = upgrade.getToRepositoryVersion();
-      String message = MessageFormat.format(UPGRADE_IN_PROGRESS_MSG,
-          repositoryVersion.getVersion());
+      Direction direction = upgrade.getDirection();
+      String message = MessageFormat.format(UPGRADE_IN_PROGRESS_MSG, direction.getText(false));
 
       return Collections.singletonList(
           buildAlert(cluster, myDefinition, AlertState.SKIPPED, message));
@@ -117,27 +106,15 @@ public class ComponentVersionAlertRunnable extends AlertRunnable {
     TreeMap<Host, Set<ServiceComponentHost>> versionMismatches = new TreeMap<>();
     Collection<Host> hosts = cluster.getHosts();
 
-    // no cluster version is very bad ...
-    ClusterVersionEntity clusterVersionEntity = cluster.getCurrentClusterVersion();
-    if (null == clusterVersionEntity) {
-      if (cluster.getProvisioningState() == State.INIT
-          || cluster.getAllClusterVersions().size() == 1) {
-        return Collections.singletonList(
-            buildAlert(cluster, myDefinition, AlertState.SKIPPED, CLUSTER_PROVISIONING_MSG));
-      } else {
-        return Collections.singletonList(
-            buildAlert(cluster, myDefinition, AlertState.CRITICAL, CLUSTER_OUT_OF_SYNC_MSG));
-      }
-    }
-
-    RepositoryVersionEntity repositoryVersionEntity = clusterVersionEntity.getRepositoryVersion();
-    String clusterVersion = repositoryVersionEntity.getVersion();
-
     for (Host host : hosts) {
-      List<ServiceComponentHost> hostComponents = cluster.getServiceComponentHosts(
-          host.getHostName());
+      List<ServiceComponentHost> hostComponents = cluster.getServiceComponentHosts(host.getHostName());
       for (ServiceComponentHost hostComponent : hostComponents) {
-        StackId desiredStackId = hostComponent.getDesiredStackVersion();
+        Service service = cluster.getService(hostComponent.getServiceName());
+        ServiceComponent serviceComponent = service.getServiceComponent(hostComponent.getServiceComponentName());
+
+        RepositoryVersionEntity desiredRepositoryVersion = service.getDesiredRepositoryVersion();
+        StackId desiredStackId = serviceComponent.getDesiredStackId();
+        String desiredVersion = desiredRepositoryVersion.getVersion();
 
         final ComponentInfo componentInfo;
         try {
@@ -159,7 +136,7 @@ public class ComponentVersionAlertRunnable extends AlertRunnable {
         }
 
         String version = hostComponent.getVersion();
-        if (!StringUtils.equals(version, clusterVersion)) {
+        if (!StringUtils.equals(version, desiredVersion)) {
           Set<ServiceComponentHost> mismatchedComponents = versionMismatches.get(host);
           if (null == mismatchedComponents) {
             mismatchedComponents = new HashSet<>();
@@ -194,4 +171,4 @@ public class ComponentVersionAlertRunnable extends AlertRunnable {
 
     return Collections.singletonList(buildAlert(cluster, myDefinition, alertState, alertText));
   }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java
index 2d13cba..4ca35a9 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariMetaInfo.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -140,8 +140,17 @@ public class AmbariMetaInfo {
     }
   };
   private final static Logger LOG = LoggerFactory.getLogger(AmbariMetaInfo.class);
+
+  /**
+   * Repository XML base url property name
+   */
   public static final String REPOSITORY_XML_PROPERTY_BASEURL = "baseurl";
 
+  /**
+   * Repository XML mirrors list property name
+   */
+  public static final String REPOSITORY_XML_PROPERTY_MIRRORSLIST = "mirrorslist";
+
   // all the supported OS'es
   @Inject
   private OsFamily osFamily;
@@ -258,7 +267,7 @@ public class AmbariMetaInfo {
   @Inject
   public void init() throws Exception {
     // Need to be initialized before all actions
-    ALL_SUPPORTED_OS = new ArrayList<String>(osFamily.os_list());
+    ALL_SUPPORTED_OS = new ArrayList<>(osFamily.os_list());
 
     readServerVersion();
 
@@ -371,12 +380,19 @@ public class AmbariMetaInfo {
     return foundDependency;
   }
 
+  /**
+   * Gets repositories that are keyed by operating system type.
+   * @param stackName the stack name
+   * @param version   the stack version
+   * @return
+   * @throws AmbariException
+   */
   public Map<String, List<RepositoryInfo>> getRepository(String stackName,
                                                          String version) throws AmbariException {
     StackInfo stack = getStack(stackName, version);
     List<RepositoryInfo> repository = stack.getRepositories();
 
-    Map<String, List<RepositoryInfo>> reposResult = new HashMap<String, List<RepositoryInfo>>();
+    Map<String, List<RepositoryInfo>> reposResult = new HashMap<>();
     for (RepositoryInfo repo : repository) {
       if (!reposResult.containsKey(repo.getOsType())) {
         reposResult.put(repo.getOsType(),
@@ -393,7 +409,7 @@ public class AmbariMetaInfo {
     StackInfo stack = getStack(stackName, version);
     List<RepositoryInfo> repositories = stack.getRepositories();
 
-    List<RepositoryInfo> repositoriesResult = new ArrayList<RepositoryInfo>();
+    List<RepositoryInfo> repositoriesResult = new ArrayList<>();
     for (RepositoryInfo repository : repositories) {
       if (repository.getOsType().equals(osType)) {
         repositoriesResult.add(repository);
@@ -480,10 +496,7 @@ public class AmbariMetaInfo {
   public String getComponentToService(String stackName, String version,
                                       String componentName) throws AmbariException {
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Looking for service for component"
-          + ", stackName=" + stackName
-          + ", stackVersion=" + version
-          + ", componentName=" + componentName);
+      LOG.debug("Looking for service for component, stackName={}, stackVersion={}, componentName={}", stackName, version, componentName);
     }
     Map<String, ServiceInfo> services = getServices(stackName, version);
     String retService = null;
@@ -510,7 +523,7 @@ public class AmbariMetaInfo {
    */
   public Map<String, ServiceInfo> getServices(String stackName, String version) throws AmbariException {
 
-    Map<String, ServiceInfo> servicesInfoResult = new HashMap<String, ServiceInfo>();
+    Map<String, ServiceInfo> servicesInfoResult = new HashMap<>();
 
     Collection<ServiceInfo> services;
     StackInfo stack;
@@ -529,6 +542,18 @@ public class AmbariMetaInfo {
     return servicesInfoResult;
   }
 
+  /**
+   * Convenience method to use stack id instead of separate name and version.
+   * @param service
+   *            the service business object
+   * @return  the service info instance defined from the stack for the business object
+   * @throws AmbariException
+   */
+  public ServiceInfo getService(Service service) throws AmbariException {
+    StackId stackId = service.getDesiredStackId();
+    return getService(stackId.getStackName(), stackId.getStackVersion(), service.getName());
+  }
+
   public ServiceInfo getService(String stackName, String version, String serviceName) throws AmbariException {
     ServiceInfo service = getStack(stackName, version).getService(serviceName);
 
@@ -546,10 +571,11 @@ public class AmbariMetaInfo {
     return removedServices.contains(serviceName);
   }
 
+
   public Collection<String> getMonitoringServiceNames(String stackName, String version)
     throws AmbariException{
 
-    List<String> monitoringServices = new ArrayList<String>();
+    List<String> monitoringServices = new ArrayList<>();
     for (ServiceInfo service : getServices(stackName, version).values()) {
       if ((service.isMonitoringService() != null) && service.isMonitoringService()) {
         monitoringServices.add(service.getName());
@@ -561,7 +587,7 @@ public class AmbariMetaInfo {
   public Set<String> getRestartRequiredServicesNames(String stackName, String version)
     throws AmbariException{
 
-    HashSet<String> needRestartServices = new HashSet<String>();
+    HashSet<String> needRestartServices = new HashSet<>();
     Collection<ServiceInfo> serviceInfos = getServices(stackName, version).values();
 
     for (ServiceInfo service : serviceInfos) {
@@ -588,7 +614,7 @@ public class AmbariMetaInfo {
   public Set<String> getRackSensitiveServicesNames(String stackName, String version)
       throws AmbariException {
 
-    HashSet<String> needRestartServices = new HashSet<String>();
+    HashSet<String> needRestartServices = new HashSet<>();
 
     Collection<ServiceInfo> serviceInfos = getServices(stackName, version).values();
 
@@ -617,6 +643,17 @@ public class AmbariMetaInfo {
     return stacks;
   }
 
+  /**
+   * Convenience method to get stack info from a stack id
+   * @param stackId
+   *            the stack id
+   * @return  the stack info
+   * @throws AmbariException
+   */
+  public StackInfo getStack(StackId stackId) throws AmbariException {
+    return getStack(stackId.getStackName(), stackId.getStackVersion());
+  }
+
   public StackInfo getStack(String stackName, String version) throws AmbariException {
     StackInfo stackInfoResult = stackManager.getStack(stackName, version);
 
@@ -628,7 +665,7 @@ public class AmbariMetaInfo {
   }
 
   public List<String> getStackParentVersions(String stackName, String version) {
-    List<String> parents = new ArrayList<String>();
+    List<String> parents = new ArrayList<>();
     try {
       StackInfo stackInfo = getStack(stackName, version);
       String parentVersion = stackInfo.getParentStackVersion();
@@ -669,13 +706,13 @@ public class AmbariMetaInfo {
   public Set<PropertyInfo> getServiceProperties(String stackName, String version, String serviceName)
       throws AmbariException {
 
-    return new HashSet<PropertyInfo>(getService(stackName, version, serviceName).getProperties());
+    return new HashSet<>(getService(stackName, version, serviceName).getProperties());
   }
 
   public Set<PropertyInfo> getStackProperties(String stackName, String version)
       throws AmbariException {
 
-    return new HashSet<PropertyInfo>(getStack(stackName, version).getProperties());
+    return new HashSet<>(getStack(stackName, version).getProperties());
   }
 
   public Set<PropertyInfo> getPropertiesByName(String stackName, String version, String serviceName, String propertyName)
@@ -692,7 +729,7 @@ public class AmbariMetaInfo {
           + ", propertyName=" + propertyName);
     }
 
-    Set<PropertyInfo> propertyResult = new HashSet<PropertyInfo>();
+    Set<PropertyInfo> propertyResult = new HashSet<>();
 
     for (PropertyInfo property : properties) {
       if (property.getName().equals(propertyName)) {
@@ -720,7 +757,7 @@ public class AmbariMetaInfo {
           + ", propertyName=" + propertyName);
     }
 
-    Set<PropertyInfo> propertyResult = new HashSet<PropertyInfo>();
+    Set<PropertyInfo> propertyResult = new HashSet<>();
 
     for (PropertyInfo property : properties) {
       if (property.getName().equals(propertyName)) {
@@ -743,7 +780,7 @@ public class AmbariMetaInfo {
   public Set<OperatingSystemInfo> getOperatingSystems(String stackName, String version)
       throws AmbariException {
 
-    Set<OperatingSystemInfo> operatingSystems = new HashSet<OperatingSystemInfo>();
+    Set<OperatingSystemInfo> operatingSystems = new HashSet<>();
     StackInfo stack = getStack(stackName, version);
     List<RepositoryInfo> repositories = stack.getRepositories();
     for (RepositoryInfo repository : repositories) {
@@ -794,13 +831,12 @@ public class AmbariMetaInfo {
 
   private void getCustomActionDefinitions(File customActionDefinitionRoot) throws JAXBException, AmbariException {
     if (customActionDefinitionRoot != null) {
-      LOG.debug("Loading custom action definitions from "
-          + customActionDefinitionRoot.getAbsolutePath());
+      LOG.debug("Loading custom action definitions from {}", customActionDefinitionRoot.getAbsolutePath());
 
       if (customActionDefinitionRoot.exists() && customActionDefinitionRoot.isDirectory()) {
         adManager.readCustomActionDefinitions(customActionDefinitionRoot);
       } else {
-        LOG.debug("No action definitions found at " + customActionDefinitionRoot.getAbsolutePath());
+        LOG.debug("No action definitions found at {}", customActionDefinitionRoot.getAbsolutePath());
       }
     }
   }
@@ -857,43 +893,27 @@ public class AmbariMetaInfo {
     return sb.toString();
   }
 
-  /**
-   * @param stackName the stack name
-   * @param stackVersion the stack version
-   * @param osType the os
-   * @param repoId the repo id
-   * @param newBaseUrl the new base url
-   */
-  public void updateRepoBaseURL(String stackName,
-      String stackVersion, String osType, String repoId, String newBaseUrl) throws AmbariException {
-
-    // validate existing
-    RepositoryInfo ri = getRepository(stackName, stackVersion, osType, repoId);
-
+  public void createRepo(String stackName, String stackVersion, String osType, String repoId, String baseUrl, String mirrorsList) throws AmbariException {
     if (!stackRoot.exists()) {
-      throw new StackAccessException("Stack root does not exist.");
+      throw new StackAccessException("Create repo - Stack root does not exist.");
     }
 
-    ri.setBaseUrl(newBaseUrl);
-
-    if (null != metaInfoDAO) {
-      String metaKey = generateRepoMetaKey(stackName, stackVersion, osType,
-          repoId, REPOSITORY_XML_PROPERTY_BASEURL);
-
-      MetainfoEntity entity = new MetainfoEntity();
-      entity.setMetainfoName(metaKey);
-      entity.setMetainfoValue(newBaseUrl);
-
-      // !!! need a way to remove
-      if (newBaseUrl.equals("")) {
-        metaInfoDAO.remove(entity);
-      } else {
-        metaInfoDAO.merge(entity);
-        ri.setBaseUrlFromSaved(true);
-      }
+    if (null != baseUrl) {
+      createRepoInMetaInfo(stackName, stackVersion, osType, repoId, baseUrl, REPOSITORY_XML_PROPERTY_BASEURL);
+    } else if (null != mirrorsList) {
+      createRepoInMetaInfo(stackName, stackVersion, osType, repoId, mirrorsList, REPOSITORY_XML_PROPERTY_MIRRORSLIST);
     }
   }
 
+  private void createRepoInMetaInfo(String stackName, String stackVersion, String osType, String repoId, String value, String repositoryXmlProperty) {
+    String metaKey = generateRepoMetaKey(stackName, stackVersion, osType,
+        repoId, repositoryXmlProperty);
+    MetainfoEntity entity = new MetainfoEntity();
+    entity.setMetainfoName(metaKey);
+    entity.setMetainfoValue(value);
+    metaInfoDAO.create(entity);
+  }
+
   public File getStackRoot() {
     return stackRoot;
   }
@@ -911,7 +931,7 @@ public class AmbariMetaInfo {
     ServiceInfo svc = getService(stackName, stackVersion, serviceName);
 
     if (null == svc.getMetricsFile() || !svc.getMetricsFile().exists()) {
-      LOG.debug("Metrics file for " + stackName + "/" + stackVersion + "/" + serviceName + " not found.");
+      LOG.debug("Metrics file for {}/{}/{} not found.", stackName, stackVersion, serviceName);
       return null;
     }
 
@@ -993,7 +1013,7 @@ public class AmbariMetaInfo {
   }
 
   private Map<String, Metric> getAggregateFunctionMetrics(String metricName, Metric currentMetric) {
-    Map<String, Metric> newMetrics = new HashMap<String, Metric>();
+    Map<String, Metric> newMetrics = new HashMap<>();
     if (!PropertyHelper.hasAggregateFunctionSuffix(currentMetric.getName())) {
       // For every function id
       for (String identifierToAdd : AGGREGATE_FUNCTION_IDENTIFIERS) {
@@ -1099,46 +1119,44 @@ public class AmbariMetaInfo {
     // for every cluster
     for (Cluster cluster : clusterMap.values()) {
       long clusterId = cluster.getClusterId();
-      StackId stackId = cluster.getDesiredStackVersion();
-      StackInfo stackInfo = getStack(stackId.getStackName(),
-          stackId.getStackVersion());
 
       // creating a mapping between names and service/component for fast lookups
-      Collection<ServiceInfo> stackServices = stackInfo.getServices();
-      Map<String, ServiceInfo> stackServiceMap = new HashMap<String, ServiceInfo>();
-      Map<String, ComponentInfo> stackComponentMap = new HashMap<String, ComponentInfo>();
-      for (ServiceInfo stackService : stackServices) {
-        stackServiceMap.put(stackService.getName(), stackService);
+//      Collection<ServiceInfo> stackServices = new ArrayList<>();
+      Map<String, ServiceInfo> stackServiceMap = new HashMap<>();
+      Map<String, ComponentInfo> stackComponentMap = new HashMap<>();
 
-        List<ComponentInfo> components = stackService.getComponents();
-        for (ComponentInfo component : components) {
-          stackComponentMap.put(component.getName(), component);
-        }
-      }
 
       Map<String, Service> clusterServiceMap = cluster.getServices();
       Set<String> clusterServiceNames = clusterServiceMap.keySet();
 
       // for every service installed in that cluster, get the service metainfo
       // and off of that the alert definitions
-      List<AlertDefinition> stackDefinitions = new ArrayList<AlertDefinition>(50);
-      for (String clusterServiceName : clusterServiceNames) {
-        ServiceInfo stackService = stackServiceMap.get(clusterServiceName);
+      List<AlertDefinition> stackDefinitions = new ArrayList<>(50);
+
+      for (Service service : cluster.getServices().values()) {
+        ServiceInfo stackService = getService(service.getDesiredStackId().getStackName(),
+            service.getDesiredStackId().getStackVersion(), service.getName());
+
         if (null == stackService) {
           continue;
         }
 
+        stackServiceMap.put(stackService.getName(), stackService);
+        List<ComponentInfo> components = stackService.getComponents();
+        for (ComponentInfo component : components) {
+          stackComponentMap.put(component.getName(), component);
+        }
 
         // get all alerts defined on the stack for each cluster service
         Set<AlertDefinition> serviceDefinitions = getAlertDefinitions(stackService);
         stackDefinitions.addAll(serviceDefinitions);
       }
 
-      List<AlertDefinitionEntity> persist = new ArrayList<AlertDefinitionEntity>();
+      List<AlertDefinitionEntity> persist = new ArrayList<>();
       List<AlertDefinitionEntity> entities = alertDefinitionDao.findAll(clusterId);
 
       // create a map of the entities for fast extraction
-      Map<String, AlertDefinitionEntity> mappedEntities = new HashMap<String, AlertDefinitionEntity>(100);
+      Map<String, AlertDefinitionEntity> mappedEntities = new HashMap<>(100);
       for (AlertDefinitionEntity entity : entities) {
         mappedEntities.put(entity.getDefinitionName(), entity);
       }
@@ -1219,7 +1237,7 @@ public class AmbariMetaInfo {
       // stack but still exists in the database - we disable the alert to
       // preserve historical references
       List<AlertDefinitionEntity> definitions = alertDefinitionDao.findAllEnabled(clusterId);
-      List<AlertDefinitionEntity> definitionsToDisable = new ArrayList<AlertDefinitionEntity>();
+      List<AlertDefinitionEntity> definitionsToDisable = new ArrayList<>();
 
       for (AlertDefinitionEntity definition : definitions) {
         String serviceName = definition.getServiceName();
@@ -1230,6 +1248,8 @@ public class AmbariMetaInfo {
           continue;
         }
 
+        StackId stackId = cluster.getService(serviceName).getDesiredStackId();
+
         if (!stackServiceMap.containsKey(serviceName)) {
           LOG.info(
               "The {} service has been marked as deleted for stack {}, disabling alert {}",

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/AddUpgradeRequestAuditEvent.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/AddUpgradeRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/AddUpgradeRequestAuditEvent.java
index 69b4536..59115e3 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/AddUpgradeRequestAuditEvent.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/AddUpgradeRequestAuditEvent.java
@@ -33,7 +33,7 @@ public class AddUpgradeRequestAuditEvent extends RequestAuditEvent {
     /**
      * Repository version
      */
-    private String repositoryVersion;
+    private String repositoryVersionId;
 
     /**
      * Upgrade type (rolling, non-rolling)
@@ -64,8 +64,8 @@ public class AddUpgradeRequestAuditEvent extends RequestAuditEvent {
     protected void buildAuditMessage(StringBuilder builder) {
       super.buildAuditMessage(builder);
 
-      builder.append(", Repository version(")
-        .append(repositoryVersion)
+      builder.append(", Repository version ID(")
+        .append(repositoryVersionId)
         .append("), Upgrade type(")
         .append(upgradeType)
         .append("), Cluster name(")
@@ -73,8 +73,8 @@ public class AddUpgradeRequestAuditEvent extends RequestAuditEvent {
         .append(")");
     }
 
-    public AddUpgradeRequestAuditEventBuilder withRepositoryVersion(String repositoryVersion) {
-      this.repositoryVersion = repositoryVersion;
+    public AddUpgradeRequestAuditEventBuilder withRepositoryVersionId(String repositoryVersionId) {
+      this.repositoryVersionId = repositoryVersionId;
       return this;
     }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeEventCreator.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeEventCreator.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeEventCreator.java
index e64e94c..db4549f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeEventCreator.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeEventCreator.java
@@ -83,7 +83,7 @@ public class UpgradeEventCreator implements RequestAuditEventCreator {
       .withResultStatus(result.getStatus())
       .withUrl(request.getURI())
       .withRemoteIp(request.getRemoteAddress())
-      .withRepositoryVersion(RequestAuditEventCreatorHelper.getProperty(request, UpgradeResourceProvider.UPGRADE_REPO_VERSION))
+      .withRepositoryVersionId(RequestAuditEventCreatorHelper.getProperty(request, UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID))
       .withUpgradeType(RequestAuditEventCreatorHelper.getProperty(request, UpgradeResourceProvider.UPGRADE_TYPE))
       .withClusterName(RequestAuditEventCreatorHelper.getProperty(request, UpgradeResourceProvider.UPGRADE_CLUSTER_NAME))
       .build();

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/checks/AbstractCheckDescriptor.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/checks/AbstractCheckDescriptor.java b/ambari-server/src/main/java/org/apache/ambari/server/checks/AbstractCheckDescriptor.java
index d9d8b42..fe28471 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/checks/AbstractCheckDescriptor.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/checks/AbstractCheckDescriptor.java
@@ -24,9 +24,8 @@ import java.util.Set;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.controller.PrereqCheckRequest;
 import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
+import org.apache.ambari.server.controller.PrereqCheckRequest;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.UpgradeDAO;
@@ -35,7 +34,6 @@ import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.DesiredConfig;
 import org.apache.ambari.server.state.ServiceInfo;
-import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.stack.PrereqCheckType;
 import org.apache.ambari.server.state.stack.PrerequisiteCheck;
 import org.apache.ambari.server.state.stack.UpgradePack;
@@ -61,9 +59,6 @@ public abstract class AbstractCheckDescriptor {
   Provider<Clusters> clustersProvider;
 
   @Inject
-  Provider<ClusterVersionDAO> clusterVersionDAOProvider;
-
-  @Inject
   Provider<HostVersionDAO> hostVersionDaoProvider;
 
   @Inject
@@ -241,7 +236,7 @@ public abstract class AbstractCheckDescriptor {
               c.getDesiredStackVersion().getStackName(),
               c.getDesiredStackVersion().getStackVersion());
 
-          LinkedHashSet<String> displays = new LinkedHashSet<String>();
+          LinkedHashSet<String> displays = new LinkedHashSet<>();
           for (String name : names) {
             if (services.containsKey(name)) {
               displays.add(services.get(name).getDisplayName());

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/checks/DatabaseConsistencyCheckHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/checks/DatabaseConsistencyCheckHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/checks/DatabaseConsistencyCheckHelper.java
index c6239d8..256e7b0 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/checks/DatabaseConsistencyCheckHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/checks/DatabaseConsistencyCheckHelper.java
@@ -655,8 +655,6 @@ public class DatabaseConsistencyCheckHelper {
       if (!clusterConfigEntity.isServiceDeleted()){
         continue; // skip clusterConfigs that did not leave after service deletion
       }
-      LOG.info("Removing cluster config mapping of clusterConfigEntity {} that is not mapped to any service", clusterConfigEntity);
-      clusterDAO.removeClusterConfigMappingEntityByConfig(clusterConfigEntity);
       LOG.info("Removing config that is not mapped to any service {}", clusterConfigEntity);
       clusterDAO.removeConfig(clusterConfigEntity);
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/checks/HardcodedStackVersionPropertiesCheck.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/checks/HardcodedStackVersionPropertiesCheck.java b/ambari-server/src/main/java/org/apache/ambari/server/checks/HardcodedStackVersionPropertiesCheck.java
index f5e2b10..b103bca 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/checks/HardcodedStackVersionPropertiesCheck.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/checks/HardcodedStackVersionPropertiesCheck.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -17,33 +17,25 @@
  */
 package org.apache.ambari.server.checks;
 
-import com.google.inject.Inject;
-import com.google.inject.Provider;
-import com.google.inject.Singleton;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.controller.PrereqCheckRequest;
-import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.ConfigHelper;
-import org.apache.ambari.server.state.ConfigMergeHelper;
-import org.apache.ambari.server.state.ConfigMergeHelper.ThreeWayValue;
 import org.apache.ambari.server.state.DesiredConfig;
 import org.apache.ambari.server.state.stack.PrereqCheckStatus;
 import org.apache.ambari.server.state.stack.PrerequisiteCheck;
 import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
+import org.apache.commons.lang.StringUtils;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import com.google.inject.Singleton;
 
 /**
  * Checks for properties that contain hardcoded CURRENT hdp version string.
@@ -57,12 +49,6 @@ import java.util.regex.Pattern;
     required = { UpgradeType.ROLLING, UpgradeType.NON_ROLLING, UpgradeType.HOST_ORDERED })
 public class HardcodedStackVersionPropertiesCheck extends AbstractCheckDescriptor {
 
-  @Inject
-  private Provider<Clusters> m_clusters;
-
-  @Inject
-  private Provider<ConfigHelper> m_config_helper_provider;
-
   public HardcodedStackVersionPropertiesCheck() {
     super(CheckDescription.HARDCODED_STACK_VERSION_PROPERTIES_CHECK);
   }
@@ -71,18 +57,13 @@ public class HardcodedStackVersionPropertiesCheck extends AbstractCheckDescripto
   public void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request)
       throws AmbariException {
 
-    String stackName = request.getTargetStackId().getStackName();
-    RepositoryVersionEntity rve = repositoryVersionDaoProvider.get().
-      findByStackNameAndVersion(stackName, request.getRepositoryVersion());
-
-    Cluster cluster = m_clusters.get().getCluster(request.getClusterName());
+    Cluster cluster = clustersProvider.get().getCluster(request.getClusterName());
 
-    String currentHdpVersion = cluster.getCurrentClusterVersion().getRepositoryVersion().getVersion();
-
-    Map<String, DesiredConfig> desiredConfigs = cluster.getDesiredConfigs();
+    Set<String> versions = new HashSet<>();
     Set<String> failures = new HashSet<>();
+    Set<String> failedVersions = new HashSet<>();
 
-    Pattern searchPattern = getHardcodeSearchPattern(currentHdpVersion);
+    Map<String, DesiredConfig> desiredConfigs = cluster.getDesiredConfigs();
     for (Entry<String, DesiredConfig> configEntry : desiredConfigs.entrySet()) {
       String configType = configEntry.getKey();
       DesiredConfig desiredConfig = configEntry.getValue();
@@ -90,23 +71,31 @@ public class HardcodedStackVersionPropertiesCheck extends AbstractCheckDescripto
 
       Map<String, String> properties = config.getProperties();
       for (Entry<String, String> property : properties.entrySet()) {
-        if (stringContainsVersionHardcode(property.getValue(), searchPattern)) {
-          failures.add(String.format(" %s/%s",
-            configType, property.getKey()));
+
+        // !!! this code is already iterating every config property, so an extra loop for the small-ish
+        // numbers of repository versions won't add that much more overhead
+        for (String version : versions) {
+          Pattern searchPattern = getHardcodeSearchPattern(version);
+          if (stringContainsVersionHardcode(property.getValue(), searchPattern)) {
+            failedVersions.add(version);
+            failures.add(String.format("%s/%s found a hardcoded value %s",
+              configType, property.getKey(), version));
+          }
         }
       }
+    }
 
-      if (failures.size() > 0) {
-        prerequisiteCheck.setStatus(PrereqCheckStatus.WARNING);
-        String failReason = getFailReason(prerequisiteCheck, request);
+    if (failures.size() > 0) {
+      prerequisiteCheck.setStatus(PrereqCheckStatus.WARNING);
+      String failReason = getFailReason(prerequisiteCheck, request);
 
-        prerequisiteCheck.setFailReason(String.format(failReason, currentHdpVersion));
-        prerequisiteCheck.setFailedOn(new LinkedHashSet<>(failures));
+      prerequisiteCheck.setFailReason(String.format(failReason, StringUtils.join(failedVersions, ',')));
+      prerequisiteCheck.setFailedOn(new LinkedHashSet<>(failures));
 
-      } else {
-        prerequisiteCheck.setStatus(PrereqCheckStatus.PASS);
-      }
+    } else {
+      prerequisiteCheck.setStatus(PrereqCheckStatus.PASS);
     }
+
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/checks/InstallPackagesCheck.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/checks/InstallPackagesCheck.java b/ambari-server/src/main/java/org/apache/ambari/server/checks/InstallPackagesCheck.java
index 38bdda2..ac2116f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/checks/InstallPackagesCheck.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/checks/InstallPackagesCheck.java
@@ -24,7 +24,6 @@ import java.util.Set;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.controller.PrereqCheckRequest;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.Cluster;
@@ -75,9 +74,7 @@ public class InstallPackagesCheck extends AbstractCheckDescriptor {
       return;
     }
 
-    final ClusterVersionEntity clusterVersion = clusterVersionDAOProvider.get().findByClusterAndStackAndVersion(
-        clusterName, targetStackId, repoVersion);
-    final Set<String> failedHosts = new HashSet<String>();
+    final Set<String> failedHosts = new HashSet<>();
 
     for (Host host : cluster.getHosts()) {
       if (host.getMaintenanceState(cluster.getClusterId()) != MaintenanceState.ON) {
@@ -95,19 +92,12 @@ public class InstallPackagesCheck extends AbstractCheckDescriptor {
               "Install Packages had failed. Please re-run Install Packages, if necessary place following hosts " +
               "in Maintenance mode: {4}", cluster.getClusterName(), targetStackId.getStackName(),
           targetStackId.getStackVersion(), repoVersion, StringUtils.join(failedHosts, ", "));
-      prerequisiteCheck.setFailedOn(new LinkedHashSet<String>(failedHosts));
-      prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL);
-      prerequisiteCheck.setFailReason(message);
-    } else if (clusterVersion.getState() == RepositoryVersionState.INSTALL_FAILED) {
-      String message = MessageFormat.format("Cluster [{0},{1},{2},{3}] is in INSTALL_FAILED state because " +
-              "Install Packages failed. Please re-run Install Packages even if you placed the failed hosts " +
-              "in Maintenance mode.", cluster.getClusterName(), targetStackId.getStackName(),
-          targetStackId.getStackVersion(), repoVersion);
-      LinkedHashSet<String> failedOn = new LinkedHashSet<String>();
-      failedOn.add(cluster.getClusterName());
-      prerequisiteCheck.setFailedOn(failedOn);
+      prerequisiteCheck.setFailedOn(new LinkedHashSet<>(failedHosts));
       prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL);
       prerequisiteCheck.setFailReason(message);
+      return;
     }
+
+    prerequisiteCheck.setStatus(PrereqCheckStatus.PASS);
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/checks/PreviousUpgradeCompleted.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/checks/PreviousUpgradeCompleted.java b/ambari-server/src/main/java/org/apache/ambari/server/checks/PreviousUpgradeCompleted.java
index 833ee73..0292b72 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/checks/PreviousUpgradeCompleted.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/checks/PreviousUpgradeCompleted.java
@@ -22,7 +22,6 @@ import java.util.LinkedHashSet;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.controller.PrereqCheckRequest;
-import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.stack.PrereqCheckStatus;
@@ -47,7 +46,7 @@ public class PreviousUpgradeCompleted extends AbstractCheckDescriptor {
   /**
    * The message displayed as part of this pre-upgrade check.
    */
-  public static final String ERROR_MESSAGE = "There is an existing {0} from {1}-{2} to {3}-{4} which has not completed. This {5} must be completed before a new upgrade or downgrade can begin.";
+  public static final String ERROR_MESSAGE = "There is an existing {0} {1} {2} which has not completed. This {3} must be completed before a new upgrade or downgrade can begin.";
 
   /**
    * Constructor.
@@ -66,13 +65,10 @@ public class PreviousUpgradeCompleted extends AbstractCheckDescriptor {
     if (null != upgradeInProgress) {
       Direction direction = upgradeInProgress.getDirection();
       String directionText = direction.getText(false);
+      String prepositionText = direction.getPreposition();
 
-      RepositoryVersionEntity fromRepositoryVersion = upgradeInProgress.getFromRepositoryVersion();
-      RepositoryVersionEntity toRepositoryVersion = upgradeInProgress.getToRepositoryVersion();
-
-      errorMessage = MessageFormat.format(ERROR_MESSAGE, directionText,
-          fromRepositoryVersion.getStackName(), fromRepositoryVersion.getVersion(),
-          toRepositoryVersion.getStackName(), toRepositoryVersion.getVersion(), directionText);
+      errorMessage = MessageFormat.format(ERROR_MESSAGE, directionText, prepositionText,
+          upgradeInProgress.getRepositoryVersion().getVersion(), directionText);
     }
 
     if (null != errorMessage) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheck.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheck.java b/ambari-server/src/main/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheck.java
index 8cb8020..ba4b61e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheck.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheck.java
@@ -82,10 +82,11 @@ public class YarnTimelineServerStatePreservingCheck extends AbstractCheckDescrip
       if(minStack.length == 2) {
         String minStackName = minStack[0];
         String minStackVersion = minStack[1];
-        String stackName = cluster.getCurrentStackVersion().getStackName();
+        Service yarnService = cluster.getService("YARN");
+        String stackName = yarnService.getDesiredStackId().getStackName();
         if (minStackName.equals(stackName)) {
-          String currentClusterRepositoryVersion = cluster.getCurrentClusterVersion().getRepositoryVersion().getVersion();
-          return VersionUtils.compareVersions(currentClusterRepositoryVersion, minStackVersion) >= 0;
+          String currentRepositoryVersion = yarnService.getDesiredRepositoryVersion().getVersion();
+          return VersionUtils.compareVersions(currentRepositoryVersion, minStackVersion) >= 0;
         }
       }
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/ActionExecutionContext.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ActionExecutionContext.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/ActionExecutionContext.java
index c361094..42a95c0 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ActionExecutionContext.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ActionExecutionContext.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,13 +19,14 @@
 
 package org.apache.ambari.server.controller;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.ambari.server.actionmanager.TargetHostType;
+import org.apache.ambari.server.agent.ExecutionCommand;
 import org.apache.ambari.server.controller.internal.RequestOperationLevel;
 import org.apache.ambari.server.controller.internal.RequestResourceFilter;
-import org.apache.ambari.server.state.StackId;
 
 /**
  * The context required to create tasks and stages for a custom action
@@ -42,7 +43,8 @@ public class ActionExecutionContext {
   private String expectedComponentName;
   private boolean hostsInMaintenanceModeExcluded = true;
   private boolean allowRetry = false;
-  private StackId stackId;
+
+  private List<ExecutionCommandVisitor> m_visitors = new ArrayList<>();
 
   /**
    * {@code true} if slave/client component failures should be automatically
@@ -171,27 +173,13 @@ public class ActionExecutionContext {
   }
 
   /**
-   * Gets the stack to use for generating stack-associated values for a command.
-   * In some cases the cluster's stack is not the correct one to use, such as
-   * when distributing a repository.
+   * Adds a command visitor that will be invoked after a command is created.  Provides access
+   * to the command.
    *
-   * @return the stackId the stack to use when generating stack-specific content
-   *         for the command.
+   * @param visitor the visitor
    */
-  public StackId getStackId() {
-    return stackId;
-  }
-
-  /**
-   * Sets the stack to use for generating stack-associated values for a command.
-   * In some cases the cluster's stack is not the correct one to use, such as
-   * when distributing a repository.
-   *
-   * @param stackId
-   *          the stackId to use for stack-based properties on the command.
-   */
-  public void setStackId(StackId stackId) {
-    this.stackId = stackId;
+  public void addVisitor(ExecutionCommandVisitor visitor) {
+    m_visitors.add(visitor);
   }
 
   @Override
@@ -233,4 +221,25 @@ public class ActionExecutionContext {
     hostsInMaintenanceModeExcluded = excluded;
   }
 
+  /**
+   * Called as a way to post-process the command after it has been created and various objects
+   * have been set.
+   *
+   * @param command the command
+   */
+  public void visitAll(ExecutionCommand command) {
+    for (ExecutionCommandVisitor visitor : m_visitors) {
+      visitor.visit(command);
+    }
+  }
+
+  /**
+   * Interface that allows a final attempt to setting values on an {@link ExecutionCommand}
+   * @author ncole
+   *
+   */
+  public static interface ExecutionCommandVisitor {
+    public void visit(ExecutionCommand command);
+  }
+
 }
\ No newline at end of file


[07/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog2121Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog2121Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog2121Test.java
deleted file mode 100644
index 1499fd1..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog2121Test.java
+++ /dev/null
@@ -1,164 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import static junit.framework.Assert.assertEquals;
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.createMockBuilder;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
-
-import java.lang.reflect.Method;
-import java.sql.SQLException;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
-import javax.persistence.EntityManager;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.H2DatabaseCleaner;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.GuiceJpaInitializer;
-import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.DaoUtils;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.entities.StackEntity;
-import org.apache.ambari.server.state.*;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.easymock.EasyMockSupport;
-import org.easymock.internal.MockBuilder;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import com.google.inject.AbstractModule;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Provider;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-
-@PrepareForTest(UpgradeCatalog2121.class)
-public class UpgradeCatalog2121Test {
-  private Injector injector;
-  private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
-  private EntityManager entityManager = createNiceMock(EntityManager.class);
-  private UpgradeCatalogHelper upgradeCatalogHelper;
-  private StackEntity desiredStackEntity;
-
-  @Before
-  public void init() {
-    reset(entityManagerProvider);
-    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
-    replay(entityManagerProvider);
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-
-    upgradeCatalogHelper = injector.getInstance(UpgradeCatalogHelper.class);
-    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
-    injector.getInstance(AmbariMetaInfo.class);
-    // load the stack entity
-    StackDAO stackDAO = injector.getInstance(StackDAO.class);
-    desiredStackEntity = stackDAO.find("PHD", "3.0.0");
-  }
-
-  @After
-  public void tearDown() throws AmbariException, SQLException {
-    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
-  }
-
-  @Test
-  public void testExecuteDMLUpdates() throws Exception {
-    Method updatePHDConfigs = UpgradeCatalog2121.class.getDeclaredMethod("updatePHDConfigs");
-    Method updateOozieConfigs = UpgradeCatalog2121.class.getDeclaredMethod("updateOozieConfigs");
-
-    UpgradeCatalog2121 upgradeCatalog2121 = createMockBuilder(UpgradeCatalog2121.class)
-        .addMockedMethod(updatePHDConfigs)
-        .addMockedMethod(updateOozieConfigs)
-        .createMock();
-
-    upgradeCatalog2121.updatePHDConfigs();
-    expectLastCall().once();
-    upgradeCatalog2121.updateOozieConfigs();
-    expectLastCall().once();
-
-    replay(upgradeCatalog2121);
-
-    upgradeCatalog2121.executeDMLUpdates();
-
-    verify(upgradeCatalog2121);
-  }
-
-  @Test
-  public void testUpdateOozieConfigs() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Map<String, String> propertiesOozieSite = new HashMap<String, String>() {{
-      put("oozie.authentication.kerberos.name.rules", "\n ");
-    }};
-    final Config oozieSiteConf = easyMockSupport.createNiceMock(Config.class);
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("oozie-site")).andReturn(oozieSiteConf).atLeastOnce();
-
-    expect(oozieSiteConf.getProperties()).andReturn(propertiesOozieSite).once();
-
-    UpgradeCatalog2121 upgradeCatalog2121 = createMockBuilder(UpgradeCatalog2121.class)
-            .withConstructor(Injector.class)
-            .withArgs(mockInjector)
-            .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-                    Map.class, Set.class, boolean.class, boolean.class)
-            .createMock();
-    upgradeCatalog2121.updateConfigurationPropertiesForCluster(mockClusterExpected,
-            "oozie-site", new HashMap<String, String>(), Collections.singleton("oozie.authentication.kerberos.name.rules"),
-            true, false);
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    replay(upgradeCatalog2121);
-    upgradeCatalog2121.updateOozieConfigs();
-    easyMockSupport.verifyAll();
-
-  }
-}
-

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog212Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog212Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog212Test.java
deleted file mode 100644
index b87d6cc..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog212Test.java
+++ /dev/null
@@ -1,545 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import static junit.framework.Assert.assertEquals;
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.anyString;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.createMockBuilder;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.newCapture;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.persistence.EntityManager;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.H2DatabaseCleaner;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
-import org.apache.ambari.server.orm.GuiceJpaInitializer;
-import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.ConfigHelper;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.easymock.Capture;
-import org.easymock.EasyMock;
-import org.easymock.EasyMockRule;
-import org.easymock.EasyMockSupport;
-import org.easymock.Mock;
-import org.easymock.MockType;
-import org.easymock.TestSubject;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-
-import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.Provider;
-
-/**
- * {@link org.apache.ambari.server.upgrade.UpgradeCatalog212} unit tests.
- */
-public class UpgradeCatalog212Test {
-
-  private static final String TOPOLOGY_REQUEST_TABLE = "topology_request";
-  private static final String TOPOLOGY_REQUEST_CLUSTER_NAME_COLUMN = "cluster_name";
-
-  private Injector injector;
-
-  @Rule
-  public EasyMockRule mocks = new EasyMockRule(this);
-
-  @Mock(type = MockType.STRICT)
-  private Provider<EntityManager> entityManagerProvider;
-
-  @Mock(type = MockType.NICE)
-  private EntityManager entityManager;
-
-  @Mock(type = MockType.NICE)
-  private DBAccessor dbAccessor;
-
-  @Mock
-  private Injector mockedInjector;
-
-  @Mock(type = MockType.NICE)
-  private Connection connection;
-
-  @Mock
-  private Statement statement;
-
-  @Mock
-  private ResultSet resultSet;
-
-  @TestSubject
-  private UpgradeCatalog212 testSubject = new UpgradeCatalog212(
-      EasyMock.createNiceMock(Injector.class));
-
-  @Before
-  public void setUp() {
-    reset(entityManagerProvider);
-    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
-    replay(entityManagerProvider);
-
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-
-    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
-    injector.getInstance(AmbariMetaInfo.class);
-  }
-
-  @After
-  public void tearDown() throws AmbariException, SQLException {
-    if (injector != null) {
-      H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
-    }
-  }
-
-
-  @Test
-  public void testFinilizeTopologyDDL() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    dbAccessor.dropColumn(eq("topology_request"), eq("cluster_name"));
-    dbAccessor.setColumnNullable(eq("topology_request"), eq("cluster_id"), eq(false));
-    dbAccessor.addFKConstraint(eq("topology_request"), eq("FK_topology_request_cluster_id"), eq("cluster_id"),
-      eq("clusters"), eq("cluster_id"), eq(false));
-
-    replay(dbAccessor);
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    UpgradeCatalog212 upgradeCatalog212 = injector.getInstance(UpgradeCatalog212.class);
-    upgradeCatalog212.finilizeTopologyDDL();
-
-    verify(dbAccessor);
-  }
-
-  @Test
-  public void testExecuteDDLUpdates() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    Configuration configuration = createNiceMock(Configuration.class);
-    Connection connection = createNiceMock(Connection.class);
-    Statement statement = createNiceMock(Statement.class);
-    ResultSet resultSet = createNiceMock(ResultSet.class);
-    expect(configuration.getDatabaseUrl()).andReturn(Configuration.JDBC_IN_MEMORY_URL).anyTimes();
-    dbAccessor.getConnection();
-    expectLastCall().andReturn(connection).anyTimes();
-    connection.createStatement();
-    expectLastCall().andReturn(statement).anyTimes();
-    statement.executeQuery(anyObject(String.class));
-    expectLastCall().andReturn(resultSet).anyTimes();
-
-    // Create DDL sections with their own capture groups
-    HostRoleCommandDDL hostRoleCommandDDL = new HostRoleCommandDDL();
-
-    // Execute any DDL schema changes
-    hostRoleCommandDDL.execute(dbAccessor);
-
-    // Replay sections
-    replay(dbAccessor, configuration, resultSet, connection, statement);
-
-    AbstractUpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
-    Class<?> c = AbstractUpgradeCatalog.class;
-    Field f = c.getDeclaredField("configuration");
-    f.setAccessible(true);
-    f.set(upgradeCatalog, configuration);
-
-    upgradeCatalog.executeDDLUpdates();
-    verify(dbAccessor, configuration, resultSet, connection, statement);
-
-    // Verify sections
-    hostRoleCommandDDL.verify(dbAccessor);
-  }
-
-  @Test
-  public void testExecuteDMLUpdates() throws Exception {
-    Method addMissingConfigs = UpgradeCatalog212.class.getDeclaredMethod("addMissingConfigs");
-    Method addNewConfigurationsFromXml = AbstractUpgradeCatalog.class.getDeclaredMethod("addNewConfigurationsFromXml");
-
-    UpgradeCatalog212 upgradeCatalog212 = createMockBuilder(UpgradeCatalog212.class)
-            .addMockedMethod(addNewConfigurationsFromXml)
-            .addMockedMethod(addMissingConfigs)
-            .createMock();
-
-    upgradeCatalog212.addNewConfigurationsFromXml();
-    expectLastCall().once();
-
-    upgradeCatalog212.addMissingConfigs();
-    expectLastCall().once();
-
-    replay(upgradeCatalog212);
-
-    upgradeCatalog212.executeDMLUpdates();
-
-    verify(upgradeCatalog212);
-  }
-
-  @Test
-  public void testUpdateHBaseAdnClusterConfigs() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-
-    final Map<String, String> propertiesHbaseSite = new HashMap<String, String>() {
-      {
-        put("hbase.bucketcache.size", "1024m");
-      }
-    };
-
-    final Map<String, String> propertiesHbaseEnv = new HashMap<String, String>() {
-      {
-        put("override_hbase_uid", "false");
-      }
-    };
-
-    final Config mockHbaseEnv = easyMockSupport.createNiceMock(Config.class);
-    expect(mockHbaseEnv.getProperties()).andReturn(propertiesHbaseEnv).once();
-    final Config mockHbaseSite = easyMockSupport.createNiceMock(Config.class);
-    expect(mockHbaseSite.getProperties()).andReturn(propertiesHbaseSite).once();
-    final Config mockClusterEnv = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> propertiesExpectedHbaseEnv = new HashMap<>();
-    final Map<String, String> propertiesExpectedClusterEnv = new HashMap<String, String>() {{
-      put("override_uid", "false");
-    }};
-    final Map<String, String> propertiesExpectedHbaseSite = new HashMap<String, String>() {{
-      put("hbase.bucketcache.size", "1024");
-    }};
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    expect(mockClusterExpected.getDesiredConfigByType("cluster-env")).andReturn(mockClusterEnv).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hbase-env")).andReturn(mockHbaseEnv).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hbase-site")).andReturn(mockHbaseSite).atLeastOnce();
-
-    expect(mockClusterEnv.getProperties()).andReturn(propertiesExpectedClusterEnv).atLeastOnce();
-    expect(mockHbaseEnv.getProperties()).andReturn(propertiesExpectedHbaseEnv).atLeastOnce();
-    expect(mockHbaseSite.getProperties()).andReturn(propertiesExpectedHbaseSite).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog212.class).updateHbaseAndClusterConfigurations();
-    easyMockSupport.verifyAll();
-
-  }
-
-  @Test
-  public void testUpdateHiveConfigs() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Config mockHiveSite = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> propertiesExpectedHiveSite = new HashMap<String, String>() {{
-      put("hive.heapsize", "512");
-      put("hive.server2.custom.authentication.class", "");
-    }};
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    expect(mockClusterExpected.getDesiredConfigByType("hive-site")).andReturn(mockHiveSite).atLeastOnce();
-    expect(mockHiveSite.getProperties()).andReturn(propertiesExpectedHiveSite).atLeastOnce();
-
-    StackId stackId = new StackId("HDP-2.2");
-    expect(mockClusterExpected.getCurrentStackVersion()).andReturn(stackId).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog212.class).updateHiveConfigs();
-    easyMockSupport.verifyAll();
-
-  }
-
-  @Test
-  public void testUpdateOozieConfigs() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Config mockOozieEnv = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> propertiesExpectedOozieEnv = new HashMap<String, String>() {{
-      put("oozie_hostname", "");
-      put("oozie_database", "123");
-    }};
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    expect(mockClusterExpected.getDesiredConfigByType("oozie-env")).andReturn(mockOozieEnv).atLeastOnce();
-    expect(mockOozieEnv.getProperties()).andReturn(propertiesExpectedOozieEnv).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog212.class).updateOozieConfigs();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateHiveEnvContent() throws Exception {
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-    String testContent = " if [ \"$SERVICE\" = \"cli\" ]; then\n" +
-            "   if [ -z \"$DEBUG\" ]; then\n" +
-            "     export HADOOP_OPTS=\"$HADOOP_OPTS -XX:NewRatio=12 -Xms10m -XX:MaxHeapFreeRatio=40 -XX:MinHeapFreeRatio=15 -XX:+UseParNewGC -XX:-UseGCOverheadLimit\"\n" +
-            "   else\n" +
-            "     export HADOOP_OPTS=\"$HADOOP_OPTS -XX:NewRatio=12 -Xms10m -XX:MaxHeapFreeRatio=40 -XX:MinHeapFreeRatio=15 -XX:-UseGCOverheadLimit\"\n" +
-            "   fi\n" +
-            " fi\n" +
-            "\n" +
-            "export HADOOP_HEAPSIZE=\"{{hive_heapsize}}\"\n" +
-            "export HADOOP_CLIENT_OPTS=\"-Xmx${HADOOP_HEAPSIZE}m $HADOOP_CLIENT_OPTS\"\n" +
-            "\n" +
-            "# Set HADOOP_HOME to point to a specific hadoop install directory\n" +
-            "HADOOP_HOME=${HADOOP_HOME:-{{hadoop_home}}}\n";
-    String expectedResult = " if [ \"$SERVICE\" = \"cli\" ]; then\n" +
-            "   if [ -z \"$DEBUG\" ]; then\n" +
-            "     export HADOOP_OPTS=\"$HADOOP_OPTS -XX:NewRatio=12 -Xms10m -XX:MaxHeapFreeRatio=40 -XX:MinHeapFreeRatio=15 -XX:+UseParNewGC -XX:-UseGCOverheadLimit\"\n" +
-            "   else\n" +
-            "     export HADOOP_OPTS=\"$HADOOP_OPTS -XX:NewRatio=12 -Xms10m -XX:MaxHeapFreeRatio=40 -XX:MinHeapFreeRatio=15 -XX:-UseGCOverheadLimit\"\n" +
-            "   fi\n" +
-            " fi\n" +
-            "\n" +
-            "\n" +
-            "\n" +
-            "# Set HADOOP_HOME to point to a specific hadoop install directory\n" +
-            "HADOOP_HOME=${HADOOP_HOME:-{{hadoop_home}}}\n";
-    assertEquals(expectedResult, mockInjector.getInstance(UpgradeCatalog212.class).updateHiveEnvContent(testContent));
-  }
-  /**
-   * @param dbAccessor
-   * @return
-   */
-  private AbstractUpgradeCatalog getUpgradeCatalog(final DBAccessor dbAccessor) {
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(EntityManager.class).toInstance(entityManager);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    return injector.getInstance(UpgradeCatalog212.class);
-  }
-
-  @Test
-  public void testGetSourceVersion() {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    UpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
-    Assert.assertEquals("2.1.1", upgradeCatalog.getSourceVersion());
-  }
-
-  @Test
-  public void testGetTargetVersion() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    UpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
-
-    Assert.assertEquals("2.1.2", upgradeCatalog.getTargetVersion());
-  }
-
-  /**
-   * Verify alert changes
-   */
-  class HostRoleCommandDDL implements SectionDDL {
-    HashMap<String, Capture<DBColumnInfo>> captures;
-
-    public HostRoleCommandDDL() {
-      captures = new HashMap<>();
-
-      Capture<DBAccessor.DBColumnInfo> hostRoleCommandAutoSkipColumnCapture = new Capture<>();
-
-      captures.put("host_role_command", hostRoleCommandAutoSkipColumnCapture);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void execute(DBAccessor dbAccessor) throws SQLException {
-      Capture<DBColumnInfo> hostRoleCommandAuotSkipColumnCapture = captures.get(
-          "host_role_command");
-
-      dbAccessor.addColumn(eq("host_role_command"),
-          capture(hostRoleCommandAuotSkipColumnCapture));
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void verify(DBAccessor dbAccessor) throws SQLException {
-      verifyHostRoleCommandSkipCapture(captures.get("host_role_command"));
-    }
-
-    private void verifyHostRoleCommandSkipCapture(
-        Capture<DBAccessor.DBColumnInfo> hostRoleCommandAuotSkipColumnCapture) {
-      DBColumnInfo clusterIdColumn = hostRoleCommandAuotSkipColumnCapture.getValue();
-      Assert.assertEquals(Integer.class, clusterIdColumn.getType());
-      Assert.assertEquals("auto_skip_on_failure", clusterIdColumn.getName());
-    }
-  }
-
-  @Test
-  public void testShouldSkipPreDMLLogicIfClusterNameColumnDoesNotExist() throws Exception {
-    // GIVEN
-    reset(dbAccessor);
-    Capture<String> tableNameCaptor = newCapture();
-    Capture<String> columnNameCaptor = newCapture();
-
-    // the column used by the logic is already deleted
-    // this could happen as a result of previously running the update
-    expect(dbAccessor.tableHasColumn(capture(tableNameCaptor), capture(columnNameCaptor))).andReturn(false);
-    replay(dbAccessor);
-
-    // WHEN
-    testSubject.executePreDMLUpdates();
-
-    // THEN
-    Assert.assertNotNull("The table name hasn't been captured", tableNameCaptor.getValue());
-    Assert.assertEquals("The table name is not as expected", TOPOLOGY_REQUEST_TABLE, tableNameCaptor.getValue());
-
-    Assert.assertNotNull("The column name hasn't been captured", columnNameCaptor.getValue());
-    Assert.assertEquals("The column name is not as expected", TOPOLOGY_REQUEST_CLUSTER_NAME_COLUMN,
-        columnNameCaptor.getValue());
-  }
-
-
-  @Test
-  public void testShouldPerformPreDMLLogicIfClusterNameColumnExists() throws Exception {
-    // GIVEN
-    reset(dbAccessor);
-    expect(dbAccessor.getConnection()).andReturn(connection).anyTimes();
-    expect(connection.createStatement()).andReturn(statement);
-
-    Capture<String> tableNameCaptor = newCapture();
-    Capture<String> columnNameCaptor = newCapture();
-
-    expect(dbAccessor.tableHasColumn(capture(tableNameCaptor), capture(columnNameCaptor))).andReturn(true);
-
-    expect(statement.executeQuery(anyString())).andReturn(resultSet);
-    statement.close();
-
-    expect(resultSet.next()).andReturn(false);
-    resultSet.close();
-
-    replay(dbAccessor, connection, statement, resultSet);
-
-    // WHEN
-    testSubject.executePreDMLUpdates();
-
-    // THEN
-    Assert.assertNotNull("The table name hasn't been captured", tableNameCaptor.getValue());
-    Assert.assertEquals("The table name is not as expected", TOPOLOGY_REQUEST_TABLE, tableNameCaptor.getValue());
-
-    Assert.assertNotNull("The column name hasn't been captured", columnNameCaptor.getValue());
-    Assert.assertEquals("The column name is not as expected", TOPOLOGY_REQUEST_CLUSTER_NAME_COLUMN,
-        columnNameCaptor.getValue());
-
-    verify(dbAccessor, statement, resultSet);
-  }
-
-}


[16/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java
deleted file mode 100644
index cd7ae45..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog250.java
+++ /dev/null
@@ -1,1344 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.ambari.server.upgrade;
-
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-import com.google.gson.JsonPrimitive;
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.actionmanager.CommandExecutionType;
-import org.apache.ambari.server.collections.Predicate;
-import org.apache.ambari.server.collections.functors.ContainsPredicate;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
-import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
-import org.apache.ambari.server.orm.dao.AlertsDAO;
-import org.apache.ambari.server.orm.dao.ArtifactDAO;
-import org.apache.ambari.server.orm.dao.DaoUtils;
-import org.apache.ambari.server.orm.dao.ViewInstanceDAO;
-import org.apache.ambari.server.orm.entities.AlertCurrentEntity;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.AlertHistoryEntity;
-import org.apache.ambari.server.orm.entities.ArtifactEntity;
-import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.kerberos.KerberosComponentDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosConfigurationDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptorFactory;
-import org.apache.ambari.server.state.kerberos.KerberosIdentityDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosKeytabDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosPrincipalDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosServiceDescriptor;
-import org.apache.ambari.server.view.ViewArchiveUtility;
-import org.apache.ambari.server.view.ViewInstanceOperationHandler;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.jdbc.support.JdbcUtils;
-
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-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;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * Upgrade catalog for version 2.5.0.
- */
-public class UpgradeCatalog250 extends AbstractUpgradeCatalog {
-
-  protected static final String HOST_VERSION_TABLE = "host_version";
-  protected static final String GROUPS_TABLE = "groups";
-  protected static final String GROUP_TYPE_COL = "group_type";
-  private static final String AMS_ENV = "ams-env";
-  private static final String AMS_GRAFANA_INI = "ams-grafana-ini";
-  private static final String AMS_SITE = "ams-site";
-  private static final String AMS_LOG4J = "ams-log4j";
-  private static final String AMS_HBASE_LOG4J = "ams-hbase-log4j";
-  private static final String AMS_MODE = "timeline.metrics.service.operation.mode";
-  private static final String AMS_HBASE_SITE = "ams-hbase-site";
-  private static final String HBASE_ROOTDIR = "hbase.rootdir";
-  private static final String HADOOP_ENV = "hadoop-env";
-  private static final String KAFKA_BROKER = "kafka-broker";
-  private static final String YARN_SITE_CONFIG = "yarn-site";
-  private static final String YARN_ENV_CONFIG = "yarn-env";
-  private static final String YARN_LCE_CGROUPS_MOUNT_PATH = "yarn.nodemanager.linux-container-executor.cgroups.mount-path";
-  private static final String YARN_CGROUPS_ENABLED = "yarn_cgroups_enabled";
-  private static final String KAFKA_TIMELINE_METRICS_HOST = "kafka.timeline.metrics.host";
-
-  public static final String COMPONENT_TABLE = "servicecomponentdesiredstate";
-  public static final String COMPONENT_VERSION_TABLE = "servicecomponent_version";
-  public static final String COMPONENT_VERSION_PK = "PK_sc_version";
-  public static final String COMPONENT_VERSION_FK_COMPONENT = "FK_scv_component_id";
-  public static final String COMPONENT_VERSION_FK_REPO_VERSION = "FK_scv_repo_version_id";
-
-  protected static final String SERVICE_DESIRED_STATE_TABLE = "servicedesiredstate";
-  protected static final String CREDENTIAL_STORE_ENABLED_COL = "credential_store_enabled";
-
-  protected static final String HOST_COMPONENT_DESIREDSTATE_TABLE = "hostcomponentdesiredstate";
-  protected static final String HOST_COMPONENT_DESIREDSTATE_ID_COL = "id";
-  protected static final String HOST_COMPONENT_DESIREDSTATE_INDEX = "UQ_hcdesiredstate_name";
-
-  @Inject
-  ViewInstanceDAO viewInstanceDAO;
-
-  @Inject
-  ViewInstanceOperationHandler viewInstanceOperationHandler;
-
-  @Inject
-  protected ViewArchiveUtility archiveUtility;
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog250.class);
-
-  @Inject
-  DaoUtils daoUtils;
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   *
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog250(Injector injector) {
-    super(injector);
-
-    daoUtils = injector.getInstance(DaoUtils.class);
-  }
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.5.0";
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getSourceVersion() {
-    return "2.4.2";
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-    updateHostVersionTable();
-    createComponentVersionTable();
-    updateGroupsTable();
-    dbAccessor.addColumn("stage",
-      new DBAccessor.DBColumnInfo("command_execution_type", String.class, 32, CommandExecutionType.STAGE.toString(),
-        false));
-    updateServiceDesiredStateTable();
-    updateHostComponentDesiredStateTable();
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executePreDMLUpdates() throws AmbariException, SQLException {
-
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    addNewConfigurationsFromXml();
-    updateAMSConfigs();
-    updateStormAlerts();
-    updateLogSearchAlert();
-    removeAlertDuplicates();
-    updateHadoopEnvConfigs();
-    updateKafkaConfigs();
-    updateHIVEInteractiveConfigs();
-    unInstallAllZeppelinViews();
-    updateZeppelinConfigs();
-    updateAtlasConfigs();
-    updateLogSearchConfigs();
-    updateAmbariInfraConfigs();
-    updateYarnSite();
-    updateRangerUrlConfigs();
-    addManageServiceAutoStartPermissions();
-    addManageAlertNotificationsPermissions();
-    updateKerberosDescriptorArtifacts();
-    fixHBaseMasterCPUUtilizationAlertDefinition();
-    updateTezHistoryUrlBase();
-  }
-
-  /**
-   * Fix the HBase Master CPU Utilization alert definition by swapping the values for <code>kerberos_keytab</code>
-   * and <code>kerberos_principal</code>.
-   */
-  protected void fixHBaseMasterCPUUtilizationAlertDefinition() {
-    AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class);
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
-    for (final Cluster cluster : clusterMap.values()) {
-      long clusterID = cluster.getClusterId();
-      AlertDefinitionEntity alertDefinition = alertDefinitionDAO.findByName(clusterID, "hbase_master_cpu");
-      if(alertDefinition != null) {
-        LOG.info("Updating alert definition {} in cluster {}", alertDefinition.getDefinitionName(), clusterID);
-        String source = alertDefinition.getSource();
-
-        if(source != null) {
-          JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-
-          if(sourceJson != null) {
-            boolean changesExist = false;
-            LOG.debug("Source before update : {}", sourceJson);
-
-            JsonObject uriJson = sourceJson.get("uri").getAsJsonObject();
-            JsonPrimitive primitive;
-
-            if (uriJson != null) {
-              // Replace
-              //  "kerberos_keytab": "{{hbase-site/hbase.security.authentication.spnego.kerberos.principal}}"
-              // With
-              //  "kerberos_keytab": "{{hbase-site/hbase.security.authentication.spnego.kerberos.keytab}}"
-              primitive = uriJson.getAsJsonPrimitive("kerberos_keytab");
-              if ((primitive != null) && primitive.isString() && "{{hbase-site/hbase.security.authentication.spnego.kerberos.principal}}".equals(primitive.getAsString())) {
-                uriJson.remove("kerberos_keytab");
-                uriJson.addProperty("kerberos_keytab", "{{hbase-site/hbase.security.authentication.spnego.kerberos.keytab}}");
-                changesExist = true;
-              }
-
-              // Replace
-              //  "kerberos_principal": "{{hbase-site/hbase.security.authentication.spnego.kerberos.keytab}}"
-              // With
-              //  "kerberos_principal": "{{hbase-site/hbase.security.authentication.spnego.kerberos.principal}}"
-              primitive = uriJson.getAsJsonPrimitive("kerberos_principal");
-              if ((primitive != null) && primitive.isString() && "{{hbase-site/hbase.security.authentication.spnego.kerberos.keytab}}".equals(primitive.getAsString())) {
-                uriJson.remove("kerberos_principal");
-                uriJson.addProperty("kerberos_principal", "{{hbase-site/hbase.security.authentication.spnego.kerberos.principal}}");
-                changesExist = true;
-              }
-            }
-
-            LOG.debug("Source after update : {}", sourceJson);
-            if(changesExist) {
-              alertDefinition.setSource(sourceJson.toString());
-              alertDefinition.setHash(UUID.randomUUID().toString());
-
-              alertDefinitionDAO.merge(alertDefinition);
-            }
-          }
-        }
-      }
-    }
-  }
-
-  /**
-   * Removes all {@link AlertCurrentEntity} duplicates from database.
-   * Alerts are considered as duplicates if their definition, host and alert instance are the same.
-   * Duplicates could be created in earlier versions of Ambari up till 2.4.1.
-   */
-  protected void removeAlertDuplicates() {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    AlertsDAO alertsDao = injector.getInstance(AlertsDAO.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
-    for (final Cluster cluster : clusterMap.values()) {
-      long clusterID = cluster.getClusterId();
-      LOG.info("Removing alert duplicates on cluster {}", cluster.getClusterName());
-      List<AlertCurrentEntity> alertCurrentEntities = alertsDao.findCurrentByCluster(clusterID);
-      Set<AlertHistoryEntity> uniqueAlerts = new HashSet<>();
-      for (AlertCurrentEntity alertCurrentEntity : alertCurrentEntities) {
-
-        AlertHistoryEntity currentAlert = new AlertHistoryEntity();
-        currentAlert.setAlertDefinition(alertCurrentEntity.getAlertHistory().getAlertDefinition());
-        currentAlert.setHostName(alertCurrentEntity.getAlertHistory().getHostName());
-        currentAlert.setAlertInstance(alertCurrentEntity.getAlertHistory().getAlertInstance());
-
-        if (uniqueAlerts.contains(currentAlert)) {
-          LOG.info("Alert entity duplicate {} will be removed",alertCurrentEntity.getAlertHistory());
-          alertsDao.remove(alertCurrentEntity);
-        } else {
-          uniqueAlerts.add(currentAlert);
-        }
-      }
-    }
-  }
-
-  protected void updateStormAlerts() {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
-    for (final Cluster cluster : clusterMap.values()) {
-      long clusterID = cluster.getClusterId();
-      LOG.info("Updating storm alert definitions on cluster : " + cluster.getClusterName());
-
-      final AlertDefinitionEntity stormServerProcessDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "storm_server_process");
-
-      final AlertDefinitionEntity stormWebAlert = alertDefinitionDAO.findByName(
-              clusterID, "storm_webui");
-
-      if (stormServerProcessDefinitionEntity != null) {
-        LOG.info("Removing alert definition : " + stormServerProcessDefinitionEntity.toString());
-        alertDefinitionDAO.remove(stormServerProcessDefinitionEntity);
-      }
-
-      if (stormWebAlert != null) {
-        LOG.info("Updating alert definition : " + stormWebAlert.getDefinitionName());
-        String source = stormWebAlert.getSource();
-        JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-        LOG.debug("Source before update : " + sourceJson);
-
-        JsonObject uriJson = sourceJson.get("uri").getAsJsonObject();
-        uriJson.remove("https");
-        uriJson.remove("https_property");
-        uriJson.remove("https_property_value");
-        uriJson.addProperty("https", "{{storm-site/ui.https.port}}");
-        uriJson.addProperty("https_property", "{{storm-site/ui.https.keystore.type}}");
-        uriJson.addProperty("https_property_value", "jks");
-
-        LOG.debug("Source after update : " + sourceJson);
-        stormWebAlert.setSource(sourceJson.toString());
-        alertDefinitionDAO.merge(stormWebAlert);
-      }
-    }
-  }
-
-  protected void updateLogSearchAlert() {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
-    for (final Cluster cluster : clusterMap.values()) {
-      long clusterID = cluster.getClusterId();
-      LOG.info("Updating Log Search web ui alert definitions on cluster : " + cluster.getClusterName());
-
-      final AlertDefinitionEntity logSearchWebAlert = alertDefinitionDAO.findByName(
-        clusterID, "logsearch_ui");
-
-      if (logSearchWebAlert != null) {
-        LOG.info("Updating alert definition : " + logSearchWebAlert.getDefinitionName());
-        String source = logSearchWebAlert.getSource();
-        JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-        LOG.debug("Source before update : " + sourceJson);
-
-        JsonObject uriJson = sourceJson.get("uri").getAsJsonObject();
-        uriJson.remove("https_property");
-        uriJson.remove("https_property_value");
-        uriJson.addProperty("https_property", "{{logsearch-env/logsearch_ui_protocol}}");
-        uriJson.addProperty("https_property_value", "https");
-
-        LOG.debug("Source after update : " + sourceJson);
-        logSearchWebAlert.setSource(sourceJson.toString());
-        alertDefinitionDAO.merge(logSearchWebAlert);
-      }
-    }
-  }
-
-  /**
-   * This will check if previous value of 'tez.tez-ui.history-url.base' contains tez view's url.
-   * If yes then it will point it to fixed url of tez view auto view as introduced in ambari-2.5.0.0.
-   * else it will log an error and move ahead.
-   */
-  protected void updateTezHistoryUrlBase() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          Set<String> installedServices = cluster.getServices().keySet();
-          if (installedServices.contains("TEZ")) {
-            Config tezSite = cluster.getDesiredConfigByType("tez-site");
-            if (tezSite != null) {
-              String currentTezHistoryUrlBase = tezSite.getProperties().get("tez.tez-ui.history-url.base");
-              if (!StringUtils.isEmpty(currentTezHistoryUrlBase)) {
-                LOG.info("Current Tez History URL base: {} ", currentTezHistoryUrlBase);
-                String newTezHistoryUrlBase = null;
-                try {
-                  newTezHistoryUrlBase = getUpdatedTezHistoryUrlBase(currentTezHistoryUrlBase);
-                } catch (AmbariException e) {
-                  LOG.error("Error occurred while creating updated URL of tez view using value in property tez.tez-ui.history-url.base." +
-                    "The current value {} is not of standard format expected by Ambari. Skipping the updation of tez.tez-ui.history-url.base." +
-                    "Please check validity of this property manually in tez site after upgrade.", currentTezHistoryUrlBase, e);
-                  return;
-                }
-                LOG.info("New Tez History URL base: {} ", newTezHistoryUrlBase);
-                updateConfigurationProperties("tez-site", Collections.singletonMap("tez.tez-ui.history-url.base", newTezHistoryUrlBase), true, false);
-              }
-            }
-          }
-        }
-      }
-    }
-  }
-
-  /**
-   * Transforms the existing tez history url base to the fixed short url for tez auto instance
-   * @param currentTezHistoryUrlBase Existing value of the tez history url base
-   * @return the updated tez history url base
-   * @throws AmbariException if currentTezHistoryUrlBase is malformed or is not compatible with the Tez View url REGEX
-   */
-  protected String getUpdatedTezHistoryUrlBase(String currentTezHistoryUrlBase) throws AmbariException{
-    String pattern = "(.*)(\\/views\\/TEZ\\/)(.*)";
-    Pattern regex = Pattern.compile(pattern);
-    Matcher matcher = regex.matcher(currentTezHistoryUrlBase);
-    String prefix;
-    if (matcher.find()) {
-      prefix = matcher.group(1);
-    } else {
-      throw new AmbariException("Cannot prepare the new value for property: 'tez.tez-ui.history-url.base' using the old value: '" + currentTezHistoryUrlBase + "'");
-    }
-
-    // adding the auto tez instance short url name instead of the tez version and tez view instance name
-    return prefix + "/view/TEZ/tez_cluster_instance";
-  }
-
-  protected void updateHostVersionTable() throws SQLException {
-    LOG.info("Updating the {} table", HOST_VERSION_TABLE);
-
-    // Add the unique constraint to the host_version table
-    dbAccessor.addUniqueConstraint(HOST_VERSION_TABLE, "UQ_host_repo", "repo_version_id", "host_id");
-  }
-
-  protected void updateGroupsTable() throws SQLException {
-    LOG.info("Updating the {} table", GROUPS_TABLE);
-
-    dbAccessor.addColumn(GROUPS_TABLE, new DBColumnInfo(GROUP_TYPE_COL, String.class, null, "LOCAL", false));
-    dbAccessor.executeQuery("UPDATE groups SET group_type='LDAP' WHERE ldap_group=1");
-    dbAccessor.addUniqueConstraint(GROUPS_TABLE, "UNQ_groups_0", "group_name", "group_type");
-  }
-
-  /**
-   * Updates {@code yarn-site} in the following ways:
-   *
-   * Remove {@code YARN_LCE_CGROUPS_MOUNT_PATH} if  {@code YARN_CGROUPS_ENABLED} is {@code false} and
-   * {@code YARN_LCE_CGROUPS_MOUNT_PATH} is empty string
-   *
-   * @throws AmbariException
-   */
-  protected void updateYarnSite() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
-    for (final Cluster cluster : clusterMap.values()) {
-      Config yarnEnvConfig = cluster.getDesiredConfigByType(YARN_ENV_CONFIG);
-      Config yarnSiteConfig = cluster.getDesiredConfigByType(YARN_SITE_CONFIG);
-      if (yarnEnvConfig != null && yarnSiteConfig != null) {
-        String cgroupEnabled = yarnEnvConfig.getProperties().get(YARN_CGROUPS_ENABLED);
-        String mountPath = yarnSiteConfig.getProperties().get(YARN_LCE_CGROUPS_MOUNT_PATH);
-        if (StringUtils.isEmpty(mountPath) && cgroupEnabled != null
-          && cgroupEnabled.trim().equalsIgnoreCase("false")){
-          removeConfigurationPropertiesFromCluster(cluster, YARN_SITE_CONFIG, new HashSet<String>(){{
-            add(YARN_LCE_CGROUPS_MOUNT_PATH);
-          }});
-        }
-      }
-    }
-  }
-
-  protected void updateAMSConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-
-          Config amsEnv = cluster.getDesiredConfigByType(AMS_ENV);
-          if (amsEnv != null) {
-            Map<String, String> amsEnvProperties = amsEnv.getProperties();
-            String content = amsEnvProperties.get("content");
-            Map<String, String> newProperties = new HashMap<>();
-            newProperties.put("content", updateAmsEnvContent(content));
-            updateConfigurationPropertiesForCluster(cluster, AMS_ENV, newProperties, true, true);
-          }
-
-
-          boolean isDistributed = false;
-          Config amsSite = cluster.getDesiredConfigByType(AMS_SITE);
-          if (amsSite != null) {
-            if ("distributed".equals(amsSite.getProperties().get(AMS_MODE))) {
-              isDistributed = true;
-            }
-
-            Map<String, String> amsSiteProperties = amsSite.getProperties();
-
-            if (amsSiteProperties != null && amsSiteProperties.containsKey("timeline.metrics.hbase.fifo.compaction.enabled")) {
-              LOG.info("Removing timeline.metrics.hbase.fifo.compaction.enabled from ams-site");
-              removeConfigurationPropertiesFromCluster(cluster, AMS_SITE, Collections.singleton("timeline.metrics.hbase.fifo.compaction.enabled"));
-            }
-          }
-
-          if (isDistributed) {
-            Config amsHbaseSite = cluster.getDesiredConfigByType(AMS_HBASE_SITE);
-            if (amsHbaseSite != null) {
-              Map<String, String> amsHbaseSiteProperties = amsHbaseSite.getProperties();
-              String rootDir = amsHbaseSiteProperties.get(HBASE_ROOTDIR);
-              if (StringUtils.isNotEmpty(rootDir) && rootDir.startsWith("hdfs://")) {
-                int indexOfSlash = rootDir.indexOf("/", 7);
-                Map<String, String> newProperties = new HashMap<>();
-                String newRootdir = rootDir.substring(indexOfSlash);
-                newProperties.put(HBASE_ROOTDIR, newRootdir);
-                LOG.info("Changing ams-hbase-site rootdir to " + newRootdir);
-                updateConfigurationPropertiesForCluster(cluster, AMS_HBASE_SITE, newProperties, true, true);
-              }
-            }
-          }
-
-          //Update AMS log4j to make rolling properties configurable as separate fields.
-          Config amsLog4jProperties = cluster.getDesiredConfigByType(AMS_LOG4J);
-          if(amsLog4jProperties != null){
-            Map<String, String> newProperties = new HashMap<>();
-
-            String content = amsLog4jProperties.getProperties().get("content");
-            content = SchemaUpgradeUtil.extractProperty(content,"ams_log_max_backup_size","ams_log_max_backup_size","log4j.appender.file.MaxFileSize=(\\w+)MB","80",newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content,"ams_log_number_of_backup_files","ams_log_number_of_backup_files","log4j.appender.file.MaxBackupIndex=(\\w+)","60",newProperties);
-            newProperties.put("content",content);
-            updateConfigurationPropertiesForCluster(cluster,AMS_LOG4J,newProperties,true,true);
-          }
-
-          Config amsHbaseLog4jProperties = cluster.getDesiredConfigByType(AMS_HBASE_LOG4J);
-          if(amsHbaseLog4jProperties != null){
-            Map<String, String> newProperties = new HashMap<>();
-
-            String content = amsHbaseLog4jProperties.getProperties().get("content");
-            content = SchemaUpgradeUtil.extractProperty(content,"ams_hbase_log_maxfilesize","ams_hbase_log_maxfilesize","hbase.log.maxfilesize=(\\w+)MB","256",newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content,"ams_hbase_log_maxbackupindex","ams_hbase_log_maxbackupindex","hbase.log.maxbackupindex=(\\w+)","20",newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content,"ams_hbase_security_log_maxfilesize","ams_hbase_security_log_maxfilesize","hbase.security.log.maxfilesize=(\\w+)MB","256",newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content,"ams_hbase_security_log_maxbackupindex","ams_hbase_security_log_maxbackupindex","hbase.security.log.maxbackupindex=(\\w+)","20",newProperties);
-            newProperties.put("content",content);
-            updateConfigurationPropertiesForCluster(cluster,AMS_HBASE_LOG4J,newProperties,true,true);
-          }
-
-          Config amsGrafanaIni = cluster.getDesiredConfigByType(AMS_GRAFANA_INI);
-          if (amsGrafanaIni != null) {
-            Map<String, String> amsGrafanaIniProperties = amsGrafanaIni.getProperties();
-            String content = amsGrafanaIniProperties.get("content");
-            Map<String, String> newProperties = new HashMap<>();
-            newProperties.put("content", updateAmsGrafanaIniContent(content));
-            updateConfigurationPropertiesForCluster(cluster, AMS_GRAFANA_INI, newProperties, true, true);
-          }
-
-        }
-      }
-    }
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void updateKerberosDescriptorArtifact(ArtifactDAO artifactDAO, ArtifactEntity artifactEntity) throws AmbariException {
-    if (artifactEntity != null) {
-      Map<String, Object> data = artifactEntity.getArtifactData();
-
-      if (data != null) {
-        final KerberosDescriptor kerberosDescriptor = new KerberosDescriptorFactory().createInstance(data);
-
-        if (kerberosDescriptor != null) {
-          KerberosServiceDescriptor logSearchKerberosDescriptor = kerberosDescriptor.getService("LOGSEARCH");
-          KerberosServiceDescriptor atlasKerberosDescriptor = kerberosDescriptor.getService("ATLAS");
-          KerberosServiceDescriptor rangerKerberosDescriptor = kerberosDescriptor.getService("RANGER");
-          addInfrSolrDescriptor(artifactDAO, artifactEntity, kerberosDescriptor, atlasKerberosDescriptor, "ATLAS_SERVER");
-          addInfrSolrDescriptor(artifactDAO, artifactEntity, kerberosDescriptor, logSearchKerberosDescriptor, "LOGSEARCH_SERVER");
-          addInfrSolrDescriptor(artifactDAO, artifactEntity, kerberosDescriptor, rangerKerberosDescriptor, "RANGER_ADMIN");
-          KerberosServiceDescriptor stormKerberosDescriptor = kerberosDescriptor.getService("STORM");
-
-          if (stormKerberosDescriptor != null) {
-            KerberosComponentDescriptor componentDescriptor = stormKerberosDescriptor.getComponent("NIMBUS");
-            if (componentDescriptor != null) {
-              KerberosIdentityDescriptor origIdentityDescriptor = componentDescriptor.getIdentity("/STORM/NIMBUS/nimbus_server");
-              if (origIdentityDescriptor != null) {
-                KerberosPrincipalDescriptor origPrincipalDescriptor = origIdentityDescriptor.getPrincipalDescriptor();
-                KerberosPrincipalDescriptor newPrincipalDescriptor = new KerberosPrincipalDescriptor(
-                    null,
-                    null,
-                    (origPrincipalDescriptor == null) ?
-                        "ranger-storm-audit/xasecure.audit.jaas.Client.option.principal" : origPrincipalDescriptor.getConfiguration(),
-                    null
-                );
-                KerberosKeytabDescriptor origKeytabDescriptor = origIdentityDescriptor.getKeytabDescriptor();
-                KerberosKeytabDescriptor newKeytabDescriptor = new KerberosKeytabDescriptor(
-                    null,
-                    null,
-                    null,
-                    null,
-                    null,
-                    (origKeytabDescriptor == null) ?
-                        "ranger-storm-audit/xasecure.audit.jaas.Client.option.keyTab" : origKeytabDescriptor.getConfiguration(),
-                    false);
-                componentDescriptor.removeIdentity("/STORM/NIMBUS/nimbus_server");
-                componentDescriptor.putIdentity(new KerberosIdentityDescriptor("/STORM/storm_components", null, newPrincipalDescriptor, newKeytabDescriptor, null));
-              }
-            }
-          }
-          KerberosServiceDescriptor yarnKerberosDescriptor = kerberosDescriptor.getService("YARN");
-          if (yarnKerberosDescriptor != null) {
-            Map<String, KerberosConfigurationDescriptor> configs = yarnKerberosDescriptor.getConfigurations();
-            KerberosConfigurationDescriptor yarnSiteConfigDescriptor = configs.get("yarn-site");
-            if (yarnSiteConfigDescriptor != null) {
-              Map<String, String> properties = yarnSiteConfigDescriptor.getProperties();
-              if (properties != null && properties.containsKey(YARN_LCE_CGROUPS_MOUNT_PATH)) {
-                properties.remove(YARN_LCE_CGROUPS_MOUNT_PATH);
-              }
-            }
-          }
-
-          // Fix HBASE_MASTER Kerberos identity for Ranger audit by clearing out any keytab file or principal name values.
-          KerberosServiceDescriptor hbaseKerberosDescriptor = kerberosDescriptor.getService("HBASE");
-          if (hbaseKerberosDescriptor != null) {
-            KerberosComponentDescriptor hbaseMasterKerberosDescriptor = hbaseKerberosDescriptor.getComponent("HBASE_MASTER");
-            if (hbaseMasterKerberosDescriptor != null) {
-              KerberosIdentityDescriptor identityDescriptor = hbaseMasterKerberosDescriptor.getIdentity("/HBASE/HBASE_MASTER/hbase_master_hbase");
-
-              if (identityDescriptor != null) {
-                KerberosPrincipalDescriptor principalDescriptor = identityDescriptor.getPrincipalDescriptor();
-                KerberosKeytabDescriptor keytabDescriptor = identityDescriptor.getKeytabDescriptor();
-
-                identityDescriptor.setReference(identityDescriptor.getName());
-                identityDescriptor.setName("ranger_hbase_audit");
-
-                principalDescriptor.setValue(null);
-                keytabDescriptor.setFile(null);
-              }
-            }
-          }
-
-          artifactEntity.setArtifactData(kerberosDescriptor.toMap());
-          artifactDAO.merge(artifactEntity);
-        }
-      }
-    }
-  }
-
-  /**
-   * Add /AMBARI-INFRA/INFRA_SOLR/infra-solr reference to specific service component
-   */
-  private void addInfrSolrDescriptor(ArtifactDAO artifactDAO, ArtifactEntity artifactEntity, KerberosDescriptor kerberosDescriptor,
-                                     KerberosServiceDescriptor serviceDescriptor, String componentName) {
-    if (serviceDescriptor != null) {
-      KerberosComponentDescriptor componentDescriptor = serviceDescriptor.getComponent(componentName);
-      if (componentDescriptor != null) {
-        KerberosIdentityDescriptor origIdentityDescriptor = componentDescriptor.getIdentity("/AMBARI_INFRA/INFRA_SOLR/infra-solr");
-        if (origIdentityDescriptor != null) {
-          LOG.info("/AMBARI_INFRA/INFRA_SOLR/infra-solr identity already exists in {} component", componentName);
-        } else {
-          Predicate predicate = ContainsPredicate.fromMap(Collections.<String, Object>singletonMap(ContainsPredicate.NAME, Arrays.asList("services", "AMBARI_INFRA")));
-          componentDescriptor.putIdentity(new KerberosIdentityDescriptor("/AMBARI_INFRA/INFRA_SOLR/infra-solr",null, null, null, predicate));
-        }
-      }
-    }
-  }
-
-  protected void unInstallAllZeppelinViews(){
-    LOG.info("Removing all Zeppelin views.");
-    List<ViewInstanceEntity> viewInstanceList =  viewInstanceDAO.findAll();
-    for( ViewInstanceEntity viewInstanceEntity : viewInstanceList ){
-      if(viewInstanceEntity.getViewName().equalsIgnoreCase("ZEPPELIN{1.0.0}")){
-        LOG.info("Uninstalling zeppelin view : {}", viewInstanceEntity);
-        try {
-          viewInstanceOperationHandler.uninstallViewInstance(viewInstanceEntity);
-        }catch(Exception e){
-          LOG.error("Exception occurred while uninstalling view {}. Ignored for now.", viewInstanceEntity);
-        }
-      }
-    }
-  }
-
-  /**
-   * Updates Zeppelin configs.
-   *
-   * @throws AmbariException
-   */
-  protected void updateZeppelinConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          Config zeppelinEnvProperties = cluster.getDesiredConfigByType("zeppelin-env");
-          if (zeppelinEnvProperties != null) {
-            String log4jPropertiesContent = zeppelinEnvProperties.getProperties().get("log4j_properties_content");
-            String shiroIniContent = zeppelinEnvProperties.getProperties().get("shiro_ini_content");
-
-            updateConfigurationProperties("zeppelin-log4j-properties", Collections.singletonMap("log4j_properties_content", log4jPropertiesContent), true, true);
-            updateConfigurationProperties("zeppelin-shiro-ini", Collections.singletonMap("shiro_ini_content", shiroIniContent), true, true);
-          }
-        }
-      }
-    }
-  }
-
-  protected String updateAmsGrafanaIniContent(String content) {
-    if (content == null) {
-      return null;
-    }
-
-    String toReplace = "admin_password = {{ams_grafana_admin_pwd}}";
-    String replaceWith = ";admin_password =";
-    content = content.replace(toReplace, replaceWith);
-    return content;
-  }
-
-  protected String updateAmsEnvContent(String content) {
-    if (content == null) {
-      return null;
-    }
-
-    List<String> toReplaceList = new ArrayList<>();
-    toReplaceList.add("\n# HBase normalizer enabled\n");
-    toReplaceList.add("\n# HBase compaction policy enabled\n");
-    toReplaceList.add("export AMS_HBASE_NORMALIZER_ENABLED={{ams_hbase_normalizer_enabled}}\n");
-    toReplaceList.add("export AMS_HBASE_FIFO_COMPACTION_ENABLED={{ams_hbase_fifo_compaction_enabled}}\n");
-
-    //Because of AMBARI-15331 : AMS HBase FIFO compaction policy and Normalizer settings are not handled correctly
-    toReplaceList.add("export HBASE_NORMALIZATION_ENABLED={{ams_hbase_normalizer_enabled}}\n");
-    toReplaceList.add("export HBASE_FIFO_COMPACTION_POLICY_ENABLED={{ams_hbase_fifo_compaction_policy_enabled}}\n");
-
-
-    for (String toReplace : toReplaceList) {
-      if (content.contains(toReplace)) {
-        content = content.replace(toReplace, StringUtils.EMPTY);
-      }
-    }
-
-    return content;
-  }
-
-  protected void updateHadoopEnvConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(
-        AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-      Map<String, String> prop = new HashMap<String, String>();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          String content = null;
-          Boolean contentUpdated = false;
-
-          if (cluster.getDesiredConfigByType(HADOOP_ENV) != null) {
-            content = cluster.getDesiredConfigByType(HADOOP_ENV).getProperties().get("content");
-          }
-
-          if (content != null) {
-            if (!content.contains("ulimit -l")) {  // Append "ulimit -l" to hadoop-env.sh
-              content += "\n" +
-                "{% if is_datanode_max_locked_memory_set %}\n" +
-                "# Fix temporary bug, when ulimit from conf files is not picked up, without full relogin. \n" +
-                "# Makes sense to fix only when runing DN as root \n" +
-                "if [ \"$command\" == \"datanode\" ] &amp;&amp; [ \"$EUID\" -eq 0 ] &amp;&amp; [ -n \"$HADOOP_SECURE_DN_USER\" ]; then\n" +
-                "  ulimit -l {{datanode_max_locked_memory}}\n" +
-                "fi\n" +
-                "{% endif %}";
-
-              contentUpdated = true;
-            }
-
-            if (!content.contains("ulimit -n")){  // Append "ulimit -n" to hadoop-env.sh
-              content += "\n" +
-                "if [ \"$command\" == \"datanode\" ] && [ \"$EUID\" -eq 0 ] && [ -n \"$HADOOP_SECURE_DN_USER\" ]; then \n" +
-                "  ulimit -n {{hdfs_user_nofile_limit}}\n" +
-                "fi";
-              contentUpdated = true;
-            }
-
-            if (contentUpdated){
-              prop.put("content", content);
-              updateConfigurationPropertiesForCluster(cluster, "hadoop-env",
-                prop, true, false);
-            }
-          }
-        }
-      }
-    }
-  }
-
-  /**
-   * Creates the servicecomponent_version table
-   *
-   * @throws SQLException
-   */
-  private void createComponentVersionTable() throws SQLException {
-
-    List<DBColumnInfo> columns = new ArrayList<>();
-
-    // Add extension link table
-    LOG.info("Creating {} table", COMPONENT_VERSION_TABLE);
-
-    columns.add(new DBColumnInfo("id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("component_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("repo_version_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("state", String.class, 32, null, false));
-    columns.add(new DBColumnInfo("user_name", String.class, 255, null, false));
-    dbAccessor.createTable(COMPONENT_VERSION_TABLE, columns, (String[]) null);
-
-    dbAccessor.addPKConstraint(COMPONENT_VERSION_TABLE, COMPONENT_VERSION_PK, "id");
-
-    dbAccessor.addFKConstraint(COMPONENT_VERSION_TABLE, COMPONENT_VERSION_FK_COMPONENT, "component_id",
-      COMPONENT_TABLE, "id", false);
-
-    dbAccessor.addFKConstraint(COMPONENT_VERSION_TABLE, COMPONENT_VERSION_FK_REPO_VERSION, "repo_version_id",
-      "repo_version", "repo_version_id", false);
-
-    addSequence("servicecomponent_version_id_seq", 0L, false);
-  }
-
-  protected void updateKafkaConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-
-          Config kafkaBrokerConfig = cluster.getDesiredConfigByType(KAFKA_BROKER);
-          if (kafkaBrokerConfig != null) {
-            Map<String, String> kafkaBrokerProperties = kafkaBrokerConfig.getProperties();
-
-            if (kafkaBrokerProperties != null && kafkaBrokerProperties.containsKey(KAFKA_TIMELINE_METRICS_HOST)) {
-              LOG.info("Removing kafka.timeline.metrics.host from kafka-broker");
-              removeConfigurationPropertiesFromCluster(cluster, KAFKA_BROKER, Collections.singleton("kafka.timeline.metrics.host"));
-            }
-          }
-        }
-      }
-    }
-  }
-
-  /**
-   * Alter servicedesiredstate table.
-   * @throws SQLException
-   */
-  private void updateServiceDesiredStateTable() throws SQLException {
-    // ALTER TABLE servicedesiredstate ADD COLUMN
-    // credential_store_enabled SMALLINT DEFAULT 0 NOT NULL
-    dbAccessor.addColumn(SERVICE_DESIRED_STATE_TABLE,
-      new DBColumnInfo(CREDENTIAL_STORE_ENABLED_COL, Short.class, null, 0, false));
-  }
-
-
-  /**
-   * Removes the compound PK from hostcomponentdesiredstate table
-   * and replaces it with a surrogate PK, but only if the table doesn't have it's new PK set.
-   * Create index and unqiue constraint on the columns that originally formed the compound PK.
-   *
-   * @throws SQLException
-   */
-  private void updateHostComponentDesiredStateTable() throws SQLException {
-    if (dbAccessor.tableHasPrimaryKey(HOST_COMPONENT_DESIREDSTATE_TABLE, HOST_COMPONENT_DESIREDSTATE_ID_COL)) {
-      LOG.info("Skipping {} table Primary Key modifications since the new {} column already exists",
-        HOST_COMPONENT_DESIREDSTATE_TABLE, HOST_COMPONENT_DESIREDSTATE_ID_COL);
-
-      return;
-    }
-    // add the new ID column as nullable until we populate
-    dbAccessor.addColumn(HOST_COMPONENT_DESIREDSTATE_TABLE,
-      new DBColumnInfo(HOST_COMPONENT_DESIREDSTATE_ID_COL, Long.class, null, null, true));
-
-    // insert sequence values
-    AtomicLong id = new AtomicLong(1);
-    Statement statement = null;
-    ResultSet resultSet = null;
-
-    try {
-      statement = dbAccessor.getConnection().createStatement();
-
-      if (statement != null) {
-        // Select records by old PK
-        String selectSQL = String.format(
-          "SELECT cluster_id, component_name, host_id, service_name FROM %s", HOST_COMPONENT_DESIREDSTATE_TABLE);
-
-        resultSet = statement.executeQuery(selectSQL);
-
-        while (resultSet.next()) {
-          final Long clusterId = resultSet.getLong("cluster_id");
-          final String componentName = resultSet.getString("component_name");
-          final Long hostId = resultSet.getLong("host_id");
-          final String serviceName = resultSet.getString("service_name");
-
-          String updateSQL = String.format(
-            "UPDATE %s SET %s = %s WHERE cluster_id = %d AND component_name = '%s' AND service_name = '%s' AND host_id = %d",
-            HOST_COMPONENT_DESIREDSTATE_TABLE, HOST_COMPONENT_DESIREDSTATE_ID_COL, id.getAndIncrement(),
-            clusterId, componentName, serviceName, hostId);
-
-          dbAccessor.executeQuery(updateSQL);
-        }
-
-        // Add sequence for hostcomponentdesiredstate table ids
-        addSequence("hostcomponentdesiredstate_id_seq", id.get(), false);
-      }
-
-    }
-    finally {
-      JdbcUtils.closeResultSet(resultSet);
-      JdbcUtils.closeStatement(statement);
-    }
-
-    // make the ID column NON NULL now
-    dbAccessor.alterColumn(HOST_COMPONENT_DESIREDSTATE_TABLE,
-      new DBColumnInfo(HOST_COMPONENT_DESIREDSTATE_ID_COL, Long.class, null, null, false));
-
-    // drop existing PK and create new one on ID column
-    String primaryKeyConstraintName = null;
-    Configuration.DatabaseType databaseType = configuration.getDatabaseType();
-
-    switch (databaseType) {
-      case POSTGRES:
-      case MYSQL:
-      case ORACLE:
-      case SQL_SERVER:
-        primaryKeyConstraintName = dbAccessor.getPrimaryKeyConstraintName(HOST_COMPONENT_DESIREDSTATE_TABLE);
-        break;
-
-      default:
-        throw new UnsupportedOperationException(String.format("Invalid database type '%s'", databaseType));
-
-    }
-
-    // warn if we can't find it
-    if (null == primaryKeyConstraintName) {
-      LOG.warn("Unable to determine the primary key constraint name for {}", HOST_COMPONENT_DESIREDSTATE_TABLE);
-    }
-    else {
-      dbAccessor.dropPKConstraint(HOST_COMPONENT_DESIREDSTATE_TABLE, primaryKeyConstraintName, true);
-    }
-
-    // create a new PK, matching the name of the constraint found in the SQL files
-    dbAccessor.addPKConstraint(HOST_COMPONENT_DESIREDSTATE_TABLE, "PK_hostcomponentdesiredstate", "id");
-
-    // create index, ensuring column order matches that of the SQL files
-    dbAccessor.addUniqueConstraint(HOST_COMPONENT_DESIREDSTATE_TABLE, HOST_COMPONENT_DESIREDSTATE_INDEX,
-      "component_name", "service_name", "host_id", "cluster_id");
-  }
-
-  protected void updateAtlasConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          updateAtlasHookConfig(cluster, "HIVE", "hive-env", "hive.atlas.hook");
-          updateAtlasHookConfig(cluster, "STORM", "storm-env", "storm.atlas.hook");
-          updateAtlasHookConfig(cluster, "FALCON", "falcon-env", "falcon.atlas.hook");
-          updateAtlasHookConfig(cluster, "SQOOP", "sqoop-env", "sqoop.atlas.hook");
-        }
-      }
-    }
-  }
-
-  protected void updateAtlasHookConfig(Cluster cluster, String serviceName, String configType, String propertyName) throws AmbariException {
-    Set<String> installedServices = cluster.getServices().keySet();
-    if (installedServices.contains("ATLAS") && installedServices.contains(serviceName)) {
-      Config configEnv = cluster.getDesiredConfigByType(configType);
-      if (configEnv != null) {
-        Map<String, String> newProperties = new HashMap<>();
-        newProperties.put(propertyName, "true");
-        boolean updateProperty = configEnv.getProperties().containsKey(propertyName);
-        updateConfigurationPropertiesForCluster(cluster, configType, newProperties, updateProperty, true);
-      }
-    }
-  }
-
-  /**
-   * Updates Hive Interactive's config in hive-interactive-site.
-   *
-   * @throws AmbariException
-   */
-  private static final String HIVE_INTERACTIVE_ENV = "hive-interactive-env";
-  private static final String HIVE_ENV = "hive-env";
-  protected void updateHIVEInteractiveConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          Config hiveInteractiveEnv = cluster.getDesiredConfigByType(HIVE_INTERACTIVE_ENV);
-          Config hiveEnv = cluster.getDesiredConfigByType(HIVE_ENV);
-          if (hiveInteractiveEnv != null) {
-            String hsiHeapSize = "512";
-            if (hiveEnv != null) {
-              if (hiveEnv.getProperties().containsKey("hive.heapsize")) {
-                hsiHeapSize = hiveEnv.getProperties().get("hive.heapsize");
-                LOG.info("Updating HSI heap size to: " + hsiHeapSize);
-              }
-            }
-            updateConfigurationProperties(HIVE_INTERACTIVE_ENV, Collections.singletonMap("hive_heapsize",
-                                                                                         hsiHeapSize), true, true);
-          }
-        }
-      }
-    }
-  }
-
-  /**
-   * Updates Log Search configs.
-   *
-   * @throws AmbariException
-   */
-  protected void updateLogSearchConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          Config logSearchProperties = cluster.getDesiredConfigByType("logsearch-properties");
-          if (logSearchProperties != null) {
-            Map<String, String> newProperties = new HashMap<>();
-            if (!logSearchProperties.getProperties().containsKey("logsearch.auth.external_auth.enabled"))
-              newProperties.put("logsearch.auth.external_auth.enabled", logSearchProperties.getProperties().get("logsearch.external.auth.enabled"));
-            if (!logSearchProperties.getProperties().containsKey("logsearch.auth.external_auth.host_url"))
-              newProperties.put("logsearch.auth.external_auth.host_url", logSearchProperties.getProperties().get("logsearch.external.auth.host_url"));
-            if (!logSearchProperties.getProperties().containsKey("logsearch.auth.external_auth.login_url"))
-              newProperties.put("logsearch.auth.external_auth.login_url", logSearchProperties.getProperties().get("logsearch.external.auth.login_url"));
-            
-            Set<String> removeProperties = new HashSet<>();
-            removeProperties.add("logsearch.external.auth.enabled");
-            removeProperties.add("logsearch.external.auth.host_url");
-            removeProperties.add("logsearch.external.auth.login_url");
-            
-            updateConfigurationPropertiesForCluster(cluster, "logsearch-properties", newProperties, removeProperties, true, true);
-          }
-          
-          Config logfeederEnvProperties = cluster.getDesiredConfigByType("logfeeder-env");
-          if (logfeederEnvProperties != null) {
-            String content = logfeederEnvProperties.getProperties().get("content");
-            if (content.contains("infra_solr_ssl_enabled")) {
-              content = content.replace("infra_solr_ssl_enabled", "logfeeder_use_ssl");
-              updateConfigurationPropertiesForCluster(cluster, "logfeeder-env", Collections.singletonMap("content", content), true, true);
-            }
-          }
-          
-          Config logsearchEnvProperties = cluster.getDesiredConfigByType("logsearch-env");
-          if (logsearchEnvProperties != null) {
-            Map<String, String> newProperties = new HashMap<>();
-            String content = logsearchEnvProperties.getProperties().get("content");
-            if (content.contains("infra_solr_ssl_enabled or logsearch_ui_protocol == 'https'")) {
-              content = content.replace("infra_solr_ssl_enabled or logsearch_ui_protocol == 'https'", "logsearch_use_ssl");
-            }
-            if (!content.equals(logsearchEnvProperties.getProperties().get("content"))) {
-              newProperties.put("content", content);
-            }
-            
-            if ("http".equals(logsearchEnvProperties.getProperties().get("logsearch_ui_protocol")) &&
-                "/etc/security/serverKeys/logsearch.trustStore.jks".equals(logsearchEnvProperties.getProperties().get("logsearch_truststore_location")) &&
-                "/etc/security/serverKeys/logsearch.keyStore.jks".equals(logsearchEnvProperties.getProperties().get("logsearch_keystore_location"))) {
-              newProperties.put("logsearch_truststore_location", "/etc/ambari-logsearch-portal/conf/keys/logsearch.jks");
-              newProperties.put("logsearch_keystore_location", "/etc/ambari-logsearch-portal/conf/keys/logsearch.jks");
-            }
-            
-            Set<String> removeProperties = new HashSet<>();
-            removeProperties.add("logsearch_solr_audit_logs_use_ranger");
-            removeProperties.add("logsearch_solr_audit_logs_zk_node");
-            removeProperties.add("logsearch_solr_audit_logs_zk_quorum");
-            
-            updateConfigurationPropertiesForCluster(cluster, "logsearch-env", newProperties, removeProperties, true, true);
-          }
-          
-          Config logfeederLog4jProperties = cluster.getDesiredConfigByType("logfeeder-log4j");
-          if (logfeederLog4jProperties != null) {
-            Map<String, String> newProperties = new HashMap<>();
-            
-            String content = logfeederLog4jProperties.getProperties().get("content");
-            content = SchemaUpgradeUtil.extractProperty(content, "logfeeder_log_maxfilesize", "logfeeder_log_maxfilesize",
-                "    <param name=\"file\" value=\"\\{\\{logfeeder_log_dir}}/logfeeder.log\"/>\n" +
-                "    <param name=\"append\" value=\"true\"/>\n" +
-                "    <param name=\"maxFileSize\" value=\"(\\w+)MB\"/>", "10", newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content, "logfeeder_log_maxbackupindex", "logfeeder_log_maxbackupindex",
-                "    <param name=\"file\" value=\"\\{\\{logfeeder_log_dir}}/logfeeder.log\"/>\n" +
-                "    <param name=\"append\" value=\"true\"/>\n" +
-                "    <param name=\"maxFileSize\" value=\"\\{\\{logfeeder_log_maxfilesize}}MB\"/>\n" +
-                "    <param name=\"maxBackupIndex\" value=\"(\\w+)\"/>", "10", newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content, "logfeeder_json_log_maxfilesize", "logfeeder_json_log_maxfilesize",
-                "    <param name=\"file\" value=\"\\{\\{logfeeder_log_dir}}/logsearch-logfeeder.json\" />\n" +
-                "    <param name=\"append\" value=\"true\" />\n" +
-                "    <param name=\"maxFileSize\" value=\"(\\w+)MB\" />", "10", newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content, "logfeeder_json_log_maxbackupindex", "logfeeder_json_log_maxbackupindex",
-                "    <param name=\"file\" value=\"\\{\\{logfeeder_log_dir}}/logsearch-logfeeder.json\" />\n" +
-                "    <param name=\"append\" value=\"true\" />\n" +
-                "    <param name=\"maxFileSize\" value=\"\\{\\{logfeeder_json_log_maxfilesize}}MB\" />\n" +
-                "    <param name=\"maxBackupIndex\" value=\"(\\w+)\" />", "10", newProperties);
-            
-            newProperties.put("content", content);
-            updateConfigurationPropertiesForCluster(cluster, "logfeeder-log4j", newProperties, true, true);
-          }
-          
-          Config logsearchLog4jProperties = cluster.getDesiredConfigByType("logsearch-log4j");
-          if (logsearchLog4jProperties != null) {
-            Map<String, String> newProperties = new HashMap<>();
-            
-            String content = logsearchLog4jProperties.getProperties().get("content");
-            if (content.contains("{{logsearch_log_dir}}/logsearch.err")) {
-              content = content.replace("{{logsearch_log_dir}}/logsearch.err", "{{logsearch_log_dir}}/logsearch.log");
-            }
-            if (content.contains("<priority value=\"warn\"/>")) {
-              content = content.replace("<priority value=\"warn\"/>", "<priority value=\"info\"/>");
-            }
-            
-            content = SchemaUpgradeUtil.extractProperty(content, "logsearch_log_maxfilesize", "logsearch_log_maxfilesize",
-                "    <param name=\"file\" value=\"\\{\\{logsearch_log_dir}}/logsearch.log\" />\n" +
-                "    <param name=\"Threshold\" value=\"info\" />\n" +
-                "    <param name=\"append\" value=\"true\" />\n" +
-                "    <param name=\"maxFileSize\" value=\"(\\w+)MB\" />\n", "10", newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content, "logsearch_log_maxbackupindex", "logsearch_log_maxbackupindex",
-                "    <param name=\"file\" value=\"\\{\\{logsearch_log_dir}}/logsearch.log\" />\n" +
-                "    <param name=\"Threshold\" value=\"info\" />\n" +
-                "    <param name=\"append\" value=\"true\" />\n" +
-                "    <param name=\"maxFileSize\" value=\"\\{\\{logsearch_log_maxfilesize}}MB\" />\n" +
-                "    <param name=\"maxBackupIndex\" value=\"(\\w+)\" />\n", "10", newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content, "logsearch_json_log_maxfilesize", "logsearch_json_log_maxfilesize",
-                "    <param name=\"file\" value=\"\\{\\{logsearch_log_dir}}/logsearch.json\"/>\n" +
-                "    <param name=\"append\" value=\"true\"/>\n" +
-                "    <param name=\"maxFileSize\" value=\"(\\w+)MB\"/>\n", "10", newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content, "logsearch_json_log_maxbackupindex", "logsearch_json_log_maxbackupindex",
-                "    <param name=\"file\" value=\"\\{\\{logsearch_log_dir}}/logsearch.json\"/>\n" +
-                "    <param name=\"append\" value=\"true\"/>\n" +
-                "    <param name=\"maxFileSize\" value=\"\\{\\{logsearch_json_log_maxfilesize}}MB\"/>\n" +
-                "    <param name=\"maxBackupIndex\" value=\"(\\w+)\"/>\n", "10", newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content, "logsearch_audit_log_maxfilesize", "logsearch_audit_log_maxfilesize",
-                "    <param name=\"file\" value=\"\\{\\{logsearch_log_dir}}/logsearch-audit.json\"/>\n" +
-                "    <param name=\"append\" value=\"true\"/>\n" +
-                "    <param name=\"maxFileSize\" value=\"(\\w+)MB\"/>\n", "10", newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content, "logsearch_audit_log_maxbackupindex", "logsearch_audit_log_maxbackupindex",
-                "    <param name=\"file\" value=\"\\{\\{logsearch_log_dir}}/logsearch-audit.json\"/>\n" +
-                "    <param name=\"append\" value=\"true\"/>\n" +
-                "    <param name=\"maxFileSize\" value=\"\\{\\{logsearch_audit_log_maxfilesize}}MB\"/>\n" +
-                "    <param name=\"maxBackupIndex\" value=\"(\\w+)\"/>\n", "10", newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content, "logsearch_perf_log_maxfilesize", "logsearch_perf_log_maxfilesize",
-                "    <param name=\"file\" value=\"\\{\\{logsearch_log_dir}}/logsearch-performance.json\"/>\n" +
-                "    <param name=\"Threshold\" value=\"info\"/>\n" +
-                "    <param name=\"append\" value=\"true\"/>\n" +
-                "    <param name=\"maxFileSize\" value=\"(\\w+)MB\"/>\n", "10", newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content, "logsearch_perf_log_maxbackupindex", "logsearch_perf_log_maxbackupindex",
-                "    <param name=\"file\" value=\"\\{\\{logsearch_log_dir}}/logsearch-performance.json\"/>\n" +
-                "    <param name=\"Threshold\" value=\"info\"/>\n" +
-                "    <param name=\"append\" value=\"true\"/>\n" +
-                "    <param name=\"maxFileSize\" value=\"\\{\\{logsearch_perf_log_maxfilesize}}MB\"/>\n" +
-                "    <param name=\"maxBackupIndex\" value=\"(\\w+)\"/>\n", "10", newProperties);
-            
-            newProperties.put("content", content);
-            if (!content.equals(logsearchLog4jProperties.getProperties().get("content"))) {
-              updateConfigurationPropertiesForCluster(cluster, "logsearch-log4j", newProperties, true, true);
-            }
-          }
-        }
-      }
-    }
-  }
-  
-  /**
-   * Updates Ambari Infra configs.
-   *
-   * @throws AmbariException
-   */
-  protected void updateAmbariInfraConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          Config infraSolrEnvProperties = cluster.getDesiredConfigByType("infra-solr-env");
-          if (infraSolrEnvProperties != null) {
-            String content = infraSolrEnvProperties.getProperties().get("content");
-            if (content.contains("SOLR_SSL_TRUST_STORE={{infra_solr_keystore_location}}")) {
-              content = content.replace("SOLR_SSL_TRUST_STORE={{infra_solr_keystore_location}}", "SOLR_SSL_TRUST_STORE={{infra_solr_truststore_location}}");
-            }
-            if (content.contains("SOLR_SSL_TRUST_STORE_PASSWORD={{infra_solr_keystore_password}}")) {
-              content = content.replace("SOLR_SSL_TRUST_STORE_PASSWORD={{infra_solr_keystore_password}}", "SOLR_SSL_TRUST_STORE_PASSWORD={{infra_solr_truststore_password}}");
-            }
-            if (content.contains("SOLR_KERB_NAME_RULES={{infra_solr_kerberos_name_rules}}")) {
-              content = content.replace("SOLR_KERB_NAME_RULES={{infra_solr_kerberos_name_rules}}", "SOLR_KERB_NAME_RULES=\"{{infra_solr_kerberos_name_rules}}\"");
-            }
-            if (content.contains(" -Dsolr.kerberos.name.rules=${SOLR_KERB_NAME_RULES}")) {
-              content = content.replace(" -Dsolr.kerberos.name.rules=${SOLR_KERB_NAME_RULES}", "");
-            }
-            if (!content.equals(infraSolrEnvProperties.getProperties().get("content"))) {
-              updateConfigurationPropertiesForCluster(cluster, "infra-solr-env", Collections.singletonMap("content", content), true, true);
-            }
-          }
-          
-          Config infraSolrLog4jProperties = cluster.getDesiredConfigByType("infra-solr-log4j");
-          if (infraSolrLog4jProperties != null) {
-            Map<String, String> newProperties = new HashMap<>();
-            
-            String content = infraSolrLog4jProperties.getProperties().get("content");
-            content = SchemaUpgradeUtil.extractProperty(content, "infra_log_maxfilesize", "infra_log_maxfilesize",
-                "log4j.appender.file.MaxFileSize=(\\w+)MB", "10", newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content, "infra_log_maxbackupindex", "infra_log_maxbackupindex",
-                "log4j.appender.file.MaxBackupIndex=(\\w+)\n", "9", newProperties);
-            
-            newProperties.put("content", content);
-            updateConfigurationPropertiesForCluster(cluster, "infra-solr-log4j", newProperties, true, true);
-          }
-          
-          Config infraSolrClientLog4jProperties = cluster.getDesiredConfigByType("infra-solr-client-log4j");
-          if (infraSolrClientLog4jProperties != null) {
-            Map<String, String> newProperties = new HashMap<>();
-            
-            String content = infraSolrClientLog4jProperties.getProperties().get("content");
-            if (content.contains("infra_client_log")) {
-              content = content.replace("infra_client_log", "solr_client_log");
-            }
-            
-            content = SchemaUpgradeUtil.extractProperty(content, "infra_client_log_maxfilesize", "solr_client_log_maxfilesize",
-                "log4j.appender.file.MaxFileSize=(\\w+)MB", "80", newProperties);
-            content = SchemaUpgradeUtil.extractProperty(content, "infra_client_log_maxbackupindex", "solr_client_log_maxbackupindex",
-                "log4j.appender.file.MaxBackupIndex=(\\w+)\n", "60", newProperties);
-            
-            newProperties.put("content", content);
-            updateConfigurationPropertiesForCluster(cluster, "infra-solr-client-log4j", newProperties, true, true);
-          }
-        }
-      }
-    }
-  }
-  
-  /**
-   * Add permissions for managing service auto-start.
-   * <p>
-   * <ul>
-   * <li>SERVICE.MANAGE_AUTO_START permissions for SERVICE.ADMINISTRATOR, CLUSTER.OPERATOR, CLUSTER.ADMINISTRATOR, AMBARI.ADMINISTRATOR</li>
-   * <li>CLUSTER.MANAGE_AUTO_START permissions for CLUSTER.OPERATOR, CLUSTER.ADMINISTRATOR, AMBARI.ADMINISTRATOR</li>
-   * </ul>
-   */
-  protected void addManageServiceAutoStartPermissions() throws SQLException {
-    Collection<String> roles;
-
-    // Add service-level auto-start permission
-    roles = Arrays.asList(
-        "AMBARI.ADMINISTRATOR:AMBARI",
-        "CLUSTER.ADMINISTRATOR:CLUSTER",
-        "CLUSTER.OPERATOR:CLUSTER",
-        "SERVICE.ADMINISTRATOR:CLUSTER");
-    addRoleAuthorization("SERVICE.MANAGE_AUTO_START", "Manage service auto-start", roles);
-
-    // Add cluster-level auto start-permission
-    roles = Arrays.asList(
-        "AMBARI.ADMINISTRATOR:AMBARI",
-        "CLUSTER.ADMINISTRATOR:CLUSTER",
-        "CLUSTER.OPERATOR:CLUSTER");
-    addRoleAuthorization("CLUSTER.MANAGE_AUTO_START", "Manage service auto-start configuration", roles);
-  }
-
-  /**
-   * Add permissions for managing alert notifications configuration.
-   * <p>
-   * <ul>
-   * <li>CLUSTER.MANAGE_ALERT_NOTIFICATIONS permissions for AMBARI.ADMINISTRATOR, CLUSTER.ADMINISTRATOR</li>
-   * </ul>
-   */
-  protected void addManageAlertNotificationsPermissions() throws SQLException {
-    Collection<String> roles;
-    roles = Arrays.asList(
-        "AMBARI.ADMINISTRATOR:AMBARI",
-        "CLUSTER.ADMINISTRATOR:CLUSTER");
-    addRoleAuthorization("CLUSTER.MANAGE_ALERT_NOTIFICATIONS", "Manage alert notifications configuration", roles);
-  }
-
-  /**
-   * Updates Ranger admin url for Ranger plugin supported configs.
-   *
-   * @throws AmbariException
-   */
-  protected void updateRangerUrlConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-
-      Config ranger_admin_properties = cluster.getDesiredConfigByType("admin-properties");
-      if(null != ranger_admin_properties) {
-        String policyUrl = ranger_admin_properties.getProperties().get("policymgr_external_url");
-        if (null != policyUrl) {
-          updateRangerUrl(cluster, "ranger-hdfs-security", "ranger.plugin.hdfs.policy.rest.url", policyUrl);
-          updateRangerUrl(cluster, "ranger-hive-security", "ranger.plugin.hive.policy.rest.url", policyUrl);
-          updateRangerUrl(cluster, "ranger-hbase-security", "ranger.plugin.hbase.policy.rest.url", policyUrl);
-          updateRangerUrl(cluster, "ranger-knox-security", "ranger.plugin.knox.policy.rest.url", policyUrl);
-          updateRangerUrl(cluster, "ranger-storm-security", "ranger.plugin.storm.policy.rest.url", policyUrl);
-          updateRangerUrl(cluster, "ranger-yarn-security", "ranger.plugin.yarn.policy.rest.url", policyUrl);
-          updateRangerUrl(cluster, "ranger-kafka-security", "ranger.plugin.kafka.policy.rest.url", policyUrl);
-          updateRangerUrl(cluster, "ranger-atlas-security", "ranger.plugin.atlas.policy.rest.url", policyUrl);
-          updateRangerUrl(cluster, "ranger-kms-security", "ranger.plugin.kms.policy.rest.url", policyUrl);
-        }
-      }
-    }
-  }
-
-  protected void updateRangerUrl(Cluster cluster, String configType, String configProperty, String policyUrl) throws AmbariException {
-    Config componentSecurity = cluster.getDesiredConfigByType(configType);
-    if(componentSecurity != null && componentSecurity.getProperties().containsKey(configProperty)) {
-      Map<String, String> updateProperty = new HashMap<>();
-      updateProperty.put(configProperty, policyUrl);
-      updateConfigurationPropertiesForCluster(cluster, configType, updateProperty, true, false);
-    }
-  }
-
-
-
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/utils/StageUtils.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/utils/StageUtils.java b/ambari-server/src/main/java/org/apache/ambari/server/utils/StageUtils.java
index 8ab5033..22b975b 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/utils/StageUtils.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/utils/StageUtils.java
@@ -83,9 +83,9 @@ public class StageUtils {
   protected static final String RACKS = "all_racks";
   protected static final String IPV4_ADDRESSES = "all_ipv4_ips";
   private static Map<String, String> componentToClusterInfoKeyMap =
-      new HashMap<String, String>();
+      new HashMap<>();
   private static Map<String, String> decommissionedToClusterInfoKeyMap =
-      new HashMap<String, String>();
+      new HashMap<>();
   private volatile static Gson gson;
 
   @Inject
@@ -96,7 +96,7 @@ public class StageUtils {
 
   @Inject
   private static Configuration configuration;
-  
+
   @Inject
   public StageUtils(StageFactory stageFactory) {
     StageUtils.stageFactory = stageFactory;
@@ -216,32 +216,32 @@ public class StageUtils {
     ExecutionCommand execCmd = s.getExecutionCommandWrapper(hostname, "NAMENODE").getExecutionCommand();
 
     execCmd.setRequestAndStage(s.getRequestId(), s.getStageId());
-    List<String> slaveHostList = new ArrayList<String>();
+    List<String> slaveHostList = new ArrayList<>();
     slaveHostList.add(hostname);
     slaveHostList.add("host2");
-    Map<String, String> hdfsSite = new TreeMap<String, String>();
+    Map<String, String> hdfsSite = new TreeMap<>();
     hdfsSite.put("dfs.block.size", "2560000000");
     Map<String, Map<String, String>> configurations =
-        new TreeMap<String, Map<String, String>>();
+        new TreeMap<>();
     configurations.put("hdfs-site", hdfsSite);
     execCmd.setConfigurations(configurations);
     Map<String, Map<String, Map<String, String>>> configurationAttributes =
-        new TreeMap<String, Map<String, Map<String, String>>>();
-    Map<String, Map<String, String>> hdfsSiteAttributes = new TreeMap<String, Map<String, String>>();
-    Map<String, String> finalAttribute = new TreeMap<String, String>();
+        new TreeMap<>();
+    Map<String, Map<String, String>> hdfsSiteAttributes = new TreeMap<>();
+    Map<String, String> finalAttribute = new TreeMap<>();
     finalAttribute.put("dfs.block.size", "true");
     hdfsSiteAttributes.put("final", finalAttribute);
     configurationAttributes.put("hdfsSite", hdfsSiteAttributes);
     execCmd.setConfigurationAttributes(configurationAttributes);
-    Map<String, String> params = new TreeMap<String, String>();
+    Map<String, String> params = new TreeMap<>();
     params.put("jdklocation", "/x/y/z");
     params.put("stack_version", "1.2.0");
     params.put("stack_name", "HDP");
     execCmd.setHostLevelParams(params);
-    Map<String, String> roleParams = new TreeMap<String, String>();
+    Map<String, String> roleParams = new TreeMap<>();
     roleParams.put("format", "false");
     execCmd.setRoleParams(roleParams);
-    Map<String, String> commandParams = new TreeMap<String, String>();
+    Map<String, String> commandParams = new TreeMap<>();
     commandParams.put(ExecutionCommand.KeyNames.COMMAND_TIMEOUT, "600");
     execCmd.setCommandParams(commandParams);
     return s;
@@ -266,10 +266,10 @@ public class StageUtils {
 
   public static Map<String, Set<String>> getClusterHostInfo(Cluster cluster) throws AmbariException {
     //Fill hosts and ports lists
-    Set<String>   hostsSet  = new LinkedHashSet<String>();
-    List<Integer> portsList = new ArrayList<Integer>();
-    List<String>  rackList  = new ArrayList<String>();
-    List<String>  ipV4List  = new ArrayList<String>();
+    Set<String>   hostsSet  = new LinkedHashSet<>();
+    List<Integer> portsList = new ArrayList<>();
+    List<String>  rackList  = new ArrayList<>();
+    List<String>  ipV4List  = new ArrayList<>();
 
     Collection<Host> allHosts = cluster.getHosts();
     for (Host host : allHosts) {
@@ -297,11 +297,11 @@ public class StageUtils {
       }
     }
 
-    List<String> hostsList = new ArrayList<String>(hostsSet);
-    Map<String, String> additionalComponentToClusterInfoKeyMap = new HashMap<String, String>();
+    List<String> hostsList = new ArrayList<>(hostsSet);
+    Map<String, String> additionalComponentToClusterInfoKeyMap = new HashMap<>();
 
     // Fill hosts for services
-    Map<String, SortedSet<Integer>> hostRolesInfo = new HashMap<String, SortedSet<Integer>>();
+    Map<String, SortedSet<Integer>> hostRolesInfo = new HashMap<>();
     for (Map.Entry<String, Service> serviceEntry : cluster.getServices().entrySet()) {
 
       Service service = serviceEntry.getValue();
@@ -332,7 +332,7 @@ public class StageUtils {
             SortedSet<Integer> hostsForComponentsHost = hostRolesInfo.get(roleName);
 
             if (hostsForComponentsHost == null) {
-              hostsForComponentsHost = new TreeSet<Integer>();
+              hostsForComponentsHost = new TreeSet<>();
               hostRolesInfo.put(roleName, hostsForComponentsHost);
             }
 
@@ -347,7 +347,7 @@ public class StageUtils {
               SortedSet<Integer> hostsForComponentsHost = hostRolesInfo.get(decomRoleName);
 
               if (hostsForComponentsHost == null) {
-                hostsForComponentsHost = new TreeSet<Integer>();
+                hostsForComponentsHost = new TreeSet<>();
                 hostRolesInfo.put(decomRoleName, hostsForComponentsHost);
               }
 
@@ -387,7 +387,7 @@ public class StageUtils {
           SortedSet<Integer> hostsForComponentsHost = hostRolesInfo.get(roleName);
 
           if (hostsForComponentsHost == null) {
-            hostsForComponentsHost = new TreeSet<Integer>();
+            hostsForComponentsHost = new TreeSet<>();
             hostRolesInfo.put(roleName, hostsForComponentsHost);
           }
 
@@ -406,10 +406,10 @@ public class StageUtils {
       }
     }
 
-    Map<String, Set<String>> clusterHostInfo = new HashMap<String, Set<String>>();
+    Map<String, Set<String>> clusterHostInfo = new HashMap<>();
 
     for (Map.Entry<String, SortedSet<Integer>> entry : hostRolesInfo.entrySet()) {
-      TreeSet<Integer> sortedSet = new TreeSet<Integer>(entry.getValue());
+      TreeSet<Integer> sortedSet = new TreeSet<>(entry.getValue());
 
       Set<String> replacedRangesSet = replaceRanges(sortedSet);
 
@@ -429,7 +429,7 @@ public class StageUtils {
      * ambari-server hostname.
      */
     clusterHostInfo.put(AMBARI_SERVER_HOST, Sets.newHashSet(getHostName()));
-    
+
     boolean serverUseSsl = configuration.getApiSSLAuthentication();
     int port = serverUseSsl ? configuration.getClientSSLApiPort() : configuration.getClientApiPort();
     clusterHostInfo.put(AMBARI_SERVER_PORT, Sets.newHashSet(Integer.toString(port)));
@@ -452,7 +452,7 @@ public class StageUtils {
    * @throws AmbariException if an index fails to map to a host name
    */
   public static Map<String, Set<String>> substituteHostIndexes(Map<String, Set<String>> clusterHostInfo) throws AmbariException {
-    Set<String> keysToSkip = new HashSet<String>(Arrays.asList(HOSTS_LIST, PORTS, AMBARI_SERVER_HOST, AMBARI_SERVER_PORT, AMBARI_SERVER_USE_SSL, RACKS, IPV4_ADDRESSES));
+    Set<String> keysToSkip = new HashSet<>(Arrays.asList(HOSTS_LIST, PORTS, AMBARI_SERVER_HOST, AMBARI_SERVER_PORT, AMBARI_SERVER_USE_SSL, RACKS, IPV4_ADDRESSES));
     String[] allHosts = {};
     if (clusterHostInfo.get(HOSTS_LIST) != null) {
       allHosts = clusterHostInfo.get(HOSTS_LIST).toArray(new String[clusterHostInfo.get(HOSTS_LIST).size()]);
@@ -462,7 +462,7 @@ public class StageUtils {
       if (keysToSkip.contains(key)) {
         continue;
       }
-      Set<String> hosts = new HashSet<String>();
+      Set<String> hosts = new HashSet<>();
       Set<String> currentHostsIndexes = clusterHostInfo.get(key);
       if (currentHostsIndexes == null) {
         continue;
@@ -497,7 +497,7 @@ public class StageUtils {
       return null;
     }
 
-    Set<String> rangedSet = new HashSet<String>();
+    Set<String> rangedSet = new HashSet<>();
 
     Integer prevElement = null;
     Integer startOfRange = set.first();
@@ -528,7 +528,7 @@ public class StageUtils {
    */
   public static <T> Set<String> replaceMappedRanges(List<T> values) {
 
-    Map<T, SortedSet<Integer>> convolutedValues = new HashMap<T, SortedSet<Integer>>();
+    Map<T, SortedSet<Integer>> convolutedValues = new HashMap<>();
 
     int valueIndex = 0;
 
@@ -537,14 +537,14 @@ public class StageUtils {
       SortedSet<Integer> correspValues = convolutedValues.get(value);
 
       if (correspValues == null) {
-        correspValues = new TreeSet<Integer>();
+        correspValues = new TreeSet<>();
         convolutedValues.put(value, correspValues);
       }
       correspValues.add(valueIndex);
       valueIndex++;
     }
 
-    Set<String> result = new HashSet<String>();
+    Set<String> result = new HashSet<>();
 
     for (Entry<T, SortedSet<Integer>> entry : convolutedValues.entrySet()) {
       Set<String> replacedRanges = replaceRanges(entry.getValue());
@@ -567,7 +567,7 @@ public class StageUtils {
    * @return a set of integers representing the original range
    */
   private static Set<Integer> rangeToSet(String range) {
-    Set<Integer> indexSet = new HashSet<Integer>();
+    Set<Integer> indexSet = new HashSet<>();
     int startIndex;
     int endIndex;
     if (range.contains("-")) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/python/ambari_server/serverUpgrade.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/ambari_server/serverUpgrade.py b/ambari-server/src/main/python/ambari_server/serverUpgrade.py
index 8263062..07f5097 100644
--- a/ambari-server/src/main/python/ambari_server/serverUpgrade.py
+++ b/ambari-server/src/main/python/ambari_server/serverUpgrade.py
@@ -60,10 +60,6 @@ SCHEMA_UPGRADE_HELPER_CMD = "{0} -cp {1} " + \
                             "org.apache.ambari.server.upgrade.SchemaUpgradeHelper" + \
                             " > " + configDefaults.SERVER_OUT_FILE + " 2>&1"
 
-STACK_UPGRADE_HELPER_CMD = "{0} -cp {1} " + \
-                           "org.apache.ambari.server.upgrade.StackUpgradeHelper" + \
-                           " {2} {3} > " + configDefaults.SERVER_OUT_FILE + " 2>&1"
-
 SCHEMA_UPGRADE_HELPER_CMD_DEBUG = "{0} " \
                          "-server -XX:NewRatio=2 " \
                          "-XX:+UseConcMarkSweepGC " + \
@@ -77,52 +73,6 @@ SCHEMA_UPGRADE_DEBUG = False
 
 SUSPEND_START_MODE = False
 
-#
-# Stack upgrade
-#
-
-def upgrade_stack(args):
-  logger.info("Upgrade stack.")
-  if not is_root():
-    err = 'Ambari-server upgradestack should be run with ' \
-          'root-level privileges'
-    raise FatalException(4, err)
-
-  check_database_name_property()
-
-  try:
-    stack_id = args[1]
-  except IndexError:
-    #stack_id is mandatory
-    raise FatalException("Invalid number of stack upgrade arguments")
-
-  try:
-    repo_url = args[2]
-  except IndexError:
-    repo_url = None
-
-  try:
-    repo_url_os = args[3]
-  except IndexError:
-    repo_url_os = None
-
-  parser = optparse.OptionParser()
-  parser.add_option("-d", type="int", dest="database_index")
-
-  db = get_ambari_properties()[JDBC_DATABASE_PROPERTY]
-
-  idx = LINUX_DBMS_KEYS_LIST.index(db)
-
-  (options, opt_args) = parser.parse_args(["-d {0}".format(idx)])
-
-  stack_name, stack_version = stack_id.split(STACK_NAME_VER_SEP)
-  retcode = run_stack_upgrade(options, stack_name, stack_version, repo_url, repo_url_os)
-
-  if not retcode == 0:
-    raise FatalException(retcode, 'Stack upgrade failed.')
-
-  return retcode
-
 def load_stack_values(version, filename):
   import xml.etree.ElementTree as ET
   values = {}
@@ -144,51 +94,6 @@ def load_stack_values(version, filename):
   return values
 
 
-def run_stack_upgrade(args, stackName, stackVersion, repo_url, repo_url_os):
-  jdk_path = get_java_exe_path()
-  if jdk_path is None:
-    print_error_msg("No JDK found, please run the \"setup\" "
-                    "command to install a JDK automatically or install any "
-                    "JDK manually to " + configDefaults.JDK_INSTALL_DIR)
-    return 1
-  stackId = {}
-  stackId[stackName] = stackVersion
-  if repo_url is not None:
-    stackId['repo_url'] = repo_url
-  if repo_url_os is not None:
-    stackId['repo_url_os'] = repo_url_os
-
-  serverClassPath = ServerClassPath(get_ambari_properties(), args)
-  command = STACK_UPGRADE_HELPER_CMD.format(jdk_path, serverClassPath.get_full_ambari_classpath_escaped_for_shell(),
-                                            "updateStackId",
-                                            "'" + json.dumps(stackId) + "'")
-  (retcode, stdout, stderr) = run_os_command(command)
-  print_info_msg("Return code from stack upgrade command, retcode = {0}".format(str(retcode)))
-  if retcode > 0:
-    print_error_msg("Error executing stack upgrade, please check the server logs.")
-  return retcode
-
-def run_metainfo_upgrade(args, keyValueMap=None):
-  jdk_path = get_java_exe_path()
-  if jdk_path is None:
-    print_error_msg("No JDK found, please run the \"setup\" "
-                    "command to install a JDK automatically or install any "
-                    "JDK manually to " + configDefaults.JDK_INSTALL_DIR)
-
-  retcode = 1
-  if keyValueMap:
-    serverClassPath = ServerClassPath(get_ambari_properties(), args)
-    command = STACK_UPGRADE_HELPER_CMD.format(jdk_path, serverClassPath.get_full_ambari_classpath_escaped_for_shell(),
-                                              'updateMetaInfo',
-                                              "'" + json.dumps(keyValueMap) + "'")
-    (retcode, stdout, stderr) = run_os_command(command)
-    print_info_msg("Return code from stack upgrade command, retcode = {0}".format(str(retcode)))
-    if retcode > 0:
-      print_error_msg("Error executing metainfo upgrade, please check the server logs.")
-
-  return retcode
-
-
 #
 # Repo upgrade
 #
@@ -203,48 +108,6 @@ def change_objects_owner(args):
 
   dbms.change_db_files_owner()
 
-def upgrade_local_repo(args):
-  properties = get_ambari_properties()
-  if properties == -1:
-    print_error_msg("Error getting ambari properties")
-    return -1
-
-  stack_location = get_stack_location(properties)
-  stack_root_local = os.path.join(stack_location, "HDPLocal")
-  if not os.path.exists(stack_root_local):
-    print_info_msg("HDPLocal stack directory does not exist, skipping")
-    return
-
-  stack_root = os.path.join(stack_location, "HDP")
-  if not os.path.exists(stack_root):
-    print_info_msg("HDP stack directory does not exist, skipping")
-    return
-
-  for stack_version_local in os.listdir(stack_root_local):
-    repo_file_local = os.path.join(stack_root_local, stack_version_local, "repos", "repoinfo.xml.rpmsave")
-    if not os.path.exists(repo_file_local):
-      repo_file_local = os.path.join(stack_root_local, stack_version_local, "repos", "repoinfo.xml")
-
-    repo_file = os.path.join(stack_root, stack_version_local, "repos", "repoinfo.xml")
-
-    print_info_msg("Local repo file: {0}".format(repo_file_local))
-    print_info_msg("Repo file: {0}".format(repo_file_local))
-
-    metainfo_update_items = {}
-
-    if os.path.exists(repo_file_local) and os.path.exists(repo_file):
-      local_values = load_stack_values(stack_version_local, repo_file_local)
-      repo_values = load_stack_values(stack_version_local, repo_file)
-      for k, v in local_values.iteritems():
-        if repo_values.has_key(k):
-          local_url = local_values[k]
-          repo_url = repo_values[k]
-          if repo_url != local_url:
-            metainfo_update_items[k] = local_url
-
-    run_metainfo_upgrade(args, metainfo_update_items)
-
-#
 # Schema upgrade
 #
 
@@ -384,9 +247,6 @@ def upgrade(args):
   else:
     adjust_directory_permissions(user)
 
-  # local repo
-  upgrade_local_repo(args)
-
   # create jdbc symlinks if jdbc drivers are available in resources
   check_jdbc_drivers(args)
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/resources/META-INF/persistence.xml
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/META-INF/persistence.xml b/ambari-server/src/main/resources/META-INF/persistence.xml
index 0375650..60c1f11 100644
--- a/ambari-server/src/main/resources/META-INF/persistence.xml
+++ b/ambari-server/src/main/resources/META-INF/persistence.xml
@@ -28,7 +28,6 @@
     <class>org.apache.ambari.server.orm.entities.ClusterEntity</class>
     <class>org.apache.ambari.server.orm.entities.ClusterServiceEntity</class>
     <class>org.apache.ambari.server.orm.entities.ClusterStateEntity</class>
-    <class>org.apache.ambari.server.orm.entities.ClusterVersionEntity</class>
     <class>org.apache.ambari.server.orm.entities.ConfigGroupConfigMappingEntity</class>
     <class>org.apache.ambari.server.orm.entities.ConfigGroupEntity</class>
     <class>org.apache.ambari.server.orm.entities.ConfigGroupHostMappingEntity</class>


[02/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog242Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog242Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog242Test.java
deleted file mode 100644
index e0b24bd..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog242Test.java
+++ /dev/null
@@ -1,448 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import javax.persistence.EntityManager;
-import junit.framework.Assert;
-import static org.easymock.EasyMock.anyString;
-import static org.easymock.EasyMock.aryEq;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.createMockBuilder;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.newCapture;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
-
-import java.lang.reflect.Method;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.H2DatabaseCleaner;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.GuiceJpaInitializer;
-import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
-import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.dao.PermissionDAO;
-import org.apache.ambari.server.orm.dao.PrincipalDAO;
-import org.apache.ambari.server.orm.dao.PrincipalTypeDAO;
-import org.apache.ambari.server.orm.dao.PrivilegeDAO;
-import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
-import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
-import org.apache.ambari.server.orm.dao.RoleAuthorizationDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.entities.PermissionEntity;
-import org.apache.ambari.server.orm.entities.PrincipalEntity;
-import org.apache.ambari.server.orm.entities.PrincipalTypeEntity;
-import org.apache.ambari.server.orm.entities.PrivilegeEntity;
-import org.apache.ambari.server.orm.entities.ResourceEntity;
-import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
-import org.apache.ambari.server.orm.entities.RoleAuthorizationEntity;
-import org.apache.ambari.server.orm.entities.StackEntity;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.easymock.Capture;
-import org.easymock.EasyMock;
-import org.easymock.EasyMockSupport;
-import org.easymock.IMocksControl;
-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.Provider;
-/**
- * {@link org.apache.ambari.server.upgrade.UpgradeCatalog242} unit tests.
- */
-public class UpgradeCatalog242Test {
-  private Injector injector;
-  private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
-  private EntityManager entityManager = createNiceMock(EntityManager.class);
-  private UpgradeCatalogHelper upgradeCatalogHelper;
-  private StackEntity desiredStackEntity;
-  private AmbariManagementController amc = createNiceMock(AmbariManagementController.class);
-  private AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class);
-  private StackDAO stackDAO = createNiceMock(StackDAO.class);
-  private RepositoryVersionDAO repositoryVersionDAO = createNiceMock(RepositoryVersionDAO.class);
-  private ClusterVersionDAO clusterVersionDAO = createNiceMock(ClusterVersionDAO.class);
-  private HostVersionDAO hostVersionDAO = createNiceMock(HostVersionDAO.class);
-  private ClusterDAO clusterDAO = createNiceMock(ClusterDAO.class);
-
-  private IMocksControl mocksControl = EasyMock.createControl();
-
-  @Before
-  public void init() {
-    reset(entityManagerProvider);
-    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
-    replay(entityManagerProvider);
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-
-    upgradeCatalogHelper = injector.getInstance(UpgradeCatalogHelper.class);
-    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
-    injector.getInstance(AmbariMetaInfo.class);
-    // load the stack entity
-    StackDAO stackDAO = injector.getInstance(StackDAO.class);
-    desiredStackEntity = stackDAO.find("HDP", "2.2.0");
-  }
-
-  @After
-  public void tearDown() throws AmbariException, SQLException {
-    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
-  }
-
-  @Test
-  public void testUpdateTablesForMysql() throws Exception{
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    final Configuration configuration = createNiceMock(Configuration.class);
-
-    Capture<DBAccessor.DBColumnInfo> extensionExtensionNameColumnChangeSize = newCapture();
-    Capture<DBAccessor.DBColumnInfo> extensionExtensionVersionColumnChangeSize = newCapture();
-    Capture<DBAccessor.DBColumnInfo> usersUserTypeColumnChangeSize = newCapture();
-    Capture<DBAccessor.DBColumnInfo> usersUserNameColumnChangeSize = newCapture();
-    Capture<DBAccessor.DBColumnInfo> hostRoleCommandRoleColumnChangeSize = newCapture();
-    Capture<DBAccessor.DBColumnInfo> hostRoleCommandStatusColumnChangeSize = newCapture();
-    Capture<DBAccessor.DBColumnInfo> blueprintBlueprintNameColumnChangeSize = newCapture();
-
-
-    expect(configuration.getDatabaseType()).andReturn(Configuration.DatabaseType.MYSQL).once();
-
-    dbAccessor.alterColumn(eq(UpgradeCatalog242.EXTENSION_TABLE), capture(extensionExtensionNameColumnChangeSize));
-    dbAccessor.alterColumn(eq(UpgradeCatalog242.EXTENSION_TABLE), capture(extensionExtensionVersionColumnChangeSize));
-
-    dbAccessor.alterColumn(eq(UpgradeCatalog242.USERS_TABLE), capture(usersUserTypeColumnChangeSize));
-    dbAccessor.alterColumn(eq(UpgradeCatalog242.USERS_TABLE), capture(usersUserNameColumnChangeSize));
-
-    dbAccessor.alterColumn(eq(UpgradeCatalog242.HOST_ROLE_COMMAND_TABLE), capture(hostRoleCommandRoleColumnChangeSize));
-    dbAccessor.alterColumn(eq(UpgradeCatalog242.HOST_ROLE_COMMAND_TABLE), capture(hostRoleCommandStatusColumnChangeSize));
-
-    dbAccessor.dropFKConstraint(eq(UpgradeCatalog242.HOST_GROUP_TABLE), eq("FK_hg_blueprint_name"));
-    dbAccessor.dropFKConstraint(eq(UpgradeCatalog242.HOST_GROUP_TABLE), eq("FK_hostgroup_blueprint_name"));
-    dbAccessor.dropFKConstraint(eq(UpgradeCatalog242.BLUEPRINT_CONFIGURATION), eq("FK_cfg_blueprint_name"));
-    dbAccessor.dropFKConstraint(eq(UpgradeCatalog242.BLUEPRINT_CONFIGURATION), eq("FK_blueprint_configuration_blueprint_name"));
-    dbAccessor.dropFKConstraint(eq(UpgradeCatalog242.BLUEPRINT_SETTING), eq("FK_blueprint_setting_blueprint_name"));
-    dbAccessor.dropFKConstraint(eq(UpgradeCatalog242.BLUEPRINT_SETTING), eq("FK_blueprint_setting_name"));
-
-    dbAccessor.alterColumn(eq(UpgradeCatalog242.BLUEPRINT_TABLE), capture(blueprintBlueprintNameColumnChangeSize));
-
-    dbAccessor.addFKConstraint(eq(UpgradeCatalog242.HOST_GROUP_TABLE), eq("FK_hg_blueprint_name"),
-            aryEq(new String[]{"blueprint_name"}), eq(UpgradeCatalog242.BLUEPRINT_TABLE), aryEq(new String[]{"blueprint_name"}), eq(false));
-
-    dbAccessor.addFKConstraint(eq(UpgradeCatalog242.BLUEPRINT_CONFIGURATION), eq("FK_cfg_blueprint_name"),
-            aryEq(new String[]{"blueprint_name"}), eq(UpgradeCatalog242.BLUEPRINT_TABLE), aryEq(new String[]{"blueprint_name"}), eq(false));
-
-    dbAccessor.addFKConstraint(eq(UpgradeCatalog242.BLUEPRINT_SETTING), eq("FK_blueprint_setting_name"),
-            aryEq(new String[]{"blueprint_name"}), eq(UpgradeCatalog242.BLUEPRINT_TABLE), aryEq(new String[]{"blueprint_name"}), eq(false));
-
-    replay(dbAccessor, configuration);
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        binder.bind(EntityManager.class).toInstance(entityManager);
-        binder.bind(Configuration.class).toInstance(configuration);
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    UpgradeCatalog242 upgradeCatalog242 = injector.getInstance(UpgradeCatalog242.class);
-    upgradeCatalog242.executeDDLUpdates();
-
-    DBAccessor.DBColumnInfo extensionExtensionNameInfo = extensionExtensionNameColumnChangeSize.getValue();
-    Assert.assertNotNull(extensionExtensionNameInfo);
-    Assert.assertEquals(UpgradeCatalog242.EXTENSION_NAME_COLUMN, extensionExtensionNameInfo.getName());
-    Assert.assertEquals(new Integer(100), extensionExtensionNameInfo.getLength());
-    Assert.assertEquals(String.class, extensionExtensionNameInfo.getType());
-    Assert.assertEquals(null, extensionExtensionNameInfo.getDefaultValue());
-    Assert.assertEquals(false, extensionExtensionNameInfo.isNullable());
-
-    DBAccessor.DBColumnInfo extensionExtensionVersionInfo = extensionExtensionVersionColumnChangeSize.getValue();
-    Assert.assertNotNull(extensionExtensionVersionInfo);
-    Assert.assertEquals(UpgradeCatalog242.EXTENSION_VERSION_COLUMN, extensionExtensionVersionInfo.getName());
-    Assert.assertEquals(new Integer(100), extensionExtensionVersionInfo.getLength());
-    Assert.assertEquals(String.class, extensionExtensionVersionInfo.getType());
-    Assert.assertEquals(null, extensionExtensionVersionInfo.getDefaultValue());
-    Assert.assertEquals(false, extensionExtensionVersionInfo.isNullable());
-
-    DBAccessor.DBColumnInfo usersUserTypeInfo = usersUserTypeColumnChangeSize.getValue();
-    Assert.assertNotNull(usersUserTypeInfo);
-    Assert.assertEquals(UpgradeCatalog242.USER_TYPE_COLUMN, usersUserTypeInfo.getName());
-    Assert.assertEquals(new Integer(100), usersUserTypeInfo.getLength());
-    Assert.assertEquals(String.class, usersUserTypeInfo.getType());
-    Assert.assertEquals(null, usersUserTypeInfo.getDefaultValue());
-    Assert.assertEquals(false, usersUserTypeInfo.isNullable());
-
-    DBAccessor.DBColumnInfo usersUserNameInfo = usersUserNameColumnChangeSize.getValue();
-    Assert.assertNotNull(usersUserNameInfo);
-    Assert.assertEquals(UpgradeCatalog242.USER_NAME_COLUMN, usersUserNameInfo.getName());
-    Assert.assertEquals(new Integer(100), usersUserNameInfo.getLength());
-    Assert.assertEquals(String.class, usersUserNameInfo.getType());
-    Assert.assertEquals(null, usersUserNameInfo.getDefaultValue());
-    Assert.assertEquals(false, usersUserNameInfo.isNullable());
-
-    DBAccessor.DBColumnInfo hostRoleCommandRoleInfo = hostRoleCommandRoleColumnChangeSize.getValue();
-    Assert.assertNotNull(hostRoleCommandRoleInfo);
-    Assert.assertEquals(UpgradeCatalog242.ROLE_COLUMN, hostRoleCommandRoleInfo.getName());
-    Assert.assertEquals(new Integer(100), hostRoleCommandRoleInfo.getLength());
-    Assert.assertEquals(String.class, hostRoleCommandRoleInfo.getType());
-    Assert.assertEquals(null, hostRoleCommandRoleInfo.getDefaultValue());
-    Assert.assertEquals(true, hostRoleCommandRoleInfo.isNullable());
-
-    DBAccessor.DBColumnInfo hostRoleCommandStatusInfo = hostRoleCommandStatusColumnChangeSize.getValue();
-    Assert.assertNotNull(hostRoleCommandStatusInfo);
-    Assert.assertEquals(UpgradeCatalog242.STATUS_COLUMN, hostRoleCommandStatusInfo.getName());
-    Assert.assertEquals(new Integer(100), hostRoleCommandStatusInfo.getLength());
-    Assert.assertEquals(String.class, hostRoleCommandStatusInfo.getType());
-    Assert.assertEquals(null, hostRoleCommandStatusInfo.getDefaultValue());
-    Assert.assertEquals(true, hostRoleCommandStatusInfo.isNullable());
-
-    DBAccessor.DBColumnInfo blueprintBlueprintNameInfo = blueprintBlueprintNameColumnChangeSize.getValue();
-    Assert.assertNotNull(blueprintBlueprintNameInfo);
-    Assert.assertEquals(UpgradeCatalog242.BLUEPRINT_NAME_COLUMN, blueprintBlueprintNameInfo.getName());
-    Assert.assertEquals(new Integer(100), blueprintBlueprintNameInfo.getLength());
-    Assert.assertEquals(String.class, blueprintBlueprintNameInfo.getType());
-    Assert.assertEquals(null, blueprintBlueprintNameInfo.getDefaultValue());
-    Assert.assertEquals(false, blueprintBlueprintNameInfo.isNullable());
-
-
-    verify(dbAccessor, configuration);
-  }
-
-  @Test
-  public void testExecuteDMLUpdates() throws Exception {
-    Method addNewConfigurationsFromXml = AbstractUpgradeCatalog.class.getDeclaredMethod("addNewConfigurationsFromXml");
-    Method convertRolePrincipals = UpgradeCatalog242.class.getDeclaredMethod("convertRolePrincipals");
-    Method createRoleAuthorizations = UpgradeCatalog242.class.getDeclaredMethod("createRoleAuthorizations");
-
-    UpgradeCatalog242 upgradeCatalog242 = createMockBuilder(UpgradeCatalog242.class)
-        .addMockedMethod(addNewConfigurationsFromXml)
-        .addMockedMethod(convertRolePrincipals)
-        .addMockedMethod(createRoleAuthorizations)
-        .createMock();
-
-
-    upgradeCatalog242.addNewConfigurationsFromXml();
-    expectLastCall().once();
-
-    upgradeCatalog242.createRoleAuthorizations();
-    expectLastCall().once();
-
-    upgradeCatalog242.convertRolePrincipals();
-    expectLastCall().once();
-
-    replay(upgradeCatalog242);
-
-    upgradeCatalog242.executeDMLUpdates();
-
-    verify(upgradeCatalog242);
-  }
-
-  @Test
-  public void testConvertRolePrincipals() throws AmbariException, SQLException {
-
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    PrincipalEntity clusterAdministratorPrincipalEntity = easyMockSupport.createMock(PrincipalEntity.class);
-
-    PermissionEntity clusterAdministratorPermissionEntity = easyMockSupport.createMock(PermissionEntity.class);
-    expect(clusterAdministratorPermissionEntity.getPrincipal())
-        .andReturn(clusterAdministratorPrincipalEntity)
-        .once();
-
-    PrincipalTypeEntity allClusterAdministratorPrincipalTypeEntity = easyMockSupport.createMock(PrincipalTypeEntity.class);
-
-    PermissionDAO permissionDAO = easyMockSupport.createMock(PermissionDAO.class);
-    expect(permissionDAO.findByName("CLUSTER.ADMINISTRATOR"))
-        .andReturn(clusterAdministratorPermissionEntity)
-        .once();
-    expect(permissionDAO.findByName(anyString()))
-        .andReturn(null)
-        .anyTimes();
-
-    PrincipalTypeDAO principalTypeDAO = easyMockSupport.createMock(PrincipalTypeDAO.class);
-    expect(principalTypeDAO.findByName("ALL.CLUSTER.ADMINISTRATOR"))
-        .andReturn(allClusterAdministratorPrincipalTypeEntity)
-        .once();
-    expect(principalTypeDAO.findByName(anyString()))
-        .andReturn(null)
-        .anyTimes();
-    principalTypeDAO.remove(allClusterAdministratorPrincipalTypeEntity);
-    expectLastCall().once();
-
-    ResourceEntity allClusterAdministratorPrivilege1Resource = easyMockSupport.createMock(ResourceEntity.class);
-    expect(allClusterAdministratorPrivilege1Resource.getId()).andReturn(1L).once();
-
-    PrincipalEntity allClusterAdministratorPrivilege1Principal = easyMockSupport.createMock(PrincipalEntity.class);
-    expect(allClusterAdministratorPrivilege1Principal.getId()).andReturn(1L).once();
-
-    PermissionEntity allClusterAdministratorPrivilege1Permission = easyMockSupport.createMock(PermissionEntity.class);
-    expect(allClusterAdministratorPrivilege1Permission.getId()).andReturn(1).once();
-
-    PrivilegeEntity allClusterAdministratorPrivilege1  = easyMockSupport.createMock(PrivilegeEntity.class);
-    expect(allClusterAdministratorPrivilege1.getId()).andReturn(1).atLeastOnce();
-    expect(allClusterAdministratorPrivilege1.getResource()).andReturn(allClusterAdministratorPrivilege1Resource).once();
-    expect(allClusterAdministratorPrivilege1.getPrincipal()).andReturn(allClusterAdministratorPrivilege1Principal).once();
-    expect(allClusterAdministratorPrivilege1.getPermission()).andReturn(allClusterAdministratorPrivilege1Permission).once();
-    allClusterAdministratorPrivilege1.setPrincipal(clusterAdministratorPrincipalEntity);
-    expectLastCall().once();
-
-    ResourceEntity allClusterAdministratorPrivilege2Resource = easyMockSupport.createMock(ResourceEntity.class);
-    expect(allClusterAdministratorPrivilege2Resource.getId()).andReturn(2L).once();
-
-    PrincipalEntity allClusterAdministratorPrivilege2Principal = easyMockSupport.createMock(PrincipalEntity.class);
-    expect(allClusterAdministratorPrivilege2Principal.getId()).andReturn(2L).once();
-
-    PermissionEntity allClusterAdministratorPrivilege2Permission = easyMockSupport.createMock(PermissionEntity.class);
-    expect(allClusterAdministratorPrivilege2Permission.getId()).andReturn(2).once();
-
-    PrivilegeEntity allClusterAdministratorPrivilege2  = easyMockSupport.createMock(PrivilegeEntity.class);
-    expect(allClusterAdministratorPrivilege2.getId()).andReturn(2).atLeastOnce();
-    expect(allClusterAdministratorPrivilege2.getResource()).andReturn(allClusterAdministratorPrivilege2Resource).once();
-    expect(allClusterAdministratorPrivilege2.getPrincipal()).andReturn(allClusterAdministratorPrivilege2Principal).once();
-    expect(allClusterAdministratorPrivilege2.getPermission()).andReturn(allClusterAdministratorPrivilege2Permission).once();
-    allClusterAdministratorPrivilege2.setPrincipal(clusterAdministratorPrincipalEntity);
-    expectLastCall().once();
-
-    Set<PrivilegeEntity> allClusterAdministratorPrivileges = new HashSet<PrivilegeEntity>();
-    allClusterAdministratorPrivileges.add(allClusterAdministratorPrivilege1);
-    allClusterAdministratorPrivileges.add(allClusterAdministratorPrivilege2);
-
-    PrincipalEntity allClusterAdministratorPrincipalEntity = easyMockSupport.createMock(PrincipalEntity.class);
-    expect(allClusterAdministratorPrincipalEntity.getPrivileges())
-        .andReturn(allClusterAdministratorPrivileges)
-        .once();
-
-    List<PrincipalEntity> allClusterAdministratorPrincipals = new ArrayList<PrincipalEntity>();
-    allClusterAdministratorPrincipals.add(allClusterAdministratorPrincipalEntity);
-
-    PrincipalDAO principalDAO = easyMockSupport.createMock(PrincipalDAO.class);
-    expect(principalDAO.findByPrincipalType("ALL.CLUSTER.ADMINISTRATOR"))
-        .andReturn(allClusterAdministratorPrincipals)
-        .once();
-    principalDAO.remove(allClusterAdministratorPrincipalEntity);
-    expectLastCall().once();
-
-
-    PrivilegeDAO privilegeDAO = easyMockSupport.createMock(PrivilegeDAO.class);
-    expect(privilegeDAO.merge(allClusterAdministratorPrivilege1))
-        .andReturn(allClusterAdministratorPrivilege1)
-        .once();
-    expect(privilegeDAO.merge(allClusterAdministratorPrivilege2))
-        .andReturn(allClusterAdministratorPrivilege2)
-        .once();
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(PrincipalTypeDAO.class)).andReturn(principalTypeDAO).atLeastOnce();
-    expect(injector.getInstance(PrincipalDAO.class)).andReturn(principalDAO).atLeastOnce();
-    expect(injector.getInstance(PermissionDAO.class)).andReturn(permissionDAO).atLeastOnce();
-    expect(injector.getInstance(PrivilegeDAO.class)).andReturn(privilegeDAO).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    UpgradeCatalog242 upgradeCatalog = new UpgradeCatalog242(injector);
-    injector.injectMembers(upgradeCatalog);
-    upgradeCatalog.convertRolePrincipals();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testCreateRoleAuthorizations() throws AmbariException, SQLException {
-
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    ResourceTypeEntity ambariResourceTypeEntity = easyMockSupport.createMock(ResourceTypeEntity.class);
-
-    ResourceTypeEntity clusterResourceTypeEntity = easyMockSupport.createMock(ResourceTypeEntity.class);
-
-    PermissionEntity clusterAdministratorPermissionEntity = new PermissionEntity();
-    clusterAdministratorPermissionEntity.setId(1);
-    PermissionEntity ambariAdministratorPermissionEntity = new PermissionEntity();
-    ambariAdministratorPermissionEntity.setId(2);
-
-    PermissionDAO permissionDAO = easyMockSupport.createMock(PermissionDAO.class);
-    expect(permissionDAO.findPermissionByNameAndType("AMBARI.ADMINISTRATOR", ambariResourceTypeEntity))
-        .andReturn(ambariAdministratorPermissionEntity)
-        .times(2);
-    expect(permissionDAO.findPermissionByNameAndType("CLUSTER.ADMINISTRATOR", clusterResourceTypeEntity))
-        .andReturn(clusterAdministratorPermissionEntity)
-        .times(1);
-    expect(permissionDAO.merge(ambariAdministratorPermissionEntity))
-        .andReturn(ambariAdministratorPermissionEntity)
-        .times(2);
-    expect(permissionDAO.merge(clusterAdministratorPermissionEntity))
-        .andReturn(clusterAdministratorPermissionEntity)
-        .times(1);
-
-    ResourceTypeDAO resourceTypeDAO = easyMockSupport.createMock(ResourceTypeDAO.class);
-    expect(resourceTypeDAO.findByName("AMBARI")).andReturn(ambariResourceTypeEntity).times(2);
-    expect(resourceTypeDAO.findByName("CLUSTER")).andReturn(clusterResourceTypeEntity).times(1);
-
-    RoleAuthorizationDAO roleAuthorizationDAO = easyMockSupport.createMock(RoleAuthorizationDAO.class);
-    expect(roleAuthorizationDAO.findById("CLUSTER.RUN_CUSTOM_COMMAND")).andReturn(null).times(1);
-    expect(roleAuthorizationDAO.findById("AMBARI.RUN_CUSTOM_COMMAND")).andReturn(null).times(1);
-
-    Capture<RoleAuthorizationEntity> captureClusterRunCustomCommandEntity = newCapture();
-    roleAuthorizationDAO.create(capture(captureClusterRunCustomCommandEntity));
-    expectLastCall().times(1);
-
-    Capture<RoleAuthorizationEntity> captureAmbariRunCustomCommandEntity = newCapture();
-    roleAuthorizationDAO.create(capture(captureAmbariRunCustomCommandEntity));
-    expectLastCall().times(1);
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(RoleAuthorizationDAO.class)).andReturn(roleAuthorizationDAO).atLeastOnce();
-    expect(injector.getInstance(PermissionDAO.class)).andReturn(permissionDAO).atLeastOnce();
-    expect(injector.getInstance(ResourceTypeDAO.class)).andReturn(resourceTypeDAO).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    new UpgradeCatalog242(injector).createRoleAuthorizations();
-    easyMockSupport.verifyAll();
-
-    RoleAuthorizationEntity ambariRunCustomCommandEntity = captureAmbariRunCustomCommandEntity.getValue();
-    RoleAuthorizationEntity clusterRunCustomCommandEntity = captureClusterRunCustomCommandEntity.getValue();
-
-    Assert.assertEquals("AMBARI.RUN_CUSTOM_COMMAND", ambariRunCustomCommandEntity.getAuthorizationId());
-    Assert.assertEquals("Perform custom administrative actions", ambariRunCustomCommandEntity.getAuthorizationName());
-
-    Assert.assertEquals("CLUSTER.RUN_CUSTOM_COMMAND", clusterRunCustomCommandEntity.getAuthorizationId());
-    Assert.assertEquals("Perform custom cluster-level actions", clusterRunCustomCommandEntity.getAuthorizationName());
-
-    Assert.assertEquals(2, ambariAdministratorPermissionEntity.getAuthorizations().size());
-    Assert.assertTrue(ambariAdministratorPermissionEntity.getAuthorizations().contains(clusterRunCustomCommandEntity));
-    Assert.assertTrue(ambariAdministratorPermissionEntity.getAuthorizations().contains(ambariRunCustomCommandEntity));
-
-    Assert.assertEquals(1, clusterAdministratorPermissionEntity.getAuthorizations().size());
-    Assert.assertTrue(clusterAdministratorPermissionEntity.getAuthorizations().contains(clusterRunCustomCommandEntity));
-  }
-}


[09/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java
deleted file mode 100644
index 7f03992..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java
+++ /dev/null
@@ -1,922 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertFalse;
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertNull;
-import static junit.framework.Assert.assertTrue;
-import static junit.framework.Assert.fail;
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.createMockBuilder;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-
-import javax.persistence.EntityManager;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.H2DatabaseCleaner;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
-import org.apache.ambari.server.orm.GuiceJpaInitializer;
-import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.ClusterServiceDAO;
-import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
-import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
-import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
-import org.apache.ambari.server.orm.entities.HostEntity;
-import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.StackEntity;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.ConfigHelper;
-import org.apache.ambari.server.state.OperatingSystemInfo;
-import org.apache.ambari.server.state.PropertyInfo;
-import org.apache.ambari.server.state.RepositoryInfo;
-import org.apache.ambari.server.state.SecurityState;
-import org.apache.ambari.server.state.SecurityType;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.StackInfo;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.easymock.Capture;
-import org.easymock.EasyMockSupport;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.Provider;
-
-/**
- * {@link UpgradeCatalog200} unit tests.
- */
-public class UpgradeCatalog200Test {
-  private final String CLUSTER_NAME = "c1";
-  private final String HOST_NAME = "h1";
-
-  private final StackId DESIRED_STACK = new StackId("HDP", "2.0.6");
-
-  private Injector injector;
-  private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
-  private EntityManager entityManager = createNiceMock(EntityManager.class);
-  private UpgradeCatalogHelper upgradeCatalogHelper;
-
-  @Before
-  public void init() {
-    reset(entityManagerProvider);
-    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
-    replay(entityManagerProvider);
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-
-    upgradeCatalogHelper = injector.getInstance(UpgradeCatalogHelper.class);
-  }
-
-  @After
-  public void tearDown() throws AmbariException, SQLException {
-    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
-  }
-
-  @Test
-  public void testExecuteDDLUpdates() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    Configuration configuration = createNiceMock(Configuration.class);
-    Connection connection = createNiceMock(Connection.class);
-    Statement statement = createNiceMock(Statement.class);
-    ResultSet resultSet = createNiceMock(ResultSet.class);
-
-    expect(configuration.getDatabaseUrl()).andReturn(Configuration.JDBC_IN_MEMORY_URL).anyTimes();
-
-    Capture<DBAccessor.DBColumnInfo> alertDefinitionIgnoreColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> alertDefinitionDescriptionColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> alertTargetGlobalColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> hostComponentStateColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> hostComponentVersionColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> clustersSecurityTypeColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> hostComponentStateSecurityStateColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> hostComponentDesiredStateSecurityStateColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> hostRoleCommandRetryColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> stageSkippableColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-
-    Capture<DBAccessor.DBColumnInfo> viewparameterLabelColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> viewparameterPlaceholderColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> viewparameterDefaultValueColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-
-    Capture<DBAccessor.DBColumnInfo> serviceDesiredStateSecurityStateColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<List<DBAccessor.DBColumnInfo>> clusterVersionCapture = new Capture<List<DBAccessor.DBColumnInfo>>();
-    Capture<List<DBAccessor.DBColumnInfo>> hostVersionCapture = new Capture<List<DBAccessor.DBColumnInfo>>();
-    Capture<DBAccessor.DBColumnInfo> valueColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> dataValueColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<List<DBAccessor.DBColumnInfo>> alertTargetStatesCapture = new Capture<List<DBAccessor.DBColumnInfo>>();
-    Capture<List<DBAccessor.DBColumnInfo>> artifactCapture = new Capture<List<DBAccessor.DBColumnInfo>>();
-    Capture<List<DBAccessor.DBColumnInfo>> kerberosPrincipalCapture = new Capture<List<DBAccessor.DBColumnInfo>>();
-    Capture<List<DBAccessor.DBColumnInfo>> kerberosPrincipalHostCapture = new Capture<List<DBAccessor.DBColumnInfo>>();
-
-    Capture<List<DBAccessor.DBColumnInfo>> upgradeCapture = new Capture<List<DBAccessor.DBColumnInfo>>();
-    Capture<List<DBAccessor.DBColumnInfo>> upgradeGroupCapture = new Capture<List<DBAccessor.DBColumnInfo>>();
-    Capture<List<DBAccessor.DBColumnInfo>> upgradeItemCapture = new Capture<List<DBAccessor.DBColumnInfo>>();
-
-    // Alert Definition
-    dbAccessor.addColumn(eq("alert_definition"),
-                          capture(alertDefinitionIgnoreColumnCapture));
-
-    dbAccessor.addColumn(eq("alert_definition"),
-        capture(alertDefinitionDescriptionColumnCapture));
-
-    dbAccessor.createTable(eq("alert_target_states"),
-        capture(alertTargetStatesCapture));
-
-    // alert target
-    dbAccessor.addColumn(eq("alert_target"),
-        capture(alertTargetGlobalColumnCapture));
-
-    // Host Component State
-    dbAccessor.addColumn(eq("hostcomponentstate"),
-        capture(hostComponentStateColumnCapture));
-
-    // Host Component Version
-    dbAccessor.addColumn(eq("hostcomponentstate"),
-        capture(hostComponentVersionColumnCapture));
-
-    // Host Role Command retry allowed
-    dbAccessor.addColumn(eq("host_role_command"),
-        capture(hostRoleCommandRetryColumnCapture));
-
-    // Stage skippable
-    dbAccessor.addColumn(eq("stage"),
-        capture(stageSkippableColumnCapture));
-
-    // Clusters: security type
-    dbAccessor.addColumn(eq("clusters"),
-        capture(clustersSecurityTypeColumnCapture));
-
-    // Host Component State: security State
-    dbAccessor.addColumn(eq("hostcomponentstate"),
-        capture(hostComponentStateSecurityStateColumnCapture));
-
-    // Host Component Desired State: security State
-    dbAccessor.addColumn(eq("hostcomponentdesiredstate"),
-        capture(hostComponentDesiredStateSecurityStateColumnCapture));
-
-    dbAccessor.addColumn(eq("viewparameter"), capture(viewparameterLabelColumnCapture));
-    dbAccessor.addColumn(eq("viewparameter"), capture(viewparameterPlaceholderColumnCapture));
-    dbAccessor.addColumn(eq("viewparameter"), capture(viewparameterDefaultValueColumnCapture));
-
-    // Service Desired State: security State
-    dbAccessor.addColumn(eq("servicedesiredstate"),
-        capture(serviceDesiredStateSecurityStateColumnCapture));
-
-    // Cluster Version
-    dbAccessor.createTable(eq("cluster_version"),
-        capture(clusterVersionCapture), eq("id"));
-
-    // Host Version
-    dbAccessor.createTable(eq("host_version"),
-        capture(hostVersionCapture), eq("id"));
-
-    // Upgrade
-    dbAccessor.createTable(eq("upgrade"), capture(upgradeCapture), eq("upgrade_id"));
-
-    // Upgrade Group item
-    dbAccessor.createTable(eq("upgrade_group"), capture(upgradeGroupCapture), eq("upgrade_group_id"));
-
-    // Upgrade item
-    dbAccessor.createTable(eq("upgrade_item"), capture(upgradeItemCapture), eq("upgrade_item_id"));
-
-    // artifact
-    dbAccessor.createTable(eq("artifact"), capture(artifactCapture),
-        eq("artifact_name"), eq("foreign_keys"));
-
-    // kerberos_principal
-    dbAccessor.createTable(eq("kerberos_principal"), capture(kerberosPrincipalCapture),
-        eq("principal_name"));
-
-    // kerberos_principal_host
-    dbAccessor.createTable(eq("kerberos_principal_host"), capture(kerberosPrincipalHostCapture),
-        eq("principal_name"), eq("host_name"));
-
-    expect(dbAccessor.tableHasColumn("kerberos_principal_host", "host_name")).andReturn(true).atLeastOnce();
-
-    dbAccessor.addFKConstraint(eq("kerberos_principal_host"), eq("FK_krb_pr_host_hostname"),
-        eq("host_name"), eq("hosts"), eq("host_name"), eq(true), eq(false));
-
-    dbAccessor.addFKConstraint(eq("kerberos_principal_host"), eq("FK_krb_pr_host_principalname"),
-        eq("principal_name"), eq("kerberos_principal"), eq("principal_name"), eq(true), eq(false));
-
-    setViewInstancePropertyExpectations(dbAccessor, valueColumnCapture);
-    setViewInstanceDataExpectations(dbAccessor, dataValueColumnCapture);
-
-    // AbstractUpgradeCatalog.addSequence()
-    dbAccessor.getConnection();
-    expectLastCall().andReturn(connection).anyTimes();
-    connection.createStatement();
-    expectLastCall().andReturn(statement).anyTimes();
-    statement.executeQuery(anyObject(String.class));
-    expectLastCall().andReturn(resultSet).anyTimes();
-
-    replay(dbAccessor, configuration, resultSet, statement, connection);
-
-    AbstractUpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
-    Class<?> c = AbstractUpgradeCatalog.class;
-    Field f = c.getDeclaredField("configuration");
-    f.setAccessible(true);
-    f.set(upgradeCatalog, configuration);
-
-    upgradeCatalog.executeDDLUpdates();
-    verify(dbAccessor, configuration, resultSet, statement, connection);
-
-    // verify columns for alert_definition
-    verifyAlertDefinitionIgnoreColumn(alertDefinitionIgnoreColumnCapture);
-    verifyAlertDefinitionDescriptionColumn(alertDefinitionDescriptionColumnCapture);
-
-    // verify alert target column for is_global
-    verifyAlertTargetGlobal(alertTargetGlobalColumnCapture);
-
-    // verify new table for alert target states
-    verifyAlertTargetStatesTable(alertTargetStatesCapture);
-
-    // Verify added column in hostcomponentstate table
-    DBAccessor.DBColumnInfo upgradeStateColumn = hostComponentStateColumnCapture.getValue();
-    assertEquals("upgrade_state", upgradeStateColumn.getName());
-    assertEquals(32, (int) upgradeStateColumn.getLength());
-    assertEquals(String.class, upgradeStateColumn.getType());
-    assertEquals("NONE", upgradeStateColumn.getDefaultValue());
-    assertFalse(upgradeStateColumn.isNullable());
-
-    // Verify added column in hostcomponentstate table
-    DBAccessor.DBColumnInfo upgradeVersionColumn = hostComponentVersionColumnCapture.getValue();
-    assertEquals("version", upgradeVersionColumn.getName());
-    assertEquals(32, (int) upgradeVersionColumn.getLength());
-    assertEquals(String.class, upgradeVersionColumn.getType());
-    assertEquals("UNKNOWN", upgradeVersionColumn.getDefaultValue());
-    assertFalse(upgradeVersionColumn.isNullable());
-
-    // Verify added column in host_role_command table
-    DBAccessor.DBColumnInfo upgradeRetryColumn = hostRoleCommandRetryColumnCapture.getValue();
-    assertEquals("retry_allowed", upgradeRetryColumn.getName());
-    assertEquals(1, (int) upgradeRetryColumn.getLength());
-    assertEquals(Integer.class, upgradeRetryColumn.getType());
-    assertEquals(0, upgradeRetryColumn.getDefaultValue());
-    assertFalse(upgradeRetryColumn.isNullable());
-
-    // Verify added column in host_role_command table
-    DBAccessor.DBColumnInfo upgradeSkippableColumn = stageSkippableColumnCapture.getValue();
-    assertEquals("skippable", upgradeSkippableColumn.getName());
-    assertEquals(1, (int) upgradeSkippableColumn.getLength());
-    assertEquals(Integer.class, upgradeSkippableColumn.getType());
-    assertEquals(0, upgradeSkippableColumn.getDefaultValue());
-    assertFalse(upgradeSkippableColumn.isNullable());
-
-    // verify security_type column
-    verifyClustersSecurityType(clustersSecurityTypeColumnCapture);
-
-    // verify security_state columns
-    verifyComponentSecurityStateColumn(hostComponentStateSecurityStateColumnCapture);
-    verifyComponentSecurityStateColumn(hostComponentDesiredStateSecurityStateColumnCapture);
-    verifyServiceSecurityStateColumn(serviceDesiredStateSecurityStateColumnCapture);
-
-    verifyViewParameterColumns(viewparameterLabelColumnCapture, viewparameterPlaceholderColumnCapture,
-        viewparameterDefaultValueColumnCapture);
-
-    // verify artifact columns
-    List<DBAccessor.DBColumnInfo> artifactColumns = artifactCapture.getValue();
-    testCreateArtifactTable(artifactColumns);
-
-    // verify kerberos_principal columns
-    testCreateKerberosPrincipalTable(kerberosPrincipalCapture.getValue());
-
-    // verify kerberos_principal_host columns
-    testCreateKerberosPrincipalHostTable(kerberosPrincipalHostCapture.getValue());
-
-    // Verify capture group sizes
-    assertEquals(7, clusterVersionCapture.getValue().size());
-    assertEquals(4, hostVersionCapture.getValue().size());
-
-    assertViewInstancePropertyColumns(valueColumnCapture);
-    assertViewInstanceDataColumns(dataValueColumnCapture);
-
-    assertEquals(6, upgradeCapture.getValue().size());
-    assertEquals(4, upgradeGroupCapture.getValue().size());
-    assertEquals(7, upgradeItemCapture.getValue().size());
-  }
-
-  /**
-   * Tests that each DML method is invoked.
-   *
-   * @throws Exception
-   */
-  @Test
-  public void testExecuteDMLUpdates() throws Exception {
-    Method removeNagiosService = UpgradeCatalog200.class.getDeclaredMethod("removeNagiosService");
-    Method updateHiveDatabaseType = UpgradeCatalog200.class.getDeclaredMethod("updateHiveDatabaseType");
-    Method addNewConfigurationsFromXml = AbstractUpgradeCatalog.class.getDeclaredMethod
-        ("addNewConfigurationsFromXml");
-    Method updateTezConfiguration = UpgradeCatalog200.class.getDeclaredMethod("updateTezConfiguration");
-    Method updateFlumeEnvConfig = UpgradeCatalog200.class.getDeclaredMethod("updateFlumeEnvConfig");
-    Method updateClusterEnvConfiguration = UpgradeCatalog200.class.getDeclaredMethod("updateClusterEnvConfiguration");
-    Method updateConfigurationProperties = AbstractUpgradeCatalog.class.getDeclaredMethod
-            ("updateConfigurationProperties", String.class, Map.class, boolean.class, boolean.class);
-    Method persistHDPRepo = UpgradeCatalog200.class.getDeclaredMethod("persistHDPRepo");
-
-    UpgradeCatalog200 upgradeCatalog = createMockBuilder(UpgradeCatalog200.class)
-        .addMockedMethod(removeNagiosService)
-        .addMockedMethod(updateHiveDatabaseType)
-        .addMockedMethod(addNewConfigurationsFromXml)
-        .addMockedMethod(updateTezConfiguration)
-        .addMockedMethod(updateFlumeEnvConfig)
-        .addMockedMethod(updateConfigurationProperties)
-        .addMockedMethod(updateClusterEnvConfiguration)
-        .addMockedMethod(persistHDPRepo)
-        .createMock();
-
-    upgradeCatalog.removeNagiosService();
-    expectLastCall().once();
-    upgradeCatalog.addNewConfigurationsFromXml();
-    expectLastCall();
-
-    upgradeCatalog.updateHiveDatabaseType();
-    expectLastCall().once();
-
-    upgradeCatalog.updateTezConfiguration();
-    expectLastCall().once();
-
-    upgradeCatalog.updateFlumeEnvConfig();
-    expectLastCall().once();
-
-    upgradeCatalog.updateConfigurationProperties("hive-site",
-            Collections.singletonMap("hive.server2.transport.mode", "binary"), false, false);
-    expectLastCall();
-
-    upgradeCatalog.persistHDPRepo();
-    expectLastCall().once();
-
-    upgradeCatalog.updateClusterEnvConfiguration();
-    expectLastCall();
-
-    replay(upgradeCatalog);
-
-    upgradeCatalog.executeDMLUpdates();
-
-    verify(upgradeCatalog);
-  }
-
-  @Test
-  public void testUpdateFlumeEnvConfig() throws AmbariException {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Map<String, String> propertiesFlumeEnv = new HashMap<String, String>() {
-      {
-        put("content", "test");
-      }
-    };
-
-    final Config mockFlumeEnv = easyMockSupport.createNiceMock(Config.class);
-    expect(mockFlumeEnv.getProperties()).andReturn(propertiesFlumeEnv).once();
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-
-    expect(mockClusterExpected.getDesiredConfigByType("flume-env")).andReturn(mockFlumeEnv).atLeastOnce();
-    expect(mockFlumeEnv.getProperties()).andReturn(propertiesFlumeEnv).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog200.class).updateFlumeEnvConfig();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testPersistHDPRepo() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createStrictMock(AmbariManagementController.class);
-    final AmbariMetaInfo mockAmbariMetaInfo = easyMockSupport.createNiceMock(AmbariMetaInfo.class);
-    final StackInfo mockStackInfo = easyMockSupport.createNiceMock(StackInfo.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockCluster = easyMockSupport.createStrictMock(Cluster.class);
-    final Map<String, Cluster> clusterMap = new HashMap<String, Cluster>();
-    clusterMap.put("c1",mockCluster);
-    OperatingSystemInfo osi = new OperatingSystemInfo("redhat6");
-    HashSet<OperatingSystemInfo> osiSet = new HashSet<OperatingSystemInfo>();
-    osiSet.add(osi);
-    StackId stackId = new StackId("HDP","2.2");
-    final RepositoryInfo mockRepositoryInfo = easyMockSupport.createNiceMock(RepositoryInfo.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(EntityManager.class).toInstance(createNiceMock(EntityManager.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getAmbariMetaInfo()).andReturn(mockAmbariMetaInfo);
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(clusterMap).once();
-    expect(mockCluster.getCurrentStackVersion()).andReturn(stackId).once();
-    expect(mockCluster.getClusterName()).andReturn("cc").anyTimes();
-    expect(mockAmbariMetaInfo.getOperatingSystems("HDP", "2.2")).andReturn(osiSet).once();
-    expect(mockAmbariMetaInfo.getRepository("HDP", "2.2", "redhat6", "HDP-2.2")).andReturn(mockRepositoryInfo).once();
-    expect(mockAmbariMetaInfo.getStack("HDP", "2.2")).andReturn(mockStackInfo);
-    expect(mockStackInfo.getRepositories()).andReturn(new ArrayList<RepositoryInfo>() {{
-      add(mockRepositoryInfo);
-    }});
-    expect(mockRepositoryInfo.getDefaultBaseUrl()).andReturn("http://baseurl").once();
-    mockAmbariMetaInfo.updateRepoBaseURL("HDP", "2.2", "redhat6", "HDP-2.2", "http://baseurl");
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog200.class).persistHDPRepo();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testRepositoryTable() {
-    final RepositoryInfo repositoryInfo1 = new RepositoryInfo();
-    repositoryInfo1.setOsType("redhat6");
-    repositoryInfo1.setRepoId("HDP-2.2");
-    repositoryInfo1.setBaseUrl("http://public-repo-1.hortonworks.com/HDP/centos6/2.x/updates/2.2.6.0");
-
-    final RepositoryInfo repositoryInfo2 = new RepositoryInfo();
-    repositoryInfo2.setOsType("suse11");
-    repositoryInfo2.setRepoId("HDP-UTILS-1.1.0.20");
-    repositoryInfo2.setBaseUrl("http://public-repo-1.hortonworks.com/HDP-UTILS-1.1.0.20/repos/suse11sp3");
-
-    List<RepositoryInfo> repos = new ArrayList<RepositoryInfo>() {{
-      add(repositoryInfo1);
-      add(repositoryInfo2);
-    }};
-    String output = UpgradeCatalog200.repositoryTable(repos);
-    assertEquals("  redhat6 |            HDP-2.2 | http://public-repo-1.hortonworks.com/HDP/centos6/2.x/updates/2.2.6.0 \n" +
-                 "   suse11 | HDP-UTILS-1.1.0.20 | http://public-repo-1.hortonworks.com/HDP-UTILS-1.1.0.20/repos/suse11sp3 \n",
-      output);
-  }
-
-  @Test
-  public void testUpdateClusterEnvConfiguration() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createStrictMock(AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createStrictMock(Cluster.class);
-    final Cluster mockClusterMissingSmokeUser = easyMockSupport.createStrictMock(Cluster.class);
-    final Cluster mockClusterMissingConfig = easyMockSupport.createStrictMock(Cluster.class);
-
-    final Config mockClusterEnvExpected = easyMockSupport.createStrictMock(Config.class);
-    final Config mockClusterEnvMissingSmokeUser = easyMockSupport.createStrictMock(Config.class);
-
-    final Map<String, String> propertiesExpectedT0 = new HashMap<String, String>();
-    propertiesExpectedT0.put("kerberos_domain", "EXAMPLE.COM");
-    propertiesExpectedT0.put("user_group", "hadoop");
-    propertiesExpectedT0.put("kinit_path_local", "/usr/bin");
-    propertiesExpectedT0.put("security_enabled", "true");
-    propertiesExpectedT0.put("smokeuser", "ambari-qa");
-    propertiesExpectedT0.put("smokeuser_keytab", "/etc/security/keytabs/smokeuser.headless.keytab");
-    propertiesExpectedT0.put("ignore_groupsusers_create", "false");
-
-    final Map<String, String> propertiesExpectedT1 = new HashMap<String, String>(propertiesExpectedT0);
-    propertiesExpectedT1.put("smokeuser_principal_name", "ambari-qa");
-
-    final Map<String, String> propertiesMissingSmokeUserT0 = new HashMap<String, String>(propertiesExpectedT0);
-    propertiesMissingSmokeUserT0.remove("smokeuser");
-
-    final Map<String, String> propertiesMissingSmokeUserT1 = new HashMap<String, String>(propertiesMissingSmokeUserT0);
-    propertiesMissingSmokeUserT1.put("smokeuser_principal_name", "ambari-qa");
-
-    final PropertyInfo mockSmokeUserPropertyInfo = easyMockSupport.createStrictMock(PropertyInfo.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-      put("missing_smokeuser", mockClusterMissingSmokeUser);
-      put("missing_cluster-env", mockClusterMissingConfig);
-
-    }}).once();
-
-      // Expected operation
-    expect(mockClusterExpected.getDesiredConfigByType("cluster-env")).andReturn(mockClusterEnvExpected).once();
-    expect(mockClusterEnvExpected.getProperties()).andReturn(propertiesExpectedT0).once();
-
-    mockConfigHelper.createConfigType(mockClusterExpected, mockAmbariManagementController,
-        "cluster-env", propertiesExpectedT1, UpgradeCatalog200.AUTHENTICATED_USER_NAME, "Upgrading to Ambari 2.0");
-    expectLastCall().once();
-
-    // Missing smokeuser
-    expect(mockClusterMissingSmokeUser.getDesiredConfigByType("cluster-env")).andReturn(mockClusterEnvMissingSmokeUser).once();
-    expect(mockClusterEnvMissingSmokeUser.getProperties()).andReturn(propertiesMissingSmokeUserT0).once();
-
-    expect(mockConfigHelper.getStackProperties(mockClusterMissingSmokeUser)).andReturn(Collections.singleton(mockSmokeUserPropertyInfo)).once();
-
-    expect(mockSmokeUserPropertyInfo.getFilename()).andReturn("cluster-env.xml").once();
-    expect(mockSmokeUserPropertyInfo.getValue()).andReturn("ambari-qa").once();
-
-    mockConfigHelper.createConfigType(mockClusterMissingSmokeUser, mockAmbariManagementController,
-        "cluster-env", propertiesMissingSmokeUserT1, UpgradeCatalog200.AUTHENTICATED_USER_NAME, "Upgrading to Ambari 2.0");
-    expectLastCall().once();
-
-    // Missing cluster-env config
-    expect(mockClusterMissingConfig.getDesiredConfigByType("cluster-env")).andReturn(null).once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog200.class).updateClusterEnvConfiguration();
-    easyMockSupport.verifyAll();
-  }
-
-  /**
-   * Tests that Nagios is correctly removed.
-   *
-   * @throws Exception
-   */
-  @Test
-  public void testDeleteNagiosService() throws Exception {
-    UpgradeCatalog200 upgradeCatalog200 = injector.getInstance(UpgradeCatalog200.class);
-    ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO = injector.getInstance(ServiceComponentDesiredStateDAO.class);
-    HostComponentDesiredStateDAO hostComponentDesiredStateDAO = injector.getInstance(HostComponentDesiredStateDAO.class);
-    HostComponentStateDAO hostComponentStateDAO = injector.getInstance(HostComponentStateDAO.class);
-    ClusterServiceDAO clusterServiceDao = injector.getInstance(ClusterServiceDAO.class);
-    StackDAO stackDAO = injector.getInstance(StackDAO.class);
-
-    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
-    injector.getInstance(AmbariMetaInfo.class);
-
-    StackEntity stackEntity = stackDAO.find(DESIRED_STACK.getStackName(),
-        DESIRED_STACK.getStackVersion());
-
-    assertNotNull(stackEntity);
-
-    final ClusterEntity clusterEntity = upgradeCatalogHelper.createCluster(
-        injector, CLUSTER_NAME, stackEntity);
-
-    final ClusterServiceEntity clusterServiceEntityNagios = upgradeCatalogHelper.addService(
-        injector, clusterEntity, "NAGIOS", stackEntity);
-
-    final HostEntity hostEntity = upgradeCatalogHelper.createHost(injector,
-        clusterEntity, HOST_NAME);
-
-    upgradeCatalogHelper.addComponent(injector, clusterEntity,
-        clusterServiceEntityNagios, hostEntity, "NAGIOS_SERVER", stackEntity);
-
-    ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName(
-        clusterEntity.getClusterId(), "NAGIOS", "NAGIOS_SERVER");
-
-    assertNotNull(serviceComponentDesiredStateEntity);
-
-    HostComponentDesiredStateEntity hcDesiredStateEntity = hostComponentDesiredStateDAO.findByIndex(
-      clusterEntity.getClusterId(),
-      "NAGIOS",
-      "NAGIOS_SERVER",
-      hostEntity.getHostId()
-    );
-    assertNotNull(hcDesiredStateEntity);
-
-    HostComponentStateEntity hcStateEntity = hostComponentStateDAO.findByIndex(
-        clusterEntity.getClusterId(), "NAGIOS", "NAGIOS_SERVER", hostEntity.getHostId());
-
-    assertNotNull(hcStateEntity);
-
-    ClusterServiceEntity clusterService = clusterServiceDao.findByClusterAndServiceNames(
-        CLUSTER_NAME, "NAGIOS");
-
-    upgradeCatalog200.removeNagiosService();
-
-    clusterService = clusterServiceDao.findByClusterAndServiceNames(
-        CLUSTER_NAME, "NAGIOS");
-
-    assertNull(clusterService);
-  }
-
-  /**
-   * @param dbAccessor
-   * @return
-   */
-  private AbstractUpgradeCatalog getUpgradeCatalog(final DBAccessor dbAccessor) {
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    return injector.getInstance(UpgradeCatalog200.class);
-  }
-
-  /**
-   * Verifies new ignore column for alert definition.
-   *
-   * @param alertDefinitionIgnoreColumnCapture
-   */
-  private void verifyAlertDefinitionIgnoreColumn(
-      Capture<DBAccessor.DBColumnInfo> alertDefinitionIgnoreColumnCapture) {
-    DBColumnInfo column = alertDefinitionIgnoreColumnCapture.getValue();
-    Assert.assertEquals(Integer.valueOf(0), column.getDefaultValue());
-    Assert.assertEquals(Integer.valueOf(1), column.getLength());
-    Assert.assertEquals(Short.class, column.getType());
-    Assert.assertEquals("ignore_host", column.getName());
-  }
-
-  /**
-   * Verifies new description column for alert definition.
-   *
-   * @param alertDefinitionDescriptionColumnCapture
-   */
-  private void verifyAlertDefinitionDescriptionColumn(
-      Capture<DBAccessor.DBColumnInfo> alertDefinitionDescriptionColumnCapture) {
-    DBColumnInfo column = alertDefinitionDescriptionColumnCapture.getValue();
-    Assert.assertEquals(null, column.getDefaultValue());
-    Assert.assertEquals(char[].class, column.getType());
-    Assert.assertEquals("description", column.getName());
-  }
-
-  /**
-   * Verifies alert_target_states table.
-   *
-   * @param alertTargetStatesCapture
-   */
-  private void verifyAlertTargetStatesTable(
-      Capture<List<DBAccessor.DBColumnInfo>> alertTargetStatesCapture) {
-    Assert.assertEquals(2, alertTargetStatesCapture.getValue().size());
-  }
-
-  /**
-   * Verifies is_global added to alert target table.
-   *
-   * @param alertTargetGlobalCapture
-   */
-  private void verifyAlertTargetGlobal(
-      Capture<DBAccessor.DBColumnInfo> alertTargetGlobalCapture) {
-    DBColumnInfo column = alertTargetGlobalCapture.getValue();
-    Assert.assertEquals(0, column.getDefaultValue());
-    Assert.assertEquals(Short.class, column.getType());
-    Assert.assertEquals("is_global", column.getName());
-  }
-
-  /**
-   * Verifies new security_state column in servicedesiredsstate table.
-   *
-   * @param securityStateColumnCapture
-   */
-  private void verifyServiceSecurityStateColumn(
-      Capture<DBAccessor.DBColumnInfo> securityStateColumnCapture) {
-    DBColumnInfo column = securityStateColumnCapture.getValue();
-    Assert.assertEquals(SecurityState.UNSECURED.toString(), column.getDefaultValue());
-    Assert.assertEquals(Integer.valueOf(32), column.getLength());
-    Assert.assertEquals(String.class, column.getType());
-    Assert.assertEquals("security_state", column.getName());
-  }
-
-  /**
-   * Verifies new security_type column in clusters table
-   *
-   * @param securityTypeColumnCapture
-   */
-  private void verifyClustersSecurityType(
-      Capture<DBAccessor.DBColumnInfo> securityTypeColumnCapture) {
-    DBColumnInfo column = securityTypeColumnCapture.getValue();
-    Assert.assertEquals(SecurityType.NONE.toString(), column.getDefaultValue());
-    Assert.assertEquals(Integer.valueOf(32), column.getLength());
-    Assert.assertEquals(String.class, column.getType());
-    Assert.assertEquals("security_type", column.getName());
-  }
-
-  /**
-   * Verifies new security_state column in hostcomponentdesiredstate and hostcomponentstate tables
-   *
-   * @param securityStateColumnCapture
-   */
-  private void verifyComponentSecurityStateColumn(
-      Capture<DBAccessor.DBColumnInfo> securityStateColumnCapture) {
-    DBColumnInfo column = securityStateColumnCapture.getValue();
-    Assert.assertEquals(SecurityState.UNSECURED.toString(), column.getDefaultValue());
-    Assert.assertEquals(Integer.valueOf(32), column.getLength());
-    Assert.assertEquals(String.class, column.getType());
-    Assert.assertEquals("security_state", column.getName());
-  }
-
-  private void verifyViewParameterColumns(
-      Capture<DBAccessor.DBColumnInfo> labelColumnCapture,
-      Capture<DBAccessor.DBColumnInfo> placeholderColumnCapture,
-      Capture<DBAccessor.DBColumnInfo> defaultValueColumnCapture) {
-
-
-    DBColumnInfo column = labelColumnCapture.getValue();
-    assertNull(column.getDefaultValue());
-    Assert.assertEquals(Integer.valueOf(255), column.getLength());
-    Assert.assertEquals(String.class, column.getType());
-    Assert.assertEquals("label", column.getName());
-
-    column = placeholderColumnCapture.getValue();
-    assertNull(column.getDefaultValue());
-    Assert.assertEquals(Integer.valueOf(255), column.getLength());
-    Assert.assertEquals(String.class, column.getType());
-    Assert.assertEquals("placeholder", column.getName());
-
-    column = defaultValueColumnCapture.getValue();
-    assertNull(column.getDefaultValue());
-    Assert.assertEquals(Integer.valueOf(2000), column.getLength());
-    Assert.assertEquals(String.class, column.getType());
-    Assert.assertEquals("default_value", column.getName());
-  }
-
-  @Test
-  public void testGetSourceVersion() {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    UpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
-    Assert.assertEquals(null, upgradeCatalog.getSourceVersion());
-  }
-
-  @Test
-  public void testGetTargetVersion() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    UpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
-
-    Assert.assertEquals("2.0.0", upgradeCatalog.getTargetVersion());
-  }
-
-  private void setViewInstancePropertyExpectations(DBAccessor dbAccessor,
-                                                   Capture<DBAccessor.DBColumnInfo> valueColumnCapture)
-      throws SQLException {
-
-    dbAccessor.alterColumn(eq("viewinstanceproperty"), capture(valueColumnCapture));
-  }
-
-  private void setViewInstanceDataExpectations(DBAccessor dbAccessor,
-                                               Capture<DBAccessor.DBColumnInfo> dataValueColumnCapture)
-      throws SQLException {
-
-    dbAccessor.alterColumn(eq("viewinstancedata"), capture(dataValueColumnCapture));
-  }
-
-  private void assertViewInstancePropertyColumns(
-      Capture<DBAccessor.DBColumnInfo> valueColumnCapture) {
-    DBAccessor.DBColumnInfo column = valueColumnCapture.getValue();
-    assertEquals("value", column.getName());
-    assertEquals(2000, (int) column.getLength());
-    assertEquals(String.class, column.getType());
-    assertNull(column.getDefaultValue());
-    assertTrue(column.isNullable());
-  }
-
-  private void assertViewInstanceDataColumns(
-      Capture<DBAccessor.DBColumnInfo> dataValueColumnCapture) {
-    DBAccessor.DBColumnInfo column = dataValueColumnCapture.getValue();
-    assertEquals("value", column.getName());
-    assertEquals(2000, (int) column.getLength());
-    assertEquals(String.class, column.getType());
-    assertNull(column.getDefaultValue());
-    assertTrue(column.isNullable());
-  }
-
-  /**
-   * assert artifact table creation
-   *
-   * @param artifactColumns artifact table columns
-   */
-  private void testCreateArtifactTable(List<DBColumnInfo> artifactColumns) {
-    assertEquals(3, artifactColumns.size());
-    for (DBColumnInfo column : artifactColumns) {
-      if (column.getName().equals("artifact_name")) {
-        assertNull(column.getDefaultValue());
-        assertEquals(String.class, column.getType());
-        assertEquals(255, (int) column.getLength());
-        assertEquals(false, column.isNullable());
-      } else if (column.getName().equals("foreign_keys")) {
-        assertNull(column.getDefaultValue());
-        assertEquals(String.class, column.getType());
-        assertEquals(255, (int) column.getLength());
-        assertEquals(false, column.isNullable());
-      } else if (column.getName().equals("artifact_data")) {
-        assertNull(column.getDefaultValue());
-        assertEquals(char[].class, column.getType());
-        assertEquals(false, column.isNullable());
-      } else {
-        fail("unexpected column name");
-      }
-    }
-  }
-
-  private void testCreateKerberosPrincipalTable(List<DBColumnInfo> columns) {
-    assertEquals(3, columns.size());
-    for (DBColumnInfo column : columns) {
-      if (column.getName().equals("principal_name")) {
-        assertNull(column.getDefaultValue());
-        assertEquals(String.class, column.getType());
-        assertEquals(255, (int) column.getLength());
-        assertEquals(false, column.isNullable());
-      } else if (column.getName().equals("is_service")) {
-        assertEquals(1, column.getDefaultValue());
-        assertEquals(Short.class, column.getType());
-        assertEquals(1, (int) column.getLength());
-        assertEquals(false, column.isNullable());
-      } else if (column.getName().equals("cached_keytab_path")) {
-        assertNull(column.getDefaultValue());
-        assertEquals(String.class, column.getType());
-        assertEquals(255, (int) column.getLength());
-        assertEquals(true, column.isNullable());
-      } else {
-        fail("unexpected column name");
-      }
-    }
-  }
-
-  private void testCreateKerberosPrincipalHostTable(List<DBColumnInfo> columns) {
-    assertEquals(2, columns.size());
-    for (DBColumnInfo column : columns) {
-      if (column.getName().equals("principal_name")) {
-        assertNull(column.getDefaultValue());
-        assertEquals(String.class, column.getType());
-        assertEquals(255, (int) column.getLength());
-        assertEquals(false, column.isNullable());
-      } else if (column.getName().equals("host_name")) {
-        assertNull(column.getDefaultValue());
-        assertEquals(String.class, column.getType());
-        assertEquals(255, (int) column.getLength());
-        assertEquals(false, column.isNullable());
-      } else {
-        fail("unexpected column name");
-      }
-    }
-  }
-}


[10/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java
index 25abada..8cf7373 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -17,6 +17,7 @@
  */
 package org.apache.ambari.server.state;
 
+import static org.easymock.EasyMock.eq;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.expectLastCall;
 import static org.easymock.EasyMock.replay;
@@ -39,6 +40,8 @@ import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.ambari.annotations.Experimental;
+import org.apache.ambari.annotations.ExperimentalFeature;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.actionmanager.HostRoleCommandFactory;
@@ -49,7 +52,10 @@ import org.apache.ambari.server.controller.ConfigurationRequest;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
+import org.apache.ambari.server.orm.dao.ServiceConfigDAO;
+import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
+import org.apache.ambari.server.orm.entities.ServiceConfigEntity;
 import org.apache.ambari.server.security.TestAuthenticationFactory;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.stack.HostsType;
@@ -79,6 +85,7 @@ import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
 import org.apache.ambari.server.utils.EventBusSynchronizer;
 import org.easymock.Capture;
 import org.easymock.EasyMock;
+import org.easymock.EasyMockSupport;
 import org.easymock.IAnswer;
 import org.junit.After;
 import org.junit.Before;
@@ -93,13 +100,14 @@ import com.google.inject.Binder;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Module;
+import com.google.inject.Provider;
 import com.google.inject.assistedinject.FactoryModuleBuilder;
 import com.google.inject.util.Modules;
 
 /**
  * Tests the {@link UpgradeHelper} class
  */
-public class UpgradeHelperTest {
+public class UpgradeHelperTest extends EasyMockSupport {
 
   private static final StackId STACK_ID_HDP_211 = new StackId("HDP-2.1.1");
   private static final StackId STACK_ID_HDP_220 = new StackId("HDP-2.2.0");
@@ -187,12 +195,11 @@ public class UpgradeHelperTest {
     final String upgradeToVersion = "2.2.0";
     final Direction upgradeDirection = Direction.UPGRADE;
     final UpgradeType upgradeType = UpgradeType.ROLLING;
-    final String stackName = "HDP";
 
     makeCluster();
     try {
       String preferredUpgradePackName = "upgrade_test";
-      UpgradePack up = m_upgradeHelper.suggestUpgradePack(clusterName, upgradeFromVersion, upgradeToVersion, upgradeDirection, upgradeType, stackName, preferredUpgradePackName);
+      UpgradePack up = m_upgradeHelper.suggestUpgradePack(clusterName, upgradeFromVersion, upgradeToVersion, upgradeDirection, upgradeType, preferredUpgradePackName);
       assertEquals(upgradeType, up.getType());
     } catch (AmbariException e){
       assertTrue(false);
@@ -217,8 +224,7 @@ public class UpgradeHelperTest {
 
     Cluster cluster = makeCluster();
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
 
@@ -278,6 +284,7 @@ public class UpgradeHelperTest {
     ambariMetaInfo.init();
   }
 
+  @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES)
   @Test
   public void testPartialUpgradeOrchestration() throws Exception {
     Map<String, UpgradePack> upgrades = ambariMetaInfo.getUpgradePacks("foo", "bar");
@@ -296,9 +303,10 @@ public class UpgradeHelperTest {
 
     Cluster cluster = makeCluster();
 
+    Set<String> services = Collections.singleton("ZOOKEEPER");
+
     UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210, Collections.singleton("ZOOKEEPER"), m_masterHostResolver,
-        UpgradeScope.PARTIAL, true);
+        repositoryVersion2210, RepositoryType.PATCH, services);
 
     List<Grouping> groupings = upgrade.getGroups(Direction.UPGRADE);
     assertEquals(8, groupings.size());
@@ -325,7 +333,7 @@ public class UpgradeHelperTest {
     assertEquals("Save Cluster State", postGroup.items.get(1).getText());
     assertEquals(StageWrapper.Type.SERVER_SIDE_ACTION, postGroup.items.get(1).getType());
 
-    assertEquals(3, groups.get(0).items.size());
+    assertEquals(2, groups.get(0).items.size());
     assertEquals(7, groups.get(1).items.size());
     assertEquals(2, groups.get(2).items.size());
 
@@ -353,9 +361,7 @@ public class UpgradeHelperTest {
     Cluster cluster = makeCluster();
 
     UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210, Collections.singleton("ZOOKEEPER"), m_masterHostResolver,
-        UpgradeScope.COMPLETE, true);
-
+        repositoryVersion2210, RepositoryType.STANDARD, Collections.singleton("ZOOKEEPER"));
 
     List<Grouping> groupings = upgrade.getGroups(Direction.UPGRADE);
     assertEquals(8, groupings.size());
@@ -383,7 +389,7 @@ public class UpgradeHelperTest {
     assertEquals("Save Cluster State", postGroup.items.get(1).getText());
     assertEquals(StageWrapper.Type.SERVER_SIDE_ACTION, postGroup.items.get(1).getType());
 
-    assertEquals(3, groups.get(0).items.size());
+    assertEquals(2, groups.get(0).items.size());
     assertEquals(6, groups.get(1).items.size());
     assertEquals(1, groups.get(2).items.size());
 
@@ -407,8 +413,7 @@ public class UpgradeHelperTest {
 
     Cluster cluster = makeCluster();
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210, m_masterHostResolver, true);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
 
@@ -456,11 +461,14 @@ public class UpgradeHelperTest {
     Host hostInMaintenanceMode = cluster.getHosts().iterator().next();
     hostInMaintenanceMode.setMaintenanceState(cluster.getClusterId(), MaintenanceState.ON);
 
+    UpgradeContext context = getMockUpgradeContextNoReplay(cluster, Direction.UPGRADE,
+        UpgradeType.ROLLING, repositoryVersion2210);
+
     // use a "real" master host resolver here so that we can actually test MM
-    MasterHostResolver masterHostResolver = new MasterHostResolver(null, cluster, "");
+    MasterHostResolver masterHostResolver = new MasterHostResolver(cluster, null, context);
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210, masterHostResolver, true);
+    expect(context.getResolver()).andReturn(masterHostResolver).anyTimes();
+    replay(context);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
     assertEquals(7, groups.size());
@@ -490,8 +498,7 @@ public class UpgradeHelperTest {
 
     Cluster cluster = makeCluster();
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
 
@@ -539,8 +546,7 @@ public class UpgradeHelperTest {
     assertEquals(1, schs.size());
     assertEquals(HostState.HEARTBEAT_LOST, schs.get(0).getHostState());
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
 
@@ -616,8 +622,7 @@ public class UpgradeHelperTest {
 
     Cluster cluster = makeCluster();
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
 
@@ -645,10 +650,7 @@ public class UpgradeHelperTest {
     assertNotNull(upgrade);
 
     Cluster cluster = makeCluster();
-
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
-
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
 
     assertEquals(7, groups.size());
@@ -674,8 +676,7 @@ public class UpgradeHelperTest {
 
     Cluster cluster = makeCluster();
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
 
@@ -745,8 +746,7 @@ public class UpgradeHelperTest {
 
     Cluster cluster = makeCluster();
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade,
         context);
@@ -856,8 +856,7 @@ public class UpgradeHelperTest {
 
     Cluster cluster = makeCluster();
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade,
         context);
@@ -918,8 +917,7 @@ public class UpgradeHelperTest {
 
     Cluster cluster = makeCluster();
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade,
         context);
@@ -984,8 +982,7 @@ public class UpgradeHelperTest {
     assertNotNull(upgrade);
     Cluster cluster = makeCluster();
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
 
@@ -1034,12 +1031,9 @@ public class UpgradeHelperTest {
     UpgradePack upgrade = upgrades.get("upgrade_test_checks");
     assertNotNull(upgrade);
 
-    Cluster c = makeCluster();
     // HBASE and PIG have service checks, but not TEZ.
     Set<String> additionalServices = new HashSet<String>() {{ add("HBASE"); add("PIG"); add("TEZ"); add("AMBARI_METRICS"); }};
-    for(String service : additionalServices) {
-      c.addService(service);
-    }
+    Cluster c = makeCluster(true, additionalServices);
 
     int numServiceChecksExpected = 0;
     Collection<Service> services = c.getServices().values();
@@ -1060,14 +1054,13 @@ public class UpgradeHelperTest {
       numServiceChecksExpected++;
     }
 
-    UpgradeContext context = getMockUpgradeContext(c, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(c, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
 
-    assertEquals(7, groups.size());
+    assertEquals(8, groups.size());
 
-    UpgradeGroupHolder holder = groups.get(3);
+    UpgradeGroupHolder holder = groups.get(4);
     assertEquals(holder.name, "SERVICE_CHECK_1");
     assertEquals(7, holder.items.size());
     int numServiceChecksActual = 0;
@@ -1094,6 +1087,13 @@ public class UpgradeHelperTest {
     assertEquals(
         "This is a manual task with a placeholder of placeholder-rendered-properly",
         manualTask.messages.get(0));
+
+    UpgradeGroupHolder clusterGroup = groups.get(3);
+    assertEquals(clusterGroup.name, "HBASE");
+    assertEquals(clusterGroup.title, "Update HBase Configuration");
+    assertEquals(1, clusterGroup.items.size());
+    StageWrapper stage = clusterGroup.items.get(0);
+    assertEquals(stage.getText(), "Update HBase Configuration");
   }
 
   @Test
@@ -1140,8 +1140,7 @@ public class UpgradeHelperTest {
 
     Cluster cluster = makeCluster();
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
 
@@ -1217,27 +1216,31 @@ public class UpgradeHelperTest {
    * @throws AmbariException
    */
   private Cluster makeCluster(boolean clean) throws AmbariException, AuthorizationException {
+    return makeCluster(clean, new HashSet<String>());
+  }
+
+  /**
+   * Create an HA cluster
+   * @throws AmbariException
+   */
+  private Cluster makeCluster(boolean clean, Set<String> additionalServices) throws AmbariException, AuthorizationException {
     Clusters clusters = injector.getInstance(Clusters.class);
     ServiceFactory serviceFactory = injector.getInstance(ServiceFactory.class);
 
     String clusterName = "c1";
 
+    String repositoryVersionString = "2.1.1-1234";
     StackId stackId = new StackId("HDP-2.1.1");
-    StackId stackId2 = new StackId("HDP-2.2.0");
 
     clusters.addCluster(clusterName, stackId);
     Cluster c = clusters.getCluster(clusterName);
 
-    helper.getOrCreateRepositoryVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion());
+    RepositoryVersionEntity repositoryVersion = helper.getOrCreateRepositoryVersion(stackId,
+        repositoryVersionString);
 
-    helper.getOrCreateRepositoryVersion(stackId2,"2.2.0");
+    helper.getOrCreateRepositoryVersion(STACK_ID_HDP_220, "2.2.0");
 
-    helper.getOrCreateRepositoryVersion(stackId2, UPGRADE_VERSION);
-
-    c.createClusterVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion(), "admin",
-        RepositoryVersionState.INSTALLING);
+    helper.getOrCreateRepositoryVersion(STACK_ID_HDP_220, UPGRADE_VERSION);
 
     for (int i = 0; i < 4; i++) {
       String hostName = "h" + (i+1);
@@ -1254,11 +1257,11 @@ public class UpgradeHelperTest {
     }
 
     // !!! add services
-    c.addService(serviceFactory.createNew(c, "HDFS"));
-    c.addService(serviceFactory.createNew(c, "YARN"));
-    c.addService(serviceFactory.createNew(c, "ZOOKEEPER"));
-    c.addService(serviceFactory.createNew(c, "HIVE"));
-    c.addService(serviceFactory.createNew(c, "OOZIE"));
+    c.addService(serviceFactory.createNew(c, "HDFS", repositoryVersion));
+    c.addService(serviceFactory.createNew(c, "YARN", repositoryVersion));
+    c.addService(serviceFactory.createNew(c, "ZOOKEEPER", repositoryVersion));
+    c.addService(serviceFactory.createNew(c, "HIVE", repositoryVersion));
+    c.addService(serviceFactory.createNew(c, "OOZIE", repositoryVersion));
 
     Service s = c.getService("HDFS");
     ServiceComponent sc = s.addServiceComponent("NAMENODE");
@@ -1361,6 +1364,15 @@ public class UpgradeHelperTest {
 
     expect(m_masterHostResolver.getCluster()).andReturn(c).anyTimes();
 
+    for(String service : additionalServices) {
+      c.addService(service, repositoryVersion);
+      if (service.equals("HBASE")) {
+        type = new HostsType();
+        type.hosts.addAll(Arrays.asList("h1", "h2"));
+        expect(m_masterHostResolver.getMasterAndHosts("HBASE", "HBASE_MASTER")).andReturn(type).anyTimes();
+      }
+    }
+
     replay(m_masterHostResolver);
 
     return c;
@@ -1390,8 +1402,7 @@ public class UpgradeHelperTest {
 
     Cluster cluster = makeCluster();
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
 
@@ -1444,16 +1455,12 @@ public class UpgradeHelperTest {
 
     String clusterName = "c1";
 
+    String version = "2.1.1.0-1234";
     StackId stackId = new StackId("HDP-2.1.1");
     clusters.addCluster(clusterName, stackId);
     Cluster c = clusters.getCluster(clusterName);
 
-    helper.getOrCreateRepositoryVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion());
-
-    c.createClusterVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion(), "admin",
-        RepositoryVersionState.INSTALLING);
+    RepositoryVersionEntity repositoryVersion = helper.getOrCreateRepositoryVersion(stackId, version);
 
     for (int i = 0; i < 2; i++) {
       String hostName = "h" + (i+1);
@@ -1470,7 +1477,7 @@ public class UpgradeHelperTest {
     }
 
     // !!! add services
-    c.addService(serviceFactory.createNew(c, "HDFS"));
+    c.addService(serviceFactory.createNew(c, "HDFS", repositoryVersion));
 
     Service s = c.getService("HDFS");
     ServiceComponent sc = s.addServiceComponent("NAMENODE");
@@ -1527,13 +1534,6 @@ public class UpgradeHelperTest {
     clusters.addCluster(clusterName, stackId);
     Cluster c = clusters.getCluster(clusterName);
 
-    helper.getOrCreateRepositoryVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion());
-
-    c.createClusterVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion(), "admin",
-        RepositoryVersionState.INSTALLING);
-
     for (int i = 0; i < 2; i++) {
       String hostName = "h" + (i+1);
       clusters.addHost(hostName);
@@ -1549,28 +1549,34 @@ public class UpgradeHelperTest {
     }
 
     // !!! add services
-    c.addService(serviceFactory.createNew(c, "ZOOKEEPER"));
+    c.addService(serviceFactory.createNew(c, "ZOOKEEPER", repositoryVersion2110));
 
     Service s = c.getService("ZOOKEEPER");
     ServiceComponent sc = s.addServiceComponent("ZOOKEEPER_SERVER");
 
     ServiceComponentHost sch1 = sc.addServiceComponentHost("h1");
-    sch1.setVersion("2.1.1.0-1234");
+    sch1.setVersion(repositoryVersion2110.getVersion());
 
     ServiceComponentHost sch2 = sc.addServiceComponentHost("h2");
-    sch2.setVersion("2.1.1.0-1234");
+    sch2.setVersion(repositoryVersion2110.getVersion());
 
     List<ServiceComponentHost> schs = c.getServiceComponentHosts("ZOOKEEPER", "ZOOKEEPER_SERVER");
     assertEquals(2, schs.size());
-    MasterHostResolver mhr = new MasterHostResolver(null, c, "2.1.1.0-1234");
 
-    HostsType ht = mhr.getMasterAndHosts("ZOOKEEPER", "ZOOKEEPER_SERVER");
+    UpgradeContext context = getMockUpgradeContextNoReplay(c, Direction.UPGRADE,
+        UpgradeType.HOST_ORDERED, repositoryVersion2110);
+
+    MasterHostResolver resolver = new MasterHostResolver(c, m_configHelper, context);
+    expect(context.getResolver()).andReturn(resolver).anyTimes();
+    replay(context);
+
+    HostsType ht = resolver.getMasterAndHosts("ZOOKEEPER", "ZOOKEEPER_SERVER");
     assertEquals(0, ht.hosts.size());
 
     // !!! if one of them is failed, it should be scheduled
     sch2.setUpgradeState(UpgradeState.FAILED);
 
-    ht = mhr.getMasterAndHosts("ZOOKEEPER", "ZOOKEEPER_SERVER");
+    ht = resolver.getMasterAndHosts("ZOOKEEPER", "ZOOKEEPER_SERVER");
 
     assertEquals(1, ht.hosts.size());
     assertEquals("h2", ht.hosts.iterator().next());
@@ -1592,12 +1598,8 @@ public class UpgradeHelperTest {
     clusters.addCluster(clusterName, stackId);
     Cluster c = clusters.getCluster(clusterName);
 
-    helper.getOrCreateRepositoryVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion());
-
-    c.createClusterVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion(), "admin",
-        RepositoryVersionState.INSTALLING);
+    RepositoryVersionEntity repositoryVersion211 = helper.getOrCreateRepositoryVersion(stackId,
+        version);
 
     for (int i = 0; i < 2; i++) {
       String hostName = "h" + (i+1);
@@ -1614,7 +1616,7 @@ public class UpgradeHelperTest {
     }
 
     // Add services
-    c.addService(serviceFactory.createNew(c, "HDFS"));
+    c.addService(serviceFactory.createNew(c, "HDFS", repositoryVersion211));
 
     Service s = c.getService("HDFS");
     ServiceComponent sc = s.addServiceComponent("NAMENODE");
@@ -1634,7 +1636,15 @@ public class UpgradeHelperTest {
     expect(m_configHelper.getValueFromDesiredConfigurations(c, "hdfs-site", "dfs.namenode.http-address.ha.nn2")).andReturn("H2:50070").anyTimes();
     replay(m_configHelper);
 
-    MasterHostResolver mhr = new MockMasterHostResolver(m_configHelper, c, version);
+    UpgradeContext context = getMockUpgradeContextNoReplay(c, Direction.UPGRADE,
+        UpgradeType.NON_ROLLING, repositoryVersion211);
+
+    // use a "real" master host resolver here so that we can actually test MM
+    MasterHostResolver mhr = new MockMasterHostResolver(c, m_configHelper, context);
+
+    expect(context.getResolver()).andReturn(mhr).anyTimes();
+    replay(context);
+
 
     HostsType ht = mhr.getMasterAndHosts("HDFS", "NAMENODE");
     assertNotNull(ht.master);
@@ -1658,12 +1668,7 @@ public class UpgradeHelperTest {
     clusters.addCluster(clusterName, stackId);
     Cluster c = clusters.getCluster(clusterName);
 
-    helper.getOrCreateRepositoryVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion());
-
-    c.createClusterVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion(), "admin",
-        RepositoryVersionState.INSTALLING);
+    RepositoryVersionEntity repositoryVersion211 = helper.getOrCreateRepositoryVersion(stackId, version);
 
     for (int i = 0; i < 2; i++) {
       String hostName = "h" + (i+1);
@@ -1680,7 +1685,7 @@ public class UpgradeHelperTest {
     }
 
     // Add services
-    c.addService(serviceFactory.createNew(c, "HDFS"));
+    c.addService(serviceFactory.createNew(c, "HDFS", repositoryVersion211));
 
     Service s = c.getService("HDFS");
     ServiceComponent sc = s.addServiceComponent("NAMENODE");
@@ -1700,7 +1705,14 @@ public class UpgradeHelperTest {
     expect(m_configHelper.getValueFromDesiredConfigurations(c, "hdfs-site", "dfs.namenode.http-address.ha.nn2")).andReturn("H2:50070").anyTimes();
     replay(m_configHelper);
 
-    MasterHostResolver mhr = new BadMasterHostResolver(m_configHelper, c, version);
+    UpgradeContext context = getMockUpgradeContextNoReplay(c, Direction.UPGRADE,
+        UpgradeType.NON_ROLLING, repositoryVersion211);
+
+    // use a "real" master host resolver here so that we can actually test MM
+    MasterHostResolver mhr = new BadMasterHostResolver(c, m_configHelper, context);
+
+    expect(context.getResolver()).andReturn(mhr).anyTimes();
+    replay(context);
 
     HostsType ht = mhr.getMasterAndHosts("HDFS", "NAMENODE");
     assertNotNull(ht.master);
@@ -1726,22 +1738,19 @@ public class UpgradeHelperTest {
     final String upgradeToVersion = "2.2.0";
     final Direction upgradeDirection = Direction.UPGRADE;
     final UpgradeType upgradeType = UpgradeType.ROLLING;
-    final String stackName = "HDP";
 
     Cluster cluster = makeCluster();
 
     // grab the right pack
     String preferredUpgradePackName = "upgrade_grouping_rolling";
     UpgradePack upgradePack = m_upgradeHelper.suggestUpgradePack(clusterName, upgradeFromVersion,
-        upgradeToVersion, upgradeDirection, upgradeType, stackName, preferredUpgradePackName);
+        upgradeToVersion, upgradeDirection, upgradeType, preferredUpgradePackName);
 
     assertEquals(upgradeType, upgradePack.getType());
 
     // get an upgrade
     UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210, Collections.singleton("ZOOKEEPER"), m_masterHostResolver,
-        UpgradeScope.COMPLETE, true);
-
+        repositoryVersion2210, RepositoryType.STANDARD, Collections.singleton("ZOOKEEPER"));
 
     List<Grouping> groupings = upgradePack.getGroups(Direction.UPGRADE);
     assertEquals(2, groupings.size());
@@ -1770,18 +1779,16 @@ public class UpgradeHelperTest {
 
     String clusterName = "c1";
 
+    String version = "2.1.1.0-1234";
     StackId stackId = new StackId("HDP-2.1.1");
     StackId stackId2 = new StackId("HDP-2.2.0");
     clusters.addCluster(clusterName, stackId);
     Cluster c = clusters.getCluster(clusterName);
 
-    helper.getOrCreateRepositoryVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion());
-    helper.getOrCreateRepositoryVersion(stackId2,"2.2.0");
+    RepositoryVersionEntity repoVersion211 = helper.getOrCreateRepositoryVersion(stackId,
+        version);
 
-    c.createClusterVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion(), "admin",
-        RepositoryVersionState.INSTALLING);
+    RepositoryVersionEntity repoVersion220 = helper.getOrCreateRepositoryVersion(stackId2, "2.2.0");
 
     for (int i = 0; i < 2; i++) {
       String hostName = "h" + (i+1);
@@ -1798,7 +1805,7 @@ public class UpgradeHelperTest {
     }
 
     // !!! add storm
-    c.addService(serviceFactory.createNew(c, "STORM"));
+    c.addService(serviceFactory.createNew(c, "STORM", repoVersion211));
 
     Service s = c.getService("STORM");
     ServiceComponent sc = s.addServiceComponent("NIMBUS");
@@ -1836,21 +1843,30 @@ public class UpgradeHelperTest {
 
     };
 
-    MasterHostResolver resolver = new MasterHostResolver(m_configHelper, c);
+    UpgradeContext context = getMockUpgradeContextNoReplay(c, Direction.UPGRADE,
+        UpgradeType.NON_ROLLING, repoVersion220);
 
-    UpgradeContext context = getMockUpgradeContext(c, Direction.UPGRADE, UpgradeType.NON_ROLLING,
-        repositoryVersion2200, resolver, true);
+    // use a "real" master host resolver here so that we can actually test MM
+    MasterHostResolver masterHostResolver = new MasterHostResolver(c, m_configHelper, context);
+
+    expect(context.getResolver()).andReturn(masterHostResolver).anyTimes();
+    replay(context);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgradePack, context);
 
     assertEquals(1, groups.size());
 
-    sch1.setVersion("2.1.1");
-    sch2.setVersion("2.1.1");
-    resolver = new MasterHostResolver(m_configHelper, c, "2.1.1");
+    sch1.setVersion(repoVersion211.getVersion());
+    sch2.setVersion(repoVersion211.getVersion());
 
-    context = getMockUpgradeContext(c, Direction.DOWNGRADE, UpgradeType.NON_ROLLING,
-        repositoryVersion2110, resolver, true);
+    context = getMockUpgradeContextNoReplay(c, Direction.DOWNGRADE, UpgradeType.NON_ROLLING,
+        repoVersion211);
+
+    // use a "real" master host resolver here so that we can actually test MM
+    masterHostResolver = new MasterHostResolver(c, m_configHelper, context);
+
+    expect(context.getResolver()).andReturn(masterHostResolver).anyTimes();
+    replay(context);
 
     groups = m_upgradeHelper.createSequence(upgradePack, context);
 
@@ -1866,23 +1882,20 @@ public class UpgradeHelperTest {
 
     String clusterName = "c1";
 
+    String version = "2.1.1.0-1234";
     StackId stackId = new StackId("HDP-2.1.1");
     StackId stackId2 = new StackId("HDP-2.2.0");
 
     clusters.addCluster(clusterName, stackId);
     Cluster c = clusters.getCluster(clusterName);
 
-    helper.getOrCreateRepositoryVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion());
+    RepositoryVersionEntity repositoryVersion = helper.getOrCreateRepositoryVersion(stackId,
+        version);
 
-    helper.getOrCreateRepositoryVersion(stackId2,"2.2.0");
+    RepositoryVersionEntity repoVersion220 = helper.getOrCreateRepositoryVersion(stackId2, "2.2.0");
 
     helper.getOrCreateRepositoryVersion(stackId2, UPGRADE_VERSION);
 
-    c.createClusterVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion(), "admin",
-        RepositoryVersionState.INSTALLING);
-
     for (int i = 0; i < 2; i++) {
       String hostName = "h" + (i+1);
       clusters.addHost(hostName);
@@ -1897,7 +1910,7 @@ public class UpgradeHelperTest {
     }
 
     // !!! add services
-    c.addService(serviceFactory.createNew(c, "ZOOKEEPER"));
+    c.addService(serviceFactory.createNew(c, "ZOOKEEPER", repositoryVersion));
 
     Service s = c.getService("ZOOKEEPER");
     ServiceComponent sc = s.addServiceComponent("ZOOKEEPER_SERVER");
@@ -1934,7 +1947,7 @@ public class UpgradeHelperTest {
     assertNotNull(upgrade);
 
     UpgradeContext context = getMockUpgradeContext(c, Direction.UPGRADE, UpgradeType.NON_ROLLING,
-        repositoryVersion2200);
+        repoVersion220);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
 
@@ -2055,18 +2068,15 @@ public class UpgradeHelperTest {
 
     String clusterName = "c1";
 
+    String version = "2.1.1.0-1234";
     StackId stackId = new StackId("HDP-2.1.1");
     StackId stackId2 = new StackId("HDP-2.2.0");
     clusters.addCluster(clusterName, stackId);
     Cluster c = clusters.getCluster(clusterName);
 
-    helper.getOrCreateRepositoryVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion());
-    helper.getOrCreateRepositoryVersion(stackId2, "2.2.0");
+    RepositoryVersionEntity repoVersion211 = helper.getOrCreateRepositoryVersion(stackId, version);
 
-    c.createClusterVersion(stackId,
-        c.getDesiredStackVersion().getStackVersion(), "admin",
-        RepositoryVersionState.INSTALLING);
+    RepositoryVersionEntity repoVersion220 = helper.getOrCreateRepositoryVersion(stackId2, "2.2.0");
 
     // create 2 hosts
     for (int i = 0; i < 2; i++) {
@@ -2085,8 +2095,8 @@ public class UpgradeHelperTest {
 
     // add ZK Server to both hosts, and then Nimbus to only 1 - this will test
     // how the HOU breaks out dependencies into stages
-    c.addService(serviceFactory.createNew(c, "ZOOKEEPER"));
-    c.addService(serviceFactory.createNew(c, "HBASE"));
+    c.addService(serviceFactory.createNew(c, "ZOOKEEPER", repoVersion211));
+    c.addService(serviceFactory.createNew(c, "HBASE", repoVersion211));
     Service zookeeper = c.getService("ZOOKEEPER");
     Service hbase = c.getService("HBASE");
     ServiceComponent zookeeperServer = zookeeper.addServiceComponent("ZOOKEEPER_SERVER");
@@ -2117,10 +2127,13 @@ public class UpgradeHelperTest {
     field.setAccessible(true);
     field.set(upgradePack, UpgradeType.HOST_ORDERED);
 
-    MasterHostResolver resolver = new MasterHostResolver(m_configHelper, c);
+    UpgradeContext context = getMockUpgradeContextNoReplay(c, Direction.UPGRADE,
+        UpgradeType.HOST_ORDERED, repoVersion220);
+
+    MasterHostResolver resolver = new MasterHostResolver(c, m_configHelper, context);
+    expect(context.getResolver()).andReturn(resolver).anyTimes();
+    replay(context);
 
-    UpgradeContext context = getMockUpgradeContext(c, Direction.UPGRADE, UpgradeType.HOST_ORDERED,
-        repositoryVersion2200, resolver, true);
 
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgradePack, context);
 
@@ -2154,13 +2167,16 @@ public class UpgradeHelperTest {
     assertEquals(StageWrapper.Type.SERVICE_CHECK, holder.items.get(8).getType());
 
     // !!! test downgrade when all host components have failed
-    zookeeperServer1.setVersion("2.1.1");
-    zookeeperServer2.setVersion("2.1.1");
-    hbaseMaster1.setVersion("2.1.1");
-    resolver = new MasterHostResolver(m_configHelper, c, "2.1.1");
+    zookeeperServer1.setVersion(repoVersion211.getVersion());
+    zookeeperServer2.setVersion(repoVersion211.getVersion());
+    hbaseMaster1.setVersion(repoVersion211.getVersion());
 
-    context = getMockUpgradeContext(c, Direction.DOWNGRADE, UpgradeType.HOST_ORDERED,
-        repositoryVersion2110, resolver, true);
+    context = getMockUpgradeContextNoReplay(c, Direction.DOWNGRADE, UpgradeType.HOST_ORDERED,
+        repoVersion211);
+
+    resolver = new MasterHostResolver(c, m_configHelper, context);
+    expect(context.getResolver()).andReturn(resolver).anyTimes();
+    replay(context);
 
     groups = m_upgradeHelper.createSequence(upgradePack, context);
 
@@ -2168,13 +2184,16 @@ public class UpgradeHelperTest {
     assertEquals(2, groups.get(0).items.size());
 
     // !!! test downgrade when one of the hosts had failed
-    zookeeperServer1.setVersion("2.1.1");
-    zookeeperServer2.setVersion("2.2.0");
-    hbaseMaster1.setVersion("2.1.1");
-    resolver = new MasterHostResolver(m_configHelper, c, "2.1.1");
+    zookeeperServer1.setVersion(repoVersion211.getVersion());
+    zookeeperServer2.setVersion(repoVersion220.getVersion());
+    hbaseMaster1.setVersion(repoVersion211.getVersion());
+
+    context = getMockUpgradeContextNoReplay(c, Direction.DOWNGRADE, UpgradeType.HOST_ORDERED,
+        repoVersion211);
 
-    context = getMockUpgradeContext(c, Direction.DOWNGRADE, UpgradeType.HOST_ORDERED,
-        repositoryVersion2110, resolver, true);
+    resolver = new MasterHostResolver(c, m_configHelper, context);
+    expect(context.getResolver()).andReturn(resolver).anyTimes();
+    replay(context);
 
     groups = m_upgradeHelper.createSequence(upgradePack, context);
 
@@ -2197,8 +2216,7 @@ public class UpgradeHelperTest {
 
     Cluster cluster = makeCluster();
 
-    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING,
-        repositoryVersion2210);
+    UpgradeContext context = getMockUpgradeContext(cluster, Direction.UPGRADE, UpgradeType.ROLLING);
 
     // initially, no conditions should be met
     List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
@@ -2232,6 +2250,192 @@ public class UpgradeHelperTest {
   }
 
   /**
+   * Tests merging configurations between existing and new stack values on
+   * upgrade.
+   *
+   * @throws Exception
+   */
+  @Test
+  public void testMergeConfigurations() throws Exception {
+    RepositoryVersionEntity repoVersion211 = createNiceMock(RepositoryVersionEntity.class);
+    RepositoryVersionEntity repoVersion220 = createNiceMock(RepositoryVersionEntity.class);
+
+    StackId stack211 = new StackId("HDP-2.1.1");
+    StackId stack220 = new StackId("HDP-2.2.0");
+
+    String version211 = "2.1.1.0-1234";
+    String version220 = "2.2.0.0-1234";
+
+    expect(repoVersion211.getStackId()).andReturn(stack211).atLeastOnce();
+    expect(repoVersion211.getVersion()).andReturn(version211).atLeastOnce();
+
+    expect(repoVersion220.getStackId()).andReturn(stack220).atLeastOnce();
+    expect(repoVersion220.getVersion()).andReturn(version220).atLeastOnce();
+
+    Map<String, Map<String, String>> stack211Configs = new HashMap<>();
+    Map<String, String> stack211FooType = new HashMap<>();
+    Map<String, String> stack211BarType = new HashMap<>();
+    Map<String, String> stack211BazType = new HashMap<>();
+    stack211Configs.put("foo-site", stack211FooType);
+    stack211Configs.put("bar-site", stack211BarType);
+    stack211Configs.put("baz-site", stack211BazType);
+    stack211FooType.put("1", "one");
+    stack211FooType.put("1A", "one-A");
+    stack211BarType.put("2", "two");
+    stack211BazType.put("3", "three");
+
+    Map<String, Map<String, String>> stack220Configs = new HashMap<>();
+    Map<String, String> stack220FooType = new HashMap<>();
+    Map<String, String> stack220BazType = new HashMap<>();
+    stack220Configs.put("foo-site", stack220FooType);
+    stack220Configs.put("baz-site", stack220BazType);
+    stack220FooType.put("1", "one-new");
+    stack220FooType.put("1A1", "one-A-one");
+    stack220BazType.put("3", "three-new");
+
+    Map<String, String> existingFooType = new HashMap<>();
+    Map<String, String> existingBarType = new HashMap<>();
+    Map<String, String> existingBazType = new HashMap<>();
+
+    ClusterConfigEntity fooConfigEntity = createNiceMock(ClusterConfigEntity.class);
+    ClusterConfigEntity barConfigEntity = createNiceMock(ClusterConfigEntity.class);
+    ClusterConfigEntity bazConfigEntity = createNiceMock(ClusterConfigEntity.class);
+
+    expect(fooConfigEntity.getType()).andReturn("foo-site");
+    expect(barConfigEntity.getType()).andReturn("bar-site");
+    expect(bazConfigEntity.getType()).andReturn("baz-site");
+
+    Config fooConfig = createNiceMock(Config.class);
+    Config barConfig = createNiceMock(Config.class);
+    Config bazConfig = createNiceMock(Config.class);
+
+    existingFooType.put("1", "one");
+    existingFooType.put("1A", "one-A");
+    existingBarType.put("2", "two");
+    existingBazType.put("3", "three-changed");
+
+    expect(fooConfig.getType()).andReturn("foo-site").atLeastOnce();
+    expect(barConfig.getType()).andReturn("bar-site").atLeastOnce();
+    expect(bazConfig.getType()).andReturn("baz-site").atLeastOnce();
+    expect(fooConfig.getProperties()).andReturn(existingFooType);
+    expect(barConfig.getProperties()).andReturn(existingBarType);
+    expect(bazConfig.getProperties()).andReturn(existingBazType);
+
+    Map<String, DesiredConfig> desiredConfigurations = new HashMap<>();
+    desiredConfigurations.put("foo-site", null);
+    desiredConfigurations.put("bar-site", null);
+    desiredConfigurations.put("baz-site", null);
+
+    Service zookeeper = createNiceMock(Service.class);
+    expect(zookeeper.getName()).andReturn("ZOOKEEPER").atLeastOnce();
+    expect(zookeeper.getServiceComponents()).andReturn(
+        new HashMap<String, ServiceComponent>()).once();
+    zookeeper.setDesiredRepositoryVersion(repoVersion220);
+    expectLastCall().once();
+
+    Cluster cluster = createNiceMock(Cluster.class);
+    expect(cluster.getCurrentStackVersion()).andReturn(stack211).atLeastOnce();
+    expect(cluster.getDesiredStackVersion()).andReturn(stack220);
+    expect(cluster.getDesiredConfigs()).andReturn(desiredConfigurations);
+    expect(cluster.getDesiredConfigByType("foo-site")).andReturn(fooConfig);
+    expect(cluster.getDesiredConfigByType("bar-site")).andReturn(barConfig);
+    expect(cluster.getDesiredConfigByType("baz-site")).andReturn(bazConfig);
+    expect(cluster.getService("ZOOKEEPER")).andReturn(zookeeper);
+    expect(cluster.getDesiredConfigByType("foo-type")).andReturn(fooConfig);
+    expect(cluster.getDesiredConfigByType("bar-type")).andReturn(barConfig);
+    expect(cluster.getDesiredConfigByType("baz-type")).andReturn(bazConfig);
+
+    // setup the config helper for placeholder resolution
+    @SuppressWarnings("unchecked")
+    Provider<ConfigHelper> configHelperProvider = EasyMock.createNiceMock(Provider.class);
+    ConfigHelper configHelper = EasyMock.createNiceMock(ConfigHelper.class);
+
+    expect(configHelperProvider.get()).andStubReturn(configHelper);
+
+    expect(configHelper.getDefaultProperties(stack211, "ZOOKEEPER")).andReturn(
+        stack211Configs).anyTimes();
+
+    expect(configHelper.getDefaultProperties(stack220, "ZOOKEEPER")).andReturn(
+        stack220Configs).anyTimes();
+
+    Capture<Map<String, Map<String, String>>> expectedConfigurationsCapture = EasyMock.newCapture();
+
+    configHelper.createConfigTypes(EasyMock.anyObject(Cluster.class),
+        EasyMock.anyObject(StackId.class), EasyMock.anyObject(AmbariManagementController.class),
+        EasyMock.capture(expectedConfigurationsCapture), EasyMock.anyObject(String.class),
+        EasyMock.anyObject(String.class));
+
+    expectLastCall().once();
+    EasyMock.replay(configHelperProvider, configHelper);
+
+    // mock the service config DAO and replay it
+    ServiceConfigEntity zookeeperServiceConfig = createNiceMock(ServiceConfigEntity.class);
+    expect(zookeeperServiceConfig.getClusterConfigEntities()).andReturn(
+        Lists.newArrayList(fooConfigEntity, barConfigEntity, bazConfigEntity));
+
+    ServiceConfigDAO serviceConfigDAOMock;
+    serviceConfigDAOMock = EasyMock.createNiceMock(ServiceConfigDAO.class);
+
+    List<ServiceConfigEntity> latestServiceConfigs = Lists.newArrayList(zookeeperServiceConfig);
+    expect(serviceConfigDAOMock.getLastServiceConfigsForService(EasyMock.anyLong(),
+        eq("ZOOKEEPER"))).andReturn(latestServiceConfigs).once();
+
+    replay(serviceConfigDAOMock);
+
+    Map<String, UpgradePack> upgradePacks = ambariMetaInfo.getUpgradePacks("HDP", "2.1.1");
+    UpgradePack upgradePack = upgradePacks.get("upgrade_to_new_stack");
+
+    UpgradeContext context = createNiceMock(UpgradeContext.class);
+    expect(context.getCluster()).andReturn(cluster).atLeastOnce();
+    expect(context.getType()).andReturn(UpgradeType.ROLLING).atLeastOnce();
+    expect(context.getDirection()).andReturn(Direction.UPGRADE).atLeastOnce();
+    expect(context.getRepositoryVersion()).andReturn(repoVersion220).anyTimes();
+    expect(context.getSupportedServices()).andReturn(Sets.newHashSet("ZOOKEEPER")).atLeastOnce();
+    expect(context.getSourceRepositoryVersion(EasyMock.anyString())).andReturn(repoVersion211).atLeastOnce();
+    expect(context.getTargetRepositoryVersion(EasyMock.anyString())).andReturn(repoVersion220).atLeastOnce();
+    expect(context.getOrchestrationType()).andReturn(RepositoryType.STANDARD).anyTimes();
+    expect(context.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
+    expect(context.getHostRoleCommandFactory()).andStubReturn(injector.getInstance(HostRoleCommandFactory.class));
+    expect(context.getRoleGraphFactory()).andStubReturn(injector.getInstance(RoleGraphFactory.class));
+    expect(context.getUpgradePack()).andReturn(upgradePack).atLeastOnce();
+
+    replayAll();
+
+    UpgradeHelper upgradeHelper = injector.getInstance(UpgradeHelper.class);
+    upgradeHelper.m_serviceConfigDAO = serviceConfigDAOMock;
+    upgradeHelper.m_configHelperProvider = configHelperProvider;
+    upgradeHelper.updateDesiredRepositoriesAndConfigs(context);
+
+    Map<String, Map<String, String>> expectedConfigurations = expectedConfigurationsCapture.getValue();
+    Map<String, String> expectedFooType = expectedConfigurations.get("foo-site");
+    Map<String, String> expectedBarType = expectedConfigurations.get("bar-site");
+    Map<String, String> expectedBazType = expectedConfigurations.get("baz-site");
+
+    // As the upgrade pack did not have any Flume updates, its configs should
+    // not be updated.
+    assertEquals(3, expectedConfigurations.size());
+
+    // the really important values are one-new and three-changed; one-new
+    // indicates that the new stack value is changed since it was not customized
+    // while three-changed represents that the customized value was preserved
+    // even though the stack value changed
+    assertEquals("one-new", expectedFooType.get("1"));
+    assertEquals("one-A", expectedFooType.get("1A"));
+    assertEquals("two", expectedBarType.get("2"));
+    assertEquals("three-changed", expectedBazType.get("3"));
+  }
+
+  /**
+   * @param cluster
+   * @param direction
+   * @param type
+   * @return
+   */
+  private UpgradeContext getMockUpgradeContext(Cluster cluster, Direction direction, UpgradeType type){
+    return getMockUpgradeContext(cluster, direction, type, repositoryVersion2210);
+  }
+
+  /**
    * @param cluster
    * @param direction
    * @param type
@@ -2239,22 +2443,36 @@ public class UpgradeHelperTest {
    */
   private UpgradeContext getMockUpgradeContext(Cluster cluster, Direction direction,
       UpgradeType type, RepositoryVersionEntity repositoryVersion) {
-    return getMockUpgradeContext(cluster, direction, type, repositoryVersion, m_masterHostResolver, true);
+    Set<String> allServices = cluster.getServices().keySet();
+    return getMockUpgradeContext(cluster, direction, type, repositoryVersion,
+        RepositoryType.STANDARD, allServices);
   }
 
   /**
    * @param cluster
    * @param direction
    * @param type
-   * @param repositoryType
-   * @param services
    * @return
    */
   private UpgradeContext getMockUpgradeContext(Cluster cluster, Direction direction,
-      UpgradeType type, RepositoryVersionEntity repositoryVersion, MasterHostResolver resolver,
-      boolean replay) {
+      UpgradeType type, RepositoryVersionEntity repositoryVersion, RepositoryType repositoryType,
+      Set<String> services) {
     return getMockUpgradeContext(cluster, direction, type, repositoryVersion,
-        new HashSet<String>(), resolver, UpgradeScope.COMPLETE, true);
+        repositoryType, services, m_masterHostResolver, true);
+  }
+
+  /**
+   * @param cluster
+   * @param direction
+   * @param type
+   * @return
+   */
+  private UpgradeContext getMockUpgradeContextNoReplay(Cluster cluster, Direction direction,
+      UpgradeType type, RepositoryVersionEntity repositoryVersion) {
+    Set<String> allServices = cluster.getServices().keySet();
+
+    return getMockUpgradeContext(cluster, direction, type, repositoryVersion,
+        RepositoryType.STANDARD, allServices, null, false);
   }
 
   /**
@@ -2266,66 +2484,65 @@ public class UpgradeHelperTest {
    * @return
    */
   private UpgradeContext getMockUpgradeContext(Cluster cluster, Direction direction,
-      UpgradeType type, RepositoryVersionEntity repositoryVersion, final Set<String> services,
-      MasterHostResolver resolver, final UpgradeScope scope, boolean replay) {
-    RepositoryVersionEntity sourceRepositoryVersion = cluster.getAllClusterVersions().iterator().next().getRepositoryVersion();
-
+      UpgradeType type, RepositoryVersionEntity repositoryVersion, final RepositoryType repositoryType,
+      Set<String> services, MasterHostResolver resolver, boolean replay) {
     UpgradeContext context = EasyMock.createNiceMock(UpgradeContext.class);
     expect(context.getCluster()).andReturn(cluster).anyTimes();
     expect(context.getType()).andReturn(type).anyTimes();
     expect(context.getDirection()).andReturn(direction).anyTimes();
-    expect(context.getSourceRepositoryVersion()).andReturn(sourceRepositoryVersion).anyTimes();
-    expect(context.getTargetRepositoryVersion()).andReturn(repositoryVersion).anyTimes();
-    expect(context.getTargetStackId()).andReturn(repositoryVersion.getStackId()).anyTimes();
+    expect(context.getRepositoryVersion()).andReturn(repositoryVersion).anyTimes();
+    expect(context.getSupportedServices()).andReturn(services).anyTimes();
+    expect(context.getOrchestrationType()).andReturn(repositoryType).anyTimes();
     expect(context.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
-    expect(context.getHostRoleCommandFactory()).andStubReturn(injector.getInstance(HostRoleCommandFactory.class));
-    expect(context.getRoleGraphFactory()).andStubReturn(injector.getInstance(RoleGraphFactory.class));
+    expect(context.getHostRoleCommandFactory()).andStubReturn(
+        injector.getInstance(HostRoleCommandFactory.class));
+    expect(context.getRoleGraphFactory()).andStubReturn(
+        injector.getInstance(RoleGraphFactory.class));
 
     // only set this if supplied
     if (null != resolver) {
       expect(context.getResolver()).andReturn(resolver).anyTimes();
     }
 
-    final Capture<UpgradeScope> isScopedCapture = EasyMock.newCapture();
-    expect(context.isScoped(EasyMock.capture(isScopedCapture))).andStubAnswer(
-        new IAnswer<Boolean>() {
-          @Override
-          public Boolean answer() throws Throwable {
-            UpgradeScope capturedScope = isScopedCapture.getValue();
-            if (capturedScope == UpgradeScope.ANY) {
-              return true;
-            }
+    final Map<String, RepositoryVersionEntity> targetRepositoryVersions = new HashMap<>();
+    for( String serviceName : services ){
+      targetRepositoryVersions.put(serviceName, repositoryVersion);
+    }
 
-            return scope.isScoped(capturedScope);
-          }
-        });
+    final Capture<String> repoVersionServiceName = EasyMock.newCapture();
+    expect(
+        context.getTargetRepositoryVersion(EasyMock.capture(repoVersionServiceName))).andAnswer(
+            new IAnswer<RepositoryVersionEntity>() {
+              @Override
+              public RepositoryVersionEntity answer() {
+                return targetRepositoryVersions.get(repoVersionServiceName.getValue());
+              }
+            }).anyTimes();
 
     final Capture<String> serviceNameSupported = EasyMock.newCapture();
     expect(context.isServiceSupported(EasyMock.capture(serviceNameSupported))).andAnswer(
         new IAnswer<Boolean>() {
           @Override
           public Boolean answer() {
-            if (services.isEmpty()) {
-              return true;
-            }
-
-            return services.contains(serviceNameSupported.getValue());
+            return targetRepositoryVersions.containsKey(serviceNameSupported.getValue());
           }
         }).anyTimes();
 
+
     final Map<String, String> serviceNames = new HashMap<>();
     final Capture<String> serviceDisplayNameArg1 = EasyMock.newCapture();
     final Capture<String> serviceDisplayNameArg2 = EasyMock.newCapture();
 
-    context.setServiceDisplay(EasyMock.capture(serviceDisplayNameArg1),
-        EasyMock.capture(serviceDisplayNameArg2));
-    expectLastCall().andAnswer(new IAnswer<Object>() {
-      @Override
-      public Object answer() {
-        serviceNames.put(serviceDisplayNameArg1.getValue(), serviceDisplayNameArg2.getValue());
-        return null;
-      }
-    }).anyTimes();
+    context.setServiceDisplay(EasyMock.capture(serviceDisplayNameArg1), EasyMock.capture(serviceDisplayNameArg2));
+    expectLastCall().andAnswer(
+        new IAnswer<Object>() {
+          @Override
+          public Object answer() {
+            serviceNames.put(serviceDisplayNameArg1.getValue(), serviceDisplayNameArg2.getValue());
+            return null;
+          }
+        }).anyTimes();
+
 
     final Map<String, String> componentNames = new HashMap<>();
     final Capture<String> componentDisplayNameArg1 = EasyMock.newCapture();
@@ -2346,13 +2563,14 @@ public class UpgradeHelperTest {
     }).anyTimes();
 
     final Capture<String> getServiceDisplayArgument1 = EasyMock.newCapture();
-    expect(context.getServiceDisplay(EasyMock.capture(getServiceDisplayArgument1))).andAnswer(
-        new IAnswer<String>() {
-          @Override
-          public String answer() {
-            return serviceNames.get(getServiceDisplayArgument1.getValue());
-          }
-        }).anyTimes();
+    expect(
+        context.getServiceDisplay(EasyMock.capture(getServiceDisplayArgument1))).andAnswer(
+            new IAnswer<String>() {
+              @Override
+              public String answer() {
+                return serviceNames.get(getServiceDisplayArgument1.getValue());
+              }
+            }).anyTimes();
 
     final Capture<String> getComponentDisplayArgument1 = EasyMock.newCapture();
     final Capture<String> getComponentDisplayArgument2 = EasyMock.newCapture();
@@ -2365,6 +2583,24 @@ public class UpgradeHelperTest {
           }
         }).anyTimes();
 
+    final Capture<UpgradeScope> isScopedCapture = EasyMock.newCapture();
+    expect(context.isScoped(EasyMock.capture(isScopedCapture))).andStubAnswer(
+        new IAnswer<Boolean>() {
+          @Override
+          public Boolean answer() throws Throwable {
+            UpgradeScope scope = isScopedCapture.getValue();
+            if (scope == UpgradeScope.ANY) {
+              return true;
+            }
+
+            if (scope == UpgradeScope.PARTIAL) {
+              return repositoryType != RepositoryType.STANDARD;
+            }
+
+            return repositoryType == RepositoryType.STANDARD;
+          }
+        });
+
     if (replay) {
       replay(context);
     }
@@ -2378,8 +2614,8 @@ public class UpgradeHelperTest {
    */
   private class MockMasterHostResolver extends MasterHostResolver {
 
-    public MockMasterHostResolver(ConfigHelper configHelper, Cluster cluster, String version) {
-      super(configHelper, cluster, version);
+    public MockMasterHostResolver(Cluster cluster, ConfigHelper configHelper, UpgradeContext context) {
+      super(cluster, configHelper, context);
     }
 
     /**
@@ -2421,8 +2657,8 @@ public class UpgradeHelperTest {
 
   private static class BadMasterHostResolver extends MasterHostResolver {
 
-    public BadMasterHostResolver(ConfigHelper configHelper, Cluster cluster, String version) {
-      super(configHelper, cluster, version);
+    public BadMasterHostResolver(Cluster cluster, ConfigHelper configHelper, UpgradeContext context) {
+      super(cluster, configHelper, context);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/state/repository/VersionDefinitionTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/repository/VersionDefinitionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/repository/VersionDefinitionTest.java
index 54b3cd9..ac7c39a 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/repository/VersionDefinitionTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/repository/VersionDefinitionTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/state/services/RetryUpgradeActionServiceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/services/RetryUpgradeActionServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/services/RetryUpgradeActionServiceTest.java
index 85c08fe..e80a43e 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/services/RetryUpgradeActionServiceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/services/RetryUpgradeActionServiceTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -33,7 +33,6 @@ import org.apache.ambari.server.orm.OrmTestHelper;
 import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.RequestDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.dao.StageDAO;
 import org.apache.ambari.server.orm.dao.UpgradeDAO;
 import org.apache.ambari.server.orm.entities.HostRoleCommandEntity;
@@ -45,7 +44,6 @@ import org.apache.ambari.server.orm.entities.StageEntityPK;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.stack.upgrade.Direction;
 import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
@@ -64,7 +62,6 @@ public class RetryUpgradeActionServiceTest {
 
   private Injector injector;
 
-  private StackDAO stackDAO;
   private Clusters clusters;
   private RepositoryVersionDAO repoVersionDAO;
   private UpgradeDAO upgradeDAO;
@@ -76,19 +73,16 @@ public class RetryUpgradeActionServiceTest {
   // Instance variables shared by all tests
   String clusterName = "c1";
   Cluster cluster;
+  StackId stack220 = new StackId("HDP-2.2.0");
   StackEntity stackEntity220;
-  StackId stack220;
   Long upgradeRequestId = 1L;
   Long stageId = 1L;
 
-  RepositoryVersionEntity repoVersion2200;
-
   @Before
-  public void before() throws NoSuchFieldException, IllegalAccessException {
+  public void before() throws Exception {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
 
-    stackDAO = injector.getInstance(StackDAO.class);
     clusters = injector.getInstance(Clusters.class);
     repoVersionDAO = injector.getInstance(RepositoryVersionDAO.class);
     upgradeDAO = injector.getInstance(UpgradeDAO.class);
@@ -96,6 +90,7 @@ public class RetryUpgradeActionServiceTest {
     stageDAO = injector.getInstance(StageDAO.class);
     hostRoleCommandDAO = injector.getInstance(HostRoleCommandDAO.class);
     helper = injector.getInstance(OrmTestHelper.class);
+    stackEntity220 = helper.createStack(stack220);
   }
 
   @After
@@ -237,23 +232,18 @@ public class RetryUpgradeActionServiceTest {
    * @throws AmbariException
    */
   private void createCluster() throws AmbariException {
-    stackEntity220 = stackDAO.find("HDP", "2.2.0");
-    stack220 = new StackId("HDP-2.2.0");
 
     clusters.addCluster(clusterName, stack220);
     cluster = clusters.getCluster("c1");
 
-    repoVersion2200 = new RepositoryVersionEntity();
-    repoVersion2200.setDisplayName("Initial Version");
-    repoVersion2200.setOperatingSystems("");
-    repoVersion2200.setStack(stackEntity220);
-    repoVersion2200.setVersion("2.2.0.0");
-    repoVersionDAO.create(repoVersion2200);
+    RepositoryVersionEntity repoVersionEntity = new RepositoryVersionEntity();
+    repoVersionEntity.setDisplayName("Initial Version");
+    repoVersionEntity.setOperatingSystems("");
+    repoVersionEntity.setStack(stackEntity220);
+    repoVersionEntity.setVersion("2.2.0.0");
+    repoVersionDAO.create(repoVersionEntity);
 
     helper.getOrCreateRepositoryVersion(stack220, stack220.getStackVersion());
-
-    cluster.createClusterVersion(stack220, stack220.getStackVersion(), "admin", RepositoryVersionState.INSTALLING);
-    cluster.transitionClusterVersion(stack220, stack220.getStackVersion(), RepositoryVersionState.CURRENT);
   }
 
   /**
@@ -261,19 +251,18 @@ public class RetryUpgradeActionServiceTest {
    * @throws AmbariException
    */
   private void prepareUpgrade() throws AmbariException {
-    RepositoryVersionEntity repoVersion2201 = new RepositoryVersionEntity();
-    repoVersion2201.setDisplayName("Version to Upgrade To");
-    repoVersion2201.setOperatingSystems("");
-    repoVersion2201.setStack(stackEntity220);
-    repoVersion2201.setVersion("2.2.0.1");
-    repoVersionDAO.create(repoVersion2201);
+    RepositoryVersionEntity repoVersionEntity = new RepositoryVersionEntity();
+    repoVersionEntity.setDisplayName("Version to Upgrade To");
+    repoVersionEntity.setOperatingSystems("");
+    repoVersionEntity.setStack(stackEntity220);
+    repoVersionEntity.setVersion("2.2.0.1");
+    repoVersionDAO.create(repoVersionEntity);
 
     helper.getOrCreateRepositoryVersion(stack220, stack220.getStackVersion());
 
     RequestEntity requestEntity = new RequestEntity();
     requestEntity.setRequestId(upgradeRequestId);
     requestEntity.setClusterId(cluster.getClusterId());
-    requestEntity.setStatus(HostRoleStatus.PENDING);
     requestDAO.create(requestEntity);
 
     // Create the stage and add it to the request
@@ -293,8 +282,7 @@ public class RetryUpgradeActionServiceTest {
     upgrade.setUpgradePackage("some-name");
     upgrade.setUpgradeType(UpgradeType.ROLLING);
     upgrade.setDirection(Direction.UPGRADE);
-    upgrade.setFromRepositoryVersion(repoVersion2200);
-    upgrade.setToRepositoryVersion(repoVersion2201);
+    upgrade.setRepositoryVersion(repoVersionEntity);
     upgradeDAO.create(upgrade);
 
     cluster.setUpgradeEntity(upgrade);

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelperTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelperTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelperTest.java
index 01e91d1..422c0ec 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelperTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelperTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -17,19 +17,15 @@
  */
 package org.apache.ambari.server.state.stack.upgrade;
 
+import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.List;
 
-import junit.framework.Assert;
-
 import org.apache.ambari.server.state.RepositoryInfo;
-import org.junit.Before;
+import org.junit.Assert;
 import org.junit.Test;
 
 import com.google.gson.Gson;
-import com.google.inject.AbstractModule;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
 
 /**
  * Tests the {@link RepositoryVersionHelper} class
@@ -38,21 +34,16 @@ public class RepositoryVersionHelperTest {
 
   private RepositoryVersionHelper helper;
 
-  @Before
-  public void before() throws Exception {
-    final Injector injector = Guice.createInjector(new AbstractModule() {
-
-      @Override
-      protected void configure() {
-        bind(Gson.class).toInstance(new Gson());
-      }
-    });
-    helper = injector.getInstance(RepositoryVersionHelper.class);
-  }
-
   @Test
   public void testSerializeOperatingSystems() throws Exception {
-    final List<RepositoryInfo> repositories = new ArrayList<RepositoryInfo>();
+    Gson gson = new Gson();
+    Field field = RepositoryVersionHelper.class.getDeclaredField("gson");
+    field.setAccessible(true);
+
+    RepositoryVersionHelper helper = new RepositoryVersionHelper();
+    field.set(helper, gson);
+
+    final List<RepositoryInfo> repositories = new ArrayList<>();
     final RepositoryInfo repository = new RepositoryInfo();
     repository.setBaseUrl("baseurl");
     repository.setOsType("os");

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/StageWrapperBuilderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/StageWrapperBuilderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/StageWrapperBuilderTest.java
index 5cef95d..03e8825 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/StageWrapperBuilderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/stack/upgrade/StageWrapperBuilderTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -51,8 +51,6 @@ public class StageWrapperBuilderTest extends EasyMockSupport {
   @Test
   public void testBuildOrder() throws Exception {
     Cluster cluster = createNiceMock(Cluster.class);
-    EasyMock.expect(cluster.getCurrentStackVersion()).andReturn(HDP_21).anyTimes();
-    EasyMock.expect(cluster.getDesiredStackVersion()).andReturn(HDP_21).anyTimes();
 
     RepositoryVersionEntity repoVersionEntity = createNiceMock(RepositoryVersionEntity.class);
     EasyMock.expect(repoVersionEntity.getStackId()).andReturn(HDP_21).anyTimes();
@@ -65,8 +63,7 @@ public class StageWrapperBuilderTest extends EasyMockSupport {
     EasyMock.expect(upgradeContext.getCluster()).andReturn(cluster).anyTimes();
     EasyMock.expect(upgradeContext.getType()).andReturn(UpgradeType.ROLLING).anyTimes();
     EasyMock.expect(upgradeContext.getDirection()).andReturn(Direction.UPGRADE).anyTimes();
-    EasyMock.expect(upgradeContext.getSourceRepositoryVersion()).andReturn(repoVersionEntity).anyTimes();
-    EasyMock.expect(upgradeContext.getTargetRepositoryVersion()).andReturn(repoVersionEntity).anyTimes();
+    EasyMock.expect(upgradeContext.getRepositoryVersion()).andReturn(repoVersionEntity).anyTimes();
     EasyMock.expect(upgradeContext.isComponentFailureAutoSkipped()).andReturn(false).anyTimes();
     EasyMock.expect(upgradeContext.isServiceCheckFailureAutoSkipped()).andReturn(false).anyTimes();
 
@@ -95,8 +92,6 @@ public class StageWrapperBuilderTest extends EasyMockSupport {
   @Test
   public void testAutoSkipCheckInserted() throws Exception {
     Cluster cluster = createNiceMock(Cluster.class);
-    EasyMock.expect(cluster.getCurrentStackVersion()).andReturn(HDP_21).anyTimes();
-    EasyMock.expect(cluster.getDesiredStackVersion()).andReturn(HDP_21).anyTimes();
 
     RepositoryVersionEntity repoVersionEntity = createNiceMock(RepositoryVersionEntity.class);
     EasyMock.expect(repoVersionEntity.getStackId()).andReturn(HDP_21).anyTimes();
@@ -109,8 +104,7 @@ public class StageWrapperBuilderTest extends EasyMockSupport {
     EasyMock.expect(upgradeContext.getCluster()).andReturn(cluster).anyTimes();
     EasyMock.expect(upgradeContext.getType()).andReturn(UpgradeType.ROLLING).anyTimes();
     EasyMock.expect(upgradeContext.getDirection()).andReturn(Direction.UPGRADE).anyTimes();
-    EasyMock.expect(upgradeContext.getSourceRepositoryVersion()).andReturn(repoVersionEntity).anyTimes();
-    EasyMock.expect(upgradeContext.getTargetRepositoryVersion()).andReturn(repoVersionEntity).anyTimes();
+    EasyMock.expect(upgradeContext.getRepositoryVersion()).andReturn(repoVersionEntity).anyTimes();
     EasyMock.expect(upgradeContext.isComponentFailureAutoSkipped()).andReturn(true).anyTimes();
     EasyMock.expect(upgradeContext.isServiceCheckFailureAutoSkipped()).andReturn(true).anyTimes();
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/StackUpgradeUtilTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/StackUpgradeUtilTest.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/StackUpgradeUtilTest.java
deleted file mode 100644
index a15d05d..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/StackUpgradeUtilTest.java
+++ /dev/null
@@ -1,145 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.ambari.server.upgrade;
-
-import java.util.Collection;
-import java.util.List;
-import java.util.Map.Entry;
-
-import org.apache.ambari.server.H2DatabaseCleaner;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.orm.GuiceJpaInitializer;
-import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.MetainfoDAO;
-import org.apache.ambari.server.orm.entities.MetainfoEntity;
-import org.apache.ambari.server.state.RepositoryInfo;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-
-/**
- * Tests the StackUpgradeHelper
- */
-public class StackUpgradeUtilTest {
-
-  private Injector injector;
-
-  @Before
-  public void setup() throws Exception {
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-  }
-
-  @After
-  public void teardown() throws Exception {
-    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
-  }
-
-  private void reset(String stackName, String stackVersion) throws Exception {
-    AmbariMetaInfo ami = injector.getInstance(AmbariMetaInfo.class);
-
-    for (Entry<String, List<RepositoryInfo>> entry : ami.getRepository(stackName, stackVersion).entrySet()) {
-      for (RepositoryInfo ri : entry.getValue()) {
-        if (-1 == ri.getRepoId().indexOf("epel")) {
-          ami.updateRepoBaseURL(stackName, stackVersion,
-              ri.getOsType(), ri.getRepoId(), "");
-        }
-      }
-    }
-
-  }
-
-  @Test
-  public void testUpgradeStack() throws Exception {
-    StackUpgradeUtil stackUpgradeUtil = injector.getInstance(StackUpgradeUtil.class);
-
-    String stackName = "HDP";
-    String stackVersion = "1.3.0";
-    String localRepoUrl = "http://foo.bar";
-
-    // check updating all
-    stackUpgradeUtil.updateLocalRepo(stackName, stackVersion, localRepoUrl, null);
-
-    MetainfoDAO dao = injector.getInstance(MetainfoDAO.class);
-
-    Collection<MetainfoEntity> entities = dao.findAll();
-    Assert.assertTrue(entities.size() > 0);
-
-    for (MetainfoEntity entity : entities) {
-      Assert.assertTrue(entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/"));
-      Assert.assertEquals(localRepoUrl, entity.getMetainfoValue());
-    }
-
-    reset (stackName, stackVersion);
-    entities = dao.findAll();
-    Assert.assertEquals(0, entities.size());
-
-    // check updating only centos6
-    stackUpgradeUtil.updateLocalRepo(stackName, stackVersion, localRepoUrl, "centos6");
-
-    entities = dao.findAll();
-    for (MetainfoEntity entity : entities) {
-      Assert.assertTrue(entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/centos6") ||
-          entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/redhat6"));
-      Assert.assertEquals(localRepoUrl, entity.getMetainfoValue());
-    }
-
-    reset (stackName, stackVersion);
-    entities = dao.findAll();
-    Assert.assertTrue(0 == entities.size());
-
-    // check updating only centos6 and centos5
-    stackUpgradeUtil.updateLocalRepo(stackName, stackVersion, localRepoUrl, "centos6,centos5");
-
-    entities = dao.findAll();
-    for (MetainfoEntity entity : entities) {
-      Assert.assertTrue(entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/centos6") ||
-          entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/redhat6") ||
-          entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/redhat5") ||
-          entity.getMetainfoName().startsWith("repo:/HDP/1.3.0/centos5"));
-      Assert.assertEquals(localRepoUrl, entity.getMetainfoValue());
-    }
-
-    // verify that a change to centos6 also changes redhat6
-    localRepoUrl = "http://newfoo.bar";
-    stackUpgradeUtil.updateLocalRepo(stackName, stackVersion, localRepoUrl, "centos6");
-    entities = dao.findAll();
-    boolean foundCentos6 = false;
-    boolean foundRedhat6 = false;
-    for (MetainfoEntity entity : entities) {
-      if (-1 != entity.getMetainfoName().indexOf("centos6")) {
-        foundCentos6 = true;
-        Assert.assertEquals(localRepoUrl, entity.getMetainfoValue());
-      } else if (-1 != entity.getMetainfoName().indexOf("redhat6")) {
-        foundRedhat6 = true;
-        Assert.assertEquals(localRepoUrl, entity.getMetainfoValue());
-      } else {
-        Assert.assertFalse(localRepoUrl.equals(entity.getMetainfoValue()));
-      }
-    }
-    Assert.assertTrue(foundCentos6);
-    Assert.assertTrue(foundRedhat6);
-
-  }
-
-
-}


[32/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
index 0cc1745..387e0dc 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -56,6 +56,8 @@ import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.controller.utilities.ServiceCalculatedStateFactory;
 import org.apache.ambari.server.controller.utilities.state.ServiceCalculatedState;
 import org.apache.ambari.server.metadata.RoleCommandOrder;
+import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.security.authorization.AuthorizationHelper;
 import org.apache.ambari.server.security.authorization.ResourceType;
@@ -83,37 +85,76 @@ import com.google.inject.assistedinject.AssistedInject;
  * Resource provider for service resources.
  */
 public class ServiceResourceProvider extends AbstractControllerResourceProvider {
+  public static final String SERVICE_CLUSTER_NAME_PROPERTY_ID = PropertyHelper.getPropertyId(
+      "ServiceInfo", "cluster_name");
 
+  public static final String SERVICE_SERVICE_NAME_PROPERTY_ID = PropertyHelper.getPropertyId(
+      "ServiceInfo", "service_name");
 
-  // ----- Property ID constants ---------------------------------------------
+  public static final String SERVICE_SERVICE_STATE_PROPERTY_ID = PropertyHelper.getPropertyId(
+      "ServiceInfo", "state");
 
-  // Services
-  public static final String SERVICE_CLUSTER_NAME_PROPERTY_ID    = PropertyHelper.getPropertyId("ServiceInfo", "cluster_name");
-  public static final String SERVICE_SERVICE_NAME_PROPERTY_ID    = PropertyHelper.getPropertyId("ServiceInfo", "service_name");
-  public static final String SERVICE_SERVICE_STATE_PROPERTY_ID   = PropertyHelper.getPropertyId("ServiceInfo", "state");
-  public static final String SERVICE_MAINTENANCE_STATE_PROPERTY_ID = PropertyHelper.getPropertyId("ServiceInfo", "maintenance_state");
-  public static final String SERVICE_CREDENTIAL_STORE_SUPPORTED_PROPERTY_ID =
-    PropertyHelper.getPropertyId("ServiceInfo", "credential_store_supported");
-  public static final String SERVICE_CREDENTIAL_STORE_ENABLED_PROPERTY_ID =
-    PropertyHelper.getPropertyId("ServiceInfo", "credential_store_enabled");
+  public static final String SERVICE_MAINTENANCE_STATE_PROPERTY_ID = PropertyHelper.getPropertyId(
+      "ServiceInfo", "maintenance_state");
 
-  public static final String SERVICE_ATTRIBUTES_PROPERTY_ID = PropertyHelper.getPropertyId("Services", "attributes");
+  public static final String SERVICE_CREDENTIAL_STORE_SUPPORTED_PROPERTY_ID = PropertyHelper.getPropertyId(
+      "ServiceInfo", "credential_store_supported");
 
-  //Parameters from the predicate
-  private static final String QUERY_PARAMETERS_RUN_SMOKE_TEST_ID =
-    "params/run_smoke_test";
+  public static final String SERVICE_CREDENTIAL_STORE_ENABLED_PROPERTY_ID = PropertyHelper.getPropertyId(
+      "ServiceInfo", "credential_store_enabled");
+
+  public static final String SERVICE_ATTRIBUTES_PROPERTY_ID = PropertyHelper.getPropertyId(
+      "Services", "attributes");
 
-  private static final String QUERY_PARAMETERS_RECONFIGURE_CLIENT =
-    "params/reconfigure_client";
+  public static final String SERVICE_DESIRED_STACK_PROPERTY_ID = PropertyHelper.getPropertyId(
+      "ServiceInfo", "desired_stack");
 
-  private static final String QUERY_PARAMETERS_START_DEPENDENCIES =
-    "params/start_dependencies";
+  public static final String SERVICE_DESIRED_REPO_VERSION_ID_PROPERTY_ID = PropertyHelper.getPropertyId(
+      "ServiceInfo", "desired_repository_version_id");
+
+  protected static final String SERVICE_REPOSITORY_STATE = "ServiceInfo/repository_state";
+
+  //Parameters from the predicate
+  private static final String QUERY_PARAMETERS_RUN_SMOKE_TEST_ID = "params/run_smoke_test";
+  private static final String QUERY_PARAMETERS_RECONFIGURE_CLIENT = "params/reconfigure_client";
+  private static final String QUERY_PARAMETERS_START_DEPENDENCIES = "params/start_dependencies";
 
   private static Set<String> pkPropertyIds =
-      new HashSet<String>(Arrays.asList(new String[]{
-          SERVICE_CLUSTER_NAME_PROPERTY_ID,
-          SERVICE_SERVICE_NAME_PROPERTY_ID}));
+    new HashSet<>(Arrays.asList(new String[]{
+      SERVICE_CLUSTER_NAME_PROPERTY_ID,
+      SERVICE_SERVICE_NAME_PROPERTY_ID}));
 
+  /**
+   * The property ids for an service resource.
+   */
+  private static final Set<String> PROPERTY_IDS = new HashSet<>();
+
+  /**
+   * The key property ids for an service resource.
+   */
+  private static final Map<Resource.Type, String> KEY_PROPERTY_IDS = new HashMap<>();
+
+  static {
+    // properties
+    PROPERTY_IDS.add(SERVICE_CLUSTER_NAME_PROPERTY_ID);
+    PROPERTY_IDS.add(SERVICE_SERVICE_NAME_PROPERTY_ID);
+    PROPERTY_IDS.add(SERVICE_SERVICE_STATE_PROPERTY_ID);
+    PROPERTY_IDS.add(SERVICE_MAINTENANCE_STATE_PROPERTY_ID);
+    PROPERTY_IDS.add(SERVICE_CREDENTIAL_STORE_SUPPORTED_PROPERTY_ID);
+    PROPERTY_IDS.add(SERVICE_CREDENTIAL_STORE_ENABLED_PROPERTY_ID);
+    PROPERTY_IDS.add(SERVICE_ATTRIBUTES_PROPERTY_ID);
+    PROPERTY_IDS.add(SERVICE_DESIRED_STACK_PROPERTY_ID);
+    PROPERTY_IDS.add(SERVICE_DESIRED_REPO_VERSION_ID_PROPERTY_ID);
+    PROPERTY_IDS.add(SERVICE_REPOSITORY_STATE);
+
+    PROPERTY_IDS.add(QUERY_PARAMETERS_RUN_SMOKE_TEST_ID);
+    PROPERTY_IDS.add(QUERY_PARAMETERS_RECONFIGURE_CLIENT);
+    PROPERTY_IDS.add(QUERY_PARAMETERS_START_DEPENDENCIES);
+
+    // keys
+    KEY_PROPERTY_IDS.put(Resource.Type.Service, SERVICE_SERVICE_NAME_PROPERTY_ID);
+    KEY_PROPERTY_IDS.put(Resource.Type.Cluster, SERVICE_CLUSTER_NAME_PROPERTY_ID);
+  }
 
   private MaintenanceStateHelper maintenanceStateHelper;
 
@@ -123,22 +164,25 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
   @Inject
   private KerberosHelper kerberosHelper;
 
+  /**
+   * Used to lookup the repository when creating services.
+   */
+  private final RepositoryVersionDAO repositoryVersionDAO;
+
   // ----- Constructors ----------------------------------------------------
 
   /**
    * Create a  new resource provider for the given management controller.
    *
-   * @param propertyIds           the property ids
-   * @param keyPropertyIds        the key property ids
    * @param managementController  the management controller
    */
   @AssistedInject
-  public ServiceResourceProvider(@Assisted Set<String> propertyIds,
-                          @Assisted Map<Resource.Type, String> keyPropertyIds,
-                          @Assisted AmbariManagementController managementController,
-                          MaintenanceStateHelper maintenanceStateHelper) {
-    super(propertyIds, keyPropertyIds, managementController);
+  public ServiceResourceProvider(
+      @Assisted AmbariManagementController managementController,
+      MaintenanceStateHelper maintenanceStateHelper, RepositoryVersionDAO repositoryVersionDAO) {
+    super(PROPERTY_IDS, KEY_PROPERTY_IDS, managementController);
     this.maintenanceStateHelper = maintenanceStateHelper;
+    this.repositoryVersionDAO = repositoryVersionDAO;
 
     setRequiredCreateAuthorizations(EnumSet.of(RoleAuthorization.SERVICE_ADD_DELETE_SERVICES));
     setRequiredUpdateAuthorizations(RoleAuthorization.AUTHORIZATIONS_UPDATE_SERVICE);
@@ -175,7 +219,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
   protected Set<Resource> getResourcesAuthorized(Request request, Predicate predicate) throws
       SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
 
-    final Set<ServiceRequest> requests = new HashSet<ServiceRequest>();
+    final Set<ServiceRequest> requests = new HashSet<>();
 
     for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) {
       requests.add(getRequest(propertyMap));
@@ -189,7 +233,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
     });
 
     Set<String>   requestedIds = getRequestPropertyIds(request, predicate);
-    Set<Resource> resources    = new HashSet<Resource>();
+    Set<Resource> resources    = new HashSet<>();
 
     for (ServiceResponse response : responses) {
       Resource resource = new ResourceImpl(Resource.Type.Service);
@@ -207,6 +251,19 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
       setResourceProperty(resource, SERVICE_CREDENTIAL_STORE_ENABLED_PROPERTY_ID,
           String.valueOf(response.isCredentialStoreEnabled()), requestedIds);
 
+      RepositoryVersionEntity repoVersion = repositoryVersionDAO.findByPK(response.getDesiredRepositoryVersionId());
+
+      // !!! TODO is the UI using this?
+      if (null != repoVersion) {
+        setResourceProperty(resource, SERVICE_DESIRED_STACK_PROPERTY_ID, repoVersion.getStackId(), requestedIds);
+      }
+
+      setResourceProperty(resource, SERVICE_DESIRED_REPO_VERSION_ID_PROPERTY_ID,
+          response.getDesiredRepositoryVersionId(), requestedIds);
+
+      setResourceProperty(resource, SERVICE_REPOSITORY_STATE,
+          response.getRepositoryVersionState(), requestedIds);
+
       Map<String, Object> serviceSpecificProperties = getServiceSpecificProperties(
           response.getClusterName(), response.getServiceName(), requestedIds);
 
@@ -243,7 +300,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
   protected RequestStatus deleteResourcesAuthorized(Request request, Predicate predicate)
       throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
 
-    final Set<ServiceRequest> requests = new HashSet<ServiceRequest>();
+    final Set<ServiceRequest> requests = new HashSet<>();
     for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) {
       requests.add(getRequest(propertyMap));
     }
@@ -265,7 +322,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
     if (propertyIds.isEmpty()) {
       return propertyIds;
     }
-    Set<String> unsupportedProperties = new HashSet<String>();
+    Set<String> unsupportedProperties = new HashSet<>();
 
     for (String propertyId : propertyIds) {
       if (!propertyId.equals("config")) {
@@ -291,7 +348,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
   private RequestStageContainer doUpdateResources(final RequestStageContainer stages, final Request request, Predicate predicate)
       throws UnsupportedPropertyException, SystemException, NoSuchResourceException, NoSuchParentResourceException {
 
-    final Set<ServiceRequest> requests = new HashSet<ServiceRequest>();
+    final Set<ServiceRequest> requests = new HashSet<>();
     RequestStageContainer requestStages = null;
 
     Iterator<Map<String,Object>> iterator = request.getProperties().iterator();
@@ -328,9 +385,13 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
    * @return the service request object
    */
   private ServiceRequest getRequest(Map<String, Object> properties) {
+
+    String desiredRepoId = (String) properties.get(SERVICE_DESIRED_REPO_VERSION_ID_PROPERTY_ID);
+
     ServiceRequest svcRequest = new ServiceRequest(
         (String) properties.get(SERVICE_CLUSTER_NAME_PROPERTY_ID),
         (String) properties.get(SERVICE_SERVICE_NAME_PROPERTY_ID),
+        null == desiredRepoId ? null : Long.valueOf(desiredRepoId),
         (String) properties.get(SERVICE_SERVICE_STATE_PROPERTY_ID),
         (String) properties.get(SERVICE_CREDENTIAL_STORE_ENABLED_PROPERTY_ID));
 
@@ -362,14 +423,19 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
     for (ServiceRequest request : requests) {
       Cluster cluster = clusters.getCluster(request.getClusterName());
 
-      // Already checked that service does not exist
-      Service s = cluster.addService(request.getServiceName());
+      RepositoryVersionEntity repositoryVersion = request.getResolvedRepository();
+
+      if (null == repositoryVersion) {
+        throw new AmbariException("Could not find any repository on the request.");
+      }
+
+      Service s = cluster.addService(request.getServiceName(), repositoryVersion);
 
-      /**
+      /*
        * Get the credential_store_supported field only from the stack definition.
        * Not possible to update the value through a request.
        */
-      StackId stackId = cluster.getDesiredStackVersion();
+      StackId stackId = repositoryVersion.getStackId();
       AmbariMetaInfo ambariMetaInfo = getManagementController().getAmbariMetaInfo();
       ServiceInfo serviceInfo = ambariMetaInfo.getService(stackId.getStackName(),
           stackId.getStackVersion(), request.getServiceName());
@@ -379,7 +445,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
 
       LOG.info("Service: {}, credential_store_supported = {} and credential_store_required = {} from stack definition",
                request.getServiceName(), credentialStoreSupported, credentialStoreRequired);
-      /**
+      /*
        * If request does not have credential_store_enabled field,
        * then get the default from the stack definition.
        */
@@ -407,7 +473,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
   // Get services from the given set of requests.
   protected Set<ServiceResponse> getServices(Set<ServiceRequest> requests)
       throws AmbariException {
-    Set<ServiceResponse> response = new HashSet<ServiceResponse>();
+    Set<ServiceResponse> response = new HashSet<>();
     for (ServiceRequest request : requests) {
       try {
         response.addAll(getServices(request));
@@ -440,7 +506,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
       throw new ParentObjectNotFoundException("Parent Cluster resource doesn't exist", e);
     }
 
-    Set<ServiceResponse> response = new HashSet<ServiceResponse>();
+    Set<ServiceResponse> response = new HashSet<>();
     if (request.getServiceName() != null) {
       Service s = cluster.getService(request.getServiceName());
       response.add(s.convertToResponse());
@@ -485,17 +551,17 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
     }
 
     Map<State, List<Service>> changedServices
-        = new EnumMap<State, List<Service>>(State.class);
+        = new EnumMap<>(State.class);
     Map<State, List<ServiceComponent>> changedComps =
-        new EnumMap<State, List<ServiceComponent>>(State.class);
+      new EnumMap<>(State.class);
     Map<String, Map<State, List<ServiceComponentHost>>> changedScHosts =
-        new HashMap<String, Map<State, List<ServiceComponentHost>>>();
+      new HashMap<>();
     Collection<ServiceComponentHost> ignoredScHosts =
-        new ArrayList<ServiceComponentHost>();
+      new ArrayList<>();
 
-    Set<String> clusterNames = new HashSet<String>();
-    Map<String, Set<String>> serviceNames = new HashMap<String, Set<String>>();
-    Set<State> seenNewStates = new HashSet<State>();
+    Set<String> clusterNames = new HashSet<>();
+    Map<String, Set<String>> serviceNames = new HashMap<>();
+    Set<State> seenNewStates = new HashSet<>();
     Map<Service, Boolean> serviceCredentialStoreEnabledMap = new HashMap<>();
 
     // Determine operation level
@@ -515,8 +581,6 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
 
     // We don't expect batch requests for different clusters, that's why
     // nothing bad should happen if value is overwritten few times
-    String maintenanceCluster = null;
-
     for (ServiceRequest request : requests) {
       if (request.getClusterName() == null
           || request.getClusterName().isEmpty()
@@ -529,7 +593,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
       LOG.info("Received a updateService request"
           + ", clusterName=" + request.getClusterName()
           + ", serviceName=" + request.getServiceName()
-          + ", request=" + request.toString());
+          + ", request=" + request);
 
       clusterNames.add(request.getClusterName());
 
@@ -541,6 +605,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
       if (!serviceNames.containsKey(request.getClusterName())) {
         serviceNames.put(request.getClusterName(), new HashSet<String>());
       }
+
       if (serviceNames.get(request.getClusterName())
           .contains(request.getServiceName())) {
         // TODO throw single exception
@@ -563,7 +628,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
 
       // Setting Maintenance state for service
       if (null != request.getMaintenanceState()) {
-        if(!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, cluster.getResourceId(), RoleAuthorization.SERVICE_TOGGLE_MAINTENANCE)) {
+        if (!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, cluster.getResourceId(), RoleAuthorization.SERVICE_TOGGLE_MAINTENANCE)) {
           throw new AuthorizationException("The authenticated user is not authorized to toggle the maintainence state of services");
         }
 
@@ -575,17 +640,16 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
               "maintenance state to one of " + EnumSet.of(MaintenanceState.OFF, MaintenanceState.ON));
           } else {
             s.setMaintenanceState(newMaint);
-            maintenanceCluster = cluster.getClusterName();
           }
         }
       }
 
-      /**
+      /*
        * Get the credential_store_supported field only from the stack definition during creation.
        * Not possible to update the value of credential_store_supported through a request.
        */
 
-      /**
+      /*
        * Gather the credential_store_enabled field per service.
        */
       if (StringUtils.isNotEmpty(request.getCredentialStoreEnabled())) {
@@ -610,10 +674,8 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
 
       if (newState == null) {
         if (LOG.isDebugEnabled()) {
-          LOG.debug("Nothing to do for new updateService request"
-              + ", clusterName=" + request.getClusterName()
-              + ", serviceName=" + request.getServiceName()
-              + ", newDesiredState=null");
+          LOG.debug("Nothing to do for new updateService request, clusterName={}, serviceName={}, newDesiredState=null",
+            request.getClusterName(), request.getServiceName());
         }
         continue;
       }
@@ -657,7 +719,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
     }
 
     if (startDependencies && changedServices.containsKey(State.STARTED)) {
-      HashSet<Service> depServices = new HashSet<Service>();
+      HashSet<Service> depServices = new HashSet<>();
       for (Service service : changedServices.get(State.STARTED)) {
         RoleCommandOrder rco = controller.getRoleCommandOrder(service.getCluster());
         Set<Service> dependencies = rco.getTransitiveServices(service, RoleCommand.START);
@@ -667,6 +729,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
           }
         }
       }
+
       for (Service service : depServices) {
         updateServiceComponents(requestStages, changedComps, changedScHosts,
           ignoredScHosts, reqOpLvl, service, State.STARTED);
@@ -688,6 +751,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
       service.setCredentialStoreEnabled(credentialStoreEnabled);
     }
 
+
     Cluster cluster = clusters.getCluster(clusterNames.iterator().next());
 
     return controller.addStages(requestStages, cluster, requestProperties,
@@ -730,12 +794,8 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
         changedComps.get(newState).add(sc);
       }
       if (LOG.isDebugEnabled()) {
-        LOG.debug("Handling update to ServiceComponent"
-            + ", clusterName=" + cluster.getClusterName()
-            + ", serviceName=" + service.getName()
-            + ", componentName=" + sc.getName()
-            + ", currentDesiredState=" + oldScState
-            + ", newDesiredState=" + newState);
+        LOG.debug("Handling update to ServiceComponent, clusterName={}, serviceName={}, componentName={}, currentDesiredState={}, newDesiredState={}",
+          cluster.getClusterName(), service.getName(), sc.getName(), oldScState, newState);
       }
 
       for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
@@ -743,13 +803,8 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
         if (oldSchState == State.DISABLED || oldSchState == State.UNKNOWN) {
           //Ignore host components updates in this state
           if (LOG.isDebugEnabled()) {
-            LOG.debug("Ignoring ServiceComponentHost"
-                + ", clusterName=" + cluster.getClusterName()
-                + ", serviceName=" + service.getName()
-                + ", componentName=" + sc.getName()
-                + ", hostname=" + sch.getHostName()
-                + ", currentState=" + oldSchState
-                + ", newDesiredState=" + newState);
+            LOG.debug("Ignoring ServiceComponentHost, clusterName={}, serviceName={}, componentName={}, hostname={}, currentState={}, newDesiredState={}",
+              cluster.getClusterName(), service.getName(), sc.getName(), sch.getHostName(), oldSchState, newState);
           }
           continue;
         }
@@ -757,13 +812,8 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
         if (newState == oldSchState) {
           ignoredScHosts.add(sch);
           if (LOG.isDebugEnabled()) {
-            LOG.debug("Ignoring ServiceComponentHost"
-                + ", clusterName=" + cluster.getClusterName()
-                + ", serviceName=" + service.getName()
-                + ", componentName=" + sc.getName()
-                + ", hostname=" + sch.getHostName()
-                + ", currentState=" + oldSchState
-                + ", newDesiredState=" + newState);
+            LOG.debug("Ignoring ServiceComponentHost, clusterName={}, serviceName={}, componentName={}, hostname={}, currentState={}, newDesiredState={}",
+              cluster.getClusterName(), service.getName(), sc.getName(), sch.getHostName(), oldSchState, newState);
           }
           continue;
         }
@@ -771,11 +821,8 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
         if (! maintenanceStateHelper.isOperationAllowed(reqOpLvl, sch)) {
           ignoredScHosts.add(sch);
           if (LOG.isDebugEnabled()) {
-            LOG.debug("Ignoring ServiceComponentHost"
-                + ", clusterName=" + cluster.getClusterName()
-                + ", serviceName=" + service.getName()
-                + ", componentName=" + sc.getName()
-                + ", hostname=" + sch.getHostName());
+            LOG.debug("Ignoring ServiceComponentHost, clusterName={}, serviceName={}, componentName={}, hostname={}",
+              cluster.getClusterName(), service.getName(), sc.getName(), sch.getHostName());
           }
           continue;
         }
@@ -784,7 +831,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
             !newState.isValidClientComponentState()) {
           continue;
         }
-        /**
+        /*
          * This is hack for now wherein we don't fail if the
          * sch is in INSTALL_FAILED
          */
@@ -798,7 +845,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
               + ", hostname=" + sch.getHostName()
               + ", currentState=" + oldSchState
               + ", newDesiredState=" + newState;
-          StackId sid = cluster.getDesiredStackVersion();
+          StackId sid = service.getDesiredStackId();
 
           if ( ambariMetaInfo.getComponent(
               sid.getStackName(), sid.getStackVersion(), sc.getServiceName(),
@@ -818,13 +865,8 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
               new ArrayList<ServiceComponentHost>());
         }
         if (LOG.isDebugEnabled()) {
-          LOG.debug("Handling update to ServiceComponentHost"
-              + ", clusterName=" + cluster.getClusterName()
-              + ", serviceName=" + service.getName()
-              + ", componentName=" + sc.getName()
-              + ", hostname=" + sch.getHostName()
-              + ", currentState=" + oldSchState
-              + ", newDesiredState=" + newState);
+          LOG.debug("Handling update to ServiceComponentHost, clusterName={}, serviceName={}, componentName={}, hostname={}, currentState={}, newDesiredState={}",
+            cluster.getClusterName(), service.getName(), sc.getName(), sch.getHostName(), oldSchState, newState);
         }
         changedScHosts.get(sc.getName()).get(newState).add(sch);
       }
@@ -837,7 +879,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
 
     Clusters clusters    = getManagementController().getClusters();
 
-    Set<Service> removable = new HashSet<Service>();
+    Set<Service> removable = new HashSet<>();
 
     for (ServiceRequest serviceRequest : request) {
       if (StringUtils.isEmpty(serviceRequest.getClusterName()) || StringUtils.isEmpty(serviceRequest.getServiceName())) {
@@ -928,14 +970,14 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
    * @param requestedIds relevant request property ids
    */
   private Map<String, Object> getServiceSpecificProperties(String clusterName, String serviceName, Set<String> requestedIds) {
-    Map<String, Object> serviceSpecificProperties = new HashMap<String, Object>();
+    Map<String, Object> serviceSpecificProperties = new HashMap<>();
     if (serviceName.equals("KERBEROS")) {
       // Only include details on whether the KDC administrator credentials are set and correct if
       // implicitly (Service/attributes) or explicitly (Service/attributes/kdc_...) queried
       if (requestedIds.contains(SERVICE_ATTRIBUTES_PROPERTY_ID) ||
           isPropertyCategoryRequested(SERVICE_ATTRIBUTES_PROPERTY_ID, requestedIds) ||
           isPropertyEntryRequested(SERVICE_ATTRIBUTES_PROPERTY_ID, requestedIds)) {
-        Map<String, String> kerberosAttributes = new HashMap<String, String>();
+        Map<String, String> kerberosAttributes = new HashMap<>();
         String kdcValidationResult = "OK";
         String failureDetails = "";
         try {
@@ -971,6 +1013,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
     AmbariMetaInfo ambariMetaInfo = getManagementController().getAmbariMetaInfo();
     Map<String, Set<String>> serviceNames = new HashMap<>();
     Set<String> duplicates = new HashSet<>();
+
     for (ServiceRequest request : requests) {
       final String clusterName = request.getClusterName();
       final String serviceName = request.getServiceName();
@@ -978,8 +1021,7 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
       Validate.notEmpty(serviceName, "Service name should be provided when creating a service");
 
       if (LOG.isDebugEnabled()) {
-        LOG.debug("Received a createService request"
-                + ", clusterName=" + clusterName + ", serviceName=" + serviceName + ", request=" + request);
+        LOG.debug("Received a createService request, clusterName={}, serviceName={}, request={}", clusterName, serviceName, request);
       }
 
       if(!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, getClusterResourceId(clusterName), RoleAuthorization.SERVICE_ADD_DELETE_SERVICES)) {
@@ -1023,7 +1065,21 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider
         // Expected
       }
 
-      StackId stackId = cluster.getDesiredStackVersion();
+      Long desiredRepositoryVersion = request.getDesiredRepositoryVersionId();
+      if (null == desiredRepositoryVersion) {
+        throw new IllegalArgumentException(String.format("%s is required when adding a service.", SERVICE_DESIRED_REPO_VERSION_ID_PROPERTY_ID));
+      }
+
+      RepositoryVersionEntity repositoryVersion = repositoryVersionDAO.findByPK(desiredRepositoryVersion);
+
+      if (null == repositoryVersion) {
+        throw new IllegalArgumentException(String.format("Could not find any repositories defined by %d", desiredRepositoryVersion));
+      }
+
+      StackId stackId = repositoryVersion.getStackId();
+
+      request.setResolvedRepository(repositoryVersion);
+
       if (!ambariMetaInfo.isValidService(stackId.getStackName(),
               stackId.getStackVersion(), request.getServiceName())) {
         throw new IllegalArgumentException("Unsupported or invalid service in stack, clusterName=" + clusterName


[17/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog2402.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog2402.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog2402.java
deleted file mode 100644
index 4e54888..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog2402.java
+++ /dev/null
@@ -1,207 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import com.google.common.collect.Lists;
-import com.google.common.reflect.TypeToken;
-import com.google.gson.Gson;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-import com.google.gson.JsonPrimitive;
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import com.google.inject.persist.Transactional;
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.agent.RecoveryConfigHelper;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
-import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
-import org.apache.ambari.server.orm.dao.ArtifactDAO;
-import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.PermissionDAO;
-import org.apache.ambari.server.orm.dao.PrincipalDAO;
-import org.apache.ambari.server.orm.dao.PrincipalTypeDAO;
-import org.apache.ambari.server.orm.dao.PrivilegeDAO;
-import org.apache.ambari.server.orm.dao.RemoteAmbariClusterDAO;
-import org.apache.ambari.server.orm.dao.RequestScheduleDAO;
-import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
-import org.apache.ambari.server.orm.dao.RoleAuthorizationDAO;
-import org.apache.ambari.server.orm.dao.UserDAO;
-import org.apache.ambari.server.orm.dao.ViewInstanceDAO;
-import org.apache.ambari.server.orm.dao.WidgetDAO;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.ArtifactEntity;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.PermissionEntity;
-import org.apache.ambari.server.orm.entities.PrincipalEntity;
-import org.apache.ambari.server.orm.entities.PrincipalTypeEntity;
-import org.apache.ambari.server.orm.entities.PrivilegeEntity;
-import org.apache.ambari.server.orm.entities.RemoteAmbariClusterEntity;
-import org.apache.ambari.server.orm.entities.RequestScheduleEntity;
-import org.apache.ambari.server.orm.entities.ResourceEntity;
-import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
-import org.apache.ambari.server.orm.entities.RoleAuthorizationEntity;
-import org.apache.ambari.server.orm.entities.UserEntity;
-import org.apache.ambari.server.orm.entities.ViewEntityEntity;
-import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
-import org.apache.ambari.server.orm.entities.WidgetEntity;
-import org.apache.ambari.server.security.authorization.ResourceType;
-import org.apache.ambari.server.security.authorization.User;
-import org.apache.ambari.server.security.authorization.Users;
-import org.apache.ambari.server.state.AlertFirmness;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.ConfigHelper;
-import org.apache.ambari.server.state.PropertyInfo;
-import org.apache.ambari.server.state.RepositoryType;
-import org.apache.ambari.server.state.SecurityType;
-import org.apache.ambari.server.state.ServiceInfo;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.StackInfo;
-import org.apache.ambari.server.state.State;
-import org.apache.ambari.server.state.kerberos.KerberosComponentDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptorFactory;
-import org.apache.ambari.server.state.kerberos.KerberosIdentityDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosKeytabDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosPrincipalDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosServiceDescriptor;
-import org.apache.ambari.server.state.stack.WidgetLayout;
-import org.apache.ambari.server.state.stack.WidgetLayoutInfo;
-import org.apache.ambari.server.view.DefaultMasker;
-import org.apache.ambari.view.ClusterType;
-import org.apache.ambari.view.MaskException;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.jdbc.support.JdbcUtils;
-
-import javax.persistence.EntityManager;
-import javax.persistence.Query;
-import javax.persistence.TypedQuery;
-import java.io.File;
-import java.io.FileReader;
-import java.lang.reflect.Type;
-import java.sql.Clob;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.UUID;
-import java.util.concurrent.atomic.AtomicLong;
-
-/**
- * Upgrade catalog for version 2.4.0.
- */
-public class UpgradeCatalog2402 extends AbstractUpgradeCatalog {
-
-  @Inject
-  ClusterDAO clusterDAO;
-
-  @Inject
-  Configuration config;
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog2402.class);
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   *
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog2402(Injector injector) {
-    super(injector);
-    injector.injectMembers(this);
-  }
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.4.0.2";
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getSourceVersion() {
-    return "2.4.0";
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executePreDMLUpdates() throws AmbariException, SQLException {
-
-  }
-
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    updateKafkaWidgetDefinition();
-  }
-
-
-  protected void updateKafkaWidgetDefinition() throws AmbariException {
-    LOG.info("Updating Kafka widget definition.");
-
-    Map<String, List<String>> widgetMap = new HashMap<>();
-    Map<String, String> sectionLayoutMap = new HashMap<>();
-
-    List<String> kafkaSummaryWidgets = new ArrayList<>(Arrays.asList("Active Controller Count"));
-    widgetMap.put("KAFKA_SUMMARY", kafkaSummaryWidgets);
-    sectionLayoutMap.put("KAFKA_SUMMARY", "default_kafka_dashboard");
-
-    updateWidgetDefinitionsForService("KAFKA", widgetMap, sectionLayoutMap);
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog242.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog242.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog242.java
deleted file mode 100644
index 6f927ab..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog242.java
+++ /dev/null
@@ -1,272 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import java.sql.SQLException;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.dao.PermissionDAO;
-import org.apache.ambari.server.orm.dao.PrincipalDAO;
-import org.apache.ambari.server.orm.dao.PrincipalTypeDAO;
-import org.apache.ambari.server.orm.dao.PrivilegeDAO;
-import org.apache.ambari.server.orm.entities.PermissionEntity;
-import org.apache.ambari.server.orm.entities.PrincipalEntity;
-import org.apache.ambari.server.orm.entities.PrincipalTypeEntity;
-import org.apache.ambari.server.orm.entities.PrivilegeEntity;
-import org.apache.ambari.server.orm.entities.ResourceEntity;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import com.google.inject.persist.Transactional;
-
-/**
- * Upgrade catalog for version 2.4.2.
- */
-public class UpgradeCatalog242 extends AbstractUpgradeCatalog {
-
-  protected static final String EXTENSION_TABLE = "extension";
-  protected static final String USERS_TABLE = "users";
-  protected static final String HOST_ROLE_COMMAND_TABLE = "host_role_command";
-  protected static final String BLUEPRINT_TABLE = "blueprint";
-  protected static final String HOST_GROUP_TABLE = "hostgroup";
-  protected static final String BLUEPRINT_CONFIGURATION = "blueprint_configuration";
-  protected static final String BLUEPRINT_SETTING = "blueprint_setting";
-  protected static final String HOSTGROUP_COMPONENT = "hostgroup_component";
-  protected static final String HOSTGROUP_CONFIGURATION = "hostgroup_configuration";
-
-  protected static final String BLUEPRINT_NAME_COLUMN = "blueprint_name";
-  protected static final String EXTENSION_NAME_COLUMN = "extension_name";
-  protected static final String EXTENSION_VERSION_COLUMN = "extension_version";
-  protected static final String USER_TYPE_COLUMN = "user_type";
-  protected static final String USER_NAME_COLUMN = "user_name";
-  protected static final String ROLE_COLUMN = "role";
-  protected static final String STATUS_COLUMN = "status";
-  protected static final String NAME_COLUMN = "name";
-
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog242.class);
-
-
-
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   *
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog242(Injector injector) {
-    super(injector);
-    this.injector = injector;
-  }
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.4.2";
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getSourceVersion() {
-    return "2.4.0.2";
-  }
-
-
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-    updateTablesForMysql();
-  }
-
-  @Override
-  protected void executePreDMLUpdates() throws AmbariException, SQLException {
-    //To change body of implemented methods use File | Settings | File Templates.
-  }
-
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    addNewConfigurationsFromXml();
-    createRoleAuthorizations();
-    convertRolePrincipals();
-  }
-
-  /**
-   * Create new role authorizations: CLUSTER.RUN_CUSTOM_COMMAND and AMBARI.RUN_CUSTOM_COMMAND
-   *
-   * @throws SQLException
-   */
-  @Transactional
-  protected void createRoleAuthorizations() throws SQLException {
-    addRoleAuthorization("CLUSTER.RUN_CUSTOM_COMMAND", "Perform custom cluster-level actions",
-        Arrays.asList("AMBARI.ADMINISTRATOR:AMBARI", "CLUSTER.ADMINISTRATOR:CLUSTER"));
-
-    addRoleAuthorization("AMBARI.RUN_CUSTOM_COMMAND", "Perform custom administrative actions",
-        Collections.singletonList("AMBARI.ADMINISTRATOR:AMBARI"));
-  }
-
-  protected void updateTablesForMysql() throws SQLException {
-    final Configuration.DatabaseType databaseType = configuration.getDatabaseType();
-    if (databaseType == Configuration.DatabaseType.MYSQL) {
-      dbAccessor.alterColumn(EXTENSION_TABLE, new DBAccessor.DBColumnInfo(EXTENSION_NAME_COLUMN, String.class, 100, null, false));
-      dbAccessor.alterColumn(EXTENSION_TABLE, new DBAccessor.DBColumnInfo(EXTENSION_VERSION_COLUMN, String.class, 100, null, false));
-
-      dbAccessor.alterColumn(USERS_TABLE, new DBAccessor.DBColumnInfo(USER_TYPE_COLUMN, String.class, 100, null, false));
-      dbAccessor.alterColumn(USERS_TABLE, new DBAccessor.DBColumnInfo(USER_NAME_COLUMN, String.class, 100, null, false));
-
-      dbAccessor.alterColumn(HOST_ROLE_COMMAND_TABLE, new DBAccessor.DBColumnInfo(ROLE_COLUMN, String.class, 100, null, true));
-      dbAccessor.alterColumn(HOST_ROLE_COMMAND_TABLE, new DBAccessor.DBColumnInfo(STATUS_COLUMN, String.class, 100, null, true));
-
-      dbAccessor.dropFKConstraint(HOST_GROUP_TABLE, "FK_hg_blueprint_name");
-
-      dbAccessor.dropFKConstraint(HOST_GROUP_TABLE, "FK_hostgroup_blueprint_name");
-
-      dbAccessor.dropFKConstraint(BLUEPRINT_CONFIGURATION, "FK_cfg_blueprint_name");
-
-      dbAccessor.dropFKConstraint(BLUEPRINT_CONFIGURATION, "FK_blueprint_configuration_blueprint_name");
-
-      dbAccessor.dropFKConstraint(BLUEPRINT_SETTING, "FK_blueprint_setting_blueprint_name");
-
-      dbAccessor.dropFKConstraint(BLUEPRINT_SETTING, "FK_blueprint_setting_name");
-
-      dbAccessor.alterColumn(BLUEPRINT_TABLE, new DBAccessor.DBColumnInfo(BLUEPRINT_NAME_COLUMN, String.class, 100, null, false));
-
-      String[] uniqueColumns1 = new String[] { BLUEPRINT_NAME_COLUMN };
-
-      dbAccessor.addFKConstraint(HOST_GROUP_TABLE, "FK_hg_blueprint_name",
-              uniqueColumns1, BLUEPRINT_TABLE, uniqueColumns1, false);
-
-      dbAccessor.addFKConstraint(BLUEPRINT_CONFIGURATION, "FK_cfg_blueprint_name",
-              uniqueColumns1, BLUEPRINT_TABLE, uniqueColumns1, false);
-
-      dbAccessor.addFKConstraint(BLUEPRINT_SETTING, "FK_blueprint_setting_name",
-              uniqueColumns1, BLUEPRINT_TABLE, uniqueColumns1, false);
-    }
-  }
-
-  /**
-   * Convert the previously set inherited privileges to the more generic inherited privileges model
-   * based on role-based principals rather than specialized principal types.
-   */
-  @Transactional
-  void convertRolePrincipals() {
-    LOG.info("Converting pseudo principle types to role principals");
-
-    PermissionDAO permissionDAO = injector.getInstance(PermissionDAO.class);
-    PrivilegeDAO privilegeDAO = injector.getInstance(PrivilegeDAO.class);
-    PrincipalDAO principalDAO = injector.getInstance(PrincipalDAO.class);
-    PrincipalTypeDAO principalTypeDAO = injector.getInstance(PrincipalTypeDAO.class);
-
-    Map<String, String> principalTypeToRole = new HashMap<String, String>();
-    principalTypeToRole.put("ALL.CLUSTER.ADMINISTRATOR", "CLUSTER.ADMINISTRATOR");
-    principalTypeToRole.put("ALL.CLUSTER.OPERATOR", "CLUSTER.OPERATOR");
-    principalTypeToRole.put("ALL.CLUSTER.USER", "CLUSTER.USER");
-    principalTypeToRole.put("ALL.SERVICE.ADMINISTRATOR", "SERVICE.ADMINISTRATOR");
-    principalTypeToRole.put("ALL.SERVICE.OPERATOR", "SERVICE.OPERATOR");
-
-    // Handle a typo introduced in org.apache.ambari.server.upgrade.UpgradeCatalog240.updateClusterInheritedPermissionsConfig
-    principalTypeToRole.put("ALL.SERVICE.OPERATIOR", "SERVICE.OPERATOR");
-
-    for (Map.Entry<String, String> entry : principalTypeToRole.entrySet()) {
-      String principalTypeName = entry.getKey();
-      String roleName = entry.getValue();
-
-      PermissionEntity role = permissionDAO.findByName(roleName);
-      PrincipalEntity rolePrincipalEntity = (role == null) ? null : role.getPrincipal();
-
-      // Convert Privilege Records
-      PrincipalTypeEntity principalTypeEntity = principalTypeDAO.findByName(principalTypeName);
-
-      if (principalTypeEntity != null) {
-        List<PrincipalEntity> principalEntities = principalDAO.findByPrincipalType(principalTypeName);
-
-        for (PrincipalEntity principalEntity : principalEntities) {
-          Set<PrivilegeEntity> privilegeEntities = principalEntity.getPrivileges();
-
-          for (PrivilegeEntity privilegeEntity : privilegeEntities) {
-            if (rolePrincipalEntity == null) {
-              LOG.info("Removing privilege (id={}) since no role principle was found for {}:\n{}",
-                  privilegeEntity.getId(), roleName, formatPrivilegeEntityDetails(privilegeEntity));
-              // Remove this privilege
-              privilegeDAO.remove(privilegeEntity);
-            } else {
-              LOG.info("Updating privilege (id={}) to use role principle for {}:\n{}",
-                  privilegeEntity.getId(), roleName, formatPrivilegeEntityDetails(privilegeEntity));
-
-              // Set the principal to the updated principal value
-              privilegeEntity.setPrincipal(rolePrincipalEntity);
-              privilegeDAO.merge(privilegeEntity);
-            }
-          }
-
-          // Remove the obsolete principal
-          principalDAO.remove(principalEntity);
-        }
-
-        // Remove the obsolete principal type
-        principalTypeDAO.remove(principalTypeEntity);
-      }
-    }
-
-    LOG.info("Converting pseudo principle types to role principals - complete.");
-  }
-
-  private String formatPrivilegeEntityDetails(PrivilegeEntity privilegeEntity) {
-    if (privilegeEntity == null) {
-      return "";
-    } else {
-      ResourceEntity resource = privilegeEntity.getResource();
-      PrincipalEntity principal = privilegeEntity.getPrincipal();
-      PermissionEntity permission = privilegeEntity.getPermission();
-
-      return String.format("" +
-              "\tPrivilege ID: %d" +
-              "\n\tResource ID: %d" +
-              "\n\tPrincipal ID: %d" +
-              "\n\tPermission ID: %d",
-          privilegeEntity.getId(),
-          resource.getId(),
-          principal.getId(),
-          permission.getId()
-      );
-    }
-  }
-}


[08/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java
deleted file mode 100644
index 71bf6b7..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog210Test.java
+++ /dev/null
@@ -1,1345 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import javax.persistence.EntityManager;
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertNull;
-import static junit.framework.Assert.assertTrue;
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.anyString;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.createMockBuilder;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.verify;
-
-import java.io.File;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.net.URL;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-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 org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.H2DatabaseCleaner;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.controller.ServiceConfigVersionResponse;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
-import org.apache.ambari.server.orm.GuiceJpaInitializer;
-import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.ArtifactDAO;
-import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.ClusterStateDAO;
-import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.entities.ArtifactEntity;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
-import org.apache.ambari.server.orm.entities.ClusterStateEntity;
-import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.HostEntity;
-import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.StackEntity;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.ConfigHelper;
-import org.apache.ambari.server.state.Host;
-import org.apache.ambari.server.state.HostComponentAdminState;
-import org.apache.ambari.server.state.Service;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptorFactory;
-import org.apache.ambari.server.state.kerberos.KerberosServiceDescriptor;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.easymock.Capture;
-import org.easymock.CaptureType;
-import org.easymock.EasyMockSupport;
-import org.junit.Assert;
-import org.junit.Test;
-
-import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.Provider;
-
-/**
- * {@link org.apache.ambari.server.upgrade.UpgradeCatalog210} unit tests.
- */
-public class UpgradeCatalog210Test {
-  private Injector injector;
-  private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
-  private EntityManager entityManager = createNiceMock(EntityManager.class);
-  private UpgradeCatalogHelper upgradeCatalogHelper;
-  private StackEntity desiredStackEntity;
-
-  public void initData() {
-    //reset(entityManagerProvider);
-    //expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
-    //replay(entityManagerProvider);
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-
-    upgradeCatalogHelper = injector.getInstance(UpgradeCatalogHelper.class);
-    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
-    injector.getInstance(AmbariMetaInfo.class);
-    // load the stack entity
-    StackDAO stackDAO = injector.getInstance(StackDAO.class);
-    desiredStackEntity = stackDAO.find("HDP", "2.2.0");
-  }
-
-  public void tearDown() throws AmbariException, SQLException {
-    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
-  }
-
-  @Test
-  public void testExecuteDDLUpdates() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    Configuration configuration = createNiceMock(Configuration.class);
-    Connection connection = createNiceMock(Connection.class);
-    Statement statement = createNiceMock(Statement.class);
-    ResultSet resultSet = createNiceMock(ResultSet.class);
-    expect(configuration.getDatabaseUrl()).andReturn(Configuration.JDBC_IN_MEMORY_URL).anyTimes();
-    dbAccessor.getConnection();
-    expectLastCall().andReturn(connection).anyTimes();
-    connection.createStatement();
-    expectLastCall().andReturn(statement).anyTimes();
-    statement.executeQuery(anyObject(String.class));
-    expectLastCall().andReturn(resultSet).anyTimes();
-
-    // Create DDL sections with their own capture groups
-    AlertSectionDDL alertSectionDDL = new AlertSectionDDL();
-    HostSectionDDL hostSectionDDL = new HostSectionDDL();
-    WidgetSectionDDL widgetSectionDDL = new WidgetSectionDDL();
-    ViewSectionDDL viewSectionDDL = new ViewSectionDDL();
-
-    // Execute any DDL schema changes
-    alertSectionDDL.execute(dbAccessor);
-    hostSectionDDL.execute(dbAccessor);
-    widgetSectionDDL.execute(dbAccessor);
-    viewSectionDDL.execute(dbAccessor);
-
-    // Replay sections
-    replay(dbAccessor, configuration, resultSet, connection, statement);
-
-    AbstractUpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
-    Class<?> c = AbstractUpgradeCatalog.class;
-    Field f = c.getDeclaredField("configuration");
-    f.setAccessible(true);
-    f.set(upgradeCatalog, configuration);
-
-    upgradeCatalog.executeDDLUpdates();
-    verify(dbAccessor, configuration, resultSet, connection, statement);
-
-    // Verify sections
-    alertSectionDDL.verify(dbAccessor);
-    hostSectionDDL.verify(dbAccessor);
-    widgetSectionDDL.verify(dbAccessor);
-    viewSectionDDL.verify(dbAccessor);
-  }
-
-  @Test
-  public void testExecutePreDMLUpdates() throws Exception {
-    Method executeStackPreDMLUpdates = UpgradeCatalog210.class.getDeclaredMethod("executeStackPreDMLUpdates");
-    Method cleanupStackUpdates = UpgradeCatalog210.class.getDeclaredMethod("cleanupStackUpdates");
-
-    final UpgradeCatalog210 upgradeCatalog210 = createMockBuilder(UpgradeCatalog210.class)
-        .addMockedMethod(executeStackPreDMLUpdates)
-        .addMockedMethod(cleanupStackUpdates).createMock();
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(UpgradeCatalog210.class).toInstance(upgradeCatalog210);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    upgradeCatalog210.executeStackPreDMLUpdates();
-    expectLastCall().once();
-
-    replay(upgradeCatalog210);
-    mockInjector.getInstance(UpgradeCatalog210.class).executePreDMLUpdates();
-
-    verify(upgradeCatalog210);
-  }
-
-  @Test
-  public void testExecuteDMLUpdates() throws Exception {
-    Method addNewConfigurationsFromXml =
-      AbstractUpgradeCatalog.class.getDeclaredMethod("addNewConfigurationsFromXml");
-
-    Method initializeClusterAndServiceWidgets =
-      UpgradeCatalog210.class.getDeclaredMethod("initializeClusterAndServiceWidgets");
-
-    Method addMissingConfigs = UpgradeCatalog210.class.getDeclaredMethod("addMissingConfigs");
-
-    Method updateAlertDefinitions = UpgradeCatalog210.class.getDeclaredMethod("updateAlertDefinitions");
-
-    Method removeStormRestApiServiceComponent =
-      UpgradeCatalog210.class.getDeclaredMethod("removeStormRestApiServiceComponent");
-
-    Method updateKerberosDescriptorArtifacts =
-      AbstractUpgradeCatalog.class.getDeclaredMethod("updateKerberosDescriptorArtifacts");
-
-    UpgradeCatalog210 upgradeCatalog210 = createMockBuilder(UpgradeCatalog210.class)
-        .addMockedMethod(addNewConfigurationsFromXml)
-        .addMockedMethod(initializeClusterAndServiceWidgets)
-        .addMockedMethod(addMissingConfigs)
-        .addMockedMethod(updateAlertDefinitions)
-        .addMockedMethod(removeStormRestApiServiceComponent)
-        .addMockedMethod(updateKerberosDescriptorArtifacts)
-        .createMock();
-
-    upgradeCatalog210.addNewConfigurationsFromXml();
-    expectLastCall().once();
-
-    upgradeCatalog210.initializeClusterAndServiceWidgets();
-    expectLastCall().once();
-
-    upgradeCatalog210.addMissingConfigs();
-    expectLastCall().once();
-
-    upgradeCatalog210.updateAlertDefinitions();
-    expectLastCall().once();
-
-    upgradeCatalog210.removeStormRestApiServiceComponent();
-    expectLastCall().once();
-
-    upgradeCatalog210.updateKerberosDescriptorArtifacts();
-    expectLastCall().once();
-
-    replay(upgradeCatalog210);
-
-    upgradeCatalog210.executeDMLUpdates();
-
-    verify(upgradeCatalog210);
-  }
-
-  @Test
-  public void testUpdateRangerHiveConfigs() throws Exception{
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(
-        AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Config mockRangerPlugin = easyMockSupport.createNiceMock(Config.class);
-    final Config mockHiveEnv = easyMockSupport.createNiceMock(Config.class);
-    final Config mockHiveServer = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> propertiesExpectedRangerPlugin = new HashMap<String, String>();
-    propertiesExpectedRangerPlugin.put("ranger-hive-plugin-enabled", "yes");
-    final Map<String, String> propertiesExpectedHiveEnv = new HashMap<String, String>();
-    final Map<String, String> propertiesExpectedHiveServer2 = new HashMap<String, String>();
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    expect(mockClusterExpected.getDesiredConfigByType("ranger-hive-plugin-properties")).andReturn(mockRangerPlugin).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnv).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hiveserver2-site")).andReturn(mockHiveServer).atLeastOnce();
-    expect(mockRangerPlugin.getProperties()).andReturn(propertiesExpectedRangerPlugin).anyTimes();
-    expect(mockHiveEnv.getProperties()).andReturn(propertiesExpectedHiveEnv).anyTimes();
-    expect(mockHiveServer.getProperties()).andReturn(propertiesExpectedHiveServer2).anyTimes();
-
-    ServiceConfigVersionResponse r = null;
-    expect(mockClusterExpected.getConfig(anyObject(String.class), anyObject(String.class))).
-        andReturn(mockHiveServer).anyTimes();
-    expect(mockClusterExpected.addDesiredConfig("ambari-upgrade", Collections.singleton(mockHiveServer))).
-        andReturn(r).times(3);
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog210.class).updateRangerHiveConfigs();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateHiveConfigs() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final ServiceConfigVersionResponse mockServiceConfigVersionResponse = easyMockSupport.createNiceMock(ServiceConfigVersionResponse.class);
-    final Config mockHiveEnv = easyMockSupport.createNiceMock(Config.class);
-    final Config mockHiveServerSite = easyMockSupport.createNiceMock(Config.class);
-    final Config mockHiveSite = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> propertiesExpectedHiveEnv = new HashMap<String, String>() {{
-      put("hive_security_authorization", "none");
-    }};
-    final Map<String, String> propertiesExpectedHiveSite = new HashMap<String, String>() {{
-      put("hive.server2.authentication", "pam");
-      put("hive.server2.custom.authentication.class", "");
-    }};
-    final Map<String, String> propertiesExpectedHiveServerSite = new HashMap<String, String>() {{
-      put("hive.security.authorization.manager", "");
-      put("hive.security.authenticator.manager", "");
-    }};
-    final Map<String, Service> servicesExpected = new HashMap<String, Service>();
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    Capture<String> configTypeEnv = new Capture<String>();
-    Capture<String> configTypeSite = new Capture<String>();
-    Capture<String> configTypeServerSite = new Capture<String>();
-
-    expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnv).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hiveserver2-site")).andReturn(mockHiveServerSite).atLeastOnce();
-    expect(mockHiveEnv.getProperties()).andReturn(propertiesExpectedHiveEnv).anyTimes();
-    expect(mockHiveServerSite.getProperties()).andReturn(propertiesExpectedHiveServerSite).anyTimes();
-    expect(mockClusterExpected.getConfig(capture(configTypeEnv), anyObject(String.class))).andReturn(mockHiveEnv).once();
-    expect(mockClusterExpected.getConfig(capture(configTypeServerSite), anyObject(String.class))).andReturn(mockHiveServerSite).once();
-    expect(mockClusterExpected.addDesiredConfig("ambari-upgrade", Collections.singleton(mockHiveEnv))).andReturn(mockServiceConfigVersionResponse).once();
-    expect(mockClusterExpected.addDesiredConfig("ambari-upgrade", Collections.singleton(mockHiveServerSite))).andReturn(mockServiceConfigVersionResponse).once();
-
-    expect(mockClusterExpected.getDesiredConfigByType("hive-site")).andReturn(mockHiveSite).atLeastOnce();
-    expect(mockHiveSite.getProperties()).andReturn(propertiesExpectedHiveSite).anyTimes();
-    expect(mockClusterExpected.getServices()).andReturn(servicesExpected).once();
-    expect(mockClusterExpected.getConfig(capture(configTypeSite), anyObject(String.class))).andReturn(mockHiveSite).once();
-    expect(mockClusterExpected.addDesiredConfig("ambari-upgrade", Collections.singleton(mockHiveSite))).andReturn(mockServiceConfigVersionResponse).once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog210.class).updateHiveConfigs();
-    easyMockSupport.verifyAll();
-
-    assertEquals("hive-env", configTypeEnv.getValue());
-    assertEquals("hive-site", configTypeSite.getValue());
-    assertEquals("hiveserver2-site", configTypeServerSite.getValue());
-  }
-
-  @Test
-  public void TestRangerSitePropertyConversion() throws Exception{
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final String clusterName = "c1";
-    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createStrictMock(Cluster.class);
-    final Config config = easyMockSupport.createNiceMock(Config.class);
-    final Map<String,Cluster> clusters = new HashMap<String,Cluster>(){{
-      put(clusterName, cluster);
-    }};
-    final Map<String,String> properties = new HashMap<String, String>() {{
-      put("HTTPS_CLIENT_AUTH", "test123");
-      put("HTTPS_KEYSTORE_FILE", "test123");
-      put("HTTPS_KEYSTORE_PASS", "test123");
-      put("HTTPS_KEY_ALIAS", "test123");
-      put("HTTPS_SERVICE_PORT", "test123");
-      put("HTTP_ENABLED", "test123");
-      put("HTTP_SERVICE_PORT", "test123");
-    }};
-
-    final Map<String, String> expectedPropertyMap = new HashMap<String, String>() {{
-      put("HTTPS_CLIENT_AUTH", "https.attrib.clientAuth");
-      put("HTTPS_KEYSTORE_FILE", "https.attrib.keystoreFile");
-      put("HTTPS_KEYSTORE_PASS", "https.attrib.keystorePass");
-      put("HTTPS_KEY_ALIAS", "https.attrib.keyAlias");
-      put("HTTP_SERVICE_PORT", "http.service.port");
-      put("HTTPS_SERVICE_PORT", "https.service.port");
-      put("HTTP_ENABLED", "http.enabled");
-    }};
-
-    final Map<String,String> convertedProperties = new HashMap<>();
-    final Set<String> removedProperties = new HashSet<>();
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    UpgradeCatalog210 upgradeCatalog210 = new UpgradeCatalog210(mockInjector) {
-
-      @Override
-      protected void updateConfigurationPropertiesForCluster(Cluster cluster, String configType,
-        Map<String, String> properties, boolean updateIfExists, boolean createNewConfigType) throws AmbariException {
-        convertedProperties.putAll(properties);
-      }
-
-      @Override
-      protected void removeConfigurationPropertiesFromCluster(Cluster cluster, String configType, Set<String> removePropertiesList)
-        throws AmbariException {
-        removedProperties.addAll(removePropertiesList);
-      }
-    };
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).atLeastOnce();
-    expect(mockClusters.getClusters()).andReturn(clusters).atLeastOnce();
-    expect(config.getProperties()).andReturn(properties).atLeastOnce();
-    expect(cluster.getDesiredConfigByType("ranger-site")).andReturn(config).atLeastOnce();
-
-    replay(mockAmbariManagementController, mockClusters, cluster, config);
-
-    upgradeCatalog210.updateRangerSiteConfigs();
-
-
-    for (Map.Entry<String,String> propertyEntry: expectedPropertyMap.entrySet()){
-      String oldKey = propertyEntry.getKey();
-      String newKey = propertyEntry.getValue();
-      assertTrue(String.format("Old property %s doesn't migrated to new name %s", oldKey, newKey), convertedProperties.containsKey(newKey));
-      assertTrue(String.format("Property value %s doesn't preserved after renaming: %s",properties.get(oldKey), convertedProperties.get(newKey)),
-        convertedProperties.get(newKey).equals(properties.get(oldKey)));
-      assertTrue(String.format("Old property %s doesn't removed after renaming", oldKey), removedProperties.contains(oldKey));
-    }
-  }
-
-  @Test
-  public void testUpdateHiveConfigsWithKerberos() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Config mockHiveEnv = easyMockSupport.createNiceMock(Config.class);
-    final Config mockHiveSite = easyMockSupport.createNiceMock(Config.class);
-    final Config mockHiveServerSite = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> propertiesExpectedHiveEnv = new HashMap<String, String>();
-    final Map<String, String> propertiesExpectedHiveSite = new HashMap<String, String>() {{
-      put("hive.server2.authentication", "kerberos");
-    }};
-    final Map<String, String> propertiesExpectedHiveServerSite = new HashMap<String, String>() {{
-    }};
-    final Map<String, Service> servicesExpected = new HashMap<String, Service>(){{
-      put("KERBEROS", null);
-    }};
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-      }
-    });
-
-    final UpgradeCatalog210 upgradeCatalog210 =  mockInjector.getInstance(UpgradeCatalog210.class);
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    Capture<Map<String,String>> configCreation = Capture.newInstance(CaptureType.ALL);
-
-    expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnv).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hiveserver2-site")).andReturn(mockHiveServerSite).atLeastOnce();
-    expect(mockHiveEnv.getProperties()).andReturn(propertiesExpectedHiveEnv).anyTimes();
-    expect(mockHiveServerSite.getProperties()).andReturn(propertiesExpectedHiveServerSite).anyTimes();
-
-    expect(mockClusterExpected.getDesiredConfigByType("hive-site")).andReturn(mockHiveSite).atLeastOnce();
-    expect(mockHiveSite.getProperties()).andReturn(propertiesExpectedHiveSite).anyTimes();
-    expect(mockClusterExpected.getServices()).andReturn(servicesExpected).atLeastOnce();
-    expect(mockAmbariManagementController.createConfig((Cluster)anyObject(),
-      anyString(),
-      capture(configCreation),
-      anyString(),
-      (Map<String, Map<String, String>>)anyObject())).andReturn(null).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    upgradeCatalog210.updateHiveConfigs();
-    easyMockSupport.verifyAll();
-
-    Assert.assertEquals(2, configCreation.getValues().size());
-
-    boolean hiveSecFound = false;
-
-    for (Map<String, String> cfg: configCreation.getValues()){
-      if (cfg.containsKey("hive_security_authorization")) {
-        hiveSecFound = true;
-        Assert.assertTrue("sqlstdauth".equalsIgnoreCase(cfg.get("hive_security_authorization")));
-        break;
-      }
-    }
-
-    Assert.assertTrue(hiveSecFound);
-  }
-
-  @Test
-  public void testUpdateHiveConfigsWithRangerPlugin() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Config mockHiveEnv = easyMockSupport.createNiceMock(Config.class);
-    final Config mockHiveSite = easyMockSupport.createNiceMock(Config.class);
-    final Config mockHiveServerSite = easyMockSupport.createNiceMock(Config.class);
-    final Config mockHivePluginProperies = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> propertiesExpectedHiveEnv = new HashMap<String, String>() {{
-      put("hive_security_authorization", "none");
-    }};
-    final Map<String, String> propertiesExpectedHiveSite = new HashMap<String, String>() {{
-    }};
-
-    final Map<String, String> propertiesExpectedPluginProperies = new HashMap<String, String>() {{
-      put("ranger-hive-plugin-enabled", "yes");
-    }};
-    final Map<String, String> propertiesExpectedHiveServerSite = new HashMap<String, String>() {{
-      put("hive.security.authorization.manager", "test");
-      put("hive.security.authenticator.manager", "test");
-    }};
-    final Map<String, Service> servicesExpected = new HashMap<String, Service>() {{
-      put("RANGER", null);
-    }};
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-      }
-    });
-
-    final UpgradeCatalog210 upgradeCatalog210 = mockInjector.getInstance(UpgradeCatalog210.class);
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    Capture<Map<String, String>> configCreation = Capture.newInstance(CaptureType.ALL);
-
-    expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnv).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hiveserver2-site")).andReturn(mockHiveServerSite).atLeastOnce();
-    expect(mockHiveEnv.getProperties()).andReturn(propertiesExpectedHiveEnv).anyTimes();
-    expect(mockHiveServerSite.getProperties()).andReturn(propertiesExpectedHiveServerSite).anyTimes();
-
-    expect(mockClusterExpected.getDesiredConfigByType("ranger-hive-plugin-properties")).andReturn(mockHivePluginProperies).once();
-    expect(mockClusterExpected.getDesiredConfigByType("hive-site")).andReturn(mockHiveSite).atLeastOnce();
-    expect(mockHiveSite.getProperties()).andReturn(propertiesExpectedHiveSite).anyTimes();
-    expect(mockHivePluginProperies.getProperties()).andReturn(propertiesExpectedPluginProperies).anyTimes();
-    expect(mockClusterExpected.getServices()).andReturn(servicesExpected).atLeastOnce();
-    expect(mockAmbariManagementController.createConfig((Cluster) anyObject(),
-        anyString(),
-        capture(configCreation),
-        anyString(),
-        (Map<String, Map<String, String>>) anyObject())).andReturn(null).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    upgradeCatalog210.updateHiveConfigs();
-    easyMockSupport.verifyAll();
-    Assert.assertEquals(1, configCreation.getValues().size());
-
-    boolean result = false;
-    for (Map<String, String> cfg : configCreation.getValues()) {
-      if (cfg.containsKey("hive.security.authorization.manager")) {
-        result = true;
-        break;
-      }
-    }
-    Assert.assertFalse(result);
-    result = false;
-    for (Map<String, String> cfg : configCreation.getValues()) {
-      if (cfg.containsKey("hive.security.authenticator.manager")) {
-        result = true;
-        break;
-      }
-    }
-    Assert.assertFalse(result);
-  }
-
-
-  @Test
-  public void TestUpdateHiveEnvContent() {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-    String content = "# Start HIVE_AUX_JARS_PATH \n" +
-        "if [ \"${HIVE_AUX_JARS_PATH}\" != \"\" ]; then\n" +
-        "  export HIVE_AUX_JARS_PATH=${HIVE_AUX_JARS_PATH}\n" +
-        "elif [ -d \"/usr/hdp/current/hive-webhcat/share/hcatalog\" ]; then \n" +
-        "  export HIVE_AUX_JARS_PATH=/usr/hdp/current/hive-webhcat/share/hcatalog\n" +
-        "fi\n" +
-        "#End HIVE_AUX_JARS_PATH";
-    String expectedContent = "# Start HIVE_AUX_JARS_PATH \n" +
-        "if [ \"${HIVE_AUX_JARS_PATH}\" != \"\" ]; then\n" +
-        "  if [ -f \"${HIVE_AUX_JARS_PATH}\" ]; then    \n" +
-        "    export HIVE_AUX_JARS_PATH=${HIVE_AUX_JARS_PATH}\n" +
-        "  elif [ -d \"/usr/hdp/current/hive-webhcat/share/hcatalog\" ]; then\n" +
-        "    export HIVE_AUX_JARS_PATH=/usr/hdp/current/hive-webhcat/share/hcatalog/hive-hcatalog-core.jar\n" +
-        "  fi\n" +
-        "elif [ -d \"/usr/hdp/current/hive-webhcat/share/hcatalog\" ]; then\n" +
-        "  export HIVE_AUX_JARS_PATH=/usr/hdp/current/hive-webhcat/share/hcatalog/hive-hcatalog-core.jar\n" +
-        "fi\n" +
-        "#End HIVE_AUX_JARS_PATH";
-
-    String modifiedContent = mockInjector.getInstance(UpgradeCatalog210.class).updateHiveEnvContent(content);
-    Assert.assertEquals(modifiedContent, expectedContent);
-  }
-
-  @Test
-  public void testInitializeClusterAndServiceWidgets() throws Exception {
-    final AmbariManagementController controller = createStrictMock(AmbariManagementController.class);
-    final Clusters clusters = createStrictMock(Clusters.class);
-    final Cluster cluster = createStrictMock(Cluster.class);
-    final Service service = createStrictMock(Service.class);
-    final Map<String, Cluster> clusterMap = Collections.singletonMap("c1", cluster);
-    final Map<String, Service> services = Collections.singletonMap("HBASE", service);
-
-
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(AmbariManagementController.class).toInstance(controller);
-        binder.bind(Clusters.class).toInstance(clusters);
-        binder.bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    };
-
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(clusters.getClusters()).andReturn(clusterMap).anyTimes();
-    controller.initializeWidgetsAndLayouts(cluster, null);
-    expectLastCall().once();
-
-    expect(cluster.getServices()).andReturn(services).once();
-    controller.initializeWidgetsAndLayouts(cluster, service);
-    expectLastCall().once();
-
-    replay(controller, clusters, cluster);
-
-    Injector injector = Guice.createInjector(module);
-    injector.getInstance(UpgradeCatalog210.class).initializeClusterAndServiceWidgets();
-
-    verify(controller, clusters, cluster);
-  }
-
-  @Test
-  public void testUpdateStormConfiguration() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(
-        AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Config mockClusterEnv = easyMockSupport.createNiceMock(Config.class);
-    final Config mockStormSite = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> propertiesExpectedClusterEnv = new HashMap<String, String>();
-    propertiesExpectedClusterEnv.put("security_enabled", "true");
-    final Map<String, String> propertiesExpectedStormSite = new HashMap<String, String>();
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    expect(mockClusterExpected.getDesiredConfigByType("cluster-env")).andReturn(mockClusterEnv).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("storm-site")).andReturn(mockStormSite).atLeastOnce();
-    expect(mockClusterEnv.getProperties()).andReturn(propertiesExpectedClusterEnv).anyTimes();
-    expect(mockStormSite.getProperties()).andReturn(propertiesExpectedStormSite).anyTimes();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog210.class).updateStormConfigs();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateHBaseConfiguration() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Host mockHost = easyMockSupport.createNiceMock(Host.class);
-
-    final Config mockHBaseSite = easyMockSupport.createNiceMock(Config.class);
-    final Config mockHBaseEnv = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> propertiesExpectedHBaseSite = new HashMap<String, String>();
-    propertiesExpectedHBaseSite.put("hbase.region.server.rpc.scheduler.factory.class",
-        "org.apache.phoenix.hbase.index.ipc.PhoenixIndexRpcSchedulerFactory");
-    propertiesExpectedHBaseSite.put("hbase.security.authorization", "true");
-
-    final Map<String, String> propertiesExpectedHBaseEnv = new HashMap<String, String>();
-    propertiesExpectedHBaseEnv.put("phoenix_sql_enabled", "false");
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    expect(mockClusterExpected.getDesiredConfigByType("hbase-site")).andReturn(mockHBaseSite).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hbase-env")).andReturn(mockHBaseEnv).atLeastOnce();
-    expect(mockHBaseSite.getProperties()).andReturn(propertiesExpectedHBaseSite).anyTimes();
-    expect(mockHBaseEnv.getProperties()).andReturn(propertiesExpectedHBaseEnv).anyTimes();
-
-    Capture<String> configType = new Capture<String>();
-    Capture<String> configTag = new Capture<String>();
-    expect(mockClusterExpected.getConfig(capture(configType), capture(configTag))).
-            andReturn(mockHBaseSite).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog210.class).updateHBaseConfigs();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testDeleteStormRestApiServiceComponent() throws Exception {
-    initData();
-    ClusterEntity clusterEntity = upgradeCatalogHelper.createCluster(injector,
-      "c1", desiredStackEntity);
-    ClusterServiceEntity clusterServiceEntity = upgradeCatalogHelper.createService(
-        injector, clusterEntity, "STORM");
-    HostEntity hostEntity = upgradeCatalogHelper.createHost(injector,
-        clusterEntity, "h1");
-
-    // Set current stack version
-    ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
-    ClusterStateDAO clusterStateDAO = injector.getInstance(ClusterStateDAO.class);
-    ClusterStateEntity clusterStateEntity = new ClusterStateEntity();
-    clusterStateEntity.setClusterId(clusterEntity.getClusterId());
-    clusterStateEntity.setClusterEntity(clusterEntity);
-    clusterStateEntity.setCurrentStack(desiredStackEntity);
-    clusterStateDAO.create(clusterStateEntity);
-    clusterEntity.setClusterStateEntity(clusterStateEntity);
-    clusterDAO.merge(clusterEntity);
-
-    ServiceComponentDesiredStateEntity componentDesiredStateEntity = new ServiceComponentDesiredStateEntity();
-    componentDesiredStateEntity.setClusterId(clusterEntity.getClusterId());
-    componentDesiredStateEntity.setServiceName(clusterServiceEntity.getServiceName());
-    componentDesiredStateEntity.setClusterServiceEntity(clusterServiceEntity);
-    componentDesiredStateEntity.setComponentName("STORM_REST_API");
-    componentDesiredStateEntity.setDesiredStack(desiredStackEntity);
-
-    ServiceComponentDesiredStateDAO componentDesiredStateDAO =
-      injector.getInstance(ServiceComponentDesiredStateDAO.class);
-
-    componentDesiredStateDAO.create(componentDesiredStateEntity);
-
-    HostComponentDesiredStateDAO hostComponentDesiredStateDAO =
-      injector.getInstance(HostComponentDesiredStateDAO.class);
-
-    HostComponentDesiredStateEntity hostComponentDesiredStateEntity = new HostComponentDesiredStateEntity();
-
-    hostComponentDesiredStateEntity.setClusterId(clusterEntity.getClusterId());
-    hostComponentDesiredStateEntity.setComponentName("STORM_REST_API");
-    hostComponentDesiredStateEntity.setAdminState(HostComponentAdminState.INSERVICE);
-    hostComponentDesiredStateEntity.setServiceName(clusterServiceEntity.getServiceName());
-    hostComponentDesiredStateEntity.setServiceComponentDesiredStateEntity(componentDesiredStateEntity);
-    hostComponentDesiredStateEntity.setHostEntity(hostEntity);
-    hostComponentDesiredStateEntity.setDesiredStack(desiredStackEntity);
-
-    hostComponentDesiredStateDAO.create(hostComponentDesiredStateEntity);
-
-    HostComponentDesiredStateEntity entity = hostComponentDesiredStateDAO.findAll().get(0);
-
-    Assert.assertEquals(HostComponentAdminState.INSERVICE.name(), entity.getAdminState().name());
-
-    // ensure the desired state exists
-    Assert.assertNotNull(componentDesiredStateDAO.findByName(clusterEntity.getClusterId(), "STORM",
-        "STORM_REST_API"));
-
-    UpgradeCatalog210 upgradeCatalog210 = injector.getInstance(UpgradeCatalog210.class);
-    upgradeCatalog210.removeStormRestApiServiceComponent();
-
-    Assert.assertNull(componentDesiredStateDAO.findByName(clusterEntity.getClusterId(), "STORM",
-        "STORM_REST_API"));
-    tearDown();
-  }
-
-
-  @Test
-  public void testUpdateHDFSConfiguration() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Config mockHdfsSite = easyMockSupport.createNiceMock(Config.class);
-    final Config mockCoreSite = easyMockSupport.createStrictMock(Config.class);
-
-    final Map<String, String> propertiesExpectedHdfs = new HashMap<String, String>();
-    final Map<String, String> propertiesExpectedCoreSite = new HashMap<String, String>();
-    propertiesExpectedHdfs.put("dfs.nameservices", "nncl1,nncl2");
-    propertiesExpectedHdfs.put("dfs.ha.namenodes.nncl2", "nn1,nn2");
-    propertiesExpectedCoreSite.put("fs.defaultFS", "hdfs://EXAMPLE.COM:8020");
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    // Expected operation
-    expect(mockClusterExpected.getDesiredConfigByType("hadoop-env")).andReturn(null).once();
-
-    // Expected operation
-    expect(mockClusterExpected.getDesiredConfigByType("hdfs-site")).andReturn(mockHdfsSite).atLeastOnce();
-    expect(mockClusterExpected.getHosts("HDFS", "NAMENODE")).andReturn( new HashSet<String>() {{
-      add("host1");
-    }}).atLeastOnce();
-    expect(mockHdfsSite.getProperties()).andReturn(propertiesExpectedHdfs).anyTimes();
-
-    expect(mockClusterExpected.getDesiredConfigByType("core-site")).andReturn(mockCoreSite).anyTimes();
-    expect(mockCoreSite.getProperties()).andReturn(propertiesExpectedCoreSite).anyTimes();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog210.class).updateHdfsConfigs();
-    easyMockSupport.verifyAll();
-  }
-
-  /**
-   * @param dbAccessor
-   * @return
-   */
-  private AbstractUpgradeCatalog getUpgradeCatalog(final DBAccessor dbAccessor) {
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(EntityManager.class).toInstance(entityManager);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    return injector.getInstance(UpgradeCatalog210.class);
-  }
-
-  @Test
-  public void testGetSourceVersion() {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    UpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
-    Assert.assertEquals("2.0.0", upgradeCatalog.getSourceVersion());
-  }
-
-  @Test
-  public void testGetTargetVersion() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    UpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
-
-    Assert.assertEquals("2.1.0", upgradeCatalog.getTargetVersion());
-  }
-
-  @Test
-  public void testUpdateKerberosDescriptorArtifact_Simple() throws Exception {
-    final KerberosDescriptorFactory kerberosDescriptorFactory = new KerberosDescriptorFactory();
-
-    KerberosServiceDescriptor serviceDescriptor;
-
-    URL systemResourceURL = ClassLoader.getSystemResource("kerberos/test_kerberos_descriptor_simple.json");
-    assertNotNull(systemResourceURL);
-
-    final KerberosDescriptor kerberosDescriptorOrig = kerberosDescriptorFactory.createInstance(new File(systemResourceURL.getFile()));
-    assertNotNull(kerberosDescriptorOrig);
-    assertNotNull(kerberosDescriptorOrig.getIdentity("hdfs"));
-
-    serviceDescriptor = kerberosDescriptorOrig.getService("HDFS");
-    assertNotNull(serviceDescriptor);
-    assertNotNull(serviceDescriptor.getIdentity("/hdfs"));
-    assertNull(serviceDescriptor.getIdentity("hdfs"));
-
-    serviceDescriptor = kerberosDescriptorOrig.getService("OOZIE");
-    assertNotNull(serviceDescriptor);
-    assertNotNull(serviceDescriptor.getIdentity("/hdfs"));
-    assertNull(serviceDescriptor.getIdentity("/HDFS/hdfs"));
-
-    UpgradeCatalog210 upgradeMock = createMockBuilder(UpgradeCatalog210.class).createMock();
-
-    Capture<Map<String, Object>> updatedData = new Capture<Map<String, Object>>();
-
-    ArtifactEntity artifactEntity = createNiceMock(ArtifactEntity.class);
-    expect(artifactEntity.getArtifactData())
-        .andReturn(kerberosDescriptorOrig.toMap())
-        .once();
-
-    artifactEntity.setArtifactData(capture(updatedData));
-    expectLastCall().once();
-
-    replay(artifactEntity, upgradeMock);
-    upgradeMock.updateKerberosDescriptorArtifact(createNiceMock(ArtifactDAO.class), artifactEntity);
-    verify(artifactEntity, upgradeMock);
-
-    KerberosDescriptor kerberosDescriptorUpdated = new KerberosDescriptorFactory().createInstance(updatedData.getValue());
-    assertNotNull(kerberosDescriptorUpdated);
-    assertNull(kerberosDescriptorUpdated.getIdentity("/hdfs"));
-
-    serviceDescriptor = kerberosDescriptorUpdated.getService("HDFS");
-    assertNotNull(serviceDescriptor);
-    assertNull(serviceDescriptor.getIdentity("/hdfs"));
-    assertNotNull(serviceDescriptor.getIdentity("hdfs"));
-
-    serviceDescriptor = kerberosDescriptorUpdated.getService("OOZIE");
-    assertNotNull(serviceDescriptor);
-    assertNull(serviceDescriptor.getIdentity("/hdfs"));
-    assertNotNull(serviceDescriptor.getIdentity("/HDFS/hdfs"));
-  }
-
-  @Test
-  public void testUpdateKerberosDescriptorArtifact_NoHDFSService() throws Exception {
-    final KerberosDescriptorFactory kerberosDescriptorFactory = new KerberosDescriptorFactory();
-
-    KerberosServiceDescriptor serviceDescriptor;
-
-    URL systemResourceURL = ClassLoader.getSystemResource("kerberos/test_kerberos_descriptor_no_hdfs.json");
-    assertNotNull(systemResourceURL);
-
-    final KerberosDescriptor kerberosDescriptorOrig = kerberosDescriptorFactory.createInstance(new File(systemResourceURL.getFile()));
-    assertNotNull(kerberosDescriptorOrig);
-    assertNotNull(kerberosDescriptorOrig.getIdentity("hdfs"));
-
-    serviceDescriptor = kerberosDescriptorOrig.getService("HDFS");
-    assertNull(serviceDescriptor);
-
-    serviceDescriptor = kerberosDescriptorOrig.getService("OOZIE");
-    assertNotNull(serviceDescriptor);
-    assertNotNull(serviceDescriptor.getIdentity("/hdfs"));
-    assertNull(serviceDescriptor.getIdentity("/HDFS/hdfs"));
-
-    UpgradeCatalog210 upgradeMock = createMockBuilder(UpgradeCatalog210.class).createMock();
-
-    Capture<Map<String, Object>> updatedData = new Capture<Map<String, Object>>();
-
-    ArtifactEntity artifactEntity = createNiceMock(ArtifactEntity.class);
-    expect(artifactEntity.getArtifactData())
-        .andReturn(kerberosDescriptorOrig.toMap())
-        .once();
-
-    artifactEntity.setArtifactData(capture(updatedData));
-    expectLastCall().once();
-
-    replay(artifactEntity, upgradeMock);
-    upgradeMock.updateKerberosDescriptorArtifact(createNiceMock(ArtifactDAO.class), artifactEntity);
-    verify(artifactEntity, upgradeMock);
-
-    KerberosDescriptor kerberosDescriptorUpdated = new KerberosDescriptorFactory().createInstance(updatedData.getValue());
-    assertNotNull(kerberosDescriptorUpdated);
-    assertNull(kerberosDescriptorUpdated.getIdentity("/hdfs"));
-
-    serviceDescriptor = kerberosDescriptorUpdated.getService("HDFS");
-    assertNotNull(serviceDescriptor);
-    assertNull(serviceDescriptor.getIdentity("/hdfs"));
-    assertNotNull(serviceDescriptor.getIdentity("hdfs"));
-
-    serviceDescriptor = kerberosDescriptorUpdated.getService("OOZIE");
-    assertNotNull(serviceDescriptor);
-    assertNull(serviceDescriptor.getIdentity("/hdfs"));
-    assertNotNull(serviceDescriptor.getIdentity("/HDFS/hdfs"));
-  }
-
-  // *********** Inner Classes that represent sections of the DDL ***********
-  // ************************************************************************
-
-  /**
-   * Verify that all of the host-related tables added a column for the host_id
-   */
-  class HostSectionDDL implements SectionDDL {
-
-    HashMap<String, Capture<DBColumnInfo>> captures;
-
-    public HostSectionDDL() {
-      // Capture all tables that will have the host_id column added to it.
-      captures = new HashMap<String, Capture<DBColumnInfo>>();
-
-      // Column Capture section
-      // Hosts
-      Capture<DBAccessor.DBColumnInfo> clusterHostMappingColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> configGroupHostMappingColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> hostConfigMappingColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> hostsColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> hostComponentStateColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> hostComponentDesiredStateColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> hostRoleCommandColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> hostStateColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> hostVersionColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> kerberosPrincipalHostColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> requestOperationLevelColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> serviceConfigHostsColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-
-      captures.put("ClusterHostMapping", clusterHostMappingColumnCapture);
-      captures.put("configgrouphostmapping", configGroupHostMappingColumnCapture);
-      captures.put("hostconfigmapping", hostConfigMappingColumnCapture);
-      captures.put("hosts", hostsColumnCapture);
-      captures.put("hostcomponentstate", hostComponentStateColumnCapture);
-      captures.put("hostcomponentdesiredstate", hostComponentDesiredStateColumnCapture);
-      captures.put("host_role_command", hostRoleCommandColumnCapture);
-      captures.put("hoststate", hostStateColumnCapture);
-      captures.put("host_version", hostVersionColumnCapture);
-      captures.put("kerberos_principal_host", kerberosPrincipalHostColumnCapture);
-      captures.put("requestoperationlevel", requestOperationLevelColumnCapture);
-      captures.put("serviceconfighosts", serviceConfigHostsColumnCapture);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void execute(DBAccessor dbAccessor) throws SQLException {
-      // Add columns and alter table section
-      dbAccessor.addColumn(eq("ClusterHostMapping"), capture(captures.get("ClusterHostMapping")));
-      dbAccessor.addColumn(eq("configgrouphostmapping"), capture(captures.get("configgrouphostmapping")));
-      dbAccessor.addColumn(eq("hostconfigmapping"), capture(captures.get("hostconfigmapping")));
-      dbAccessor.addColumn(eq("hosts"), capture(captures.get("hosts")));
-      dbAccessor.addColumn(eq("hostcomponentstate"), capture(captures.get("hostcomponentstate")));
-      dbAccessor.addColumn(eq("hostcomponentdesiredstate"), capture(captures.get("hostcomponentdesiredstate")));
-      dbAccessor.addColumn(eq("host_role_command"), capture(captures.get("host_role_command")));
-      dbAccessor.addColumn(eq("hoststate"), capture(captures.get("hoststate")));
-      dbAccessor.addColumn(eq("host_version"), capture(captures.get("host_version")));
-      dbAccessor.addColumn(eq("kerberos_principal_host"), capture(captures.get("kerberos_principal_host")));
-      dbAccessor.addColumn(eq("requestoperationlevel"), capture(captures.get("requestoperationlevel")));
-      dbAccessor.addColumn(eq("serviceconfighosts"), capture(captures.get("serviceconfighosts")));
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void verify(DBAccessor dbAccessor) throws SQLException {
-      // Verification section
-      for (Capture<DBColumnInfo> columnCapture : captures.values()) {
-        verifyContainsHostIdColumn(columnCapture);
-      }
-    }
-
-    /**
-     * Verify that the column capture of the table contains a host_id column of type Long.
-     * This is needed for all of the host-related tables that are switching from the
-     * host_name to the host_id.
-     * @param columnCapture
-     */
-    private void verifyContainsHostIdColumn(Capture<DBAccessor.DBColumnInfo> columnCapture) {
-      DBColumnInfo idColumn = columnCapture.getValue();
-      Assert.assertEquals(Long.class, idColumn.getType());
-      Assert.assertEquals("host_id", idColumn.getName());
-    }
-  }
-
-  /**
-   * Verify that the widget, widget_layout, and widget_layout_user_widget tables are created correctly.
-   */
-  class WidgetSectionDDL implements SectionDDL {
-
-    HashMap<String, Capture<List<DBColumnInfo>>> captures;
-    Capture<DBColumnInfo> userActiveLayoutsColumnCapture;
-
-    public WidgetSectionDDL() {
-      captures = new HashMap<String, Capture<List<DBColumnInfo>>>();
-
-      Capture<List<DBColumnInfo>> userWidgetColumnsCapture = new Capture<List<DBColumnInfo>>();
-      Capture<List<DBColumnInfo>> widgetLayoutColumnsCapture = new Capture<List<DBColumnInfo>>();
-      Capture<List<DBColumnInfo>> widgetLayoutUserWidgetColumnsCapture = new Capture<List<DBColumnInfo>>();
-
-      captures.put("widget", userWidgetColumnsCapture);
-      captures.put("widget_layout", widgetLayoutColumnsCapture);
-      captures.put("widget_layout_user_widget", widgetLayoutUserWidgetColumnsCapture);
-      userActiveLayoutsColumnCapture = new Capture<DBColumnInfo>();
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void execute(DBAccessor dbAccessor) throws SQLException {
-      Capture<List<DBColumnInfo>> userWidgetColumnsCapture = captures.get("widget");
-      Capture<List<DBColumnInfo>> widgetLayoutColumnsCapture = captures.get("widget_layout");
-      Capture<List<DBColumnInfo>> widgetLayoutUserWidgetColumnsCapture = captures.get("widget_layout_user_widget");
-
-      // User Widget
-      dbAccessor.createTable(eq("widget"),
-          capture(userWidgetColumnsCapture), eq("id"));
-
-      // Widget Layout
-      dbAccessor.createTable(eq("widget_layout"),
-          capture(widgetLayoutColumnsCapture), eq("id"));
-
-      // Widget Layout User Widget
-      dbAccessor.createTable(eq("widget_layout_user_widget"),
-          capture(widgetLayoutUserWidgetColumnsCapture), eq("widget_layout_id"), eq("widget_id"));
-
-      dbAccessor.addColumn(eq("users"), capture(userActiveLayoutsColumnCapture));
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void verify(DBAccessor dbAccessor) throws SQLException {
-      Capture<List<DBColumnInfo>> widgetColumnsCapture = captures.get("widget");
-      Capture<List<DBColumnInfo>> widgetLayoutColumnsCapture = captures.get("widget_layout");
-      Capture<List<DBColumnInfo>> widgetLayoutUserWidgetColumnsCapture = captures.get("widget_layout_user_widget");
-
-      // Verify widget tables
-      assertEquals(12, widgetColumnsCapture.getValue().size());
-      assertEquals(7, widgetLayoutColumnsCapture.getValue().size());
-      assertEquals(3, widgetLayoutUserWidgetColumnsCapture.getValue().size());
-
-      DBColumnInfo idColumn = userActiveLayoutsColumnCapture.getValue();
-      Assert.assertEquals(String.class, idColumn.getType());
-      Assert.assertEquals("active_widget_layouts", idColumn.getName());
-    }
-  }
-
-  /**
-   * Verify view changes
-   */
-  class ViewSectionDDL implements SectionDDL {
-
-    HashMap<String, Capture<DBColumnInfo>> captures;
-
-    public ViewSectionDDL() {
-      captures = new HashMap<String, Capture<DBColumnInfo>>();
-
-      Capture<DBAccessor.DBColumnInfo> viewInstanceColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> viewInstanceAlterNamesColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> viewParamColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-      Capture<DBAccessor.DBColumnInfo> viewBuildColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-
-      captures.put("viewinstance", viewInstanceColumnCapture);
-      captures.put("viewinstance_alter_names", viewInstanceAlterNamesColumnCapture);
-      captures.put("viewparameter", viewParamColumnCapture);
-      captures.put("viewmain", viewBuildColumnCapture);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void execute(DBAccessor dbAccessor) throws SQLException {
-      Capture<DBColumnInfo> viewInstanceColumnCapture = captures.get("viewinstance");
-      Capture<DBColumnInfo> viewInstanceAlterNamesColumnCapture = captures.get("viewinstance_alter_names");
-      Capture<DBColumnInfo> viewParamColumnCapture = captures.get("viewparameter");
-      Capture<DBColumnInfo> viewBuildColumnCapture = captures.get("viewmain");
-
-      dbAccessor.addColumn(eq("viewinstance"), capture(viewInstanceColumnCapture));
-      dbAccessor.addColumn(eq("viewinstance"), capture(viewInstanceAlterNamesColumnCapture));
-      dbAccessor.addColumn(eq("viewparameter"), capture(viewParamColumnCapture));
-      dbAccessor.addColumn(eq("viewmain"), capture(viewBuildColumnCapture));
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void verify(DBAccessor dbAccessor) throws SQLException {
-      verifyViewInstance(captures.get("viewinstance"));
-      verifyViewInstanceAlterNames(captures.get("viewinstance_alter_names"));
-      verifyViewParameter(captures.get("viewparameter"));
-      verifyViewBuild(captures.get("viewmain"));
-    }
-
-    private void verifyViewInstance(Capture<DBAccessor.DBColumnInfo> viewInstanceColumnCapture) {
-      DBColumnInfo clusterIdColumn = viewInstanceColumnCapture.getValue();
-      Assert.assertEquals(String.class, clusterIdColumn.getType());
-      Assert.assertEquals("cluster_handle", clusterIdColumn.getName());
-    }
-
-    private void verifyViewInstanceAlterNames(Capture<DBAccessor.DBColumnInfo> viewInstanceAlterNamesColumnCapture) {
-      DBColumnInfo clusterIdColumn = viewInstanceAlterNamesColumnCapture.getValue();
-      Assert.assertEquals(Integer.class, clusterIdColumn.getType());
-      Assert.assertEquals("alter_names", clusterIdColumn.getName());
-    }
-
-    private void verifyViewParameter(Capture<DBAccessor.DBColumnInfo> viewParamColumnCapture) {
-      DBColumnInfo clusterConfigColumn = viewParamColumnCapture.getValue();
-      Assert.assertEquals(String.class, clusterConfigColumn.getType());
-      Assert.assertEquals("cluster_config", clusterConfigColumn.getName());
-    }
-
-    private void verifyViewBuild(Capture<DBAccessor.DBColumnInfo> viewBuildColumnCapture) {
-      DBColumnInfo clusterConfigColumn = viewBuildColumnCapture.getValue();
-      Assert.assertEquals(String.class, clusterConfigColumn.getType());
-      Assert.assertEquals("build", clusterConfigColumn.getName());
-    }
-  }
-
-  /**
-   * Verify alert changes
-   */
-  class AlertSectionDDL implements SectionDDL {
-    HashMap<String, Capture<String>> stringCaptures;
-    HashMap<String, Capture<Class>> classCaptures;
-
-
-    public AlertSectionDDL() {
-      stringCaptures = new HashMap<String, Capture<String>>();
-      classCaptures = new HashMap<String, Capture<Class>>();
-
-      Capture<String> textCaptureC = new Capture<String>();
-      Capture<String> textCaptureH = new Capture<String>();
-      Capture<Class>  classFromC = new Capture<Class>();
-      Capture<Class>  classFromH = new Capture<Class>();
-      Capture<Class>  classToC = new Capture<Class>();
-      Capture<Class>  classToH = new Capture<Class>();
-
-      stringCaptures.put("textCaptureC", textCaptureC);
-      stringCaptures.put("textCaptureH", textCaptureH);
-      classCaptures.put("classFromC", classFromC);
-      classCaptures.put("classFromH", classFromH);
-      classCaptures.put("classToC", classToC);
-      classCaptures.put("classToH", classToH);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void execute(DBAccessor dbAccessor) throws SQLException {
-      Capture<String> textCaptureC = stringCaptures.get("textCaptureC");
-      Capture<String> textCaptureH = stringCaptures.get("textCaptureH");
-      Capture<Class>  classFromC = classCaptures.get("classFromC");
-      Capture<Class>  classFromH = classCaptures.get("classFromH");
-      Capture<Class>  classToC = classCaptures.get("classToC");
-      Capture<Class>  classToH = classCaptures.get("classToH");
-
-      dbAccessor.changeColumnType(eq("alert_current"), capture(textCaptureC), capture(classFromC), capture(classToC));
-      dbAccessor.changeColumnType(eq("alert_history"), capture(textCaptureH), capture(classFromH), capture(classToH));
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void verify(DBAccessor dbAccessor) throws SQLException {
-      Capture<String> textCaptureC = stringCaptures.get("textCaptureC");
-      Capture<String> textCaptureH = stringCaptures.get("textCaptureH");
-      Capture<Class>  classFromC = classCaptures.get("classFromC");
-      Capture<Class>  classFromH = classCaptures.get("classFromH");
-      Capture<Class>  classToC = classCaptures.get("classToC");
-      Capture<Class>  classToH = classCaptures.get("classToH");
-
-      Assert.assertEquals("latest_text", textCaptureC.getValue());
-      Assert.assertEquals(String.class, classFromC.getValue());
-      Assert.assertEquals(char[].class, classToC.getValue());
-
-      Assert.assertEquals("alert_text", textCaptureH.getValue());
-      Assert.assertEquals(String.class, classFromH.getValue());
-      Assert.assertEquals(char[].class, classToH.getValue());
-    }
-  }
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog211Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog211Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog211Test.java
deleted file mode 100644
index 616d37f..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog211Test.java
+++ /dev/null
@@ -1,444 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.Statement;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
-
-import javax.persistence.EntityManager;
-
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.configuration.Configuration.DatabaseType;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.controller.ConfigurationRequest;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.GuiceJpaInitializer;
-import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.easymock.Capture;
-import org.easymock.EasyMockSupport;
-import org.junit.Assert;
-import org.junit.Test;
-
-import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.Provider;
-import com.google.inject.persist.PersistService;
-
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.newCapture;
-
-
-/**
- * {@link UpgradeCatalog211} unit tests.
- */
-public class UpgradeCatalog211Test extends EasyMockSupport {
-
-  @Test
-  public void testExecuteDDLUpdates() throws Exception {
-    Injector injector = initInjector();
-
-    try {
-      Provider<EntityManager> entityManagerProvider = initEntityManagerProvider();
-
-      final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-      final OsFamily osFamily = createNiceMock(OsFamily.class);
-      Configuration configuration = createNiceMock(Configuration.class);
-      Connection connection = createNiceMock(Connection.class);
-      Statement statement = createNiceMock(Statement.class);
-      ResultSet resultSet = createNiceMock(ResultSet.class);
-      expect(configuration.getDatabaseUrl()).andReturn(Configuration.JDBC_IN_MEMORY_URL).anyTimes();
-      expect(configuration.getDatabaseType()).andReturn(DatabaseType.DERBY).anyTimes();
-      dbAccessor.getConnection();
-      expectLastCall().andReturn(connection).anyTimes();
-      connection.createStatement();
-      expectLastCall().andReturn(statement).anyTimes();
-      statement.executeQuery("SELECT COUNT(*) from ambari_sequences where sequence_name='hostcomponentstate_id_seq'");
-      expectLastCall().andReturn(resultSet).atLeastOnce();
-
-      ResultSet rs1 = createNiceMock(ResultSet.class);
-      expect(rs1.next()).andReturn(Boolean.TRUE).once();
-
-      statement.executeQuery(anyObject(String.class));
-      expectLastCall().andReturn(rs1).anyTimes();
-
-      Capture<String> queryCapture = new Capture<String>();
-      dbAccessor.executeQuery(capture(queryCapture));
-      expectLastCall().once();
-
-      dbAccessor.setColumnNullable("viewinstanceproperty", "value", true);
-      expectLastCall().once();
-      dbAccessor.setColumnNullable("viewinstancedata", "value", true);
-      expectLastCall().once();
-
-      // Create DDL sections with their own capture groups
-      // Example: AlertSectionDDL alertSectionDDL = new AlertSectionDDL();
-
-      // Execute any DDL schema changes
-      // Example: alertSectionDDL.execute(dbAccessor);
-
-      // Replay sections
-      replayAll();
-
-      AbstractUpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor, osFamily, entityManagerProvider.get());
-      Class<?> c = AbstractUpgradeCatalog.class;
-      Field f = c.getDeclaredField("configuration");
-      f.setAccessible(true);
-      f.set(upgradeCatalog, configuration);
-
-      f = UpgradeCatalog211.class.getDeclaredField("m_hcsId");
-      f.setAccessible(true);
-      f.set(upgradeCatalog, new AtomicLong(1001));
-
-      upgradeCatalog.executeDDLUpdates();
-      verifyAll();
-
-      Assert.assertTrue(queryCapture.hasCaptured());
-      Assert.assertTrue(queryCapture.getValue().contains("1001"));
-
-      // Verify sections
-      // Example: alertSectionDDL.verify(dbAccessor);
-    } finally {
-      destroyInjector(injector);
-    }
-  }
-
-  @Test
-  public void testExecutePreDMLUpdates() throws Exception {
-
-    final UpgradeCatalog211 upgradeCatalog211 = createMockBuilder(UpgradeCatalog211.class)
-        // Add mocked methods. Example: .addMockedMethod(cleanupStackUpdates)
-        .createMock();
-
-    final Injector injector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(UpgradeCatalog211.class).toInstance(upgradeCatalog211);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    setInjector(upgradeCatalog211, injector);
-
-    replayAll();
-
-    injector.getInstance(UpgradeCatalog211.class).executePreDMLUpdates();
-
-    verifyAll();
-  }
-
-  @Test
-  public void testExecuteDMLUpdates() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    final OsFamily osFamily = createNiceMock(OsFamily.class);
-
-    final Cluster cluster = createMock(Cluster.class);
-
-    final Clusters clusters = createMock(Clusters.class);
-    expect(clusters.getClusters())
-        .andReturn(Collections.singletonMap("c1", cluster));
-
-    final AmbariManagementController controller = createNiceMock(AmbariManagementController.class);
-    expect(controller.getClusters())
-        .andReturn(clusters)
-        .once();
-
-    final Injector injector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(controller);
-        bind(DBAccessor.class).toInstance(dbAccessor);
-        bind(OsFamily.class).toInstance(osFamily);
-      }
-    });
-
-    Method addNewConfigurationsFromXml =
-        AbstractUpgradeCatalog.class.getDeclaredMethod("addNewConfigurationsFromXml");
-
-    Method updateKerberosConfigurations =
-        UpgradeCatalog211.class.getDeclaredMethod("updateKerberosConfigurations", Cluster.class);
-
-    UpgradeCatalog211 upgradeCatalog211 = createMockBuilder(UpgradeCatalog211.class)
-        .addMockedMethod(addNewConfigurationsFromXml)
-        .addMockedMethod(updateKerberosConfigurations)
-        .createMock();
-
-    setInjector(upgradeCatalog211, injector);
-
-    upgradeCatalog211.addNewConfigurationsFromXml();
-    expectLastCall().once();
-
-    upgradeCatalog211.updateKerberosConfigurations(anyObject(Cluster.class));
-    expectLastCall().once();
-
-    replayAll();
-
-    upgradeCatalog211.executeDMLUpdates();
-
-    verifyAll();
-  }
-
-  @Test
-  public void testUpdateKerberosConfiguration() throws Exception {
-    final AmbariManagementController controller = createNiceMock(AmbariManagementController.class);
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    final OsFamily osFamily = createNiceMock(OsFamily.class);
-
-    final Map<String, String> propertiesKerberosEnv = new HashMap<String, String>() {
-      {
-        put("create_attributes_template", "create_attributes_template content");
-        put("realm", "EXAMPLE.COM");
-        put("container_dn", "");
-        put("ldap_url", "");
-        put("encryption_types", "aes des3-cbc-sha1 rc4 des-cbc-md5");
-        put("kdc_host", "c6407.ambari.apache.org");
-        put("admin_server_host", "c6407.ambari.apache.org");
-        put("kdc_type", "mit-kdc");
-      }
-    };
-
-    final Config configKerberosEnv = createNiceMock(Config.class);
-    expect(configKerberosEnv.getProperties()).andReturn(propertiesKerberosEnv).anyTimes();
-    expect(configKerberosEnv.getTag()).andReturn("tag1").anyTimes();
-
-    final Cluster cluster = createNiceMock(Cluster.class);
-    expect(cluster.getDesiredConfigByType("kerberos-env")).andReturn(configKerberosEnv).once();
-
-    final Injector injector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(controller);
-        bind(DBAccessor.class).toInstance(dbAccessor);
-        bind(OsFamily.class).toInstance(osFamily);
-      }
-    });
-
-    /* *********************************************************
-     * Expects for updateConfigurationPropertiesForCluster
-     * **** */
-    expect(cluster.getConfigsByType("kerberos-env"))
-        .andReturn(Collections.singletonMap("tag1", configKerberosEnv))
-        .once();
-
-    expect(cluster.getDesiredConfigByType("kerberos-env"))
-        .andReturn(configKerberosEnv)
-        .once();
-
-    Capture<ConfigurationRequest> captureCR = new Capture<ConfigurationRequest>();
-    Capture<Cluster> clusterCapture = newCapture();
-    Capture<String> typeCapture = newCapture();
-    Capture<Map> propertiesCapture = newCapture();
-    Capture<String> tagCapture = newCapture();
-    Capture<Map> attributesCapture = newCapture();
-
-
-    expect(controller.createConfig(capture(clusterCapture), capture(typeCapture),
-        capture(propertiesCapture), capture(tagCapture), capture(attributesCapture) ))
-        .andReturn(createNiceMock(Config.class))
-        .once();
-
-    /* ****
-     * Expects for updateConfigurationPropertiesForCluster (end)
-     * ********************************************************* */
-
-    replayAll();
-
-    injector.getInstance(UpgradeCatalog211.class).updateKerberosConfigurations(cluster);
-
-    verifyAll();
-
-    Map<String, String> capturedCRProperties = propertiesCapture.getValue();
-    Assert.assertNotNull(capturedCRProperties);
-    Assert.assertFalse(capturedCRProperties.containsKey("create_attributes_template"));
-    Assert.assertTrue(capturedCRProperties.containsKey("ad_create_attributes_template"));
-
-    for (String property : propertiesKerberosEnv.keySet()) {
-      if ("create_attributes_template".equals(property)) {
-        Assert.assertEquals("create_attributes_template/ad_create_attributes_template", propertiesKerberosEnv.get(property), capturedCRProperties.get("ad_create_attributes_template"));
-      } else {
-        Assert.assertEquals(property, propertiesKerberosEnv.get(property), capturedCRProperties.get(property));
-      }
-    }
-  }
-
-  @Test
-  public void testGetSourceVersion() {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    final OsFamily osFamily = createNiceMock(OsFamily.class);
-    Provider<EntityManager> entityManagerProvider = initEntityManagerProvider();
-
-    replayAll();
-
-    UpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor, osFamily, entityManagerProvider.get());
-
-    Assert.assertEquals("2.1.0", upgradeCatalog.getSourceVersion());
-
-    verifyAll();
-  }
-
-  @Test
-  public void testGetTargetVersion() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    final OsFamily osFamily = createNiceMock(OsFamily.class);
-    Provider<EntityManager> entityManagerProvider = initEntityManagerProvider();
-
-    replayAll();
-
-    UpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor, osFamily, entityManagerProvider.get());
-
-    Assert.assertEquals("2.1.1", upgradeCatalog.getTargetVersion());
-
-    verifyAll();
-  }
-
-  private Provider<EntityManager> initEntityManagerProvider() {
-    Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
-
-    EntityManager entityManager = createNiceMock(EntityManager.class);
-    expect(entityManagerProvider.get())
-        .andReturn(entityManager)
-        .anyTimes();
-
-    return entityManagerProvider;
-  }
-
-  private Injector initInjector() {
-    Injector injector;
-
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-
-    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
-    injector.getInstance(AmbariMetaInfo.class);
-
-    // load the stack entity
-    StackDAO stackDAO = injector.getInstance(StackDAO.class);
-    stackDAO.find("HDP", "2.2.0");
-
-    return injector;
-  }
-
-  private void destroyInjector(Injector injector) {
-    injector.getInstance(PersistService.class).stop();
-  }
-
-  private AbstractUpgradeCatalog getUpgradeCatalog(final DBAccessor dbAccessor, final OsFamily osFamily, final EntityManager entityManager) {
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(OsFamily.class).toInstance(osFamily);
-        binder.bind(EntityManager.class).toInstance(entityManager);
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    return injector.getInstance(UpgradeCatalog211.class);
-  }
-
-  private void setInjector(UpgradeCatalog211 upgradeCatalog211, Injector injector) throws NoSuchFieldException, IllegalAccessException {
-    Field fieldInjector = AbstractUpgradeCatalog.class.getDeclaredField("injector");
-    if (fieldInjector != null) {
-      fieldInjector.set(upgradeCatalog211, injector);
-    }
-  }
-
-  // *********** Inner Classes that represent sections of the DDL ***********
-  // ************************************************************************
-
-  /**
-   * Example *SectionDDL class
-   */
-  /*
-  class AlertSectionDDL implements SectionDDL {
-    HashMap<String, Capture<String>> stringCaptures;
-    HashMap<String, Capture<Class>> classCaptures;
-
-
-    public AlertSectionDDL() {
-      stringCaptures = new HashMap<String, Capture<String>>();
-      classCaptures = new HashMap<String, Capture<Class>>();
-
-      Capture<String> textCaptureC = new Capture<String>();
-      Capture<String> textCaptureH = new Capture<String>();
-      Capture<Class>  classFromC = new Capture<Class>();
-      Capture<Class>  classFromH = new Capture<Class>();
-      Capture<Class>  classToC = new Capture<Class>();
-      Capture<Class>  classToH = new Capture<Class>();
-
-      stringCaptures.put("textCaptureC", textCaptureC);
-      stringCaptures.put("textCaptureH", textCaptureH);
-      classCaptures.put("classFromC", classFromC);
-      classCaptures.put("classFromH", classFromH);
-      classCaptures.put("classToC", classToC);
-      classCaptures.put("classToH", classToH);
-    }
-
-    @Override
-    public void execute(DBAccessor dbAccessor) throws SQLException {
-      Capture<String> textCaptureC = stringCaptures.get("textCaptureC");
-      Capture<String> textCaptureH = stringCaptures.get("textCaptureH");
-      Capture<Class>  classFromC = classCaptures.get("classFromC");
-      Capture<Class>  classFromH = classCaptures.get("classFromH");
-      Capture<Class>  classToC = classCaptures.get("classToC");
-      Capture<Class>  classToH = classCaptures.get("classToH");
-
-      dbAccessor.changeColumnType(eq("alert_current"), capture(textCaptureC), capture(classFromC), capture(classToC));
-      dbAccessor.changeColumnType(eq("alert_history"), capture(textCaptureH), capture(classFromH), capture(classToH));
-    }
-
-    @Override
-    public void verify(DBAccessor dbAccessor) throws SQLException {
-      Capture<String> textCaptureC = stringCaptures.get("textCaptureC");
-      Capture<String> textCaptureH = stringCaptures.get("textCaptureH");
-      Capture<Class>  classFromC = classCaptures.get("classFromC");
-      Capture<Class>  classFromH = classCaptures.get("classFromH");
-      Capture<Class>  classToC = classCaptures.get("classToC");
-      Capture<Class>  classToH = classCaptures.get("classToH");
-
-      Assert.assertEquals("latest_text", textCaptureC.getValue());
-      Assert.assertEquals(String.class, classFromC.getValue());
-      Assert.assertEquals(char[].class, classToC.getValue());
-
-      Assert.assertEquals("alert_text", textCaptureH.getValue());
-      Assert.assertEquals(String.class, classFromH.getValue());
-      Assert.assertEquals(char[].class, classToH.getValue());
-    }
-  }
-  */
-}


[06/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog220Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog220Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog220Test.java
deleted file mode 100644
index 731b0b5..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog220Test.java
+++ /dev/null
@@ -1,1633 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import com.google.common.collect.Maps;
-import com.google.gson.Gson;
-import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.Provider;
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.H2DatabaseCleaner;
-import org.apache.ambari.server.actionmanager.ActionManager;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.controller.AmbariManagementControllerImpl;
-import org.apache.ambari.server.controller.ConfigurationRequest;
-import org.apache.ambari.server.controller.ConfigurationResponse;
-import org.apache.ambari.server.controller.KerberosHelper;
-import org.apache.ambari.server.controller.MaintenanceStateHelper;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.GuiceJpaInitializer;
-import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
-import org.apache.ambari.server.orm.dao.ArtifactDAO;
-import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
-import org.apache.ambari.server.orm.dao.DaoUtils;
-import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.ArtifactEntity;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
-import org.apache.ambari.server.orm.entities.HostEntity;
-import org.apache.ambari.server.orm.entities.HostVersionEntity;
-import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
-import org.apache.ambari.server.orm.entities.StackEntity;
-import org.apache.ambari.server.stack.StackManagerFactory;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.ConfigHelper;
-import org.apache.ambari.server.state.RepositoryVersionState;
-import org.apache.ambari.server.state.SecurityType;
-import org.apache.ambari.server.state.Service;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.StackInfo;
-import org.apache.ambari.server.state.kerberos.KerberosComponentDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptorFactory;
-import org.apache.ambari.server.state.kerberos.KerberosServiceDescriptor;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
-import org.easymock.Capture;
-import org.easymock.EasyMock;
-import org.easymock.EasyMockSupport;
-import org.easymock.IMocksControl;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-import javax.persistence.EntityManager;
-import java.io.File;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.net.URL;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertNull;
-import static org.easymock.EasyMock.anyLong;
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.anyString;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.createMockBuilder;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
-import static org.junit.Assert.assertTrue;
-/**
- * {@link org.apache.ambari.server.upgrade.UpgradeCatalog220} unit tests.
- */
-public class UpgradeCatalog220Test {
-  private static Injector injector;
-  private static Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
-  private static EntityManager entityManager = createNiceMock(EntityManager.class);
-  private static UpgradeCatalogHelper upgradeCatalogHelper;
-  private static StackEntity desiredStackEntity;
-  private AmbariManagementController amc = createNiceMock(AmbariManagementController.class);
-  private AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class);
-  private StackDAO stackDAO = createNiceMock(StackDAO.class);
-  private RepositoryVersionDAO repositoryVersionDAO = createNiceMock(RepositoryVersionDAO.class);
-  private ClusterVersionDAO clusterVersionDAO = createNiceMock(ClusterVersionDAO.class);
-  private HostVersionDAO hostVersionDAO = createNiceMock(HostVersionDAO.class);
-  private ClusterDAO clusterDAO = createNiceMock(ClusterDAO.class);
-
-  private IMocksControl mocksControl = EasyMock.createControl();
-
-  @BeforeClass
-  public static void init() {
-    reset(entityManagerProvider);
-    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
-    replay(entityManagerProvider);
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-
-    upgradeCatalogHelper = injector.getInstance(UpgradeCatalogHelper.class);
-    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
-    injector.getInstance(AmbariMetaInfo.class);
-    // load the stack entity
-    StackDAO stackDAO = injector.getInstance(StackDAO.class);
-    desiredStackEntity = stackDAO.find("HDP", "2.2.0");
-  }
-
-  @AfterClass
-  public static void tearDown() throws AmbariException, SQLException {
-    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
-  }
-
-  @Test
-  public void testExecuteUpgradeDDLUpdates() throws Exception{
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-
-    dbAccessor.addColumn(eq("upgrade"), anyObject(DBAccessor.DBColumnInfo.class));
-    expectLastCall().times(3);
-
-    replay(dbAccessor);
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        binder.bind(EntityManager.class).toInstance(entityManager);
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    UpgradeCatalog220 upgradeCatalog220 = injector.getInstance(UpgradeCatalog220.class);
-    upgradeCatalog220.executeUpgradeDDLUpdates();
-    verify(dbAccessor);
-  }
-
-  @Test
-  public void testExecuteStageDDLUpdates() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-
-    dbAccessor.addColumn(eq("stage"), anyObject(DBAccessor.DBColumnInfo.class));
-    expectLastCall().times(1);
-
-    replay(dbAccessor);
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        binder.bind(EntityManager.class).toInstance(entityManager);
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    UpgradeCatalog220 upgradeCatalog220 = injector.getInstance(UpgradeCatalog220.class);
-    upgradeCatalog220.executeStageDDLUpdates();
-    verify(dbAccessor);
-  }
-
-  @Test
-  public void testExecuteDMLUpdates() throws Exception {
-    // TODO AMBARI-13001, readd unit test section.
-    /*
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    Configuration configuration = createNiceMock(Configuration.class);
-    Connection connection = createNiceMock(Connection.class);
-    Statement statement = createNiceMock(Statement.class);
-    ResultSet resultSet = createNiceMock(ResultSet.class);
-    expect(configuration.getDatabaseUrl()).andReturn(Configuration.JDBC_IN_MEMORY_URL).anyTimes();
-    dbAccessor.getConnection();
-    expectLastCall().andReturn(connection).anyTimes();
-    connection.createStatement();
-    expectLastCall().andReturn(statement).anyTimes();
-    statement.executeQuery(anyObject(String.class));
-    expectLastCall().andReturn(resultSet).anyTimes();
-
-    // Technically, this is a DDL, but it has to be ran during the DML portion
-    // because it requires the persistence layer to be started.
-    UpgradeSectionDDL upgradeSectionDDL = new UpgradeSectionDDL();
-
-    // Execute any DDL schema changes
-    upgradeSectionDDL.execute(dbAccessor);
-
-    // Begin DML verifications
-    verifyBootstrapHDP21();
-
-    // Replay main sections
-    replay(dbAccessor, configuration, resultSet, connection, statement);
-
-
-    AbstractUpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
-    Class<?> c = AbstractUpgradeCatalog.class;
-    Field f = c.getDeclaredField("configuration");
-    f.setAccessible(true);
-    f.set(upgradeCatalog, configuration);
-    */
-
-    Method updateStormConfigs = UpgradeCatalog220.class.getDeclaredMethod("updateStormConfigs");
-    Method updateAMSConfigs = UpgradeCatalog220.class.getDeclaredMethod("updateAMSConfigs");
-    Method updateHDFSConfigs = UpgradeCatalog220.class.getDeclaredMethod("updateHDFSConfigs");
-    Method updateKafkaConfigs = UpgradeCatalog220.class.getDeclaredMethod("updateKafkaConfigs");
-    Method addNewConfigurationsFromXml = AbstractUpgradeCatalog.class.getDeclaredMethod("addNewConfigurationsFromXml");
-    Method updateHbaseEnvConfig = UpgradeCatalog220.class.getDeclaredMethod("updateHbaseEnvConfig");
-    Method updateFlumeEnvConfig = UpgradeCatalog220.class.getDeclaredMethod("updateFlumeEnvConfig");
-    Method updateZookeeperLog4j = UpgradeCatalog220.class.getDeclaredMethod("updateZookeeperLog4j");
-    Method updateHadoopEnvConfig = UpgradeCatalog220.class.getDeclaredMethod("updateHadoopEnv");
-    Method updateAlertDefinitions = UpgradeCatalog220.class.getDeclaredMethod("updateAlertDefinitions");
-    Method updateRangerEnvConfig = UpgradeCatalog220.class.getDeclaredMethod("updateRangerEnvConfig");
-    Method updateRangerUgsyncSiteConfig = UpgradeCatalog220.class.getDeclaredMethod("updateRangerUgsyncSiteConfig");
-    Method updateHiveConfig = UpgradeCatalog220.class.getDeclaredMethod("updateHiveConfig");
-    Method updateAccumuloConfigs = UpgradeCatalog220.class.getDeclaredMethod("updateAccumuloConfigs");
-    Method updateKerberosDescriptorArtifacts = AbstractUpgradeCatalog.class.getDeclaredMethod("updateKerberosDescriptorArtifacts");
-    Method updateKnoxTopology = UpgradeCatalog220.class.getDeclaredMethod("updateKnoxTopology");
-
-    UpgradeCatalog220 upgradeCatalog220 = createMockBuilder(UpgradeCatalog220.class)
-      .addMockedMethod(updateAMSConfigs)
-      .addMockedMethod(updateHDFSConfigs)
-      .addMockedMethod(updateStormConfigs)
-      .addMockedMethod(addNewConfigurationsFromXml)
-      .addMockedMethod(updateHbaseEnvConfig)
-      .addMockedMethod(updateFlumeEnvConfig)
-      .addMockedMethod(updateAlertDefinitions)
-      .addMockedMethod(updateKafkaConfigs)
-      .addMockedMethod(updateZookeeperLog4j)
-      .addMockedMethod(updateHadoopEnvConfig)
-      .addMockedMethod(updateRangerEnvConfig)
-      .addMockedMethod(updateRangerUgsyncSiteConfig)
-      .addMockedMethod(updateHiveConfig)
-      .addMockedMethod(updateAccumuloConfigs)
-      .addMockedMethod(updateKerberosDescriptorArtifacts)
-      .addMockedMethod(updateKnoxTopology)
-      .createMock();
-
-    upgradeCatalog220.updateHbaseEnvConfig();
-    expectLastCall().once();
-    upgradeCatalog220.updateFlumeEnvConfig();
-    upgradeCatalog220.addNewConfigurationsFromXml();
-    expectLastCall().once();
-    upgradeCatalog220.updateStormConfigs();
-    expectLastCall().once();
-    upgradeCatalog220.updateHadoopEnv();
-    expectLastCall().once();
-    upgradeCatalog220.updateAMSConfigs();
-    expectLastCall().once();
-    upgradeCatalog220.updateAlertDefinitions();
-    expectLastCall().once();
-    upgradeCatalog220.updateKafkaConfigs();
-    expectLastCall().once();
-    upgradeCatalog220.updateHDFSConfigs();
-    expectLastCall().once();
-    upgradeCatalog220.updateZookeeperLog4j();
-    expectLastCall().once();
-    upgradeCatalog220.updateRangerEnvConfig();
-    expectLastCall().once();
-    upgradeCatalog220.updateRangerUgsyncSiteConfig();
-    expectLastCall().once();
-    upgradeCatalog220.updateHiveConfig();
-    expectLastCall().once();
-    upgradeCatalog220.updateAccumuloConfigs();
-    expectLastCall().once();
-    upgradeCatalog220.updateKnoxTopology();
-    expectLastCall().once();
-    upgradeCatalog220.updateKerberosDescriptorArtifacts();
-    expectLastCall().once();
-
-    replay(upgradeCatalog220);
-
-    upgradeCatalog220.executeDMLUpdates();
-
-    verify(upgradeCatalog220);
-  }
-
-  /**
-   * Verify that when bootstrapping HDP 2.1, records get inserted into the
-   * repo_version, cluster_version, and host_version tables.
-   * @throws AmbariException
-   */
-  private void verifyBootstrapHDP21() throws Exception, AmbariException {
-    final String stackName = "HDP";
-    final String stackVersion = "2.1";
-    final String stackNameAndVersion = stackName + "-" + stackVersion;
-    final String buildNumber = "2.1.0.0-0001";
-    final String stackAndBuild = stackName + "-" + buildNumber;
-    final String clusterName = "c1";
-
-    expect(amc.getAmbariMetaInfo()).andReturn(metaInfo);
-
-    // Mock the actions to bootstrap if using HDP 2.1
-    Clusters clusters = createNiceMock(Clusters.class);
-    expect(amc.getClusters()).andReturn(clusters);
-
-    Map<String, Cluster> clusterHashMap = new HashMap<String, Cluster>();
-    Cluster cluster = createNiceMock(Cluster.class);
-    clusterHashMap.put(clusterName, cluster);
-    expect(clusters.getClusters()).andReturn(clusterHashMap);
-
-    StackId stackId = new StackId(stackNameAndVersion);
-    expect(cluster.getCurrentStackVersion()).andReturn(stackId);
-
-    StackInfo stackInfo = new StackInfo();
-    stackInfo.setVersion(buildNumber);
-    expect(metaInfo.getStack(stackName, stackVersion)).andReturn(stackInfo);
-
-    StackEntity stackEntity = createNiceMock(StackEntity.class);
-    expect(stackEntity.getStackName()).andReturn(stackName);
-    expect(stackEntity.getStackVersion()).andReturn(stackVersion);
-
-    expect(stackDAO.find(stackName, stackVersion)).andReturn(stackEntity);
-
-    replay(amc, metaInfo, clusters, cluster, stackEntity, stackDAO);
-
-    // Mock more function calls
-    // Repository Version
-    RepositoryVersionEntity repositoryVersionEntity = createNiceMock(RepositoryVersionEntity.class);
-    expect(repositoryVersionDAO.findByDisplayName(stackAndBuild)).andReturn(null);
-    expect(repositoryVersionDAO.findMaxId("id")).andReturn(0L);
-    expect(repositoryVersionDAO.findAll()).andReturn(Collections.<RepositoryVersionEntity>emptyList());
-    expect(repositoryVersionDAO.create(anyObject(StackEntity.class), anyObject(String.class), anyObject(String.class), anyObject(String.class))).andReturn(repositoryVersionEntity);
-    expect(repositoryVersionEntity.getId()).andReturn(1L);
-    expect(repositoryVersionEntity.getVersion()).andReturn(buildNumber);
-    replay(repositoryVersionDAO, repositoryVersionEntity);
-
-    // Cluster Version
-    ClusterVersionEntity clusterVersionEntity = createNiceMock(ClusterVersionEntity.class);
-    expect(clusterVersionEntity.getId()).andReturn(1L);
-    expect(clusterVersionEntity.getState()).andReturn(RepositoryVersionState.CURRENT);
-    expect(clusterVersionEntity.getRepositoryVersion()).andReturn(repositoryVersionEntity);
-
-    expect(clusterVersionDAO.findByClusterAndStackAndVersion(anyObject(String.class), anyObject(StackId.class), anyObject(String.class))).andReturn(null);
-    expect(clusterVersionDAO.findMaxId("id")).andReturn(0L);
-    expect(clusterVersionDAO.findAll()).andReturn(Collections.<ClusterVersionEntity>emptyList());
-    expect(clusterVersionDAO.create(anyObject(ClusterEntity.class), anyObject(RepositoryVersionEntity.class), anyObject(RepositoryVersionState.class), anyLong(), anyLong(), anyObject(String.class))).andReturn(clusterVersionEntity);
-    replay(clusterVersionDAO, clusterVersionEntity);
-
-    // Host Version
-    ClusterEntity clusterEntity = createNiceMock(ClusterEntity.class);
-    expect(clusterEntity.getClusterName()).andReturn(clusterName).anyTimes();
-    expect(clusterDAO.findByName(anyObject(String.class))).andReturn(clusterEntity);
-
-    Collection<HostEntity> hostEntities = new ArrayList<HostEntity>();
-    HostEntity hostEntity1 = createNiceMock(HostEntity.class);
-    HostEntity hostEntity2 = createNiceMock(HostEntity.class);
-    expect(hostEntity1.getHostName()).andReturn("host1");
-    expect(hostEntity2.getHostName()).andReturn("host2");
-    hostEntities.add(hostEntity1);
-    hostEntities.add(hostEntity2);
-    expect(clusterEntity.getHostEntities()).andReturn(hostEntities);
-
-    expect(hostVersionDAO.findByClusterStackVersionAndHost(anyObject(String.class), anyObject(StackId.class), anyObject(String.class), anyObject(String.class))).andReturn(null);
-    expect(hostVersionDAO.findMaxId("id")).andReturn(0L);
-    expect(hostVersionDAO.findAll()).andReturn(Collections.<HostVersionEntity>emptyList());
-
-    replay(clusterEntity, clusterDAO, hostVersionDAO, hostEntity1, hostEntity2);
-  }
-
-  @Test
-  public void testExecuteUpgradePreDMLUpdates() throws Exception {
-    Method executeStackPreDMLUpdates = UpgradeCatalog220.class.getDeclaredMethod("executeUpgradePreDMLUpdates");
-    Method executeStackUpgradeDDLUpdates = UpgradeCatalog220.class.getDeclaredMethod("executeStackUpgradeDDLUpdates");
-    Method bootstrapRepoVersionForHDP21 = UpgradeCatalog220.class.getDeclaredMethod("bootstrapRepoVersionForHDP21");
-
-    final UpgradeCatalog220 upgradeCatalog220 = createMockBuilder(UpgradeCatalog220.class)
-      .addMockedMethod(executeStackUpgradeDDLUpdates)
-      .addMockedMethod(bootstrapRepoVersionForHDP21)
-      .addMockedMethod(executeStackPreDMLUpdates).createMock();
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(UpgradeCatalog220.class).toInstance(upgradeCatalog220);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        bind(EntityManager.class).toInstance(entityManager);
-      }
-    });
-
-    upgradeCatalog220.executeUpgradePreDMLUpdates();
-    expectLastCall().once();
-
-    upgradeCatalog220.executeStackUpgradeDDLUpdates();
-    expectLastCall().once();
-
-    upgradeCatalog220.bootstrapRepoVersionForHDP21();
-    expectLastCall().once();
-
-    replay(upgradeCatalog220);
-    mockInjector.getInstance(UpgradeCatalog220.class).executePreDMLUpdates();
-
-    verify(upgradeCatalog220);
-  }
-
-  @Test
-  public void testUpdateStormSiteConfigs() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Map<String, String> propertiesStormSite = new HashMap<String, String>() {
-      {
-        put("nimbus.monitor.freq.secs", "10");
-        put("metrics.reporter.register", "org.apache.hadoop.metrics2.sink.storm.StormTimelineMetricsReporter");
-      }
-    };
-
-    final Config mockStormSite = easyMockSupport.createNiceMock(Config.class);
-    expect(mockStormSite.getProperties()).andReturn(propertiesStormSite).once();
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    expect(mockClusterExpected.getDesiredConfigByType("storm-site")).andReturn(mockStormSite).atLeastOnce();
-    expect(mockStormSite.getProperties()).andReturn(propertiesStormSite).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog220.class).updateStormConfigs();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateKerberosDescriptorArtifact() throws Exception {
-    final KerberosDescriptorFactory kerberosDescriptorFactory = new KerberosDescriptorFactory();
-
-    KerberosServiceDescriptor serviceDescriptor;
-
-    URL systemResourceURL = ClassLoader.getSystemResource("kerberos/test_kerberos_descriptor_2_1_3.json");
-    assertNotNull(systemResourceURL);
-
-    final KerberosDescriptor kerberosDescriptorOrig = kerberosDescriptorFactory.createInstance(new File(systemResourceURL.getFile()));
-    assertNotNull(kerberosDescriptorOrig);
-
-    serviceDescriptor = kerberosDescriptorOrig.getService("HDFS");
-    assertNotNull(serviceDescriptor);
-    assertNotNull(serviceDescriptor.getIdentity("hdfs"));
-
-    serviceDescriptor = kerberosDescriptorOrig.getService("OOZIE");
-    assertNotNull(serviceDescriptor);
-    assertNotNull(serviceDescriptor.getIdentity("/HDFS/hdfs"));
-
-    UpgradeCatalog220 upgradeMock = createMockBuilder(UpgradeCatalog220.class).createMock();
-
-    Capture<Map<String, Object>> updatedData = new Capture<Map<String, Object>>();
-
-    ArtifactEntity artifactEntity = createNiceMock(ArtifactEntity.class);
-    expect(artifactEntity.getArtifactData())
-        .andReturn(kerberosDescriptorOrig.toMap())
-        .once();
-
-    artifactEntity.setArtifactData(capture(updatedData));
-    expectLastCall().once();
-
-    replay(artifactEntity, upgradeMock);
-    upgradeMock.updateKerberosDescriptorArtifact(createNiceMock(ArtifactDAO.class), artifactEntity);
-    verify(artifactEntity, upgradeMock);
-
-    KerberosDescriptor kerberosDescriptorUpdated = new KerberosDescriptorFactory().createInstance(updatedData.getValue());
-    assertNotNull(kerberosDescriptorUpdated);
-
-    serviceDescriptor = kerberosDescriptorUpdated.getService("HDFS");
-    assertNotNull(serviceDescriptor);
-    assertNull(serviceDescriptor.getIdentity("hdfs"));
-
-    KerberosComponentDescriptor namenodeComponent = serviceDescriptor.getComponent("NAMENODE");
-    assertNotNull(namenodeComponent.getIdentity("hdfs"));
-
-    serviceDescriptor = kerberosDescriptorUpdated.getService("OOZIE");
-    assertNotNull(serviceDescriptor);
-    assertNull(serviceDescriptor.getIdentity("/HDFS/hdfs"));
-    assertNotNull(serviceDescriptor.getIdentity("/HDFS/NAMENODE/hdfs"));
-
-    // check execution with empty kerberos descriptor
-    KerberosDescriptor kerberosDescriptor= new KerberosDescriptorFactory().createInstance(kerberosDescriptorOrig.toMap());
-    ArtifactEntity artifactEntityOrig = createNiceMock(ArtifactEntity.class);
-
-    kerberosDescriptor.getService("HDFS").removeIdentity("hdfs");
-
-    expect(artifactEntityOrig.getArtifactData()).andReturn(kerberosDescriptor.toMap()).once();
-   //expect(artifactDAO.merge((ArtifactEntity) anyObject())).andReturn(null).atLeastOnce();
-    replay(artifactEntityOrig);
-
-    upgradeMock.updateKerberosDescriptorArtifact(createNiceMock(ArtifactDAO.class), artifactEntityOrig);
-    verify(artifactEntityOrig);
-  }
-
-
-
-  @Test
-  public void testUpdateHbaseEnvConfig() throws AmbariException {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Map<String, String> propertiesHbaseEnv = new HashMap<String, String>() {
-      {
-        put("content", "test");
-      }
-    };
-
-    final Config mockHbaseEnv = easyMockSupport.createNiceMock(Config.class);
-    expect(mockHbaseEnv.getProperties()).andReturn(propertiesHbaseEnv).once();
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getCurrentStackVersion()).andReturn(new StackId("HDP", "2.2"));
-
-    expect(mockClusterExpected.getDesiredConfigByType("hbase-env")).andReturn(mockHbaseEnv).atLeastOnce();
-    expect(mockHbaseEnv.getProperties()).andReturn(propertiesHbaseEnv).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog220.class).updateHbaseEnvConfig();
-    easyMockSupport.verifyAll();
-
-  }
-
-  @Test
-  public void testUpdateHDFSConfiguration() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Config mockHdfsSite = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> propertiesExpectedHdfs = new HashMap<String, String>();
-    propertiesExpectedHdfs.put("dfs.namenode.rpc-address", "nn.rpc.address");
-    propertiesExpectedHdfs.put("dfs.nameservices", "nn1");
-    propertiesExpectedHdfs.put("dfs.ha.namenodes.nn1", "value");
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    // Expected operation
-    expect(mockClusterExpected.getDesiredConfigByType("hdfs-site")).andReturn(mockHdfsSite).atLeastOnce();
-    expect(mockHdfsSite.getProperties()).andReturn(propertiesExpectedHdfs).anyTimes();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog220.class).updateHDFSConfigs();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateAmsHbaseEnvContent() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
-    Method updateAmsHbaseEnvContent = UpgradeCatalog220.class.getDeclaredMethod("updateAmsHbaseEnvContent", String.class);
-    UpgradeCatalog220 upgradeCatalog220 = new UpgradeCatalog220(injector);
-    String oldContent = "export HBASE_CLASSPATH=${HBASE_CLASSPATH}\n" +
-      "\n" +
-      "# The maximum amount of heap to use, in MB. Default is 1000.\n" +
-      "export HBASE_HEAPSIZE={{hbase_heapsize}}\n";
-
-    String expectedContent = "export HBASE_CLASSPATH=${HBASE_CLASSPATH}\n" +
-      "\n" +
-      "# The maximum amount of heap to use, in MB. Default is 1000.\n" +
-      "#export HBASE_HEAPSIZE={{hbase_heapsize}}\n" +
-      "\n" +
-      "# The maximum amount of heap to use for hbase shell.\n" +
-      "export HBASE_SHELL_OPTS=\"-Xmx256m\"\n";
-    String result = (String) updateAmsHbaseEnvContent.invoke(upgradeCatalog220, oldContent);
-    Assert.assertEquals(expectedContent, result);
-  }
-
-  @Test
-  public void testAmsSiteUpdateConfigs() throws Exception{
-
-    Map<String, String> oldPropertiesAmsSite = new HashMap<String, String>() {
-      {
-        //Including only those properties that might be present in an older version.
-        put("timeline.metrics.service.default.result.limit", String.valueOf(5760));
-        put("timeline.metrics.cluster.aggregator.minute.interval", String.valueOf(1000));
-        put("timeline.metrics.host.aggregator.minute.interval", String.valueOf(1000));
-        put("timeline.metrics.cluster.aggregator.minute.ttl", String.valueOf(1000));
-      }
-    };
-    Map<String, String> newPropertiesAmsSite = new HashMap<String, String>() {
-      {
-        put("timeline.metrics.service.default.result.limit", String.valueOf(15840));
-        put("timeline.metrics.cluster.aggregator.second.interval", String.valueOf(120));
-        put("timeline.metrics.cluster.aggregator.minute.interval", String.valueOf(300));
-        put("timeline.metrics.host.aggregator.minute.interval", String.valueOf(300));
-        put("timeline.metrics.cluster.aggregator.second.ttl", String.valueOf(2592000));
-        put("timeline.metrics.cluster.aggregator.minute.ttl", String.valueOf(7776000));
-        put("timeline.metrics.cluster.aggregator.second.checkpointCutOffMultiplier", String.valueOf(2));
-        put("timeline.metrics.cluster.aggregator.second.disabled", String.valueOf(false));
-        put("timeline.metrics.hbase.fifo.compaction.enabled", String.valueOf(true));
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    Config mockAmsSite = easyMockSupport.createNiceMock(Config.class);
-
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("ams-site")).andReturn(mockAmsSite).atLeastOnce();
-    expect(mockAmsSite.getProperties()).andReturn(oldPropertiesAmsSite).times(2);
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockAmsSite, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-      .addMockedMethod("createConfiguration")
-      .addMockedMethod("getClusters", new Class[] { })
-      .addMockedMethod("createConfig")
-      .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-      .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-        anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog220(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesAmsSite, updatedProperties).areEqual());
-
-  }
-
-  @Test
-  public void testAmsHbaseSiteUpdateConfigs() throws Exception{
-
-    Map<String, String> oldPropertiesAmsHbaseSite = new HashMap<String, String>() {
-      {
-        //Including only those properties that might be present in an older version.
-        put("zookeeper.session.timeout.localHBaseCluster", String.valueOf(20000));
-      }
-    };
-    Map<String, String> newPropertiesAmsSite = new HashMap<String, String>() {
-      {
-        put("zookeeper.session.timeout.localHBaseCluster", String.valueOf(120000));
-        put("hbase.normalizer.enabled", String.valueOf(true));
-        put("hbase.normalizer.period", String.valueOf(600000));
-        put("hbase.master.normalizer.class", "org.apache.hadoop.hbase.master.normalizer.SimpleRegionNormalizer");
-
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    Config mockAmsHbaseSite = easyMockSupport.createNiceMock(Config.class);
-
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("ams-hbase-site")).andReturn(mockAmsHbaseSite).atLeastOnce();
-    expect(mockAmsHbaseSite.getProperties()).andReturn(oldPropertiesAmsHbaseSite).atLeastOnce();
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockAmsHbaseSite, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-      .addMockedMethod("createConfiguration")
-      .addMockedMethod("getClusters", new Class[] { })
-      .addMockedMethod("createConfig")
-      .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-      .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-        anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog220(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesAmsSite, updatedProperties).areEqual());
-  }
-
-  @Test
-  public void testUpdateAlertDefinitions() {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    UpgradeCatalog220 upgradeCatalog220 = new UpgradeCatalog220(injector);
-    long clusterId = 1;
-
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final AlertDefinitionEntity mockJournalNodeProcessAlertDefinitionEntity = easyMockSupport.createNiceMock(AlertDefinitionEntity.class);
-    final AlertDefinitionEntity mockHostDiskUsageAlertDefinitionEntity = easyMockSupport.createNiceMock(AlertDefinitionEntity.class);
-
-    final String journalNodeProcessAlertSource = "{\"uri\":\"{{hdfs-site/dfs.journalnode.http-address}}\",\"default_port\":8480," +
-        "\"type\":\"PORT\",\"reporting\":{\"ok\":{\"text\":\"TCP OK - {0:.3f}s response on port {1}\"}," +
-        "\"warning\":{\"text\":\"TCP OK - {0:.3f}s response on port {1}\",\"value\":1.5}," +
-        "\"critical\":{\"text\":\"Connection failed: {0} to {1}:{2}\",\"value\":5.0}}}";
-    final String journalNodeProcessAlertSourceExpected = "{\"reporting\":{\"ok\":{\"text\":\"HTTP {0} response in {2:.3f}s\"}," +
-        "\"warning\":{\"text\":\"HTTP {0} response from {1} in {2:.3f}s ({3})\"}," +
-        "\"critical\":{\"text\":\"Connection failed to {1} ({3})\"}},\"type\":\"WEB\"," +
-        "\"uri\":{\"http\":\"{{hdfs-site/dfs.journalnode.http-address}}\"," +
-        "\"https\":\"{{hdfs-site/dfs.journalnode.https-address}}\"," +
-        "\"kerberos_keytab\":\"{{hdfs-site/dfs.web.authentication.kerberos.keytab}}\","+
-        "\"kerberos_principal\":\"{{hdfs-site/dfs.web.authentication.kerberos.principal}}\"," +
-        "\"https_property\":\"{{hdfs-site/dfs.http.policy}}\"," +
-        "\"https_property_value\":\"HTTPS_ONLY\",\"connection_timeout\":5.0}}";
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-        bind(AlertDefinitionDAO.class).toInstance(mockAlertDefinitionDAO);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-
-    expect(mockClusterExpected.getClusterId()).andReturn(clusterId).anyTimes();
-
-    expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("journalnode_process"))).andReturn(mockJournalNodeProcessAlertDefinitionEntity).atLeastOnce();
-    expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("ambari_agent_disk_usage"))).andReturn(mockHostDiskUsageAlertDefinitionEntity).atLeastOnce();
-
-    expect(mockJournalNodeProcessAlertDefinitionEntity.getSource()).andReturn(journalNodeProcessAlertSource).atLeastOnce();
-    Assert.assertEquals(journalNodeProcessAlertSourceExpected, upgradeCatalog220.modifyJournalnodeProcessAlertSource(journalNodeProcessAlertSource));
-
-    mockHostDiskUsageAlertDefinitionEntity.setDescription(eq("This host-level alert is triggered if the amount of disk space " +
-        "used goes above specific thresholds. The default threshold values are 50% for WARNING and 80% for CRITICAL."));
-    expectLastCall().atLeastOnce();
-    mockHostDiskUsageAlertDefinitionEntity.setLabel(eq("Host Disk Usage"));
-    expectLastCall().atLeastOnce();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog220.class).updateAlertDefinitions();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateAmsEnvContent() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
-    Method updateAmsEnvContent = UpgradeCatalog220.class.getDeclaredMethod("updateAmsEnvContent", String.class);
-    UpgradeCatalog220 upgradeCatalog220 = new UpgradeCatalog220(injector);
-    String oldContent = "some_content";
-
-    String expectedContent = "some_content" + "\n" +
-      "# AMS Collector GC options\n" +
-      "export AMS_COLLECTOR_GC_OPTS=\"-XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70 " +
-      "-XX:+UseCMSInitiatingOccupancyOnly -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps " +
-      "-XX:+UseGCLogFileRotation -XX:GCLogFileSize=10M " +
-      "-Xloggc:{{ams_collector_log_dir}}/collector-gc.log-`date +'%Y%m%d%H%M'`\"\n" +
-      "export AMS_COLLECTOR_OPTS=\"$AMS_COLLECTOR_OPTS $AMS_COLLECTOR_GC_OPTS\"\n"+
-      "\n" +
-      "# HBase normalizer enabled\n" +
-      "export AMS_HBASE_NORMALIZER_ENABLED={{ams_hbase_normalizer_enabled}}\n" +
-      "\n" +
-      "# HBase compaction policy enabled\n" +
-      "export AMS_HBASE_FIFO_COMPACTION_ENABLED={{ams_hbase_fifo_compaction_enabled}}\n";
-
-    String result = (String) updateAmsEnvContent.invoke(upgradeCatalog220, oldContent);
-    Assert.assertEquals(expectedContent, result);
-  }
-
-  public void testUpdateKafkaConfigs() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final ConfigurationResponse mockConfigurationResponse = easyMockSupport.createMock(ConfigurationResponse.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Map<String, String> propertiesKafkaEnv = new HashMap<String, String>() {
-      {
-        put("content", "test");
-      }
-    };
-    Map<String, String> updates = Collections.singletonMap("content", "test\n\nexport KAFKA_KERBEROS_PARAMS=\"$KAFKA_KERBEROS_PARAMS {{kafka_kerberos_params}}");
-
-    final Map<String, String> propertiesAmsEnv = new HashMap<String, String>() {
-      {
-        put("kafka.metrics.reporters", "{{kafka_metrics_reporters}}");
-      }
-    };
-    final Map<String, Service> installedServices = new HashMap<String, Service>() {
-      {
-        put("KAFKA", null);
-        put("AMBARI_METRICS", null);
-      }
-    };
-
-    final Config mockAmsEnv = easyMockSupport.createNiceMock(Config.class);
-    final Config mockKafkaEnv = easyMockSupport.createNiceMock(Config.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    expect(mockClusterExpected.getServices()).andReturn(installedServices).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("kafka-broker")).andReturn(mockAmsEnv).atLeastOnce();
-    expect(mockAmsEnv.getProperties()).andReturn(propertiesAmsEnv).atLeastOnce();
-
-    expect(mockClusterExpected.getDesiredConfigByType("kafka-env")).andReturn(mockKafkaEnv).atLeastOnce();
-    expect(mockKafkaEnv.getProperties()).andReturn(propertiesKafkaEnv).atLeastOnce();
-
-    UpgradeCatalog220 upgradeCatalog220 = createMockBuilder(UpgradeCatalog220.class)
-        .withConstructor(Injector.class)
-        .withArgs(mockInjector)
-        .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-            Map.class, boolean.class, boolean.class)
-        .createMock();
-    upgradeCatalog220.updateConfigurationPropertiesForCluster(mockClusterExpected,
-      "kafka-env", updates, true, false);
-    expectLastCall().once();
-
-    expect(mockAmbariManagementController.createConfiguration(EasyMock.<ConfigurationRequest>anyObject())).andReturn(mockConfigurationResponse);
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog220.class).updateKafkaConfigs();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateFlumeEnvConfig() throws AmbariException {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Map<String, String> propertiesFlumeEnv = new HashMap<String, String>() {
-      {
-        put("content", "test");
-      }
-    };
-
-    final Config mockFlumeEnv = easyMockSupport.createNiceMock(Config.class);
-    expect(mockFlumeEnv.getProperties()).andReturn(propertiesFlumeEnv).once();
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-
-    expect(mockClusterExpected.getDesiredConfigByType("flume-env")).andReturn(mockFlumeEnv).atLeastOnce();
-    expect(mockFlumeEnv.getProperties()).andReturn(propertiesFlumeEnv).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog220.class).updateFlumeEnvConfig();
-    easyMockSupport.verifyAll();
-  }
-
-  /**
-   * @param dbAccessor
-   * @return
-   */
-  private AbstractUpgradeCatalog getUpgradeCatalog(final DBAccessor dbAccessor) {
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(EntityManager.class).toInstance(entityManager);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        binder.bind(DaoUtils.class).toInstance(createNiceMock(DaoUtils.class));
-        binder.bind(ClusterDAO.class).toInstance(clusterDAO);
-        binder.bind(RepositoryVersionHelper.class).toInstance(createNiceMock(RepositoryVersionHelper.class));
-        binder.bind(Clusters.class).toInstance(createNiceMock(Clusters.class));
-        binder.bind(AmbariManagementController.class).toInstance(amc);
-        binder.bind(AmbariMetaInfo.class).toInstance(metaInfo);
-        binder.bind(StackManagerFactory.class).toInstance(createNiceMock(StackManagerFactory.class));
-        binder.bind(StackDAO.class).toInstance(stackDAO);
-        binder.bind(RepositoryVersionDAO.class).toInstance(repositoryVersionDAO);
-        binder.bind(ClusterVersionDAO.class).toInstance(clusterVersionDAO);
-        binder.bind(HostVersionDAO.class).toInstance(hostVersionDAO);
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    return injector.getInstance(UpgradeCatalog220.class);
-  }
-
-  @Test
-  public void testUpdateZookeeperLog4jConfig() throws AmbariException {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Map<String, String> propertiesZookeeperLog4j = new HashMap<String, String>() {
-      {
-        put("content", "log4j.rootLogger=INFO, CONSOLE");
-      }
-    };
-
-    final Config mockZookeeperLog4j = easyMockSupport.createNiceMock(Config.class);
-    expect(mockZookeeperLog4j.getProperties()).andReturn(propertiesZookeeperLog4j).once();
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-
-    expect(mockClusterExpected.getDesiredConfigByType("zookeeper-log4j")).andReturn(mockZookeeperLog4j).atLeastOnce();
-    expect(mockZookeeperLog4j.getProperties()).andReturn(propertiesZookeeperLog4j).atLeastOnce();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog220.class).updateZookeeperLog4j();
-    easyMockSupport.verifyAll();
-
-  }
-
-  @Test
-  public void testUpdateRangerEnvConfig() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Map<String, String> propertiesHiveEnv = new HashMap<String, String>() {{
-        put("hive_security_authorization", "Ranger");
-    }};
-    final Map<String, String> propertiesRangerHdfsPlugin = new HashMap<String, String>() {{
-      put("ranger-hdfs-plugin-enabled", "Yes");
-    }};
-    final Map<String, String> propertiesRangerHbasePlugin = new HashMap<String, String>() {{
-      put("ranger-hbase-plugin-enabled", "Yes");
-    }};
-    final Map<String, String> propertiesRangerKafkaPlugin = new HashMap<String, String>() {{
-      put("ranger-kafka-plugin-enabled", "Yes");
-    }};
-    final Map<String, String> propertiesRangerYarnPlugin = new HashMap<String, String>() {{
-      put("ranger-yarn-plugin-enabled", "No");
-    }};
-
-    final Config mockHiveEnvConf = easyMockSupport.createNiceMock(Config.class);
-    final Config mockRangerHdfsPluginConf = easyMockSupport.createNiceMock(Config.class);
-    final Config mockRangerHbasePluginConf = easyMockSupport.createNiceMock(Config.class);
-    final Config mockRangerKafkaPluginConf = easyMockSupport.createNiceMock(Config.class);
-    final Config mockRangerYarnPluginConf = easyMockSupport.createNiceMock(Config.class);
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnvConf).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("ranger-hdfs-plugin-properties")).andReturn(mockRangerHdfsPluginConf).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("ranger-hbase-plugin-properties")).andReturn(mockRangerHbasePluginConf).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("ranger-kafka-plugin-properties")).andReturn(mockRangerKafkaPluginConf).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("ranger-yarn-plugin-properties")).andReturn(mockRangerYarnPluginConf).atLeastOnce();
-
-    expect(mockHiveEnvConf.getProperties()).andReturn(propertiesHiveEnv).times(2);
-    expect(mockRangerHdfsPluginConf.getProperties()).andReturn(propertiesRangerHdfsPlugin).times(2);
-    expect(mockRangerHbasePluginConf.getProperties()).andReturn(propertiesRangerHbasePlugin).times(2);
-    expect(mockRangerKafkaPluginConf.getProperties()).andReturn(propertiesRangerKafkaPlugin).times(2);
-    expect(mockRangerYarnPluginConf.getProperties()).andReturn(propertiesRangerYarnPlugin).times(2);
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog220.class).updateRangerEnvConfig();
-    easyMockSupport.verifyAll();
-
-  }
-
-  @Test
-  public void testGetSourceVersion() {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    UpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
-    Assert.assertEquals("2.1.2.1", upgradeCatalog.getSourceVersion());
-  }
-
-  @Test
-  public void testGetTargetVersion() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    UpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
-
-    Assert.assertEquals("2.2.0", upgradeCatalog.getTargetVersion());
-  }
-
-  // *********** Inner Classes that represent sections of the DDL ***********
-  // ************************************************************************
-
-  /**
-   * Verify that the upgrade table has two columns added to it.
-   */
-  class UpgradeSectionDDL implements SectionDDL {
-
-    Capture<DBAccessor.DBColumnInfo> upgradeTablePackageNameColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-    Capture<DBAccessor.DBColumnInfo> upgradeTableUpgradeTypeColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void execute(DBAccessor dbAccessor) throws SQLException {
-      // Add columns
-      dbAccessor.addColumn(eq("upgrade"), capture(upgradeTablePackageNameColumnCapture));
-      dbAccessor.addColumn(eq("upgrade"), capture(upgradeTableUpgradeTypeColumnCapture));
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void verify(DBAccessor dbAccessor) throws SQLException {
-      // Verification section
-      DBAccessor.DBColumnInfo packageNameCol = upgradeTablePackageNameColumnCapture.getValue();
-      Assert.assertEquals(String.class, packageNameCol.getType());
-      Assert.assertEquals("upgrade_package", packageNameCol.getName());
-
-      DBAccessor.DBColumnInfo upgradeTypeCol = upgradeTableUpgradeTypeColumnCapture.getValue();
-      Assert.assertEquals(String.class, upgradeTypeCol.getType());
-      Assert.assertEquals("upgrade_type", upgradeTypeCol.getName());
-    }
-  }
-
-  @Test
-  public void testUpdateRangerUgsyncSiteConfig() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Map<String, String> propertiesRangerUgsyncSite = new HashMap<String, String>() {{
-        put("ranger.usersync.source.impl.class", "ldap");
-    }};
-
-    final Config mockRangerUgsyncSite = easyMockSupport.createNiceMock(Config.class);
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("ranger-ugsync-site")).andReturn(mockRangerUgsyncSite).atLeastOnce();
-
-    expect(mockRangerUgsyncSite.getProperties()).andReturn(propertiesRangerUgsyncSite).atLeastOnce();
-
-    Map<String, String> updates = Collections.singletonMap("ranger.usersync.source.impl.class", "org.apache.ranger.ldapusersync.process.LdapUserGroupBuilder");
-    UpgradeCatalog220 upgradeCatalog220 = createMockBuilder(UpgradeCatalog220.class)
-            .withConstructor(Injector.class)
-            .withArgs(mockInjector)
-            .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-                    Map.class, boolean.class, boolean.class)
-            .createMock();
-    upgradeCatalog220.updateConfigurationPropertiesForCluster(mockClusterExpected,
-            "ranger-ugsync-site", updates, true, false);
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog220.class).updateRangerUgsyncSiteConfig();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testShouldDDLsBeExecutedOnUpgrade() throws Exception {
-    // GIVEN
-    Injector mockedInjector = mocksControl.createMock(Injector.class);
-    DBAccessor mockedDbAccessor = mocksControl.createMock(DBAccessor.class);
-    DaoUtils mockedDaoUtils = mocksControl.createMock(DaoUtils.class);
-    Configuration mockedConfiguration = mocksControl.createMock(Configuration.class);
-    StackUpgradeUtil mockedStackUpgradeUtil = mocksControl.createMock(StackUpgradeUtil.class);
-
-    Capture<String> capturedTableName = EasyMock.newCapture();
-    Capture<String> capturedPKColumn = EasyMock.newCapture();
-    Capture<List<DBAccessor.DBColumnInfo>> capturedColumns = EasyMock.newCapture();
-    Capture<DBAccessor.DBColumnInfo> capturedColumn = EasyMock.newCapture();
-    Capture<DBAccessor.DBColumnInfo> capturedHostRoleCommandColumn = EasyMock.newCapture();
-
-    Capture<String> capturedBlueprintTableName = EasyMock.newCapture();
-    Capture<DBAccessor.DBColumnInfo> capturedNewBlueprintColumn1 = EasyMock.newCapture();
-    Capture<DBAccessor.DBColumnInfo> capturedNewBlueprintColumn2 = EasyMock.newCapture();
-
-    Capture<DBAccessor.DBColumnInfo> stageSkipColumnCapture = EasyMock.newCapture();
-
-    EasyMock.expect(mockedInjector.getInstance(DaoUtils.class)).andReturn(mockedDaoUtils);
-    mockedInjector.injectMembers(anyObject(UpgradeCatalog.class));
-    EasyMock.expect(mockedConfiguration.getDatabaseType()).andReturn(Configuration.DatabaseType.POSTGRES).anyTimes();
-    EasyMock.expect(mockedConfiguration.getDatabaseUser()).andReturn("ambari");
-    EasyMock.expect(mockedConfiguration.getServerJDBCPostgresSchemaName()).andReturn("fo");
-
-
-    mockedDbAccessor.executeQuery("ALTER SCHEMA fo OWNER TO \"ambari\";");
-    mockedDbAccessor.executeQuery("ALTER ROLE \"ambari\" SET search_path to 'fo';");
-
-    // executeUpgradeDDLUpdates
-    mockedDbAccessor.addColumn(eq("upgrade"), capture(capturedColumn));
-    mockedDbAccessor.addColumn(eq("upgrade"), capture(capturedColumn));
-    mockedDbAccessor.addColumn(eq("upgrade"), capture(capturedColumn));
-
-    // addKerberosDescriptorTable
-    mockedDbAccessor.createTable(capture(capturedTableName), capture(capturedColumns), capture(capturedPKColumn));
-    mockedDbAccessor.alterColumn(eq("host_role_command"), capture(capturedHostRoleCommandColumn));
-
-    mockedDbAccessor.addColumn(capture(capturedBlueprintTableName), capture(capturedNewBlueprintColumn1));
-    mockedDbAccessor.addColumn(capture(capturedBlueprintTableName), capture(capturedNewBlueprintColumn2));
-
-    mockedDbAccessor.addColumn(eq("stage"), capture(stageSkipColumnCapture));
-
-    mocksControl.replay();
-
-    UpgradeCatalog220 testSubject = new UpgradeCatalog220(mockedInjector);
-    EasyMockSupport.injectMocks(testSubject);
-
-    //todo refactor the DI approach, don't directly access these members!!!
-    testSubject.stackUpgradeUtil = mockedStackUpgradeUtil;
-    testSubject.dbAccessor = mockedDbAccessor;
-    testSubject.configuration = mockedConfiguration;
-
-    // WHEN
-    testSubject.upgradeSchema();
-
-    // THEN
-    Assert.assertEquals("The table name is wrong!", "kerberos_descriptor", capturedTableName.getValue());
-    Assert.assertEquals("The primary key is wrong!", "kerberos_descriptor_name", capturedPKColumn.getValue());
-    Assert.assertTrue("Ther number of columns is wrong!", capturedColumns.getValue().size() == 2);
-
-    Assert.assertEquals("The table name is wrong!", "blueprint", capturedBlueprintTableName.getValue());
-
-    Assert.assertEquals("The column name is wrong!", "security_type", capturedNewBlueprintColumn1.getValue().getName());
-    Assert.assertEquals("The column name is wrong!", "security_descriptor_reference", capturedNewBlueprintColumn2
-      .getValue().getName());
-
-    Assert.assertEquals("The column name is wrong!", "supports_auto_skip_failure",
-        stageSkipColumnCapture.getValue().getName());
-  }
-
-  @Test
-  public void testUpdateHiveConfig() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Map<String, String> propertiesHiveSite = new HashMap<String, String>() {{
-      put("hive.server2.logging.operation.log.location", "${system:java.io.tmpdir}/${system:user.name}/operation_logs");
-    }};
-    final Map<String, String> propertiesHiveSiteExpected = new HashMap<String, String>() {{
-      put("hive.server2.logging.operation.log.location", "/tmp/hive/operation_logs");
-    }};
-    final Map<String, String> propertiesHiveEnv = new HashMap<String, String>() {{
-      put("content", "test content");
-    }};
-    final Config hiveSiteConf = easyMockSupport.createNiceMock(Config.class);
-    final Config hiveEnvConf = easyMockSupport.createNiceMock(Config.class);
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hive-site")).andReturn(hiveSiteConf).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(hiveEnvConf).atLeastOnce();
-
-    expect(hiveSiteConf.getProperties()).andReturn(propertiesHiveSite).once();
-    expect(hiveEnvConf.getProperties()).andReturn(propertiesHiveEnv).once();
-
-    UpgradeCatalog220 upgradeCatalog220 = createMockBuilder(UpgradeCatalog220.class)
-            .withConstructor(Injector.class)
-            .withArgs(mockInjector)
-            .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-                    Map.class, boolean.class, boolean.class)
-            .createMock();
-    upgradeCatalog220.updateConfigurationPropertiesForCluster(mockClusterExpected,
-            "hive-site", propertiesHiveSiteExpected, true, false);
-    expectLastCall().once();
-    upgradeCatalog220.updateConfigurationPropertiesForCluster(mockClusterExpected,
-            "hive-env", propertiesHiveEnv, true, true);
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    replay(upgradeCatalog220);
-    upgradeCatalog220.updateHiveConfig();
-    easyMockSupport.verifyAll();
-
-  }
-
-  @Test
-  public void testUpdateHiveEnvContentHDP23() throws Exception {
-    UpgradeCatalog220 upgradeCatalog220 = new UpgradeCatalog220(injector);
-    String testContent = "# The heap size of the jvm stared by hive shell script can be controlled via:\n" +
-            "\n" +
-            "# Larger heap size may be required when running queries over large number of files or partitions.\n";
-    String expectedResult = "# The heap size of the jvm stared by hive shell script can be controlled via:\n" +
-            "\n" +
-            "if [ \"$SERVICE\" = \"metastore\" ]; then\n" +
-            "  export HADOOP_HEAPSIZE={{hive_metastore_heapsize}} # Setting for HiveMetastore\n" +
-            "else\n" +
-            "  export HADOOP_HEAPSIZE={{hive_heapsize}} # Setting for HiveServer2 and Client\n" +
-            "fi\n" +
-            "\n" +
-            "export HADOOP_CLIENT_OPTS=\"$HADOOP_CLIENT_OPTS  -Xmx${HADOOP_HEAPSIZE}m\"\n" +
-            "\n" +
-            "# Larger heap size may be required when running queries over large number of files or partitions.\n";
-    Assert.assertEquals(expectedResult, upgradeCatalog220.updateHiveEnvContentHDP23(testContent));
-  }
-
-
-  @Test
-  public void testUpdateHiveEnvContent() throws Exception {
-    UpgradeCatalog220 upgradeCatalog220 = new UpgradeCatalog220(injector);
-    // Test first case
-    String testContent = "# The heap size of the jvm stared by hive shell script can be controlled via:\n" +
-            "\n" +
-            "if [ \"$SERVICE\" = \"metastore\" ]; then\n" +
-            "  export HADOOP_HEAPSIZE=\"{{hive_metastore_heapsize}}\"\n" +
-            "else\n" +
-            "  export HADOOP_HEAPSIZE=\"{{hive_heapsize}}\"\n" +
-            "fi\n" +
-            "\n" +
-            "export HADOOP_CLIENT_OPTS=\"-Xmx${HADOOP_HEAPSIZE}m $HADOOP_CLIENT_OPTS\"\n" +
-            "\n" +
-            "# Larger heap size may be required when running queries over large number of files or partitions.\n";
-    String expectedResult = "# The heap size of the jvm stared by hive shell script can be controlled via:\n" +
-            "\n" +
-            "if [ \"$SERVICE\" = \"metastore\" ]; then\n" +
-            "  export HADOOP_HEAPSIZE=\"{{hive_metastore_heapsize}}\"\n" +
-            "else\n" +
-            "  export HADOOP_HEAPSIZE=\"{{hive_heapsize}}\"\n" +
-            "fi\n" +
-            "\n" +
-            "export HADOOP_CLIENT_OPTS=\"$HADOOP_CLIENT_OPTS  -Xmx${HADOOP_HEAPSIZE}m\"\n" +
-            "\n" +
-            "# Larger heap size may be required when running queries over large number of files or partitions.\n";
-    Assert.assertEquals(expectedResult, upgradeCatalog220.updateHiveEnvContent(testContent));
-    // Test second case
-    testContent = "# The heap size of the jvm stared by hive shell script can be controlled via:\n" +
-            "export SERVICE=$SERVICE\n" +
-            "if [ \"$SERVICE\" = \"metastore\" ]; then\n" +
-            "  export HADOOP_HEAPSIZE=\"{{hive_metastore_heapsize}}\"\n" +
-            "else\n" +
-            "  export HADOOP_HEAPSIZE=\"{{hive_heapsize}}\"\n" +
-            "fi\n" +
-            "\n" +
-            "# Larger heap size may be required when running queries over large number of files or partitions.\n";
-    expectedResult = "# The heap size of the jvm stared by hive shell script can be controlled via:\n" +
-            "export SERVICE=$SERVICE\n" +
-            "if [ \"$SERVICE\" = \"metastore\" ]; then\n" +
-            "  export HADOOP_HEAPSIZE=\"{{hive_metastore_heapsize}}\"\n" +
-            "else\n" +
-            "  export HADOOP_HEAPSIZE={{hive_heapsize}} # Setting for HiveServer2 and Client\n" +
-            "fi\n" +
-            "\n" +
-            "export HADOOP_CLIENT_OPTS=\"$HADOOP_CLIENT_OPTS  -Xmx${HADOOP_HEAPSIZE}m\"\n" +
-            "# Larger heap size may be required when running queries over large number of files or partitions.\n";
-    Assert.assertEquals(expectedResult, upgradeCatalog220.updateHiveEnvContent(testContent));
-  }
-
-  @Test
-  public void testupdateKnoxTopology_NoRangerPlugin() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Map<String, String> propertiesTopologyWithoutAuthorizationProvider = new HashMap<String, String>() {{
-      put("content", "<topology> <gateway>  </gateway> </topology>");
-    }};
-    final Map<String, String> propertiesTopologyExpected = new HashMap<String, String>() {{
-      put("content", "<topology> <gateway>  <provider>\n" +
-              "               <role>authorization</role>\n" +
-              "               <name>AclsAuthz</name>\n" +
-              "               <enabled>true</enabled>\n" +
-              "          </provider>\n" +
-              "     </gateway> </topology>\n");
-    }};
-    final Config mockTopologyConf = easyMockSupport.createNiceMock(Config.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("cl1", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("topology")).andReturn(mockTopologyConf).atLeastOnce();
-    expect(mockTopologyConf.getProperties()).andReturn(propertiesTopologyWithoutAuthorizationProvider).once();
-
-
-    UpgradeCatalog220 upgradeCatalog220 = createMockBuilder(UpgradeCatalog220.class)
-            .withConstructor(Injector.class)
-            .withArgs(mockInjector)
-            .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-                    Map.class, boolean.class, boolean.class)
-            .createMock();
-    upgradeCatalog220.updateConfigurationPropertiesForCluster(mockClusterExpected,
-            "topology", propertiesTopologyExpected, true, false);
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    replay(upgradeCatalog220);
-    upgradeCatalog220.updateKnoxTopology();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testupdateKnoxTopology_ProviderAlreadyExists() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Map<String, String> propertiesTopologyWitAuthorizationProvider = new HashMap<String, String>() {{
-      put("content", "<topology> <gateway>  <provider>" +
-              "<role>authorization</role>" +
-              "<name>AclsAuthz</name>" +
-              "<enabled>true</enabled>" +
-              "</provider>" +
-              "</gateway> </topology>\n");
-    }};
-
-    final Config mockTopologyConf = easyMockSupport.createNiceMock(Config.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("cl1", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("topology")).andReturn(mockTopologyConf).atLeastOnce();
-    expect(mockTopologyConf.getProperties()).andReturn(propertiesTopologyWitAuthorizationProvider).once();
-
-    // ATTENTION, this mock should not be called at all. If it was, then something wrong with code
-    UpgradeCatalog220 upgradeCatalog220 = createMockBuilder(UpgradeCatalog220.class)
-            .withConstructor(Injector.class)
-            .withArgs(mockInjector)
-            .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-                    Map.class, boolean.class, boolean.class)
-            .createMock();
-
-
-
-    easyMockSupport.replayAll();
-    replay(upgradeCatalog220);
-    upgradeCatalog220.updateKnoxTopology();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testupdateKnoxTopology_RangerPluginAvailable() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Map<String, String> propertiesTopologyWithoutAuthorizationProvider = new HashMap<String, String>() {{
-      put("content", "<topology> <gateway>  </gateway> </topology>");
-    }};
-    final Map<String, String> propertiesRangerKnoxPluginProperties = new HashMap<String, String>() {{
-      put("ranger-knox-plugin-enabled", "Yes");
-    }};
-    final Map<String, String> propertiesTopologyExpected = new HashMap<String, String>() {{
-      put("content", "<topology> <gateway>  <provider>\n" +
-              "               <role>authorization</role>\n" +
-              "               <name>XASecurePDPKnox</name>\n" +
-              "               <enabled>true</enabled>\n" +
-              "          </provider>\n" +
-              "     </gateway> </topology>\n");
-    }};
-    final Config mockTopologyConf = easyMockSupport.createNiceMock(Config.class);
-    final Config mockRangerKnoxPluginConf = easyMockSupport.createNiceMock(Config.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("cl1", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("topology")).andReturn(mockTopologyConf).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("ranger-knox-plugin-properties")).andReturn(mockRangerKnoxPluginConf).atLeastOnce();
-    expect(mockTopologyConf.getProperties()).andReturn(propertiesTopologyWithoutAuthorizationProvider).once();
-    expect(mockRangerKnoxPluginConf.getProperties()).andReturn(propertiesRangerKnoxPluginProperties).once();
-
-
-    UpgradeCatalog220 upgradeCatalog220 = createMockBuilder(UpgradeCatalog220.class)
-            .withConstructor(Injector.class)
-            .withArgs(mockInjector)
-            .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-                    Map.class, boolean.class, boolean.class)
-            .createMock();
-    upgradeCatalog220.updateConfigurationPropertiesForCluster(mockClusterExpected,
-            "topology", propertiesTopologyExpected, true, false);
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    replay(upgradeCatalog220);
-    upgradeCatalog220.updateKnoxTopology();
-    easyMockSupport.verifyAll();
-
-  }
-
-  @Test
-  public void testUpdateAccumuloConfigs() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    // We start with no client properties (< 2.2.0).
-    final Map<String, String> originalClientProperties = new HashMap<String, String>();
-    // And should get the following property on upgrade.
-    final Map<String, String> updatedClientProperties = new HashMap<String, String>() {
-      {
-        put("kerberos.server.primary", "{{bare_accumulo_principal}}");
-      }
-    };
-    
-    final Config clientConfig = easyMockSupport.createNiceMock(Config.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    // Enable KERBEROS
-    expect(mockClusterExpected.getSecurityType()).andReturn(SecurityType.KERBEROS).once();
-    // Mock out our empty original properties
-    expect(mockClusterExpected.getDesiredConfigByType("client")).andReturn(clientConfig).atLeastOnce();
-    expect(clientConfig.getProperties()).andReturn(originalClientProperties).atLeastOnce();
-
-    UpgradeCatalog220 upgradeCatalog220 = createMockBuilder(UpgradeCatalog220.class)
-            .withConstructor(Injector.class)
-            .withArgs(mockInjector)
-            .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-                    Map.class, boolean.class, boolean.class)
-            .createMock();
-    // Verify that we get this method called with the updated properties
-    upgradeCatalog220.updateConfigurationPropertiesForCluster(mockClusterExpected,
-            "client", updatedClientProperties, true, false);
-    expectLastCall().once();
-
-    // Run it
-    easyMockSupport.replayAll();
-    replay(upgradeCatalog220);
-    upgradeCatalog220.updateAccumuloConfigs();
-    easyMockSupport.verifyAll();
-  }
-}


[27/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java
index ab8026c..e178b92 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -17,14 +17,12 @@
  */
 package org.apache.ambari.server.state;
 
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -86,6 +84,7 @@ public class ConfigHelper {
   public static final String CLUSTER_ENV = "cluster-env";
   public static final String CLUSTER_ENV_ALERT_REPEAT_TOLERANCE = "alerts_repeat_tolerance";
   public static final String CLUSTER_ENV_RETRY_ENABLED = "command_retry_enabled";
+  public static final String SERVICE_CHECK_TYPE = "service_check_type";
   public static final String CLUSTER_ENV_RETRY_COMMANDS = "commands_to_retry";
   public static final String CLUSTER_ENV_RETRY_MAX_TIME_IN_SEC = "command_retry_max_time_in_sec";
   public static final String COMMAND_RETRY_MAX_TIME_IN_SEC_DEFAULT = "600";
@@ -96,6 +95,7 @@ public class ConfigHelper {
 
   public static final String HTTP_ONLY = "HTTP_ONLY";
   public static final String HTTPS_ONLY = "HTTPS_ONLY";
+  public static final String SERVICE_CHECK_MINIMAL = "minimal";
 
   /**
    * The tag given to newly created versions.
@@ -602,8 +602,9 @@ public class ConfigHelper {
                 found = true;
               }
             }
+
             if(!found) {
-             //Log error if the user-group mapping is not found
+              //Log error if the user-group mapping is not found
               LOG.error("User group mapping property {" + userGroupInfo.getType() + "/" + userGroupInfo.getName() + "} is missing for user property {" + ConfigHelper.fileNameToConfigType(userPropertyInfo.getFilename()) + "/" + userPropertyInfo.getName() + "} (username = " + userPropertyInfo.getValue() +")");
             }
           }
@@ -736,8 +737,19 @@ public class ConfigHelper {
         if (serviceProperty.getPropertyTypes().contains(propertyType)) {
           String stackPropertyConfigType = fileNameToConfigType(serviceProperty.getFilename());
           try {
-            result.add(actualConfigs.get(stackPropertyConfigType).getProperties().get(serviceProperty.getName()));
-          } catch (Exception ex) {
+            String property = actualConfigs.get(stackPropertyConfigType).getProperties().get(serviceProperty.getName());
+            if (null == property){
+              LOG.error(String.format("Unable to obtain property values for %s with property attribute %s. "
+                  + "The property does not exist in version %s of %s configuration.",
+                  serviceProperty.getName(),
+                  propertyType,
+                  desiredConfigs.get(stackPropertyConfigType),
+                  stackPropertyConfigType
+                  ));
+            } else {
+              result.add(property);
+            }
+          } catch (Exception ignored) {
           }
         }
       }
@@ -763,23 +775,31 @@ public class ConfigHelper {
    * @throws AmbariException
    */
   public String getPropertyValueFromStackDefinitions(Cluster cluster, String configType, String propertyName) throws AmbariException {
-    StackId stackId = cluster.getCurrentStackVersion();
-    StackInfo stack = ambariMetaInfo.getStack(stackId.getStackName(),
-        stackId.getStackVersion());
 
-    for (ServiceInfo serviceInfo : stack.getServices()) {
-      Set<PropertyInfo> serviceProperties = ambariMetaInfo.getServiceProperties(stack.getName(), stack.getVersion(), serviceInfo.getName());
-      Set<PropertyInfo> stackProperties = ambariMetaInfo.getStackProperties(stack.getName(), stack.getVersion());
-      serviceProperties.addAll(stackProperties);
+    Set<StackId> stackIds = new HashSet<>();
 
-      for (PropertyInfo stackProperty : serviceProperties) {
-        String stackPropertyConfigType = fileNameToConfigType(stackProperty.getFilename());
+    for (Service service : cluster.getServices().values()) {
+      stackIds.add(service.getDesiredStackId());
+    }
+
+    for (StackId stackId : stackIds) {
 
-        if (stackProperty.getName().equals(propertyName) && stackPropertyConfigType.equals(configType)) {
-          return stackProperty.getValue();
+      StackInfo stack = ambariMetaInfo.getStack(stackId.getStackName(),
+          stackId.getStackVersion());
+
+      for (ServiceInfo serviceInfo : stack.getServices()) {
+        Set<PropertyInfo> serviceProperties = ambariMetaInfo.getServiceProperties(stack.getName(), stack.getVersion(), serviceInfo.getName());
+        Set<PropertyInfo> stackProperties = ambariMetaInfo.getStackProperties(stack.getName(), stack.getVersion());
+        serviceProperties.addAll(stackProperties);
+
+        for (PropertyInfo stackProperty : serviceProperties) {
+          String stackPropertyConfigType = fileNameToConfigType(stackProperty.getFilename());
+
+          if (stackProperty.getName().equals(propertyName) && stackPropertyConfigType.equals(configType)) {
+            return stackProperty.getValue();
+          }
         }
       }
-
     }
 
     return null;
@@ -838,20 +858,22 @@ public class ConfigHelper {
   }
 
   public ServiceInfo getPropertyOwnerService(Cluster cluster, String configType, String propertyName) throws AmbariException {
-    StackId stackId = cluster.getCurrentStackVersion();
-    StackInfo stack = ambariMetaInfo.getStack(stackId.getStackName(), stackId.getStackVersion());
 
-    for (ServiceInfo serviceInfo : stack.getServices()) {
-      Set<PropertyInfo> serviceProperties = ambariMetaInfo.getServiceProperties(stack.getName(), stack.getVersion(), serviceInfo.getName());
+    for (Service service : cluster.getServices().values()) {
+      StackId stackId = service.getDesiredStackId();
+      StackInfo stack = ambariMetaInfo.getStack(stackId.getStackName(), stackId.getStackVersion());
 
-      for (PropertyInfo stackProperty : serviceProperties) {
-        String stackPropertyConfigType = fileNameToConfigType(stackProperty.getFilename());
+      for (ServiceInfo serviceInfo : stack.getServices()) {
+        Set<PropertyInfo> serviceProperties = ambariMetaInfo.getServiceProperties(stack.getName(), stack.getVersion(), serviceInfo.getName());
+
+        for (PropertyInfo stackProperty : serviceProperties) {
+          String stackPropertyConfigType = fileNameToConfigType(stackProperty.getFilename());
 
-        if (stackProperty.getName().equals(propertyName) && stackPropertyConfigType.equals(configType)) {
-          return serviceInfo;
+          if (stackProperty.getName().equals(propertyName) && stackPropertyConfigType.equals(configType)) {
+            return serviceInfo;
+          }
         }
       }
-
     }
 
     return null;
@@ -861,7 +883,9 @@ public class ConfigHelper {
     // The original implementation of this method is to return all properties regardless of whether
     // they should be excluded or not.  By setting removeExcluded to false in the method invocation
     // below, no attempt will be made to remove properties that exist in excluded types.
-    return getServiceProperties(cluster.getCurrentStackVersion(), serviceName, false);
+    Service service = cluster.getService(serviceName);
+
+    return getServiceProperties(service.getDesiredStackId(), serviceName, false);
   }
 
   /**
@@ -940,11 +964,10 @@ public class ConfigHelper {
    * @param serviceVersionNote
    * @throws AmbariException
    */
-  public void updateConfigType(Cluster cluster,
-                               AmbariManagementController controller, String configType,
-                               Map<String, String> updates, Collection<String> removals,
-                               String authenticatedUserName,
-                               String serviceVersionNote) throws AmbariException {
+  public void updateConfigType(Cluster cluster, StackId stackId,
+      AmbariManagementController controller, String configType, Map<String, String> updates,
+      Collection<String> removals, String authenticatedUserName, String serviceVersionNote)
+      throws AmbariException {
 
     // Nothing to update or remove
     if (configType == null ||
@@ -987,21 +1010,27 @@ public class ConfigHelper {
 
     if ((oldConfigProperties == null)
       || !Maps.difference(oldConfigProperties, properties).areEqual()) {
-      createConfigType(cluster, controller, configType, properties,
+      createConfigType(cluster, stackId, controller, configType, properties,
         propertiesAttributes, authenticatedUserName, serviceVersionNote);
     }
   }
 
-  private void createConfigType(Cluster cluster,
-                               AmbariManagementController controller,
-                               String configType, Map<String, String> properties,
-                               Map<String, Map<String, String>> propertyAttributes,
-                               String authenticatedUserName,
-                               String serviceVersionNote) throws AmbariException {
+  public void createConfigType(Cluster cluster, StackId stackId,
+      AmbariManagementController controller, String configType, Map<String, String> properties,
+      String authenticatedUserName, String serviceVersionNote) throws AmbariException {
+
+    createConfigType(cluster, stackId, controller, configType, properties,
+        new HashMap<String, Map<String, String>>(), authenticatedUserName, serviceVersionNote);
+  }
+
+  public void createConfigType(Cluster cluster, StackId stackId,
+      AmbariManagementController controller, String configType, Map<String, String> properties,
+      Map<String, Map<String, String>> propertyAttributes, String authenticatedUserName,
+      String serviceVersionNote) throws AmbariException {
 
     // create the configuration history entry
-    Config baseConfig = createConfig(cluster, controller, configType, FIRST_VERSION_TAG, properties,
-        propertyAttributes);
+    Config baseConfig = createConfig(cluster, stackId, controller, configType, FIRST_VERSION_TAG,
+        properties, propertyAttributes);
 
     if (baseConfig != null) {
       cluster.addDesiredConfig(authenticatedUserName,
@@ -1010,46 +1039,18 @@ public class ConfigHelper {
   }
 
   /**
-   * A helper method to create a new {@link Config} for a given configuration
-   * type. This method will perform the following tasks:
-   * <ul>
-   * <li>Create a {@link Config} in the cluster for the specified type. This
-   * will have the proper versions and tags set automatically.</li>
-   * <li>Set the cluster's {@link DesiredConfig} to the new configuration</li>
-   * <li>Create an entry in the configuration history with a note and username.</li>
-   * <ul>
-   *
-   * @param cluster
-   * @param controller
-   * @param configType
-   * @param properties
-   * @param authenticatedUserName
-   * @param serviceVersionNote
-   * @throws AmbariException
-   */
-  public void createConfigType(Cluster cluster,
-                               AmbariManagementController controller,
-                               String configType, Map<String, String> properties,
-                               String authenticatedUserName,
-                               String serviceVersionNote) throws AmbariException {
-    createConfigType(cluster, controller, configType, properties,
-      new HashMap<String, Map<String, String>>(), authenticatedUserName,
-      serviceVersionNote);
-  }
-
-  /**
    * Create configurations and assign them for services.
    * @param cluster               the cluster
    * @param controller            the controller
+   * @param stackId               the stack to create the new properties for
    * @param batchProperties       the type->config map batch of properties
    * @param authenticatedUserName the user that initiated the change
    * @param serviceVersionNote    the service version note
    * @throws AmbariException
    */
-  public void createConfigTypes(Cluster cluster,
-      AmbariManagementController controller,
-      Map<String, Map<String, String>> batchProperties, String authenticatedUserName,
-      String serviceVersionNote) throws AmbariException {
+  public void createConfigTypes(Cluster cluster, StackId stackId,
+      AmbariManagementController controller, Map<String, Map<String, String>> batchProperties,
+      String authenticatedUserName, String serviceVersionNote) throws AmbariException {
 
     Map<String, Set<Config>> serviceMapped = new HashMap<>();
 
@@ -1057,8 +1058,8 @@ public class ConfigHelper {
       String type = entry.getKey();
       Map<String, String> properties = entry.getValue();
 
-      Config baseConfig = createConfig(cluster, controller, type, FIRST_VERSION_TAG, properties,
-        Collections.<String, Map<String,String>>emptyMap());
+      Config baseConfig = createConfig(cluster, stackId, controller, type, FIRST_VERSION_TAG,
+          properties, Collections.<String, Map<String, String>> emptyMap());
 
       if (null != baseConfig) {
         try {
@@ -1090,9 +1091,13 @@ public class ConfigHelper {
    *
    * @param cluster
    *          the cluster (not {@code null}).
+   * @param stackId
+   *          the stack to create the new properties for
    * @param controller
    *          the controller which actually creates the configuration (not
    *          {@code null}).
+   * @param stackId
+   *          the stack to create the new properties for
    * @param type
    *          the new configuration type (not {@code null}).
    * @param tag
@@ -1105,8 +1110,8 @@ public class ConfigHelper {
    * @return
    * @throws AmbariException
    */
-  Config createConfig(Cluster cluster, AmbariManagementController controller, String type,
-      String tag, Map<String, String> properties,
+  Config createConfig(Cluster cluster, StackId stackId, AmbariManagementController controller,
+      String type, String tag, Map<String, String> properties,
       Map<String, Map<String, String>> propertyAttributes) throws AmbariException {
 
     // if the configuration is not new, then create a timestamp tag
@@ -1177,7 +1182,7 @@ public class ConfigHelper {
    *         default configurations.
    * @throws AmbariException
    */
-  public Map<String, Map<String, String>> getDefaultProperties(StackId stack, Cluster cluster)
+  public Map<String, Map<String, String>> getDefaultProperties(StackId stack, String serviceName)
       throws AmbariException {
     Map<String, Map<String, String>> defaultPropertiesByType = new HashMap<>();
 
@@ -1191,54 +1196,24 @@ public class ConfigHelper {
       if (!defaultPropertiesByType.containsKey(type)) {
         defaultPropertiesByType.put(type, new HashMap<String, String>());
       }
-
-      defaultPropertiesByType.get(type).put(stackDefaultProperty.getName(),
-          stackDefaultProperty.getValue());
     }
 
     // for every installed service, populate the default service properties
-    for (String serviceName : cluster.getServices().keySet()) {
-      Set<org.apache.ambari.server.state.PropertyInfo> serviceConfigurationProperties = ambariMetaInfo.getServiceProperties(
-          stack.getStackName(), stack.getStackVersion(), serviceName);
-
-      // !!! use new stack as the basis
-      for (PropertyInfo serviceDefaultProperty : serviceConfigurationProperties) {
-        String type = ConfigHelper.fileNameToConfigType(serviceDefaultProperty.getFilename());
+    Set<org.apache.ambari.server.state.PropertyInfo> serviceConfigurationProperties = ambariMetaInfo.getServiceProperties(
+        stack.getStackName(), stack.getStackVersion(), serviceName);
 
-        if (!defaultPropertiesByType.containsKey(type)) {
-          defaultPropertiesByType.put(type, new HashMap<String, String>());
-        }
+    // !!! use new stack as the basis
+    for (PropertyInfo serviceDefaultProperty : serviceConfigurationProperties) {
+      String type = ConfigHelper.fileNameToConfigType(serviceDefaultProperty.getFilename());
 
-        defaultPropertiesByType.get(type).put(serviceDefaultProperty.getName(),
-            serviceDefaultProperty.getValue());
+      if (!defaultPropertiesByType.containsKey(type)) {
+        defaultPropertiesByType.put(type, new HashMap<String, String>());
       }
     }
 
     return defaultPropertiesByType;
   }
 
-  /**
-   * Gets whether configurations are stale for a given service host component.
-   *
-   * @param sch
-   *          the SCH to calcualte config staleness for (not {@code null}).
-   * @param desiredConfigs
-   *          the desired configurations for the cluster. Obtaining these can be
-   *          expensive and since this method operates on SCH's, it could be
-   *          called 10,000's of times when generating cluster/host responses.
-   *          Therefore, the caller should build these once and pass them in. If
-   *          {@code null}, then this method will retrieve them at runtime,
-   *          incurring a performance penality.
-   * @return
-   * @throws AmbariException
-   */
-  private boolean calculateIsStaleConfigs(ServiceComponentHost sch,
-      Map<String, DesiredConfig> desiredConfigs) throws AmbariException {
-
-    HostComponentDesiredStateEntity hostComponentDesiredStateEntity = sch.getDesiredStateEntity();
-    return calculateIsStaleConfigs(sch, desiredConfigs, hostComponentDesiredStateEntity);
-  }
-
   private boolean calculateIsStaleConfigs(ServiceComponentHost sch, Map<String, DesiredConfig> desiredConfigs,
                                           HostComponentDesiredStateEntity hostComponentDesiredStateEntity) throws AmbariException {
 
@@ -1272,7 +1247,7 @@ public class ConfigHelper {
 
     stale = false;
 
-    StackId stackId = cluster.getDesiredStackVersion();
+    StackId stackId = sch.getServiceComponent().getDesiredStackId();
 
     ServiceInfo serviceInfo = ambariMetaInfo.getService(stackId.getStackName(),
             stackId.getStackVersion(), sch.getServiceName());
@@ -1346,64 +1321,6 @@ public class ConfigHelper {
   }
 
   /**
-   * @return <code>true</code> if any service on the stack defines a property
-   * for the type.
-   */
-  private boolean hasPropertyFor(StackId stack, String type,
-                                 Collection<String> keys) throws AmbariException {
-
-    for (ServiceInfo svc : ambariMetaInfo.getServices(stack.getStackName(),
-        stack.getStackVersion()).values()) {
-
-      if (svc.hasDependencyAndPropertyFor(type, keys)) {
-        return true;
-      }
-
-    }
-
-    return false;
-  }
-
-  /**
-   * @return the keys that have changed values
-   */
-  private Collection<String> findChangedKeys(Cluster cluster, String type,
-                                             Collection<String> desiredTags, Collection<String> actualTags) {
-
-    Map<String, String> desiredValues = new HashMap<>();
-    Map<String, String> actualValues = new HashMap<>();
-
-    for (String tag : desiredTags) {
-      Config config = cluster.getConfig(type, tag);
-      if (null != config) {
-        desiredValues.putAll(config.getProperties());
-      }
-    }
-
-    for (String tag : actualTags) {
-      Config config = cluster.getConfig(type, tag);
-      if (null != config) {
-        actualValues.putAll(config.getProperties());
-      }
-    }
-
-    List<String> keys = new ArrayList<>();
-
-    for (Entry<String, String> entry : desiredValues.entrySet()) {
-      String key = entry.getKey();
-      String value = entry.getValue();
-
-      if (!actualValues.containsKey(key)) {
-        keys.add(key);
-      } else if (!actualValues.get(key).equals(value)) {
-        keys.add(key);
-      }
-    }
-
-    return keys;
-  }
-
-  /**
    * @return the map of tags for a desired config
    */
   private Map<String, String> buildTags(HostConfig hc) {
@@ -1439,23 +1356,6 @@ public class ConfigHelper {
     return !desiredSet.equals(actualSet);
   }
 
-  /**
-   * @return the list of combined config property names
-   */
-  private Collection<String> mergeKeyNames(Cluster cluster, String type, Collection<String> tags) {
-    Set<String> names = new HashSet<>();
-
-    for (String tag : tags) {
-      Config config = cluster.getConfig(type, tag);
-      if (null != config) {
-        names.addAll(config.getProperties().keySet());
-      }
-    }
-
-    return names;
-  }
-
-
   public static String fileNameToConfigType(String filename) {
     int extIndex = filename.indexOf(AmbariMetaInfo.SERVICE_CONFIG_FILE_NAME_POSTFIX);
     return filename.substring(0, extIndex);

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/RepositoryVersionState.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/RepositoryVersionState.java b/ambari-server/src/main/java/org/apache/ambari/server/state/RepositoryVersionState.java
index e02e422..720f307 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/RepositoryVersionState.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/RepositoryVersionState.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -18,11 +18,16 @@
 
 package org.apache.ambari.server.state;
 
+import java.util.List;
+
 /**
- * There must be exactly one repository version that is in a CURRENT state for a particular cluster or host.
- * There may be 0 or more repository versions in an INSTALLED or INSTALLING state.
- * The operation to transition a repository version state from INSTALLED into CURRENT must be atomic and change the existing
- * relation between repository version and cluster or host from CURRENT to INSTALLED.
+ * The {@link RepositoryVersionState} represents the state of a repository on a
+ * particular host. Because hosts can contain a mixture of components from
+ * different repositories, there can be any combination of
+ * {@link RepositoryVersionState#CURRENT}} entries for a single host. A host may
+ * not have multiple entries for the same repository.
+ * <p/>
+ *
  *
  * <pre>
  * Step 1: Initial Configuration
@@ -41,20 +46,23 @@ package org.apache.ambari.server.state;
  * Version 1: CURRENT
  * Version 2: INSTALL_FAILED (a retry can set this back to INSTALLING)
  *
- * Step 4: Perform an upgrade from Version 1 to Version 2
+ * Step 4: Perform an upgrade of every component on the host from version 1 to version 2
  * Version 1: INSTALLED
  * Version 2: CURRENT
  *
- * Step 4: May revert to the original version via a downgrade, which is technically still an upgrade to a version
+ * Step 4a: Perform an upgrade of a single component, leaving other components on the prior version
+ * Version 1: CURRENT
+ * Version 2: CURRENT
+ *
+ * Step 4b: May revert to the original version via a downgrade, which is technically still an upgrade to a version
  * and eventually becomes
  *
  * Version 1: CURRENT
  * Version 2: INSTALLED
  *
  * *********************************************
- * Start states: CURRENT, INSTALLING
+ * Start states: NOT_REQUIRED, INSTALLING, CURRENT
  * Allowed Transitions:
- * INIT -> CURRENT
  * INSTALLED -> CURRENT
  * INSTALLING -> INSTALLED | INSTALL_FAILED | OUT_OF_SYNC
  * INSTALLED -> INSTALLED | INSTALLING | OUT_OF_SYNC
@@ -65,35 +73,68 @@ package org.apache.ambari.server.state;
  */
 public enum RepositoryVersionState {
   /**
-   * Repository version is initialized, and will transition to current.  This is used
-   * when creating a cluster using a specific version.  Transition occurs naturally as
-   * hosts report CURRENT.
-   */
-  INIT,
-
-  /**
    * Repository version is not required
    */
-  NOT_REQUIRED,
+  NOT_REQUIRED(0),
+
   /**
    * Repository version that is in the process of being installed.
    */
-  INSTALLING,
+  INSTALLING(3),
+
   /**
    * Repository version that is installed and supported but not the active version.
    */
-  INSTALLED,
+  INSTALLED(2),
+
   /**
    * Repository version that during the install process failed to install some components.
    */
-  INSTALL_FAILED,
+  INSTALL_FAILED(5),
+
   /**
    * Repository version that is installed for some components but not for all.
    */
-  OUT_OF_SYNC,
+  OUT_OF_SYNC(4),
+
   /**
    * Repository version that is installed and supported and is the active version.
    */
-  CURRENT,
+  CURRENT(1);
+
+  private final int weight;
+
+  /**
+   * Constructor.
+   *
+   * @param weight
+   *          the weight of the state.
+   */
+  private RepositoryVersionState(int weight) {
+    this.weight = weight;
+  }
+
+  /**
+   * Gets a single representation of the repository state based on the supplied
+   * states.
+   *
+   * @param states
+   *          the states to calculate the aggregate for.
+   * @return the "heaviest" state.
+   */
+  public static RepositoryVersionState getAggregateState(List<RepositoryVersionState> states) {
+    if (null == states || states.isEmpty()) {
+      return NOT_REQUIRED;
+    }
+
+    RepositoryVersionState heaviestState = states.get(0);
+    for (RepositoryVersionState state : states) {
+      if (state.weight > heaviestState.weight) {
+        heaviestState = state;
+      }
+    }
+
+    return heaviestState;
+  }
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/Service.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/Service.java b/ambari-server/src/main/java/org/apache/ambari/server/state/Service.java
index 5964e33..b151278 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/Service.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/Service.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -22,6 +22,7 @@ import java.util.Map;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.controller.ServiceResponse;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 
 public interface Service {
 
@@ -64,9 +65,7 @@ public interface Service {
    */
   void setSecurityState(SecurityState securityState) throws AmbariException;
 
-  StackId getDesiredStackVersion();
-
-  void setDesiredStackVersion(StackId stackVersion);
+  StackId getDesiredStackId();
 
   ServiceResponse convertToResponse();
 
@@ -139,6 +138,22 @@ public interface Service {
    */
   void setCredentialStoreEnabled(boolean credentialStoreEnabled);
 
+  /**
+   * @return
+   */
+  RepositoryVersionEntity getDesiredRepositoryVersion();
+
+  /**
+   * @param desiredRepositoryVersion
+   */
+  void setDesiredRepositoryVersion(RepositoryVersionEntity desiredRepositoryVersion);
+
+  /**
+   * Gets the repository for the desired version of this service by consulting
+   * the repository states of all known components.
+   */
+  RepositoryVersionState getRepositoryState();
+
   enum Type {
     HDFS,
     GLUSTERFS,

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponent.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponent.java b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponent.java
index e93ab9a..9fb2aba 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponent.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponent.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -22,6 +22,7 @@ import java.util.Map;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.controller.ServiceComponentResponse;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 
 public interface ServiceComponent {
 
@@ -51,13 +52,18 @@ public interface ServiceComponent {
 
   void setDesiredState(State state);
 
-  StackId getDesiredStackVersion();
+  /**
+   * Gets the desired repository for this service component.
+   *
+   * @return
+   */
+  RepositoryVersionEntity getDesiredRepositoryVersion();
 
-  void setDesiredStackVersion(StackId stackVersion);
+  StackId getDesiredStackId();
 
   String getDesiredVersion();
 
-  void setDesiredVersion(String version);
+  void setDesiredRepositoryVersion(RepositoryVersionEntity repositoryVersionEntity);
 
   /**
    * Refresh Component info due to current stack
@@ -97,4 +103,19 @@ public interface ServiceComponent {
       String hostName) throws AmbariException;
 
   void delete() throws AmbariException;
+
+  /**
+   * This method computes the state of the repository that's associated with the desired
+   * version.  It is used, for example, when a host component reports its version and the
+   * state can be in flux.
+   *
+   * @param reportedVersion
+   * @throws AmbariException
+   */
+  void updateRepositoryState(String reportedVersion) throws AmbariException;
+
+  /**
+   * @return the repository state for the desired version
+   */
+  RepositoryVersionState getRepositoryState();
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentHost.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentHost.java b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentHost.java
index 104e456..3c314d8 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentHost.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentHost.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -24,7 +24,7 @@ import java.util.Map;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.controller.ServiceComponentHostResponse;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
+import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.state.fsm.InvalidStateTransitionException;
 
 
@@ -91,10 +91,6 @@ public interface ServiceComponentHost {
 
   void setDesiredState(State state);
 
-  StackId getDesiredStackVersion();
-
-  void setDesiredStackVersion(StackId stackVersion);
-
   State getState();
 
   void setState(State state);
@@ -167,10 +163,6 @@ public interface ServiceComponentHost {
    */
   UpgradeState getUpgradeState();
 
-  StackId getStackVersion();
-
-  void setStackVersion(StackId stackVersion);
-
   HostComponentAdminState getComponentAdminState();
 
   void setComponentAdminState(HostComponentAdminState attribute);
@@ -242,13 +234,31 @@ public interface ServiceComponentHost {
    */
   void setRestartRequired(boolean restartRequired);
 
+
+  HostComponentDesiredStateEntity getDesiredStateEntity();
+
+  /**
+   * Gets the service component.
+   *
+   * @return the service component (never {@code null}).
+   */
+  ServiceComponent getServiceComponent();
+
   /**
-   * Changes host version state according to state of the components installed on the host.
-   * @return The Repository Version Entity with that component in the host
-   * @throws AmbariException if host is detached from the cluster
+   * Updates an existing {@link HostVersionEntity} for the desired repository of
+   * this component, or create one if it doesn't exist.
+   *
+   * @return Returns either the newly created or the updated Host Version
+   *         Entity.
+   * @throws AmbariException
    */
-  RepositoryVersionEntity recalculateHostVersionState() throws AmbariException;
+  HostVersionEntity recalculateHostVersionState() throws AmbariException;
 
-  HostComponentDesiredStateEntity getDesiredStateEntity();
+  /**
+   * Convenience method to get the desired stack id from the service component
+   *
+   * @return the desired stack id
+   */
+  StackId getDesiredStackId();
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
index cbf0ff9..5f85e38 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,6 +19,7 @@
 package org.apache.ambari.server.state;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
@@ -32,21 +33,30 @@ import org.apache.ambari.server.ServiceComponentHostNotFoundException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.controller.ServiceComponentResponse;
 import org.apache.ambari.server.events.ServiceComponentRecoveryChangedEvent;
+import org.apache.ambari.server.events.listeners.upgrade.StackVersionListener;
 import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 import org.apache.ambari.server.orm.dao.ClusterServiceDAO;
 import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO;
+import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
+import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
 import org.apache.ambari.server.orm.entities.ClusterServiceEntityPK;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
+import org.apache.ambari.server.orm.entities.ServiceComponentVersionEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.state.cluster.ClusterImpl;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.MapUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Function;
+import com.google.common.collect.Maps;
+import com.google.inject.Inject;
 import com.google.inject.ProvisionException;
 import com.google.inject.assistedinject.Assisted;
 import com.google.inject.assistedinject.AssistedInject;
@@ -74,24 +84,25 @@ public class ServiceComponentImpl implements ServiceComponent {
 
   private AmbariMetaInfo ambariMetaInfo;
 
-  private final ConcurrentMap<String, ServiceComponentHost> hostComponents = new ConcurrentHashMap<String, ServiceComponentHost>();
+  private final ConcurrentMap<String, ServiceComponentHost> hostComponents = new ConcurrentHashMap<>();
 
   /**
    * The ID of the persisted {@link ServiceComponentDesiredStateEntity}.
    */
   private final long desiredStateEntityId;
 
-  /**
-   * Data access object used for lookup up stacks.
-   */
-  private final StackDAO stackDAO;
+  @Inject
+  private RepositoryVersionDAO repoVersionDAO;
+
+  @Inject
+  private HostComponentStateDAO hostComponentDAO;
 
   @AssistedInject
   public ServiceComponentImpl(@Assisted Service service, @Assisted String componentName,
       AmbariMetaInfo ambariMetaInfo,
       ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO,
       ClusterServiceDAO clusterServiceDAO, ServiceComponentHostFactory serviceComponentHostFactory,
-      StackDAO stackDAO, AmbariEventPublisher eventPublisher)
+      AmbariEventPublisher eventPublisher)
       throws AmbariException {
 
     this.ambariMetaInfo = ambariMetaInfo;
@@ -100,20 +111,15 @@ public class ServiceComponentImpl implements ServiceComponent {
     this.serviceComponentDesiredStateDAO = serviceComponentDesiredStateDAO;
     this.clusterServiceDAO = clusterServiceDAO;
     this.serviceComponentHostFactory = serviceComponentHostFactory;
-    this.stackDAO = stackDAO;
     this.eventPublisher = eventPublisher;
 
-    StackId stackId = service.getDesiredStackVersion();
-    StackEntity stackEntity = stackDAO.find(stackId.getStackName(), stackId.getStackVersion());
-
     ServiceComponentDesiredStateEntity desiredStateEntity = new ServiceComponentDesiredStateEntity();
     desiredStateEntity.setComponentName(componentName);
     desiredStateEntity.setDesiredState(State.INIT);
-    desiredStateEntity.setDesiredVersion(State.UNKNOWN.toString());
     desiredStateEntity.setServiceName(service.getName());
     desiredStateEntity.setClusterId(service.getClusterId());
     desiredStateEntity.setRecoveryEnabled(false);
-    desiredStateEntity.setDesiredStack(stackEntity);
+    desiredStateEntity.setDesiredRepositoryVersion(service.getDesiredRepositoryVersion());
 
     updateComponentInfo();
 
@@ -121,8 +127,9 @@ public class ServiceComponentImpl implements ServiceComponent {
     desiredStateEntityId = desiredStateEntity.getId();
   }
 
+  @Override
   public void updateComponentInfo() throws AmbariException {
-    StackId stackId = service.getDesiredStackVersion();
+    StackId stackId = service.getDesiredStackId();
     try {
       ComponentInfo compInfo = ambariMetaInfo.getComponent(stackId.getStackName(),
           stackId.getStackVersion(), service.getName(), componentName);
@@ -147,14 +154,13 @@ public class ServiceComponentImpl implements ServiceComponent {
       ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO,
       ClusterServiceDAO clusterServiceDAO,
       HostComponentDesiredStateDAO hostComponentDesiredStateDAO,
-      ServiceComponentHostFactory serviceComponentHostFactory, StackDAO stackDAO,
+      ServiceComponentHostFactory serviceComponentHostFactory,
       AmbariEventPublisher eventPublisher)
       throws AmbariException {
     this.service = service;
     this.serviceComponentDesiredStateDAO = serviceComponentDesiredStateDAO;
     this.clusterServiceDAO = clusterServiceDAO;
     this.serviceComponentHostFactory = serviceComponentHostFactory;
-    this.stackDAO = stackDAO;
     this.eventPublisher = eventPublisher;
     this.ambariMetaInfo = ambariMetaInfo;
 
@@ -177,7 +183,7 @@ public class ServiceComponentImpl implements ServiceComponent {
           serviceComponentHostFactory.createExisting(this,
             hostComponentStateEntity, hostComponentDesiredStateEntity));
       } catch(ProvisionException ex) {
-        StackId currentStackId = service.getCluster().getCurrentStackVersion();
+        StackId currentStackId = getDesiredStackId();
         LOG.error(String.format("Can not get host component info: stackName=%s, stackVersion=%s, serviceName=%s, componentName=%s, hostname=%s",
           currentStackId.getStackName(), currentStackId.getStackVersion(),
           service.getName(),serviceComponentDesiredStateEntity.getComponentName(), hostComponentStateEntity.getHostName()));
@@ -219,11 +225,8 @@ public class ServiceComponentImpl implements ServiceComponent {
   @Override
   public void setRecoveryEnabled(boolean recoveryEnabled) {
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Setting RecoveryEnabled of Component" + ", clusterName="
-          + service.getCluster().getClusterName() + ", clusterId="
-          + service.getCluster().getClusterId() + ", serviceName=" + service.getName()
-          + ", componentName=" + getName() + ", oldRecoveryEnabled=" + isRecoveryEnabled()
-          + ", newRecoveryEnabled=" + recoveryEnabled);
+      LOG.debug("Setting RecoveryEnabled of Component, clusterName={}, clusterId={}, serviceName={}, componentName={}, oldRecoveryEnabled={}, newRecoveryEnabled={}",
+        service.getCluster().getClusterName(), service.getCluster().getClusterId(), service.getName(), getName(), isRecoveryEnabled(), recoveryEnabled);
     }
 
     ServiceComponentDesiredStateEntity desiredStateEntity = serviceComponentDesiredStateDAO.findById(
@@ -256,7 +259,7 @@ public class ServiceComponentImpl implements ServiceComponent {
 
   @Override
   public Map<String, ServiceComponentHost> getServiceComponentHosts() {
-    return new HashMap<String, ServiceComponentHost>(hostComponents);
+    return new HashMap<>(hostComponents);
   }
 
   @Override
@@ -283,11 +286,8 @@ public class ServiceComponentImpl implements ServiceComponent {
       // TODO validation
       // TODO ensure host belongs to cluster
       if (LOG.isDebugEnabled()) {
-        LOG.debug("Adding a ServiceComponentHost to ServiceComponent" + ", clusterName="
-            + service.getCluster().getClusterName() + ", clusterId="
-            + service.getCluster().getClusterId() + ", serviceName=" + service.getName()
-            + ", serviceComponentName=" + getName() + ", hostname=" + hostComponent.getHostName()
-            + ", recoveryEnabled=" + isRecoveryEnabled());
+        LOG.debug("Adding a ServiceComponentHost to ServiceComponent, clusterName={}, clusterId={}, serviceName={}, serviceComponentName={}, hostname={}, recoveryEnabled={}",
+          service.getCluster().getClusterName(), service.getCluster().getClusterId(), service.getName(), getName(), hostComponent.getHostName(), isRecoveryEnabled());
       }
 
       if (hostComponents.containsKey(hostComponent.getHostName())) {
@@ -344,11 +344,8 @@ public class ServiceComponentImpl implements ServiceComponent {
   @Override
   public void setDesiredState(State state) {
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Setting DesiredState of Service" + ", clusterName="
-          + service.getCluster().getClusterName() + ", clusterId="
-          + service.getCluster().getClusterId() + ", serviceName=" + service.getName()
-          + ", serviceComponentName=" + getName() + ", oldDesiredState=" + getDesiredState()
-          + ", newDesiredState=" + state);
+      LOG.debug("Setting DesiredState of Service, clusterName={}, clusterId={}, serviceName={}, serviceComponentName={}, oldDesiredState={}, newDesiredState={}",
+        service.getCluster().getClusterName(), service.getCluster().getClusterId(), service.getName(), getName(), getDesiredState(), state);
     }
 
     ServiceComponentDesiredStateEntity desiredStateEntity = serviceComponentDesiredStateDAO.findById(
@@ -364,7 +361,7 @@ public class ServiceComponentImpl implements ServiceComponent {
   }
 
   @Override
-  public StackId getDesiredStackVersion() {
+  public StackId getDesiredStackId() {
     ServiceComponentDesiredStateEntity desiredStateEntity = serviceComponentDesiredStateDAO.findById(
         desiredStateEntityId);
 
@@ -376,22 +373,16 @@ public class ServiceComponentImpl implements ServiceComponent {
     }
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
-  public void setDesiredStackVersion(StackId stack) {
-    if (LOG.isDebugEnabled()) {
-      LOG.debug("Setting DesiredStackVersion of Service" + ", clusterName="
-          + service.getCluster().getClusterName() + ", clusterId="
-          + service.getCluster().getClusterId() + ", serviceName=" + service.getName()
-          + ", serviceComponentName=" + getName() + ", oldDesiredStackVersion="
-          + getDesiredStackVersion() + ", newDesiredStackVersion=" + stack);
-    }
-
+  public void setDesiredRepositoryVersion(RepositoryVersionEntity repositoryVersionEntity) {
     ServiceComponentDesiredStateEntity desiredStateEntity = serviceComponentDesiredStateDAO.findById(
         desiredStateEntityId);
 
     if (desiredStateEntity != null) {
-      StackEntity stackEntity = stackDAO.find(stack.getStackName(), stack.getStackVersion());
-      desiredStateEntity.setDesiredStack(stackEntity);
+      desiredStateEntity.setDesiredRepositoryVersion(repositoryVersionEntity);
       desiredStateEntity = serviceComponentDesiredStateDAO.merge(desiredStateEntity);
     } else {
       LOG.warn("Setting a member on an entity object that may have been "
@@ -399,35 +390,37 @@ public class ServiceComponentImpl implements ServiceComponent {
     }
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
-  public String getDesiredVersion() {
+  public RepositoryVersionEntity getDesiredRepositoryVersion() {
     ServiceComponentDesiredStateEntity desiredStateEntity = serviceComponentDesiredStateDAO.findById(
         desiredStateEntityId);
 
-    return desiredStateEntity.getDesiredVersion();
+    return desiredStateEntity.getDesiredRepositoryVersion();
   }
 
   @Override
-  public void setDesiredVersion(String version) {
+  public String getDesiredVersion() {
     ServiceComponentDesiredStateEntity desiredStateEntity = serviceComponentDesiredStateDAO.findById(
         desiredStateEntityId);
 
-      if (desiredStateEntity != null) {
-        desiredStateEntity.setDesiredVersion(version);
-      desiredStateEntity = serviceComponentDesiredStateDAO.merge(desiredStateEntity);
-      } else {
-        LOG.warn("Setting a member on an entity object that may have been " +
-          "previously deleted, serviceName = " + (service != null ? service.getName() : ""));
-      }
+    return desiredStateEntity.getDesiredVersion();
   }
 
   @Override
   public ServiceComponentResponse convertToResponse() {
     Cluster cluster = service.getCluster();
+    RepositoryVersionEntity repositoryVersionEntity = getDesiredRepositoryVersion();
+    StackId desiredStackId = repositoryVersionEntity.getStackId();
+
     ServiceComponentResponse r = new ServiceComponentResponse(getClusterId(),
         cluster.getClusterName(), service.getName(), getName(),
-        getDesiredStackVersion().getStackId(), getDesiredState().toString(),
-        getServiceComponentStateCount(), isRecoveryEnabled(), displayName);
+        desiredStackId, getDesiredState().toString(),
+        getServiceComponentStateCount(), isRecoveryEnabled(), displayName,
+        repositoryVersionEntity.getVersion(), getRepositoryState());
+
     return r;
   }
 
@@ -439,11 +432,14 @@ public class ServiceComponentImpl implements ServiceComponent {
 
   @Override
   public void debugDump(StringBuilder sb) {
-    sb.append("ServiceComponent={ serviceComponentName=" + getName() + ", recoveryEnabled="
-        + isRecoveryEnabled() + ", clusterName=" + service.getCluster().getClusterName()
-        + ", clusterId=" + service.getCluster().getClusterId() + ", serviceName="
-        + service.getName() + ", desiredStackVersion=" + getDesiredStackVersion()
-        + ", desiredState=" + getDesiredState().toString() + ", hostcomponents=[ ");
+    sb.append("ServiceComponent={ serviceComponentName=").append(getName())
+      .append(", recoveryEnabled=").append(isRecoveryEnabled())
+      .append(", clusterName=").append(service.getCluster().getClusterName())
+      .append(", clusterId=").append(service.getCluster().getClusterId())
+      .append(", serviceName=").append(service.getName())
+      .append(", desiredStackVersion=").append(getDesiredStackId())
+      .append(", desiredState=").append(getDesiredState())
+      .append(", hostcomponents=[ ");
     boolean first = true;
     for (ServiceComponentHost sch : hostComponents.values()) {
       if (!first) {
@@ -572,6 +568,152 @@ public class ServiceComponentImpl implements ServiceComponent {
   }
 
 
+  /**
+   * Follows this version logic:
+   * <table border="1">
+   *   <tr>
+   *     <th>DB hostcomponent1</th>
+   *     <th>DB hostcomponentN</th>
+   *     <th>DB desired</th>
+   *     <th>New desired</th>
+   *     <th>Repo State</th>
+   *   </tr>
+   *   <tr>
+   *     <td>v1</td>
+   *     <td>v1</td>
+   *     <td>UNKNOWN</td>
+   *     <td>v1</td>
+   *     <td>CURRENT</td>
+   *   </tr>
+   *   <tr>
+   *     <td>v1</td>
+   *     <td>v2</td>
+   *     <td>UNKNOWN</td>
+   *     <td>UNKNOWN</td>
+   *     <td>OUT_OF_SYNC</td>
+   *   </tr>
+   *   <tr>
+   *     <td>v1</td>
+   *     <td>v2</td>
+   *     <td>v2</td>
+   *     <td>v2 (no change)</td>
+   *     <td>OUT_OF_SYNC</td>
+   *   </tr>
+   *   <tr>
+   *     <td>v2</td>
+   *     <td>v2</td>
+   *     <td>v1</td>
+   *     <td>v1 (no change)</td>
+   *     <td>OUT_OF_SYNC</td>
+   *   </tr>
+   *   <tr>
+   *     <td>v2</td>
+   *     <td>v2</td>
+   *     <td>v2</td>
+   *     <td>v2 (no change)</td>
+   *     <td>CURRENT</td>
+   *   </tr>
+   * </table>
+   */
+  @Override
+  @Transactional
+  public void updateRepositoryState(String reportedVersion) throws AmbariException {
+
+    ServiceComponentDesiredStateEntity component = serviceComponentDesiredStateDAO.findById(
+        desiredStateEntityId);
+
+    List<ServiceComponentVersionEntity> componentVersions = serviceComponentDesiredStateDAO.findVersions(
+        getClusterId(), getServiceName(), getName());
+
+    // per component, this list should be small, so iterating here isn't a big deal
+    Map<String, ServiceComponentVersionEntity> map = new HashMap<>(Maps.uniqueIndex(componentVersions,
+        new Function<ServiceComponentVersionEntity, String>() {
+          @Override
+          public String apply(ServiceComponentVersionEntity input) {
+            return input.getRepositoryVersion().getVersion();
+          }
+      }));
+
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("Existing versions for {}/{}/{}: {}",
+          getClusterName(), getServiceName(), getName(), map.keySet());
+    }
+
+    ServiceComponentVersionEntity componentVersion = map.get(reportedVersion);
+
+    if (null == componentVersion) {
+      RepositoryVersionEntity repoVersion = repoVersionDAO.findByStackAndVersion(
+          getDesiredStackId(), reportedVersion);
+
+      if (null != repoVersion) {
+        componentVersion = new ServiceComponentVersionEntity();
+        componentVersion.setRepositoryVersion(repoVersion);
+        componentVersion.setState(RepositoryVersionState.INSTALLED);
+        componentVersion.setUserName("auto-reported");
+
+        // since we've never seen this version before, mark the component as CURRENT
+        component.setRepositoryState(RepositoryVersionState.CURRENT);
+        component.addVersion(componentVersion);
+
+        component = serviceComponentDesiredStateDAO.merge(component);
+
+        map.put(reportedVersion, componentVersion);
+
+      } else {
+        LOG.warn("There is no repository available for stack {}, version {}",
+            getDesiredStackId(), reportedVersion);
+      }
+    }
+
+    if (MapUtils.isNotEmpty(map)) {
+      String desiredVersion = component.getDesiredVersion();
+      RepositoryVersionEntity desiredRepositoryVersion = service.getDesiredRepositoryVersion();
+
+      List<HostComponentStateEntity> hostComponents = hostComponentDAO.findByServiceAndComponentAndNotVersion(
+          component.getServiceName(), component.getComponentName(), reportedVersion);
+
+      LOG.debug("{}/{} reportedVersion={}, desiredVersion={}, non-matching desired count={}, repo_state={}",
+          component.getServiceName(), component.getComponentName(), reportedVersion,
+          desiredVersion, hostComponents.size(), component.getRepositoryState());
+
+      // !!! if we are unknown, that means it's never been set.  Try to determine it.
+      if (StackVersionListener.UNKNOWN_VERSION.equals(desiredVersion)) {
+        if (CollectionUtils.isEmpty(hostComponents)) {
+          // all host components are the same version as reported
+          component.setDesiredRepositoryVersion(desiredRepositoryVersion);
+          component.setRepositoryState(RepositoryVersionState.CURRENT);
+        } else {
+          // desired is UNKNOWN and there's a mix of versions in the host components
+          component.setRepositoryState(RepositoryVersionState.OUT_OF_SYNC);
+        }
+      } else {
+        if (!reportedVersion.equals(desiredVersion)) {
+          component.setRepositoryState(RepositoryVersionState.OUT_OF_SYNC);
+        } else if (CollectionUtils.isEmpty(hostComponents)) {
+          component.setRepositoryState(RepositoryVersionState.CURRENT);
+        }
+      }
+
+      component = serviceComponentDesiredStateDAO.merge(component);
+    }
+  }
+
+  @Override
+  public RepositoryVersionState getRepositoryState() {
+    ServiceComponentDesiredStateEntity component = serviceComponentDesiredStateDAO.findById(
+        desiredStateEntityId);
+
+    if (null != component) {
+      return component.getRepositoryState();
+    } else {
+      LOG.warn("Cannot retrieve repository state on component that may have been deleted: service {}, component {}",
+          service != null ? service.getName() : null, componentName);
+
+      return null;
+    }
+  }
+
+
   private int getSCHCountByState(State state) {
     int count = 0;
     for (ServiceComponentHost sch : hostComponents.values()) {
@@ -583,7 +725,7 @@ public class ServiceComponentImpl implements ServiceComponent {
   }
 
   private Map <String, Integer> getServiceComponentStateCount() {
-    Map <String, Integer> serviceComponentStateCountMap = new HashMap <String, Integer>();
+    Map <String, Integer> serviceComponentStateCountMap = new HashMap<>();
     serviceComponentStateCountMap.put("startedCount", getSCHCountByState(State.STARTED));
     serviceComponentStateCountMap.put("installedCount", getSCHCountByState(State.INSTALLED));
     serviceComponentStateCountMap.put("installFailedCount", getSCHCountByState(State.INSTALL_FAILED));

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceFactory.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceFactory.java b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceFactory.java
index a3a041b..a4c953f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceFactory.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceFactory.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,10 +19,33 @@
 package org.apache.ambari.server.state;
 
 import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 
 public interface ServiceFactory {
 
-  Service createNew(Cluster cluster, String serviceName);
+  /**
+   * Creates a new service in memory and then persists it to the database.
+   *
+   * @param cluster
+   *          the cluster the service is for (not {@code null).
+   * @param serviceName
+   *          the name of the service (not {@code null).
+   * @param desiredRepositoryVersion
+   *          the repository version of the service (not {@code null).
+   * @return
+   */
+  Service createNew(Cluster cluster, String serviceName,
+      RepositoryVersionEntity desiredRepositoryVersion);
 
+  /**
+   * Creates an in-memory representation of a service from an existing database
+   * object.
+   *
+   * @param cluster
+   *          the cluster the service is installed in (not {@code null).
+   * @param serviceEntity
+   *          the entity the existing database entry (not {@code null).
+   * @return
+   */
   Service createExisting(Cluster cluster, ClusterServiceEntity serviceEntity);
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java
index 26fe9c0..e2956ca 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,6 +19,7 @@
 package org.apache.ambari.server.state;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -40,12 +41,11 @@ import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.ClusterServiceDAO;
 import org.apache.ambari.server.orm.dao.ServiceConfigDAO;
 import org.apache.ambari.server.orm.dao.ServiceDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
-import org.apache.ambari.server.orm.entities.ClusterConfigMappingEntity;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
 import org.apache.ambari.server.orm.entities.ClusterServiceEntityPK;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.ServiceConfigEntity;
 import org.apache.ambari.server.orm.entities.ServiceDesiredStateEntity;
@@ -84,11 +84,6 @@ public class ServiceImpl implements Service {
   private final ServiceComponentFactory serviceComponentFactory;
 
   /**
-   * Data access object for retrieving stack instances.
-   */
-  private final StackDAO stackDAO;
-
-  /**
    * Used to publish events relating to service CRUD operations.
    */
   private final AmbariEventPublisher eventPublisher;
@@ -99,17 +94,16 @@ public class ServiceImpl implements Service {
   private final String serviceName;
 
   @AssistedInject
-  ServiceImpl(@Assisted Cluster cluster, @Assisted String serviceName, ClusterDAO clusterDAO,
+  ServiceImpl(@Assisted Cluster cluster, @Assisted String serviceName,
+      @Assisted RepositoryVersionEntity desiredRepositoryVersion, ClusterDAO clusterDAO,
       ClusterServiceDAO clusterServiceDAO, ServiceDesiredStateDAO serviceDesiredStateDAO,
-      ServiceComponentFactory serviceComponentFactory, StackDAO stackDAO,
-      AmbariMetaInfo ambariMetaInfo, AmbariEventPublisher eventPublisher)
-      throws AmbariException {
+      ServiceComponentFactory serviceComponentFactory, AmbariMetaInfo ambariMetaInfo,
+      AmbariEventPublisher eventPublisher) throws AmbariException {
     this.cluster = cluster;
     this.clusterDAO = clusterDAO;
     this.clusterServiceDAO = clusterServiceDAO;
     this.serviceDesiredStateDAO = serviceDesiredStateDAO;
     this.serviceComponentFactory = serviceComponentFactory;
-    this.stackDAO = stackDAO;
     this.eventPublisher = eventPublisher;
     this.serviceName = serviceName;
     this.ambariMetaInfo = ambariMetaInfo;
@@ -120,15 +114,14 @@ public class ServiceImpl implements Service {
     ServiceDesiredStateEntity serviceDesiredStateEntity = new ServiceDesiredStateEntity();
     serviceDesiredStateEntity.setServiceName(serviceName);
     serviceDesiredStateEntity.setClusterId(cluster.getClusterId());
+    serviceDesiredStateEntity.setDesiredRepositoryVersion(desiredRepositoryVersion);
     serviceDesiredStateEntityPK = getServiceDesiredStateEntityPK(serviceDesiredStateEntity);
     serviceEntityPK = getServiceEntityPK(serviceEntity);
 
     serviceDesiredStateEntity.setClusterServiceEntity(serviceEntity);
     serviceEntity.setServiceDesiredStateEntity(serviceDesiredStateEntity);
 
-    StackId stackId = cluster.getDesiredStackVersion();
-    StackEntity stackEntity = stackDAO.find(stackId.getStackName(), stackId.getStackVersion());
-    serviceDesiredStateEntity.setDesiredStack(stackEntity);
+    StackId stackId = desiredRepositoryVersion.getStackId();
 
     ServiceInfo sInfo = ambariMetaInfo.getService(stackId.getStackName(),
         stackId.getStackVersion(), serviceName);
@@ -145,15 +138,13 @@ public class ServiceImpl implements Service {
   ServiceImpl(@Assisted Cluster cluster, @Assisted ClusterServiceEntity serviceEntity,
       ClusterDAO clusterDAO, ClusterServiceDAO clusterServiceDAO,
       ServiceDesiredStateDAO serviceDesiredStateDAO,
-      ServiceComponentFactory serviceComponentFactory, StackDAO stackDAO,
-      AmbariMetaInfo ambariMetaInfo, AmbariEventPublisher eventPublisher)
-      throws AmbariException {
+      ServiceComponentFactory serviceComponentFactory, AmbariMetaInfo ambariMetaInfo,
+      AmbariEventPublisher eventPublisher) throws AmbariException {
     this.cluster = cluster;
     this.clusterDAO = clusterDAO;
     this.clusterServiceDAO = clusterServiceDAO;
     this.serviceDesiredStateDAO = serviceDesiredStateDAO;
     this.serviceComponentFactory = serviceComponentFactory;
-    this.stackDAO = stackDAO;
     this.eventPublisher = eventPublisher;
     serviceName = serviceEntity.getServiceName();
     this.ambariMetaInfo = ambariMetaInfo;
@@ -170,7 +161,7 @@ public class ServiceImpl implements Service {
                 serviceComponentFactory.createExisting(this,
                     serviceComponentDesiredStateEntity));
           } catch(ProvisionException ex) {
-            StackId stackId = cluster.getCurrentStackVersion();
+            StackId stackId = new StackId(serviceComponentDesiredStateEntity.getDesiredStack());
             LOG.error(String.format("Can not get component info: stackName=%s, stackVersion=%s, serviceName=%s, componentName=%s",
                 stackId.getStackName(), stackId.getStackVersion(),
                 serviceEntity.getServiceName(),serviceComponentDesiredStateEntity.getComponentName()));
@@ -179,7 +170,7 @@ public class ServiceImpl implements Service {
       }
     }
 
-    StackId stackId = getDesiredStackVersion();
+    StackId stackId = getDesiredStackId();
     ServiceInfo sInfo = ambariMetaInfo.getService(stackId.getStackName(),
         stackId.getStackVersion(), getName());
     isClientOnlyService = sInfo.isClientOnlyService();
@@ -195,8 +186,8 @@ public class ServiceImpl implements Service {
   @Override
   public void updateServiceInfo() throws AmbariException {
     try {
-      ServiceInfo serviceInfo = ambariMetaInfo.getService(cluster.getDesiredStackVersion().getStackName(),
-              cluster.getDesiredStackVersion().getStackVersion(), getName());
+      ServiceInfo serviceInfo = ambariMetaInfo.getService(this);
+
       isClientOnlyService = serviceInfo.isClientOnlyService();
       isCredentialStoreSupported = serviceInfo.isCredentialStoreSupported();
       isCredentialStoreRequired = serviceInfo.isCredentialStoreRequired();
@@ -206,7 +197,7 @@ public class ServiceImpl implements Service {
               + " not recognized in stack info"
               + ", clusterName=" + cluster.getClusterName()
               + ", serviceName=" + getName()
-              + ", stackInfo=" + cluster.getDesiredStackVersion().getStackName());
+              + ", stackInfo=" + getDesiredStackId().getStackName());
     }
   }
 
@@ -222,7 +213,7 @@ public class ServiceImpl implements Service {
 
   @Override
   public Map<String, ServiceComponent> getServiceComponents() {
-    return new HashMap<String, ServiceComponent>(components);
+    return new HashMap<>(components);
   }
 
   @Override
@@ -275,11 +266,8 @@ public class ServiceImpl implements Service {
   @Override
   public void setDesiredState(State state) {
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Setting DesiredState of Service" + ", clusterName="
-          + cluster.getClusterName() + ", clusterId="
-          + cluster.getClusterId() + ", serviceName=" + getName()
-          + ", oldDesiredState=" + getDesiredState() + ", newDesiredState="
-          + state);
+      LOG.debug("Setting DesiredState of Service, clusterName={}, clusterId={}, serviceName={}, oldDesiredState={}, newDesiredState={}",
+        cluster.getClusterName(), cluster.getClusterId(), getName(), getDesiredState(), state);
     }
 
     ServiceDesiredStateEntity serviceDesiredStateEntity = getServiceDesiredStateEntity();
@@ -300,49 +288,81 @@ public class ServiceImpl implements Service {
     }
 
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Setting DesiredSecurityState of Service" + ", clusterName="
-          + cluster.getClusterName() + ", clusterId="
-          + cluster.getClusterId() + ", serviceName=" + getName()
-          + ", oldDesiredSecurityState=" + getSecurityState()
-          + ", newDesiredSecurityState=" + securityState);
+      LOG.debug("Setting DesiredSecurityState of Service, clusterName={}, clusterId={}, serviceName={}, oldDesiredSecurityState={}, newDesiredSecurityState={}",
+        cluster.getClusterName(), cluster.getClusterId(), getName(), getSecurityState(), securityState);
     }
     ServiceDesiredStateEntity serviceDesiredStateEntity = getServiceDesiredStateEntity();
     serviceDesiredStateEntity.setSecurityState(securityState);
     serviceDesiredStateDAO.merge(serviceDesiredStateEntity);
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
-  public StackId getDesiredStackVersion() {
+  public StackId getDesiredStackId() {
     ServiceDesiredStateEntity serviceDesiredStateEntity = getServiceDesiredStateEntity();
-    StackEntity desiredStackEntity = serviceDesiredStateEntity.getDesiredStack();
-    if( null != desiredStackEntity ) {
-      return new StackId(desiredStackEntity);
-    } else {
+
+    if (null == serviceDesiredStateEntity) {
       return null;
+    } else {
+      StackEntity desiredStackEntity = serviceDesiredStateEntity.getDesiredStack();
+      return new StackId(desiredStackEntity);
     }
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
-  public void setDesiredStackVersion(StackId stack) {
-    if (LOG.isDebugEnabled()) {
-      LOG.debug("Setting DesiredStackVersion of Service" + ", clusterName="
-          + cluster.getClusterName() + ", clusterId="
-          + cluster.getClusterId() + ", serviceName=" + getName()
-          + ", oldDesiredStackVersion=" + getDesiredStackVersion()
-          + ", newDesiredStackVersion=" + stack);
-    }
+  public RepositoryVersionEntity getDesiredRepositoryVersion() {
+    ServiceDesiredStateEntity serviceDesiredStateEntity = getServiceDesiredStateEntity();
+    return serviceDesiredStateEntity.getDesiredRepositoryVersion();
+  }
 
-    StackEntity stackEntity = stackDAO.find(stack.getStackName(), stack.getStackVersion());
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  @Transactional
+  public void setDesiredRepositoryVersion(RepositoryVersionEntity repositoryVersionEntity) {
     ServiceDesiredStateEntity serviceDesiredStateEntity = getServiceDesiredStateEntity();
-    serviceDesiredStateEntity.setDesiredStack(stackEntity);
+    serviceDesiredStateEntity.setDesiredRepositoryVersion(repositoryVersionEntity);
     serviceDesiredStateDAO.merge(serviceDesiredStateEntity);
+
+    Collection<ServiceComponent> components = getServiceComponents().values();
+    for (ServiceComponent component : components) {
+      component.setDesiredRepositoryVersion(repositoryVersionEntity);
+    }
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public RepositoryVersionState getRepositoryState() {
+    if (components.isEmpty()) {
+      return RepositoryVersionState.NOT_REQUIRED;
+    }
+
+    List<RepositoryVersionState> states = new ArrayList<>();
+    for( ServiceComponent component : components.values() ){
+      states.add(component.getRepositoryState());
+    }
+
+    return RepositoryVersionState.getAggregateState(states);
   }
 
   @Override
   public ServiceResponse convertToResponse() {
+    RepositoryVersionEntity desiredRespositoryVersion = getDesiredRepositoryVersion();
+    StackId desiredStackId = desiredRespositoryVersion.getStackId();
+
     ServiceResponse r = new ServiceResponse(cluster.getClusterId(), cluster.getClusterName(),
-        getName(), getDesiredStackVersion().getStackId(), getDesiredState().toString(),
-        isCredentialStoreSupported(), isCredentialStoreEnabled());
+        getName(), desiredStackId, desiredRespositoryVersion.getVersion(), getRepositoryState(),
+        getDesiredState().toString(), isCredentialStoreSupported(), isCredentialStoreEnabled());
+
+    r.setDesiredRepositoryVersionId(desiredRespositoryVersion.getId());
 
     r.setMaintenanceState(getMaintenanceState().name());
     return r;
@@ -405,11 +425,8 @@ public class ServiceImpl implements Service {
   @Override
   public void setCredentialStoreEnabled(boolean credentialStoreEnabled) {
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Setting CredentialStoreEnabled of Service" + ", clusterName="
-              + cluster.getClusterName() + ", clusterId="
-              + cluster.getClusterId() + ", serviceName=" + getName()
-              + ", oldCredentialStoreEnabled=" + isCredentialStoreEnabled()
-              + ", newCredentialStoreEnabled=" + credentialStoreEnabled);
+      LOG.debug("Setting CredentialStoreEnabled of Service, clusterName={}, clusterId={}, serviceName={}, oldCredentialStoreEnabled={}, newCredentialStoreEnabled={}",
+        cluster.getClusterName(), cluster.getClusterId(), getName(), isCredentialStoreEnabled(), credentialStoreEnabled);
     }
 
     ServiceDesiredStateEntity desiredStateEntity = getServiceDesiredStateEntity();
@@ -425,10 +442,12 @@ public class ServiceImpl implements Service {
 
   @Override
   public void debugDump(StringBuilder sb) {
-    sb.append("Service={ serviceName=" + getName() + ", clusterName=" + cluster.getClusterName()
-        + ", clusterId=" + cluster.getClusterId() + ", desiredStackVersion="
-        + getDesiredStackVersion() + ", desiredState=" + getDesiredState().toString()
-        + ", components=[ ");
+    sb.append("Service={ serviceName=").append(getName())
+      .append(", clusterName=").append(cluster.getClusterName())
+      .append(", clusterId=").append(cluster.getClusterId())
+      .append(", desiredStackVersion=").append(getDesiredStackId())
+      .append(", desiredState=").append(getDesiredState())
+      .append(", components=[ ");
     boolean first = true;
     for (ServiceComponent sc : components.values()) {
       if (!first) {
@@ -449,7 +468,7 @@ public class ServiceImpl implements Service {
     persistEntities(serviceEntity);
 
     // publish the service installed event
-    StackId stackId = cluster.getDesiredStackVersion();
+    StackId stackId = getDesiredStackId();
     cluster.addService(this);
 
     ServiceInstalledEvent event = new ServiceInstalledEvent(getClusterId(), stackId.getStackName(),
@@ -490,34 +509,19 @@ public class ServiceImpl implements Service {
 
   @Transactional
   void deleteAllServiceConfigs() throws AmbariException {
-    ArrayList<String> serviceConfigTypes = new ArrayList<>();
-    ArrayList<ClusterConfigEntity> clusterConfigEntitiesForDeletedServices = new ArrayList<>();
-    ServiceConfigEntity lastServiceConfigEntity = serviceConfigDAO.findMaxVersion(getClusterId(), getName());
-    //ensure service config version exist
+    long clusterId = getClusterId();
+    ServiceConfigEntity lastServiceConfigEntity = serviceConfigDAO.findMaxVersion(clusterId, getName());
+    // de-select every configuration from the service
     if (lastServiceConfigEntity != null) {
-      for (ClusterConfigEntity configEntity : lastServiceConfigEntity.getClusterConfigEntities()) {
-        serviceConfigTypes.add(configEntity.getType());
-        configEntity.setServiceDeleted(true);
-        clusterConfigEntitiesForDeletedServices.add(configEntity);
+      for (ClusterConfigEntity serviceConfigEntity : lastServiceConfigEntity.getClusterConfigEntities()) {
+        LOG.info("Disabling configuration {}", serviceConfigEntity);
+        serviceConfigEntity.setSelected(false);
+        serviceConfigEntity.setServiceDeleted(true);
+        clusterDAO.merge(serviceConfigEntity);
       }
-
-      LOG.info("Deselecting config mapping for cluster, clusterId={}, configTypes={} ",
-          getClusterId(), serviceConfigTypes);
-
-      List<ClusterConfigMappingEntity> configMappingEntities =
-          clusterDAO.getSelectedConfigMappingByTypes(getClusterId(), serviceConfigTypes);
-
-      for (ClusterConfigMappingEntity configMappingEntity : configMappingEntities) {
-        configMappingEntity.setSelected(0);
-      }
-
-      clusterDAO.mergeConfigMappings(configMappingEntities);
-      clusterDAO.mergeConfigs(clusterConfigEntitiesForDeletedServices);
     }
 
-    LOG.info("Deleting all serviceconfigs for service"
-        + ", clusterName=" + cluster.getClusterName()
-        + ", serviceName=" + getName());
+    LOG.info("Deleting all configuration associations for {} on cluster {}", getName(), cluster.getClusterName());
 
     List<ServiceConfigEntity> serviceConfigEntities =
       serviceConfigDAO.findByService(cluster.getClusterId(), getName());
@@ -589,10 +593,14 @@ public class ServiceImpl implements Service {
     deleteAllComponents();
     deleteAllServiceConfigs();
 
+    StackId stackId = getDesiredStackId();
+
     removeEntities();
 
     // publish the service removed event
-    StackId stackId = cluster.getDesiredStackVersion();
+    if (null == stackId) {
+      return;
+    }
 
     ServiceRemovedEvent event = new ServiceRemovedEvent(getClusterId(), stackId.getStackName(),
         stackId.getStackVersion(), getName());
@@ -603,12 +611,7 @@ public class ServiceImpl implements Service {
   @Transactional
   protected void removeEntities() throws AmbariException {
     serviceDesiredStateDAO.removeByPK(serviceDesiredStateEntityPK);
-
-    ClusterServiceEntityPK pk = new ClusterServiceEntityPK();
-    pk.setClusterId(getClusterId());
-    pk.setServiceName(getName());
-
-    clusterServiceDAO.removeByPK(pk);
+    clusterServiceDAO.removeByPK(serviceEntityPK);
   }
 
   @Override
@@ -627,10 +630,6 @@ public class ServiceImpl implements Service {
     return getServiceDesiredStateEntity().getMaintenanceState();
   }
 
-  private ClusterServiceEntity getServiceEntity() {
-    return clusterServiceDAO.findByPK(serviceEntityPK);
-  }
-
   private ClusterServiceEntityPK getServiceEntityPK(ClusterServiceEntity serviceEntity) {
     ClusterServiceEntityPK pk = new ClusterServiceEntityPK();
     pk.setClusterId(serviceEntity.getClusterId());


[04/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog230Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog230Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog230Test.java
deleted file mode 100644
index 947ba2e..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog230Test.java
+++ /dev/null
@@ -1,313 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.ambari.server.upgrade;
-
-import static org.easymock.EasyMock.*;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.dao.DaoUtils;
-import org.apache.ambari.server.orm.dao.PermissionDAO;
-import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
-import org.apache.ambari.server.orm.dao.RoleAuthorizationDAO;
-import org.apache.ambari.server.orm.entities.PermissionEntity;
-import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
-import org.apache.ambari.server.orm.entities.RoleAuthorizationEntity;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.easymock.Capture;
-import org.easymock.EasyMock;
-import org.easymock.EasyMockSupport;
-import org.junit.Assert;
-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 javax.persistence.EntityManager;
-
-/**
- * UpgradeCatalog230 tests.
- */
-public class UpgradeCatalog230Test extends EasyMockSupport {
-
-  private Injector injector;
-
-  @Before
-  public void setup() {
-    resetAll();
-
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(createMock(DBAccessor.class));
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        binder.bind(EntityManager.class).toInstance(createNiceMock(EntityManager.class));
-        binder.bind(DaoUtils.class).toInstance(createNiceMock(DaoUtils.class));
-        binder.bind(PermissionDAO.class).toInstance(createMock(PermissionDAO.class));
-        binder.bind(ResourceTypeDAO.class).toInstance(createMock(ResourceTypeDAO.class));
-        binder.bind(RoleAuthorizationDAO.class).toInstance(createMock(RoleAuthorizationDAO.class));
-      }
-    };
-
-    injector = Guice.createInjector(module);
-  }
-
-  @Test
-  public void testExecuteDDLUpdates() throws Exception {
-
-    final DBAccessor dbAccessor = injector.getInstance(DBAccessor.class);
-
-    Configuration configuration = createNiceMock(Configuration.class);
-    expect(configuration.getDatabaseUrl()).andReturn(Configuration.JDBC_IN_MEMORY_URL).anyTimes();
-
-    Capture<DBAccessor.DBColumnInfo> columnCapture = EasyMock.newCapture();
-    Capture<DBAccessor.DBColumnInfo> columnCaptureUserType = EasyMock.newCapture();
-    Capture<DBAccessor.DBColumnInfo> columnCapturePermissionLabel = EasyMock.newCapture();
-    Capture<List<DBAccessor.DBColumnInfo>> columnsCaptureRoleAuthorization = EasyMock.newCapture();
-    Capture<List<DBAccessor.DBColumnInfo>> columnsCapturePermissionRoleAuthorization = EasyMock.newCapture();
-
-    dbAccessor.alterColumn(eq("host_role_command"), capture(columnCapture));
-    expectLastCall();
-
-    dbAccessor.executeQuery("UPDATE users SET user_type='LDAP' WHERE ldap_user=1");
-    expectLastCall();
-
-    dbAccessor.addUniqueConstraint("users", "UNQ_users_0", "user_name", "user_type");
-    expectLastCall();
-
-    dbAccessor.addColumn(eq("users"), capture(columnCaptureUserType));
-    expectLastCall();
-
-    dbAccessor.addColumn(eq("adminpermission"), capture(columnCapturePermissionLabel));
-    expectLastCall();
-
-    dbAccessor.createTable(eq("roleauthorization"), capture(columnsCaptureRoleAuthorization), eq("authorization_id"));
-    expectLastCall();
-
-    dbAccessor.createTable(eq("permission_roleauthorization"), capture(columnsCapturePermissionRoleAuthorization), eq("permission_id"), eq("authorization_id"));
-    expectLastCall();
-
-    dbAccessor.addFKConstraint("permission_roleauthorization", "FK_permission_roleauth_pid",
-        "permission_id", "adminpermission", "permission_id", false);
-    expectLastCall();
-
-    dbAccessor.addFKConstraint("permission_roleauthorization", "FK_permission_roleauth_aid",
-        "authorization_id", "roleauthorization", "authorization_id", false);
-    expectLastCall();
-
-    replayAll();
-    AbstractUpgradeCatalog upgradeCatalog = injector.getInstance(UpgradeCatalog230.class);
-    Class<?> c = AbstractUpgradeCatalog.class;
-    Field f = c.getDeclaredField("configuration");
-    f.setAccessible(true);
-    f.set(upgradeCatalog, configuration);
-
-    upgradeCatalog.executeDDLUpdates();
-    verifyAll();
-
-    assertTrue(columnCapture.getValue().isNullable());
-
-    assertEquals(columnCaptureUserType.getValue().getName(), "user_type");
-    assertEquals(columnCaptureUserType.getValue().getType(), String.class);
-    assertEquals(columnCaptureUserType.getValue().getLength(), null);
-    assertEquals(columnCaptureUserType.getValue().getDefaultValue(), "LOCAL");
-    assertEquals(columnCaptureUserType.getValue().isNullable(), true);
-
-    assertEquals(columnCapturePermissionLabel.getValue().getName(), "permission_label");
-    assertEquals(columnCapturePermissionLabel.getValue().getType(), String.class);
-    assertEquals(columnCapturePermissionLabel.getValue().getLength(), Integer.valueOf(255));
-    assertEquals(columnCapturePermissionLabel.getValue().isNullable(), true);
-
-    List<DBAccessor.DBColumnInfo> columnInfos;
-    DBAccessor.DBColumnInfo columnInfo;
-
-    // Verify roleauthorization table
-    columnInfos = columnsCaptureRoleAuthorization.getValue();
-    assertEquals(2, columnInfos.size());
-
-    columnInfo = columnInfos.get(0);
-    assertEquals("authorization_id", columnInfo.getName());
-    assertEquals(String.class, columnInfo.getType());
-    assertEquals(Integer.valueOf(100), columnInfo.getLength());
-
-    columnInfo = columnInfos.get(1);
-    assertEquals("authorization_name", columnInfo.getName());
-    assertEquals(String.class, columnInfo.getType());
-    assertEquals(Integer.valueOf(255), columnInfo.getLength());
-
-    // Verify permission_roleauthorization table
-    columnInfos = columnsCapturePermissionRoleAuthorization.getValue();
-    assertEquals(2, columnInfos.size());
-
-    columnInfo = columnInfos.get(0);
-    assertEquals("permission_id", columnInfo.getName());
-    assertEquals(Long.class, columnInfo.getType());
-    assertEquals(null, columnInfo.getLength());
-
-    columnInfo = columnInfos.get(1);
-    assertEquals("authorization_id", columnInfo.getName());
-    assertEquals(String.class, columnInfo.getType());
-    assertEquals(Integer.valueOf(100), columnInfo.getLength());
-  }
-
-  @Test
-  public void testExecuteDMLUpdates() throws Exception {
-    final DBAccessor dbAccessor = injector.getInstance(DBAccessor.class);
-    UpgradeCatalog230 upgradeCatalog = injector.getInstance(UpgradeCatalog230.class);
-
-    final ResourceTypeEntity ambariResourceTypeEntity = createMock(ResourceTypeEntity.class);
-    expect(ambariResourceTypeEntity.getId()).andReturn(1).anyTimes();
-
-    final ResourceTypeEntity clusterResourceTypeEntity = createMock(ResourceTypeEntity.class);
-    expect(clusterResourceTypeEntity.getId()).andReturn(2).anyTimes();
-
-    final ResourceTypeEntity viewResourceTypeEntity = createMock(ResourceTypeEntity.class);
-    expect(viewResourceTypeEntity.getId()).andReturn(3).anyTimes();
-
-    final ResourceTypeDAO resourceTypeDAO = injector.getInstance(ResourceTypeDAO.class);
-    expect(resourceTypeDAO.findByName("AMBARI")).andReturn(ambariResourceTypeEntity).anyTimes();
-    expect(resourceTypeDAO.findByName("CLUSTER")).andReturn(clusterResourceTypeEntity).anyTimes();
-    expect(resourceTypeDAO.findByName("VIEW")).andReturn(viewResourceTypeEntity).anyTimes();
-
-    final PermissionEntity viewUserPermissionEntity = createMock(PermissionEntity.class);
-    expect(viewUserPermissionEntity.getId()).andReturn(1).anyTimes();
-
-    final PermissionEntity ambariAdministratorPermissionEntity = createMock(PermissionEntity.class);
-    expect(ambariAdministratorPermissionEntity.getId()).andReturn(2).anyTimes();
-
-    final PermissionEntity clusterUserPermissionEntity = createMock(PermissionEntity.class);
-    expect(clusterUserPermissionEntity.getId()).andReturn(3).anyTimes();
-
-    final PermissionEntity clusterOperatorPermissionEntity = createMock(PermissionEntity.class);
-    expect(clusterOperatorPermissionEntity.getId()).andReturn(4).anyTimes();
-
-    final PermissionEntity clusterAdministratorPermissionEntity = createMock(PermissionEntity.class);
-    expect(clusterAdministratorPermissionEntity.getId()).andReturn(5).anyTimes();
-
-    final PermissionEntity serviceAdministratorPermissionEntity = createMock(PermissionEntity.class);
-    expect(serviceAdministratorPermissionEntity.getId()).andReturn(6).anyTimes();
-
-    final PermissionEntity serviceOperatorPermissionEntity = createMock(PermissionEntity.class);
-    expect(serviceOperatorPermissionEntity.getId()).andReturn(7).anyTimes();
-
-    final PermissionDAO permissionDAO = injector.getInstance(PermissionDAO.class);
-    expect(permissionDAO.findPermissionByNameAndType("VIEW.USER", viewResourceTypeEntity))
-        .andReturn(viewUserPermissionEntity)
-        .anyTimes();
-    expect(permissionDAO.findPermissionByNameAndType("AMBARI.ADMINISTRATOR", ambariResourceTypeEntity))
-        .andReturn(ambariAdministratorPermissionEntity)
-        .anyTimes();
-    expect(permissionDAO.findPermissionByNameAndType("CLUSTER.USER", clusterResourceTypeEntity))
-        .andReturn(clusterUserPermissionEntity)
-        .anyTimes();
-    expect(permissionDAO.findPermissionByNameAndType("CLUSTER.OPERATOR", clusterResourceTypeEntity))
-        .andReturn(clusterOperatorPermissionEntity)
-        .anyTimes();
-    expect(permissionDAO.findPermissionByNameAndType("CLUSTER.ADMINISTRATOR", clusterResourceTypeEntity))
-        .andReturn(clusterAdministratorPermissionEntity)
-        .anyTimes();
-    expect(permissionDAO.findPermissionByNameAndType("SERVICE.ADMINISTRATOR", clusterResourceTypeEntity))
-        .andReturn(serviceAdministratorPermissionEntity)
-        .anyTimes();
-    expect(permissionDAO.findPermissionByNameAndType("SERVICE.OPERATOR", clusterResourceTypeEntity))
-        .andReturn(serviceOperatorPermissionEntity)
-        .anyTimes();
-
-    String updateQueryPattern;
-
-    // Set permission labels
-    updateQueryPattern = "UPDATE adminpermission SET permission_label='%s' WHERE permission_id=%d";
-    expect(dbAccessor.executeUpdate(String.format(updateQueryPattern,
-        "Ambari Administrator", PermissionEntity.AMBARI_ADMINISTRATOR_PERMISSION)))
-        .andReturn(1).once();
-    expect(dbAccessor.executeUpdate(String.format(updateQueryPattern,
-        "Cluster User", PermissionEntity.CLUSTER_USER_PERMISSION)))
-        .andReturn(1).once();
-    expect(dbAccessor.executeUpdate(String.format(updateQueryPattern,
-        "Cluster Administrator", PermissionEntity.CLUSTER_ADMINISTRATOR_PERMISSION)))
-        .andReturn(1).once();
-    expect(dbAccessor.executeUpdate(String.format(updateQueryPattern,
-        "View User", PermissionEntity.VIEW_USER_PERMISSION)))
-        .andReturn(1).once();
-
-    // Update permissions names
-    updateQueryPattern = "UPDATE adminpermission SET permission_name='%s' WHERE permission_id=%d";
-    expect(dbAccessor.executeUpdate(String.format(updateQueryPattern,
-        PermissionEntity.AMBARI_ADMINISTRATOR_PERMISSION_NAME, PermissionEntity.AMBARI_ADMINISTRATOR_PERMISSION)))
-        .andReturn(1).once();
-    expect(dbAccessor.executeUpdate(String.format(updateQueryPattern,
-        PermissionEntity.CLUSTER_USER_PERMISSION_NAME, PermissionEntity.CLUSTER_USER_PERMISSION)))
-        .andReturn(1).once();
-    expect(dbAccessor.executeUpdate(String.format(updateQueryPattern,
-        PermissionEntity.CLUSTER_ADMINISTRATOR_PERMISSION_NAME, PermissionEntity.CLUSTER_ADMINISTRATOR_PERMISSION)))
-        .andReturn(1).once();
-    expect(dbAccessor.executeUpdate(String.format(updateQueryPattern,
-        PermissionEntity.VIEW_USER_PERMISSION_NAME, PermissionEntity.VIEW_USER_PERMISSION)))
-        .andReturn(1).once();
-
-    RoleAuthorizationEntity roleAuthorization = createMock(RoleAuthorizationEntity.class);
-
-    RoleAuthorizationDAO roleAuthorizationDAO = injector.getInstance(RoleAuthorizationDAO.class);
-    expect(roleAuthorizationDAO.findById(anyString())).andReturn(roleAuthorization).anyTimes();
-
-    Collection<RoleAuthorizationEntity> authorizations = new ArrayList<RoleAuthorizationEntity>();
-
-    expect(ambariAdministratorPermissionEntity.getAuthorizations()).andReturn(authorizations).atLeastOnce();
-    expect(clusterAdministratorPermissionEntity.getAuthorizations()).andReturn(authorizations).atLeastOnce();
-    expect(clusterOperatorPermissionEntity.getAuthorizations()).andReturn(authorizations).atLeastOnce();
-    expect(serviceAdministratorPermissionEntity.getAuthorizations()).andReturn(authorizations).atLeastOnce();
-    expect(serviceOperatorPermissionEntity.getAuthorizations()).andReturn(authorizations).atLeastOnce();
-    expect(clusterUserPermissionEntity.getAuthorizations()).andReturn(authorizations).atLeastOnce();
-    expect(viewUserPermissionEntity.getAuthorizations()).andReturn(authorizations).atLeastOnce();
-
-    expect(permissionDAO.merge(ambariAdministratorPermissionEntity)).andReturn(ambariAdministratorPermissionEntity).atLeastOnce();
-    expect(permissionDAO.merge(clusterAdministratorPermissionEntity)).andReturn(clusterAdministratorPermissionEntity).atLeastOnce();
-    expect(permissionDAO.merge(clusterOperatorPermissionEntity)).andReturn(clusterOperatorPermissionEntity).atLeastOnce();
-    expect(permissionDAO.merge(serviceAdministratorPermissionEntity)).andReturn(serviceAdministratorPermissionEntity).atLeastOnce();
-    expect(permissionDAO.merge(serviceOperatorPermissionEntity)).andReturn(serviceOperatorPermissionEntity).atLeastOnce();
-    expect(permissionDAO.merge(clusterUserPermissionEntity)).andReturn(clusterUserPermissionEntity).atLeastOnce();
-    expect(permissionDAO.merge(viewUserPermissionEntity)).andReturn(viewUserPermissionEntity).atLeastOnce();
-
-    replayAll();
-    upgradeCatalog.executeDMLUpdates();
-    verifyAll();
-  }
-
-  @Test
-  public void testGetTargetVersion() throws Exception {
-    UpgradeCatalog upgradeCatalog = injector.getInstance(UpgradeCatalog230.class);
-    Assert.assertEquals("2.3.0", upgradeCatalog.getTargetVersion());
-  }
-
-  @Test
-  public void testGetSourceVersion() {
-    UpgradeCatalog upgradeCatalog = injector.getInstance(UpgradeCatalog230.class);
-    Assert.assertEquals("2.2.1", upgradeCatalog.getSourceVersion());
-  }
-
-}


[30/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/DistributeRepositoriesActionListener.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/DistributeRepositoriesActionListener.java b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/DistributeRepositoriesActionListener.java
index cd82957..c3c8f67 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/DistributeRepositoriesActionListener.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/DistributeRepositoriesActionListener.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,7 +19,6 @@ package org.apache.ambari.server.events.listeners.upgrade;
 
 import java.util.List;
 
-import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.EagerSingleton;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.bootstrap.DistributeRepositoriesStructuredOutput;
@@ -29,11 +28,11 @@ import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
-import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.utils.StageUtils;
+import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -118,10 +117,11 @@ public class DistributeRepositoriesActionListener {
 
         // Handle the case in which the version to install did not contain the build number,
         // but the structured output does contain the build number.
-        if (null != structuredOutput.getActualVersion() && !structuredOutput.getActualVersion().isEmpty() &&
-            null != structuredOutput.getInstalledRepositoryVersion() && !structuredOutput.getInstalledRepositoryVersion().isEmpty() &&
-            null != structuredOutput.getStackId() && !structuredOutput.getStackId().isEmpty() &&
-            !structuredOutput.getActualVersion().equals(structuredOutput.getInstalledRepositoryVersion())) {
+        if (!StringUtils.isEmpty(structuredOutput.getActualVersion())
+            && !StringUtils.isEmpty(structuredOutput.getInstalledRepositoryVersion())
+            && !StringUtils.isEmpty(structuredOutput.getStackId())
+            && !StringUtils.equals(structuredOutput.getActualVersion(),
+                structuredOutput.getInstalledRepositoryVersion())) {
 
           // !!! getInstalledRepositoryVersion() from the agent is the one
           // entered in the UI.  getActualVersion() is computed.
@@ -176,13 +176,6 @@ public class DistributeRepositoriesActionListener {
       if (hostVersion.getState() == RepositoryVersionState.INSTALLING) {
         hostVersion.setState(newHostState);
         hostVersionDAO.get().merge(hostVersion);
-        // Update state of a cluster stack version
-        try {
-          Cluster cluster = clusters.get().getClusterById(clusterId);
-          cluster.recalculateClusterVersionState(hostVersion.getRepositoryVersion());
-        } catch (AmbariException e) {
-          LOG.error("Cannot get cluster with Id " + clusterId.toString() + " to recalculate its ClusterVersion.", e);
-        }
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java
index 51ee285..2eb89a2 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListener.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -20,12 +20,12 @@ package org.apache.ambari.server.events.listeners.upgrade;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.concurrent.locks.Lock;
 
+import org.apache.ambari.annotations.Experimental;
+import org.apache.ambari.annotations.ExperimentalFeature;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.EagerSingleton;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
@@ -38,7 +38,7 @@ import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 import org.apache.ambari.server.logging.LockFactory;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
+import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
@@ -64,8 +64,6 @@ import com.google.inject.persist.Transactional;
  * handles {@link org.apache.ambari.server.events.ServiceInstalledEvent} and
  * {@link org.apache.ambari.server.events.ServiceComponentInstalledEvent}
  * to update {@link org.apache.ambari.server.state.RepositoryVersionState}
- *
- * @see org.apache.ambari.server.state.Cluster#recalculateClusterVersionState(RepositoryVersionEntity)
  */
 @Singleton
 @EagerSingleton
@@ -87,6 +85,9 @@ public class HostVersionOutOfSyncListener {
   @Inject
   private Provider<AmbariMetaInfo> ami;
 
+  @Inject
+  private Provider<RepositoryVersionDAO> repositoryVersionDAO;
+
   /**
    * The publisher may be an asynchronous, multi-threaded one, so to avoid the (rare, but possible) case
    * of both an Install and Uninstall event occurring at the same time, we use a Lock.
@@ -140,7 +141,6 @@ public class HostVersionOutOfSyncListener {
           case NOT_REQUIRED:
             hostVersionEntity.setState(RepositoryVersionState.OUT_OF_SYNC);
             hostVersionDAO.get().merge(hostVersionEntity);
-            cluster.recalculateClusterVersionState(hostVersionEntity.getRepositoryVersion());
             break;
           default:
             break;
@@ -224,11 +224,11 @@ public class HostVersionOutOfSyncListener {
 
     try {
       Cluster cluster = clusters.get().getClusterById(event.getClusterId());
-      Set<RepositoryVersionEntity> changedRepositoryVersions = new HashSet<RepositoryVersionEntity>();
+
       Map<String, ServiceComponent> serviceComponents = cluster.getService(event.getServiceName()).getServiceComponents();
       // Determine hosts that become OUT_OF_SYNC when adding components for new service
       Map<String, List<ServiceComponent>> affectedHosts =
-              new HashMap<String, List<ServiceComponent>>();
+        new HashMap<>();
       for (ServiceComponent component : serviceComponents.values()) {
         for (String hostname : component.getServiceComponentHosts().keySet()) {
           if (! affectedHosts.containsKey(hostname)) {
@@ -262,18 +262,20 @@ public class HostVersionOutOfSyncListener {
           if (hostVersionEntity.getState().equals(RepositoryVersionState.INSTALLED)) {
             hostVersionEntity.setState(RepositoryVersionState.OUT_OF_SYNC);
             hostVersionDAO.get().merge(hostVersionEntity);
-            changedRepositoryVersions.add(repositoryVersion);
           }
         }
       }
-      for (RepositoryVersionEntity repositoryVersion : changedRepositoryVersions) {
-        cluster.recalculateClusterVersionState(repositoryVersion);
-      }
+
     } catch (AmbariException e) {
       LOG.error("Can not update hosts about out of sync", e);
     }
   }
 
+  /**
+   * When hosts are added, add a host_version record for every repo_version in the database.
+   *
+   * @param event the add event
+   */
   @Subscribe
   @Transactional
   public void onHostEvent(HostsAddedEvent event) {
@@ -281,37 +283,30 @@ public class HostVersionOutOfSyncListener {
       LOG.debug(event.toString());
     }
 
-    try {
-      Cluster cluster = clusters.get().getClusterById(event.getClusterId());
+    // create host version entries for every repository
+    @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES, comment="Eventually take into account deleted repositories")
+    List<RepositoryVersionEntity> repos = repositoryVersionDAO.get().findAll();
 
-      Collection<ClusterVersionEntity> allClusterVersions = cluster.getAllClusterVersions();
-      for (ClusterVersionEntity clusterVersion : allClusterVersions) {
-        if (clusterVersion.getState() != RepositoryVersionState.CURRENT) { // Current version is taken care of automatically
-          RepositoryVersionEntity repositoryVersion = clusterVersion.getRepositoryVersion();
-          for (String hostName : event.getHostNames()) {
-            HostEntity hostEntity = hostDAO.get().findByName(hostName);
-            HostVersionEntity missingHostVersion = new HostVersionEntity(hostEntity,
-              repositoryVersion, RepositoryVersionState.OUT_OF_SYNC);
-
-            LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
-              missingHostVersion.getHostName(), missingHostVersion.getState(),
-              missingHostVersion.getRepositoryVersion().getVersion(), missingHostVersion.getRepositoryVersion().getId());
-            hostVersionDAO.get().create(missingHostVersion);
-          }
-          cluster.recalculateClusterVersionState(repositoryVersion);
-        }
+    for (String hostName : event.getHostNames()) {
+      HostEntity hostEntity = hostDAO.get().findByName(hostName);
+
+      for (RepositoryVersionEntity repositoryVersion : repos) {
+
+        // we don't have the knowledge yet to know if we need the record
+        HostVersionEntity missingHostVersion = new HostVersionEntity(hostEntity,
+            repositoryVersion, RepositoryVersionState.NOT_REQUIRED);
+
+        LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
+          missingHostVersion.getHostName(), missingHostVersion.getState(),
+          missingHostVersion.getRepositoryVersion().getVersion(), missingHostVersion.getRepositoryVersion().getId());
+
+        hostVersionDAO.get().create(missingHostVersion);
       }
-    } catch (AmbariException e) {
-      LOG.error("Can not update hosts about out of sync", e);
     }
   }
 
   /**
-   * Recalculates the cluster repo version state when a host is removed. If
-   * hosts are removed during an upgrade, the remaining hosts will all be in the
-   * {@link RepositoryVersionState#INSTALLED} state, but the cluster will never
-   * transition into this state. This is because when the host is removed, a
-   * recalculation must happen.
+   * Host repo_version entities are removed via cascade.
    *
    * @param event
    *          the removal event.
@@ -322,36 +317,6 @@ public class HostVersionOutOfSyncListener {
     if (LOG.isDebugEnabled()) {
       LOG.debug(event.toString());
     }
-
-    try {
-      Set<Cluster> clusters = event.getClusters();
-      for (Cluster cluster : clusters) {
-        Collection<ClusterVersionEntity> allClusterVersions = cluster.getAllClusterVersions();
-
-        for (ClusterVersionEntity clusterVersion : allClusterVersions) {
-          RepositoryVersionState repositoryVersionState = clusterVersion.getState();
-
-          // the CURRENT/INSTALLED states should not be affected by a host
-          // removal - if it's already current then removing a host will never
-          // make it not CURRENT or not INSTALLED
-          switch (repositoryVersionState) {
-            case CURRENT:
-            case INSTALLED:
-              continue;
-            default:
-              break;
-          }
-
-          RepositoryVersionEntity repositoryVersion = clusterVersion.getRepositoryVersion();
-          cluster.recalculateClusterVersionState(repositoryVersion);
-        }
-      }
-
-    } catch (AmbariException ambariException) {
-      LOG.error(
-          "Unable to recalculate the cluster repository version state when a host was removed",
-          ambariException);
-    }
   }
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
index 3af5520..f8d5a5f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListener.java
@@ -17,16 +17,11 @@
  */
 package org.apache.ambari.server.events.listeners.upgrade;
 
-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.HostComponentVersionAdvertisedEvent;
 import org.apache.ambari.server.events.publishers.VersionEventPublisher;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
-import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.ServiceComponent;
@@ -56,12 +51,6 @@ public class StackVersionListener {
   private final static Logger LOG = LoggerFactory.getLogger(StackVersionListener.class);
   public static final String UNKNOWN_VERSION = State.UNKNOWN.toString();
 
-  /**
-   * Used to prevent multiple threads from trying to update the same host
-   * version simultaneously.
-   */
-  private Lock m_stackVersionLock = new ReentrantLock();
-
   @Inject
   private RepositoryVersionDAO repositoryVersionDAO;
 
@@ -104,8 +93,6 @@ public class StackVersionListener {
       }
     }
 
-    m_stackVersionLock.lock();
-
     // Update host component version value if needed
     try {
       ServiceComponent sc = cluster.getService(sch.getServiceName()).getServiceComponent(
@@ -122,12 +109,8 @@ public class StackVersionListener {
         return;
       }
 
-      boolean desiredVersionIsCurrentlyUnknown = StringUtils.equalsIgnoreCase(UNKNOWN_VERSION,
-          sc.getDesiredVersion());
-
-      // proces the UNKNOWN version being received or currently desired
-      if (StringUtils.equalsIgnoreCase(UNKNOWN_VERSION, newVersion)
-          || desiredVersionIsCurrentlyUnknown) {
+      // proces the UNKNOWN version
+      if (StringUtils.equalsIgnoreCase(UNKNOWN_VERSION, newVersion)) {
         processUnknownDesiredVersion(cluster, sc, sch, newVersion);
         return;
       }
@@ -137,8 +120,6 @@ public class StackVersionListener {
       LOG.error(
           "Unable to propagate version for ServiceHostComponent on component: {}, host: {}. Error: {}",
           sch.getServiceComponentName(), sch.getHostName(), e.getMessage());
-    } finally {
-      m_stackVersionLock.unlock();
     }
   }
 
@@ -168,57 +149,38 @@ public class StackVersionListener {
     String desiredVersion = sc.getDesiredVersion();
     UpgradeState upgradeState = sch.getUpgradeState();
 
-    // was this version expected
-    boolean newVersionMatchesDesired = StringUtils.equals(desiredVersion, newVersion);
-
-    // was the prior version UNKNOWN or null
-    boolean previousVersionIsUnknown = StringUtils.equalsIgnoreCase(UNKNOWN_VERSION,
-        previousVersion) || StringUtils.isBlank(previousVersion);
-
-    boolean desiredVersionIsUnknown = StringUtils.equalsIgnoreCase(UNKNOWN_VERSION, desiredVersion);
-
-    // is there an upgrade in progress for this component
-    boolean isUpgradeInProgressForThisComponent = null != cluster.getUpgradeInProgress()
-        && upgradeState != UpgradeState.NONE;
-
-    // if the current version is an actual value (ie 2.2.0.0-1234 and not
-    // UNKNOWN), and the newly received version is unexpected, and we are not in
-    // an upgrade - then we really should not be changing the reported version
-    if (!previousVersionIsUnknown && !desiredVersionIsUnknown && !newVersionMatchesDesired
-        && !isUpgradeInProgressForThisComponent) {
-      LOG.warn(
-          "Received a reported version of {} for {} on {}. This was not expected since the desired version is {} and the cluster is not upgrading this component. The version will not be changed.",
-          newVersion, sc.getName(), sch.getHostName(), desiredVersion);
-
-      return;
-    }
+    boolean versionIsCorrect = StringUtils.equals(desiredVersion, newVersion);
 
-    // update the SCH to the new version reported
+    // update the SCH to the new version reported only if it changed
     if (!StringUtils.equals(previousVersion, newVersion)) {
       sch.setVersion(newVersion);
     }
 
-    if (previousVersion == null || previousVersionIsUnknown) {
+    if (previousVersion == null || StringUtils.equalsIgnoreCase(UNKNOWN_VERSION, previousVersion)) {
       // value may be "UNKNOWN" when upgrading from older Ambari versions
       // or if host component reports it's version for the first time
       sch.setUpgradeState(UpgradeState.NONE);
-      recalculateHostVersionAndClusterVersion(cluster, sch);
+      sch.recalculateHostVersionState();
     } else {
-      if (newVersionMatchesDesired) {
+      if (versionIsCorrect) {
+        boolean isUpgradeInProgressForThisComponent = null != cluster.getUpgradeInProgress()
+            && upgradeState != UpgradeState.NONE;
+
         if (isUpgradeInProgressForThisComponent) {
-          sch.setStackVersion(cluster.getDesiredStackVersion());
-          setUpgradeStateIfChanged(cluster, sch, UpgradeState.COMPLETE);
+          setUpgradeStateAndRecalculateHostVersions(sch, UpgradeState.COMPLETE);
         } else {
           // no upgrade in progress for this component, then this should always
           // be NONE
-          setUpgradeStateIfChanged(cluster, sch, UpgradeState.NONE);
+          setUpgradeStateAndRecalculateHostVersions(sch, UpgradeState.NONE);
         }
       } else {
         // if the versions don't match for any reason, regardless of upgrade
         // state, then VERSION_MISMATCH it
-        setUpgradeStateIfChanged(cluster, sch, UpgradeState.VERSION_MISMATCH);
+        setUpgradeStateAndRecalculateHostVersions(sch, UpgradeState.VERSION_MISMATCH);
       }
     }
+
+    sc.updateRepositoryState(newVersion);
   }
 
   /**
@@ -232,50 +194,25 @@ public class StackVersionListener {
   private void processUnknownDesiredVersion(Cluster cluster, ServiceComponent sc,
                                             ServiceComponentHost sch,
                                             String newVersion) throws AmbariException {
-    sc.setDesiredVersion(newVersion);
     sch.setUpgradeState(UpgradeState.NONE);
     sch.setVersion(newVersion);
-
-    recalculateHostVersionAndClusterVersion(cluster, sch);
+    sch.recalculateHostVersionState();
   }
 
   /**
-   * Sets the upgrade state on the component if it has changed. This method will
-   * not trigger an sort of {@link ClusterVersionEntity} or
-   * {@link HostVersionEntity} recalculation.
-   *
    * @param sch
    * @param upgradeState
    * @throws AmbariException
    */
-  private void setUpgradeStateIfChanged(Cluster cluster, ServiceComponentHost sch,
+  private void setUpgradeStateAndRecalculateHostVersions(ServiceComponentHost sch,
       UpgradeState upgradeState) throws AmbariException {
 
-    // don't need to recalculate anything here if the upgrade state is not changing
     if (sch.getUpgradeState() == upgradeState) {
       return;
     }
 
     // if the upgrade state changes, then also recalculate host versions
     sch.setUpgradeState(upgradeState);
+    sch.recalculateHostVersionState();
   }
-
-  /**
-   * Recalculates the {@link HostVersionEntity} for the host specified by the
-   * host component, taking into account all component states on that host. This
-   * will also trigger a {@link ClusterVersionEntity} recalculatation for the
-   * cluster version as well.
-   *
-   * @param cluster
-   * @param sch
-   * @throws AmbariException
-   */
-  private void recalculateHostVersionAndClusterVersion(Cluster cluster, ServiceComponentHost sch)
-      throws AmbariException {
-    // trigger a re-calculation of the cluster state based on the SCH state
-    RepositoryVersionEntity repoVersion = sch.recalculateHostVersionState();
-    if (null != repoVersion) {
-      cluster.recalculateClusterVersionState(repoVersion);
-    }
-  }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
index c553717..a23b914 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -30,7 +30,6 @@ import javax.persistence.criteria.Root;
 
 import org.apache.ambari.server.orm.RequiresSession;
 import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
-import org.apache.ambari.server.orm.entities.ClusterConfigMappingEntity;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.state.StackId;
@@ -116,9 +115,9 @@ public class ClusterDAO {
   }
 
   @RequiresSession
-  public List<ClusterConfigEntity> getLatestClusterConfigsByTypes(Long clusterId, List<String> types) {
+  public List<ClusterConfigEntity> getEnabledConfigsByTypes(Long clusterId, Collection<String> types) {
     TypedQuery<ClusterConfigEntity> query = entityManagerProvider.get().createNamedQuery(
-      "ClusterConfigEntity.findLatestClusterConfigsByTypes",
+        "ClusterConfigEntity.findEnabledConfigsByTypes",
       ClusterConfigEntity.class);
 
     query.setParameter("clusterId", clusterId);
@@ -191,7 +190,9 @@ public class ClusterDAO {
 
   /**
    * Gets the latest configurations for a given stack for all of the
-   * configurations of the specified cluster.
+   * configurations of the specified cluster. This method does not take into
+   * account the configuration being enabled, as the latest for a given stack
+   * may not be "selected".
    *
    * @param clusterId
    *          the cluster that the service is a part of.
@@ -226,14 +227,11 @@ public class ClusterDAO {
    * @return the latest configurations for the specified cluster and stack.
    */
   @RequiresSession
-  public List<ClusterConfigMappingEntity> getClusterConfigMappingsByStack(long clusterId,
-      StackId stackId) {
-    StackEntity stackEntity = stackDAO.find(stackId.getStackName(),
-        stackId.getStackVersion());
+  public List<ClusterConfigEntity> getEnabledConfigsByStack(long clusterId, StackId stackId) {
+    StackEntity stackEntity = stackDAO.find(stackId.getStackName(), stackId.getStackVersion());
 
-    TypedQuery<ClusterConfigMappingEntity> query = entityManagerProvider.get().createNamedQuery(
-        "ClusterConfigEntity.findClusterConfigMappingsByStack",
-        ClusterConfigMappingEntity.class);
+    TypedQuery<ClusterConfigEntity> query = entityManagerProvider.get().createNamedQuery(
+        "ClusterConfigEntity.findEnabledConfigsByStack", ClusterConfigEntity.class);
 
     query.setParameter("clusterId", clusterId);
     query.setParameter("stack", stackEntity);
@@ -241,46 +239,26 @@ public class ClusterDAO {
     return daoUtils.selectList(query);
   }
 
-  @RequiresSession
-  public List<ClusterConfigMappingEntity> getClusterConfigMappingEntitiesByCluster(long clusterId) {
-    TypedQuery<ClusterConfigMappingEntity> query = entityManagerProvider.get().createQuery(
-      "SELECT mapping FROM ClusterConfigMappingEntity mapping " +
-        "WHERE mapping.clusterId = :clusterId", ClusterConfigMappingEntity.class);
-
-    query.setParameter("clusterId", clusterId);
-
-    return daoUtils.selectList(query);
-  }
-
-  @RequiresSession
-  public List<ClusterConfigMappingEntity> getLatestClusterConfigMappingsEntityByType(long clusterId, String configType) {
-    TypedQuery<ClusterConfigMappingEntity> query = entityManagerProvider.get().createNamedQuery(
-      "ClusterConfigMappingEntity.findLatestClusterConfigMappingsByType",
-      ClusterConfigMappingEntity.class);
-
-    query.setParameter("clusterId", clusterId);
-    query.setParameter("typeName", configType);
-
-    return daoUtils.selectList(query);
-  }
-
   /**
-   * Gets selected mappings for provided config types
-   * @param clusterId cluster id
-   * @param types config types for mappings
-   * @return
+   * Gets the latest config in the given cluster by type name. Only a config
+   * which is enabled can be returned.
+   *
+   * @param clusterId
+   *          the ID of the cluster.
+   * @param type
+   *          the config type (not {@code null}).
+   * @return a config, or {@code null} if there is none enabled.
    */
   @RequiresSession
-  public List<ClusterConfigMappingEntity> getSelectedConfigMappingByTypes(long clusterId, List<String> types) {
-    TypedQuery<ClusterConfigMappingEntity> query = entityManagerProvider.get().createQuery(
-        "SELECT mapping FROM ClusterConfigMappingEntity mapping " +
-            "WHERE mapping.clusterId = :clusterId AND mapping.typeName IN :type " +
-            "AND mapping.selectedInd > 0", ClusterConfigMappingEntity.class);
+  public ClusterConfigEntity findEnabledConfigByType(long clusterId, String type) {
+
+    TypedQuery<ClusterConfigEntity> query = entityManagerProvider.get().createNamedQuery(
+        "ClusterConfigEntity.findEnabledConfigByType", ClusterConfigEntity.class);
 
     query.setParameter("clusterId", clusterId);
-    query.setParameter("type", types);
+    query.setParameter("type", type);
 
-    return daoUtils.selectList(query);
+    return daoUtils.selectOne(query);
   }
 
   /**
@@ -301,24 +279,6 @@ public class ClusterDAO {
   }
 
   /**
-   * Merge a cluster configuration in the DB.
-   */
-  @Transactional
-  public void mergeConfig(ClusterConfigEntity configEntity) {
-    entityManagerProvider.get().merge(configEntity);
-  }
-
-  /**
-   * Bulk update configs in DB
-   */
-  @Transactional
-  public void mergeConfigs(Collection<ClusterConfigEntity> clusterConfigEntities) {
-    for (ClusterConfigEntity clusterConfigEntity : clusterConfigEntities) {
-      entityManagerProvider.get().merge(clusterConfigEntity);
-    }
-  }
-
-  /**
    * Remove a cluster configuration in the DB.
    */
   @Transactional
@@ -327,78 +287,6 @@ public class ClusterDAO {
   }
 
   /**
-   * Bulk update config mappings in DB
-   */
-  @Transactional
-  public void mergeConfigMappings(Collection<ClusterConfigMappingEntity> mappingEntities) {
-    for (ClusterConfigMappingEntity mappingEntity : mappingEntities) {
-      entityManagerProvider.get().merge(mappingEntity);
-    }
-  }
-
-  /**
-   * Update config mapping in DB
-   */
-  @Transactional
-  public ClusterConfigMappingEntity mergeConfigMapping(ClusterConfigMappingEntity mappingEntity) {
-    return entityManagerProvider.get().merge(mappingEntity);
-  }
-
-  /**
-   * Create cluster config mapping in DB
-   */
-  @Transactional
-  public void persistConfigMapping(ClusterConfigMappingEntity entity) {
-    entityManagerProvider.get().persist(entity);
-  }
-
-  /**
-   * Remove a cluster configuration mapping from the DB.
-   */
-  @Transactional
-  public void removeConfigMapping(ClusterConfigMappingEntity entity) {
-    entityManagerProvider.get().remove(entity);
-  }
-
-
-  /**
-   * Sets selected = 0, for clusterConfigEntities which has type_name which is in the given types list
-   *
-   * @param clusterId
-   *          the cluster that the service is a part of.
-   * @param types
-   *          the names of the configuration types.
-   */
-    @Transactional
-    public void removeClusterConfigMappingEntityByTypes(Long clusterId, List<String> types) {
-      if(types.isEmpty()) {
-        return;
-      }
-
-      TypedQuery<Long> query = entityManagerProvider.get().createQuery
-          ("DELETE FROM ClusterConfigMappingEntity configs WHERE configs" +
-            ".clusterId=?1 AND configs.typeName IN ?2", Long.class);
-
-      daoUtils.executeUpdate(query, clusterId, types);
-    }
-
-  /**
-   * Delete ClusterConfigMappingEntity corresponds to clusterConfigEntity
-   *
-   * @param clusterConfigEntity
-   *          clusterConfigEntity to match.
-   */
-  @Transactional
-  public void removeClusterConfigMappingEntityByConfig(ClusterConfigEntity clusterConfigEntity) {
-
-    TypedQuery<Long> query = entityManagerProvider.get().createQuery
-        ("DELETE FROM ClusterConfigMappingEntity configs WHERE configs" +
-            ".clusterId=?1 AND configs.typeName= ?2 AND configs.tag=?3 ", Long.class);
-
-    daoUtils.executeUpdate(query, clusterConfigEntity.getClusterId(), clusterConfigEntity.getType(), clusterConfigEntity.getTag());
-  }
-
-  /**
    * Retrieve entity data from DB
    *
    * @param clusterEntity
@@ -446,10 +334,21 @@ public class ClusterDAO {
     return clusterEntity;
   }
 
+  /**
+   * Merge the specified entity into the current persistence context.
+   *
+   * @param clusterConfigEntity
+   *          the entity to merge (not {@code null}).
+   * @return the managed entity which was merged (never {@code null}).
+   */
+  public ClusterConfigEntity merge(ClusterConfigEntity clusterConfigEntity) {
+    EntityManager entityManager = entityManagerProvider.get();
+    return entityManager.merge(clusterConfigEntity);
+  }
 
   @Transactional
   public void remove(ClusterEntity clusterEntity) {
-    entityManagerProvider.get().remove(merge(clusterEntity));
+    entityManagerProvider.get().remove(clusterEntity);
   }
 
   @Transactional

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterVersionDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterVersionDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterVersionDAO.java
deleted file mode 100644
index 1bcca60..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterVersionDAO.java
+++ /dev/null
@@ -1,233 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.ambari.server.orm.dao;
-
-import java.util.List;
-
-import javax.persistence.EntityManager;
-import javax.persistence.NoResultException;
-import javax.persistence.NonUniqueResultException;
-import javax.persistence.TypedQuery;
-
-import org.apache.ambari.server.orm.RequiresSession;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
-import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
-import org.apache.ambari.server.state.RepositoryVersionState;
-import org.apache.ambari.server.state.StackId;
-
-import com.google.inject.Singleton;
-import com.google.inject.persist.Transactional;
-
-/**
- * The {@link ClusterVersionDAO} class manages the {@link ClusterVersionEntity} instances associated with a cluster.
- * Each cluster can have multiple stack versions {@link org.apache.ambari.server.state.RepositoryVersionState#INSTALLED},
- * exactly one stack version that is {@link org.apache.ambari.server.state.RepositoryVersionState#CURRENT}, and at most one
- * stack version that is {@link org.apache.ambari.server.state.RepositoryVersionState#UPGRADING}.
- */
-@Singleton
-public class ClusterVersionDAO extends CrudDAO<ClusterVersionEntity, Long>{
-  /**
-   * Constructor.
-   */
-  public ClusterVersionDAO() {
-    super(ClusterVersionEntity.class);
-  }
-
-  /**
-   * Retrieve all of the cluster versions for the given stack and version.
-   *
-   * @param stackName
-   *          the stack name (for example "HDP")
-   * @param stackVersion
-   *          the stack version (for example "2.2")
-   * @param version
-   *          Repository version (e.g., 2.2.0.1-995)
-   * @return Return a list of cluster versions that match the stack and version.
-   */
-  @RequiresSession
-  public List<ClusterVersionEntity> findByStackAndVersion(String stackName,
-      String stackVersion, String version) {
-    final TypedQuery<ClusterVersionEntity> query = entityManagerProvider.get().createNamedQuery("clusterVersionByStackVersion", ClusterVersionEntity.class);
-    query.setParameter("stackName", stackName);
-    query.setParameter("stackVersion", stackVersion);
-    query.setParameter("version", version);
-
-    return daoUtils.selectList(query);
-  }
-
-  /**
-   * Get the cluster version for the given cluster name, stack name, and stack
-   * version.
-   *
-   * @param clusterName
-   *          Cluster name
-   * @param stackId
-   *          Stack id (e.g., HDP-2.2)
-   * @param version
-   *          Repository version (e.g., 2.2.0.1-995)
-   * @return Return all of the cluster versions associated with the given
-   *         cluster.
-   */
-  @RequiresSession
-  public ClusterVersionEntity findByClusterAndStackAndVersion(
-      String clusterName, StackId stackId, String version) {
-    final TypedQuery<ClusterVersionEntity> query = entityManagerProvider.get()
-        .createNamedQuery("clusterVersionByClusterAndStackAndVersion", ClusterVersionEntity.class);
-    query.setParameter("clusterName", clusterName);
-    query.setParameter("stackName", stackId.getStackName());
-    query.setParameter("stackVersion", stackId.getStackVersion());
-    query.setParameter("version", version);
-
-    return daoUtils.selectSingle(query);
-  }
-
-  /**
-   * Retrieve all of the cluster versions for the given cluster.
-   *
-   * @param clusterName Cluster name
-   * @return Return all of the cluster versions associated with the given cluster.
-   */
-  @RequiresSession
-  public List<ClusterVersionEntity> findByCluster(String clusterName) {
-    final TypedQuery<ClusterVersionEntity> query = entityManagerProvider.get()
-        .createNamedQuery("clusterVersionByCluster", ClusterVersionEntity.class);
-    query.setParameter("clusterName", clusterName);
-
-    return daoUtils.selectList(query);
-  }
-
-  /**
-   * Retrieve the single cluster version whose state is {@link org.apache.ambari.server.state.RepositoryVersionState#CURRENT}, of which there should be exactly one at all times
-   * for the given cluster.
-   *
-   * @param clusterName Cluster name
-   * @return Returns the single cluster version for this cluster whose state is {@link org.apache.ambari.server.state.RepositoryVersionState#CURRENT}, or {@code null} otherwise.
-   */
-  @RequiresSession
-  public ClusterVersionEntity findByClusterAndStateCurrent(String clusterName) {
-    final TypedQuery<ClusterVersionEntity> query = entityManagerProvider.get()
-        .createNamedQuery("clusterVersionByClusterAndState", ClusterVersionEntity.class);
-    query.setParameter("clusterName", clusterName);
-    query.setParameter("state", RepositoryVersionState.CURRENT);
-
-    try {
-      List results = query.getResultList();
-      if (results.isEmpty()) {
-        return null;
-      } else {
-        if (results.size() == 1) {
-          return (ClusterVersionEntity) results.get(0);
-        }
-      }
-      throw new NonUniqueResultException();
-    } catch (NoResultException ignored) {
-      return null;
-    }
-  }
-
-  /**
-   * Retrieve all of the cluster versions for the cluster with the given name and a state.
-   *
-   * @param clusterName Cluster name
-   * @param state Cluster version state
-   * @return Returns a list of cluster versions for the given cluster and a state.
-   */
-  @RequiresSession
-  public List<ClusterVersionEntity> findByClusterAndState(String clusterName, RepositoryVersionState state) {
-    final TypedQuery<ClusterVersionEntity> query = entityManagerProvider.get()
-        .createNamedQuery("clusterVersionByClusterAndState", ClusterVersionEntity.class);
-    query.setParameter("clusterName", clusterName);
-    query.setParameter("state", state);
-
-    return daoUtils.selectList(query);
-  }
-
-  /**
-   * Construct a Cluster Version. Additionally this will update parent connection relations without
-   * forcing refresh of parent entity
-   * @param entity entity to create
-   */
-  @Override
-  @Transactional
-  public void create(ClusterVersionEntity entity) throws IllegalArgumentException {
-    // check if repository version is not missing, to avoid NPE
-    if (entity.getRepositoryVersion() == null) {
-      throw new IllegalArgumentException("RepositoryVersion argument is not set for the entity");
-    }
-
-    super.create(entity);
-    entity.getRepositoryVersion().updateClusterVersionEntityRelation(entity);
-  }
-
-  /**
-   * Construct a Cluster Version and return it. This is primarily used to be able to construct the object and mock
-   * the function call.
-   * @param cluster Cluster
-   * @param repositoryVersion Repository Version
-   * @param state Initial State
-   * @param startTime Start Time
-   * @param endTime End Time
-   * @param userName Username, such as "admin"
-   * @return Return new ClusterVersion object.
-   */
-  @Transactional
-  public ClusterVersionEntity create(ClusterEntity cluster, RepositoryVersionEntity repositoryVersion,
-                                     RepositoryVersionState state, long startTime, long endTime, String userName) {
-    ClusterVersionEntity clusterVersionEntity = new ClusterVersionEntity(cluster,
-        repositoryVersion, state, startTime, endTime, userName);
-    this.create(clusterVersionEntity);
-    return clusterVersionEntity;
-  }
-
-  /**
-   * Updates the cluster version's existing CURRENT record to the INSTALLED, and the target
-   * becomes CURRENT.  This method invokes {@code clear()} on the entity manager to force entities to be refreshed.
-   * @param clusterId the cluster
-   * @param target    the repo version that will be marked as CURRENT
-   * @param current   the cluster's current record to be marked INSTALLED
-   */
-  @Transactional
-  public void updateVersions(Long clusterId, RepositoryVersionEntity target, RepositoryVersionEntity current) {
-    // !!! first update target to be current
-    StringBuilder sb = new StringBuilder("UPDATE ClusterVersionEntity cve");
-    sb.append(" SET cve.state = ?1 ");
-    sb.append(" WHERE cve.clusterId = ?2");
-    sb.append(" AND cve.repositoryVersion = ?3");
-
-    EntityManager em = entityManagerProvider.get();
-
-    TypedQuery<Long> query = em.createQuery(sb.toString(), Long.class);
-    daoUtils.executeUpdate(query, RepositoryVersionState.CURRENT, clusterId, target);
-
-    // !!! then move existing current to installed
-    sb = new StringBuilder("UPDATE ClusterVersionEntity cve");
-    sb.append(" SET cve.state = ?1 ");
-    sb.append(" WHERE cve.clusterId = ?2");
-    sb.append(" AND cve.repositoryVersion = ?3");
-    sb.append(" AND cve.state = ?4");
-
-    query = em.createQuery(sb.toString(), Long.class);
-    daoUtils.executeUpdate(query, RepositoryVersionState.INSTALLED, clusterId, current,
-        RepositoryVersionState.CURRENT);
-
-    em.clear();
-  }
-
-
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentStateDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentStateDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentStateDAO.java
index cc7b503..ad6867e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentStateDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostComponentStateDAO.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -18,7 +18,6 @@
 
 package org.apache.ambari.server.orm.dao;
 
-import java.util.Arrays;
 import java.util.List;
 
 import javax.persistence.EntityManager;
@@ -27,7 +26,6 @@ import javax.persistence.TypedQuery;
 
 import org.apache.ambari.server.orm.RequiresSession;
 import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
-import org.apache.ambari.server.state.UpgradeState;
 
 import com.google.inject.Inject;
 import com.google.inject.Provider;
@@ -180,31 +178,22 @@ public class HostComponentStateDAO {
   }
 
   /**
-   * Marks hosts components to the specified version that are NOT already set or "UNKNOWN".
-   * Also marks all host components as not being in an upgrade state.  This method
-   * invokes {@code clear()} on the entity manager to force entities to be refreshed.
-   *
-   * @param version the version
+   * @param serviceName
+   * @param componentName
+   * @param version
+   * @return a list of host components whose version that does NOT match the give version
    */
-  @Transactional
-  public void updateVersions(String version) {
-    EntityManager em = entityManagerProvider.get();
-
-    // !!! first the version
-    StringBuilder sb = new StringBuilder("UPDATE HostComponentStateEntity hostComponent");
-    sb.append(" SET hostComponent.version = ?1 ");
-    sb.append(" WHERE hostComponent.version NOT IN ?2");
-
-    TypedQuery<Long> query = em.createQuery(sb.toString(), Long.class);
-    daoUtils.executeUpdate(query, version, Arrays.asList(version, "UNKNOWN"));
+  @RequiresSession
+  public List<HostComponentStateEntity> findByServiceAndComponentAndNotVersion(String serviceName,
+      String componentName, String version) {
 
-    // !!! now the upgrade state
-    sb = new StringBuilder("UPDATE HostComponentStateEntity hostComponent");
-    sb.append(" SET hostComponent.upgradeState = ?1");
+    final TypedQuery<HostComponentStateEntity> query = entityManagerProvider.get().createNamedQuery(
+        "HostComponentStateEntity.findByServiceAndComponentAndNotVersion", HostComponentStateEntity.class);
 
-    query = em.createQuery(sb.toString(), Long.class);
-    daoUtils.executeUpdate(query, UpgradeState.NONE);
+    query.setParameter("serviceName", serviceName);
+    query.setParameter("componentName", componentName);
+    query.setParameter("version", version);
 
-    em.clear();
+    return daoUtils.selectList(query);
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostVersionDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostVersionDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostVersionDAO.java
index 930a535..158370f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostVersionDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostVersionDAO.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -22,11 +22,10 @@ import java.util.Collection;
 import java.util.List;
 
 import javax.persistence.EntityManager;
-import javax.persistence.NoResultException;
-import javax.persistence.NonUniqueResultException;
 import javax.persistence.TypedQuery;
 
 import org.apache.ambari.server.orm.RequiresSession;
+import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.RepositoryVersionState;
@@ -41,7 +40,7 @@ import com.google.inject.persist.Transactional;
  * The {@link org.apache.ambari.server.orm.dao.HostVersionDAO} class manages the {@link org.apache.ambari.server.orm.entities.HostVersionEntity}
  * instances associated with a host. Each host can have multiple stack versions in {@link org.apache.ambari.server.state.RepositoryVersionState#INSTALLED}
  * which are installed, exactly one stack version that is either {@link org.apache.ambari.server.state.RepositoryVersionState#CURRENT} or
- * {@link org.apache.ambari.server.state.RepositoryVersionState#UPGRADING}.
+ * {@link org.apache.ambari.server.state.RepositoryVersionState#INSTALLING}.
  */
 @Singleton
 public class HostVersionDAO extends CrudDAO<HostVersionEntity, Long> {
@@ -131,101 +130,43 @@ public class HostVersionDAO extends CrudDAO<HostVersionEntity, Long> {
   }
 
   /**
-   * Retrieve all of the host versions for the given cluster name, host name, and state. <br/>
-   * Consider using faster method: {@link HostVersionDAO#findByClusterHostAndState(long, long, org.apache.ambari.server.state.RepositoryVersionState)}
+   * Retrieve all of the host versions for the given cluster name, and state. <br/>
    * @param clusterName Cluster name
-   * @param hostName FQDN of host
    * @param state repository version state
    * @return Return all of the host versions that match the criteria.
    */
   @RequiresSession
-  public List<HostVersionEntity> findByClusterHostAndState(String  clusterName, String hostName, RepositoryVersionState state) {
+  public List<HostVersionEntity> findByClusterAndState(String clusterName, RepositoryVersionState state) {
     final TypedQuery<HostVersionEntity> query = entityManagerProvider.get()
-        .createNamedQuery("hostVersionByClusterHostnameAndState", HostVersionEntity.class);
+        .createNamedQuery("findByClusterAndState", HostVersionEntity.class);
     query.setParameter("clusterName", clusterName);
-    query.setParameter("hostName", hostName);
     query.setParameter("state", state);
 
-    return daoUtils.selectList(query);
+    return daoUtils.selectList(query);  
   }
-
+  
   /**
-   * Faster version of {@link HostVersionDAO#findByClusterHostAndState(java.lang.String, java.lang.String, org.apache.ambari.server.state.RepositoryVersionState)}
-   *
-   * @param clusterId Cluster ID
-   * @param hostId Host ID
+   * Retrieve all of the host versions for the given cluster name, host name, and state. <br/>
+   * @param clusterName Cluster name
+   * @param hostName FQDN of host
    * @param state repository version state
    * @return Return all of the host versions that match the criteria.
    */
   @RequiresSession
-  public List<HostVersionEntity> findByClusterHostAndState(long clusterId, long hostId, RepositoryVersionState state) {
-    TypedQuery<HostVersionEntity> query =
-        entityManagerProvider.get().createNamedQuery("hostVersionByClusterHostIdAndState", HostVersionEntity.class);
-
-    query.setParameter("clusterId", clusterId);
-    query.setParameter("hostId", hostId);
+  public List<HostVersionEntity> findByClusterHostAndState(String  clusterName, String hostName, RepositoryVersionState state) {
+    final TypedQuery<HostVersionEntity> query = entityManagerProvider.get()
+        .createNamedQuery("hostVersionByClusterHostnameAndState", HostVersionEntity.class);
+    query.setParameter("clusterName", clusterName);
+    query.setParameter("hostName", hostName);
     query.setParameter("state", state);
 
     return daoUtils.selectList(query);
   }
 
   /**
-   * Retrieve the single host version whose state is {@link org.apache.ambari.server.state.RepositoryVersionState#CURRENT}, of which there should be exactly one at all times
-   * for the given host.
-   * Consider using faster method {@link HostVersionDAO#findByHostAndStateCurrent(long, long)}
-   *
-   * @param clusterName Cluster name
-   * @param hostName Host name
-   * @return Returns the single host version for this host whose state is {@link org.apache.ambari.server.state.RepositoryVersionState#CURRENT}, or {@code null} otherwise.
-   */
-  @RequiresSession
-  public HostVersionEntity findByHostAndStateCurrent(String clusterName, String hostName) {
-    try {
-      List<?> results = findByClusterHostAndState(clusterName, hostName, RepositoryVersionState.CURRENT);
-      if (results.isEmpty()) {
-        return null;
-      } else {
-        if (results.size() == 1) {
-          return (HostVersionEntity) results.get(0);
-        }
-      }
-      throw new NonUniqueResultException();
-    } catch (NoResultException ignored) {
-      return null;
-    }
-  }
-
-  /**
-   * Retrieve the single host version whose state is {@link org.apache.ambari.server.state.RepositoryVersionState#CURRENT}, of which there should be exactly one at all times
-   * for the given host.
-   * Faster version of {@link HostVersionDAO#findByHostAndStateCurrent(java.lang.String, java.lang.String)}
-   * @param clusterId Cluster ID
-   * @param hostId host ID
-   * @return Returns the single host version for this host whose state is {@link org.apache.ambari.server.state.RepositoryVersionState#CURRENT}, or {@code null} otherwise.
-   */
-  @RequiresSession
-  public HostVersionEntity findByHostAndStateCurrent(long clusterId, long hostId) {
-    try {
-      List<?> results = findByClusterHostAndState(clusterId, hostId, RepositoryVersionState.CURRENT);
-      if (results.isEmpty()) {
-        return null;
-      } else {
-        if (results.size() == 1) {
-          return (HostVersionEntity) results.get(0);
-        }
-      }
-      throw new NonUniqueResultException();
-    } catch (NoResultException ignored) {
-      return null;
-    }
-  }
-
-  /**
    * Retrieve the single host version for the given cluster, stack name, stack
    * version, and host name. <br/>
-   * This query is slow and not suitable for frequent use. <br/>
-   * Please, use {@link HostVersionDAO#findByClusterStackVersionAndHost(long, org.apache.ambari.server.state.StackId, java.lang.String, long)} <br/>
-   * It is ~50 times faster
+   * This query is slow and not suitable for frequent use.
    *
    * @param clusterName
    *          Cluster name
@@ -253,63 +194,73 @@ public class HostVersionDAO extends CrudDAO<HostVersionEntity, Long> {
   }
 
   /**
-   * Optimized version of {@link HostVersionDAO#findByClusterStackVersionAndHost(java.lang.String, org.apache.ambari.server.state.StackId, java.lang.String, java.lang.String)}
-   * @param clusterId Id of cluster
-   * @param stackId Stack ID (e.g., HDP-2.2)
-   * @param version Stack version (e.g., 2.2.0.1-995)
-   * @param hostId Host Id
-   * @return Returns the single host version that matches the criteria.
+   * Gets all host version entities assocaited with the specified cluster and
+   * repository.
+   *
+   * @param clusterId
+   *          the cluster ID.
+   * @param repositoryVersion
+   *          the repository (not {@code null}).
+   * @return the host versions.
    */
   @RequiresSession
-  public HostVersionEntity findByClusterStackVersionAndHost(long clusterId, StackId stackId, String version,
-                                                            long hostId) {
-    TypedQuery<HostVersionEntity> query = entityManagerProvider.get()
-        .createNamedQuery("hostVersionByClusterStackVersionAndHostId", HostVersionEntity.class);
+  public List<HostVersionEntity> findHostVersionByClusterAndRepository(long clusterId,
+      RepositoryVersionEntity repositoryVersion) {
+    TypedQuery<HostVersionEntity> query = entityManagerProvider.get().createNamedQuery(
+        "findHostVersionByClusterAndRepository", HostVersionEntity.class);
 
     query.setParameter("clusterId", clusterId);
-    query.setParameter("stackName", stackId.getStackName());
-    query.setParameter("stackVersion", stackId.getStackVersion());
-    query.setParameter("version", version);
-    query.setParameter("hostId", hostId);
+    query.setParameter("repositoryVersion", repositoryVersion);
 
-    return daoUtils.selectSingle(query);
-  }
-
-  @Transactional
-  public void removeByHostName(String hostName) {
-    Collection<HostVersionEntity> hostVersions = this.findByHost(hostName);
-    this.remove(hostVersions);
+    return daoUtils.selectList(query);
   }
 
   /**
-   * Updates the host versions existing CURRENT record to the INSTALLED, and the target
-   * becomes CURRENT.  This method invokes {@code clear()} on the entity manager to force entities to be refreshed.
-   * @param target    the repo version that all hosts to mark as CURRENT
-   * @param current   the repo version that all hosts marked as INSTALLED
+   * Gets all host version entities that are of the given states
+   *
+   * @param repositoryVersion
+   *          the repository (not {@code null})
+   * @param states
+   *          the states
+   * @return the host versions
    */
-  @Transactional
-  public void updateVersions(RepositoryVersionEntity target, RepositoryVersionEntity current) {
-    // !!! first update target to be current
-    StringBuilder sb = new StringBuilder("UPDATE HostVersionEntity hve");
-    sb.append(" SET hve.state = ?1 ");
-    sb.append(" WHERE hve.repositoryVersion = ?2");
+  @RequiresSession
+  public List<HostVersionEntity> findByRepositoryAndStates(RepositoryVersionEntity repositoryVersion,
+      Collection<RepositoryVersionState> states) {
 
-    EntityManager em = entityManagerProvider.get();
+    TypedQuery<HostVersionEntity> query = entityManagerProvider.get().createNamedQuery(
+        "hostVersionByRepositoryAndStates", HostVersionEntity.class);
 
-    TypedQuery<Long> query = em.createQuery(sb.toString(), Long.class);
-    daoUtils.executeUpdate(query, RepositoryVersionState.CURRENT, target);
+    query.setParameter("repositoryVersion", repositoryVersion);
+    query.setParameter("states", states);
 
-    // !!! then move existing current to installed
-    sb = new StringBuilder("UPDATE HostVersionEntity hve");
-    sb.append(" SET hve.state = ?1 ");
-    sb.append(" WHERE hve.repositoryVersion = ?2");
-    sb.append(" AND hve.state = ?3");
+    return daoUtils.selectList(query);
+  }
 
-    query = em.createQuery(sb.toString(), Long.class);
-    daoUtils.executeUpdate(query, RepositoryVersionState.INSTALLED, current,
-        RepositoryVersionState.CURRENT);
+  /**
+   * Gets the {@link HostVersionEntity} associted with the specified host and
+   * repository.
+   *
+   * @param host
+   * @param repositoryVersion
+   * @return
+   */
+  @RequiresSession
+  public HostVersionEntity findHostVersionByHostAndRepository(HostEntity host,
+      RepositoryVersionEntity repositoryVersion) {
+    TypedQuery<HostVersionEntity> query = entityManagerProvider.get().createNamedQuery(
+        "findByHostAndRepository", HostVersionEntity.class);
+
+    query.setParameter("host", host);
+    query.setParameter("repositoryVersion", repositoryVersion);
+
+    return daoUtils.selectOne(query);
+  }
 
-    em.clear();
+  @Transactional
+  public void removeByHostName(String hostName) {
+    Collection<HostVersionEntity> hostVersions = findByHost(hostName);
+    this.remove(hostVersions);
   }
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceConfigDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceConfigDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceConfigDAO.java
index 212a9f0..6bfad54 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceConfigDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceConfigDAO.java
@@ -65,7 +65,7 @@ public class ServiceConfigDAO {
             "WHERE scv.serviceName=?1 AND scv.version=?2", ServiceConfigEntity.class);
     return daoUtils.selectOne(query, serviceName, version);
   }
-  
+
   @RequiresSession
   public List<ServiceConfigEntity> findByService(Long clusterId, String serviceName) {
     TypedQuery<ServiceConfigEntity> query = entityManagerProvider.get().
@@ -88,7 +88,7 @@ public class ServiceConfigDAO {
     cq.where(groupVersion.get("groupId").in(configGroupIds));
     cq.groupBy(groupVersion.get("groupId"));
     List<Tuple> tuples = daoUtils.selectList(entityManagerProvider.get().createQuery(cq));
-    List<ServiceConfigEntity> result = new ArrayList<ServiceConfigEntity>();
+    List<ServiceConfigEntity> result = new ArrayList<>();
     //subquery look to be very poor, no bulk select then, cache should help here as result size is naturally limited
     for (Tuple tuple : tuples) {
       CriteriaQuery<ServiceConfigEntity> sce = cb.createQuery(ServiceConfigEntity.class);
@@ -145,9 +145,9 @@ public class ServiceConfigDAO {
   }
 
   /**
-   * Get all service configurations for the specified cluster and stack. This
-   * will return different versions of the same configuration (HDFS v1 and v2)
-   * if they exist.
+   * Get service configurations for the specified cluster and stack. This will
+   * return different versions of the same configuration (HDFS v1 and v2) if
+   * they exist.
    *
    * @param clusterId
    *          the cluster (not {@code null}).
@@ -156,18 +156,19 @@ public class ServiceConfigDAO {
    * @return all service configurations for the cluster and stack.
    */
   @RequiresSession
-  public List<ServiceConfigEntity> getAllServiceConfigsForClusterAndStack(Long clusterId,
-      StackId stackId) {
+  public List<ServiceConfigEntity> getServiceConfigsForServiceAndStack(Long clusterId,
+      StackId stackId, String serviceName) {
 
     StackEntity stackEntity = stackDAO.find(stackId.getStackName(),
         stackId.getStackVersion());
 
     TypedQuery<ServiceConfigEntity> query = entityManagerProvider.get().createNamedQuery(
-        "ServiceConfigEntity.findAllServiceConfigsByStack",
+        "ServiceConfigEntity.findServiceConfigsByStack",
         ServiceConfigEntity.class);
 
     query.setParameter("clusterId", clusterId);
     query.setParameter("stack", stackEntity);
+    query.setParameter("serviceName", serviceName);
 
     return daoUtils.selectList(query);
   }
@@ -266,7 +267,7 @@ public class ServiceConfigDAO {
 
   @Transactional
   public void removeHostFromServiceConfigs(final Long hostId) {
-    List<ServiceConfigEntity> allServiceConfigs = this.findAll();
+    List<ServiceConfigEntity> allServiceConfigs = findAll();
     for (ServiceConfigEntity serviceConfigEntity : allServiceConfigs) {
       List<Long> hostIds = serviceConfigEntity.getHostIds();
       if (hostIds != null && hostIds.contains(hostId)) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceDesiredStateDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceDesiredStateDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceDesiredStateDAO.java
index 81e6d95..cdb65d5 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceDesiredStateDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceDesiredStateDAO.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -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.ServiceDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.ServiceDesiredStateEntityPK;
+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.ServiceDesiredStateEntity;
+import org.apache.ambari.server.orm.entities.ServiceDesiredStateEntityPK;
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+import com.google.inject.persist.Transactional;
 
 @Singleton
 public class ServiceDesiredStateDAO {

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/StackDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/StackDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/StackDAO.java
index 1385990..f7c5dbe 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/StackDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/StackDAO.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -25,6 +25,7 @@ import javax.persistence.TypedQuery;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.orm.RequiresSession;
 import org.apache.ambari.server.orm.entities.StackEntity;
+import org.apache.ambari.server.state.StackId;
 
 import com.google.inject.Inject;
 import com.google.inject.Provider;
@@ -94,6 +95,19 @@ public class StackDAO {
   }
 
   /**
+   * Gets the stack that matches the specified stack ID by name and version.
+   *
+   * @param stackId
+   *          the stack ID to find (not {@code null}).
+   * @return the stack matching the specified name and version or {@code null}
+   *         if none.
+   */
+  @RequiresSession
+  public StackEntity find(StackId stackId) {
+    return find(stackId.getStackName(), stackId.getStackVersion());
+  }
+
+  /**
    * Persists a new stack instance.
    *
    * @param stack

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterConfigEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterConfigEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterConfigEntity.java
index 4c2153a..34f3034 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterConfigEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterConfigEntity.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -18,9 +18,8 @@
 
 package org.apache.ambari.server.orm.entities;
 
-import com.google.common.base.Objects;
-
 import java.util.Collection;
+import java.util.Objects;
 
 import javax.persistence.Basic;
 import javax.persistence.Column;
@@ -41,6 +40,8 @@ import javax.persistence.Table;
 import javax.persistence.TableGenerator;
 import javax.persistence.UniqueConstraint;
 
+import org.apache.commons.lang.builder.EqualsBuilder;
+
 @Entity
 @Table(name = "clusterconfig",
   uniqueConstraints = {@UniqueConstraint(name = "UQ_config_type_tag", columnNames = {"cluster_id", "type_name", "version_tag"}),
@@ -51,20 +52,27 @@ import javax.persistence.UniqueConstraint;
   , initialValue = 1
 )
 @NamedQueries({
-    @NamedQuery(name = "ClusterConfigEntity.findNextConfigVersion", query = "SELECT COALESCE(MAX(clusterConfig.version),0) + 1 as nextVersion FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.type=:configType AND clusterConfig.clusterId=:clusterId"),
-    @NamedQuery(name = "ClusterConfigEntity.findAllConfigsByStack", query = "SELECT clusterConfig FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.clusterId=:clusterId AND clusterConfig.stack=:stack"),
-    @NamedQuery(name = "ClusterConfigEntity.findLatestConfigsByStack", query = "SELECT clusterConfig FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.clusterId=:clusterId AND clusterConfig.timestamp = (SELECT MAX(clusterConfig2.timestamp) FROM ClusterConfigEntity clusterConfig2 WHERE clusterConfig2.clusterId=:clusterId AND clusterConfig2.stack=:stack AND clusterConfig2.type = clusterConfig.type)"),
-    @NamedQuery(name = "ClusterConfigEntity.findNotMappedClusterConfigsToService", query = "SELECT clusterConfig FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.serviceConfigEntities IS EMPTY AND clusterConfig.type != 'cluster-env'"),
-    @NamedQuery(name = "ClusterConfigEntity.findClusterConfigMappingsByStack",
-      query = "SELECT mapping FROM ClusterConfigMappingEntity mapping " +
-        "JOIN ClusterConfigEntity config ON mapping.typeName = config.type AND mapping.tag = config.tag " +
-        "WHERE mapping.clusterId = :clusterId AND config.stack = :stack"),
-    @NamedQuery(name = "ClusterConfigEntity.findLatestClusterConfigsByTypes",
-      query = "SELECT cc FROM ClusterConfigEntity cc " +
-        "JOIN ClusterConfigMappingEntity ccm " +
-        "ON cc.clusterId = ccm.clusterId AND cc.type = ccm.typeName AND cc.tag = ccm.tag " +
-        "WHERE cc.clusterId = :clusterId AND ccm.selectedInd > 0 AND ccm.typeName IN :types")
-})
+    @NamedQuery(
+        name = "ClusterConfigEntity.findNextConfigVersion",
+        query = "SELECT COALESCE(MAX(clusterConfig.version),0) + 1 as nextVersion FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.type=:configType AND clusterConfig.clusterId=:clusterId"),
+    @NamedQuery(
+        name = "ClusterConfigEntity.findAllConfigsByStack",
+        query = "SELECT clusterConfig FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.clusterId=:clusterId AND clusterConfig.stack=:stack"),
+    @NamedQuery(
+        name = "ClusterConfigEntity.findLatestConfigsByStack",
+        query = "SELECT clusterConfig FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.clusterId = :clusterId AND clusterConfig.stack = :stack AND clusterConfig.selectedTimestamp = (SELECT MAX(clusterConfig2.selectedTimestamp) FROM ClusterConfigEntity clusterConfig2 WHERE clusterConfig2.clusterId=:clusterId AND clusterConfig2.stack=:stack AND clusterConfig2.type = clusterConfig.type)"),
+    @NamedQuery(
+        name = "ClusterConfigEntity.findNotMappedClusterConfigsToService",
+        query = "SELECT clusterConfig FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.serviceConfigEntities IS EMPTY AND clusterConfig.type != 'cluster-env'"),
+    @NamedQuery(
+        name = "ClusterConfigEntity.findEnabledConfigsByStack",
+        query = "SELECT config FROM ClusterConfigEntity config WHERE config.clusterId = :clusterId AND config.selected = 1 AND config.stack = :stack"),
+    @NamedQuery(
+        name = "ClusterConfigEntity.findEnabledConfigByType",
+        query = "SELECT config FROM ClusterConfigEntity config WHERE config.clusterId = :clusterId AND config.selected = 1 and config.type = :type"),
+    @NamedQuery(
+        name = "ClusterConfigEntity.findEnabledConfigsByTypes",
+        query = "SELECT config FROM ClusterConfigEntity config WHERE config.clusterId = :clusterId AND config.selected = 1 and config.type in :types") })
 
 public class ClusterConfigEntity {
 
@@ -85,6 +93,9 @@ public class ClusterConfigEntity {
   @Column(name = "version_tag")
   private String tag;
 
+  @Column(name = "selected", insertable = true, updatable = true, nullable = false)
+  private int selected = 0;
+
   @Basic(fetch = FetchType.LAZY)
   @Column(name = "config_data", nullable = false, insertable = true)
   @Lob
@@ -98,6 +109,16 @@ public class ClusterConfigEntity {
   @Column(name = "create_timestamp", nullable = false, insertable = true, updatable = false)
   private long timestamp;
 
+  /**
+   * The most recent time that this configuration was marked as
+   * {@link #selected}. This is useful when configruations are being reverted
+   * since a reversion does not create a new instance. Another configuration may
+   * technically be newer via its creation date ({@link #timestamp}), however
+   * that does not indicate it was the most recently enabled configuration.
+   */
+  @Column(name = "selected_timestamp", nullable = false, insertable = true, updatable = true)
+  private long selectedTimestamp = 0;
+
   @ManyToOne
   @JoinColumn(name = "cluster_id", referencedColumnName = "cluster_id", nullable = false)
   private ClusterEntity clusterEntity;
@@ -182,6 +203,10 @@ public class ClusterConfigEntity {
     timestamp = stamp;
   }
 
+  public long getSelectedTimestamp() {
+    return selectedTimestamp;
+  }
+
   public String getAttributes() {
     return configAttributesJson;
   }
@@ -209,51 +234,37 @@ public class ClusterConfigEntity {
     this.stack = stack;
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
-  public boolean equals(Object o) {
-    if (this == o) {
+  public boolean equals(Object object) {
+    if (this == object) {
       return true;
     }
 
-    if (o == null || getClass() != o.getClass()) {
+    if (object == null || getClass() != object.getClass()) {
       return false;
     }
 
-    ClusterConfigEntity that = (ClusterConfigEntity) o;
+    ClusterConfigEntity that = (ClusterConfigEntity) object;
+    EqualsBuilder equalsBuilder = new EqualsBuilder();
 
-    if (configId != null ? !configId.equals(that.configId) : that.configId != null) {
-      return false;
-    }
-
-    if (clusterId != null ? !clusterId.equals(that.clusterId) : that.clusterId != null) {
-      return false;
-    }
-
-    if (type != null ? !type.equals(that.type) : that.type != null) {
-      return false;
-    }
-
-    if (tag != null ? !tag.equals(that.tag) : that.tag != null) {
-      return false;
-    }
-
-    if (stack != null ? !stack.equals(that.stack) : that.stack != null) {
-      return false;
-    }
-
-    return true;
+    equalsBuilder.append(configId, that.configId);
+    equalsBuilder.append(clusterId, that.clusterId);
+    equalsBuilder.append(type, that.type);
+    equalsBuilder.append(tag, that.tag);
+    equalsBuilder.append(stack, that.stack);
 
+    return equalsBuilder.isEquals();
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
   public int hashCode() {
-    int result = configId != null ? configId.hashCode() : 0;
-    result = 31 * result + (clusterId != null ? clusterId.hashCode() : 0);
-    result = 31 * result + (type != null ? type.hashCode() : 0);
-    result = 31 * result + (tag != null ? tag.hashCode() : 0);
-    result = 31 * result + (stack != null ? stack.hashCode() : 0);
-
-    return result;
+    return Objects.hash(configId, clusterId, type, tag, stack);
   }
 
   public ClusterEntity getClusterEntity() {
@@ -286,12 +297,28 @@ public class ClusterConfigEntity {
    */
   @Override
   public String toString() {
-    return Objects.toStringHelper(this)
+    return com.google.common.base.Objects.toStringHelper(this)
       .add("clusterId", clusterId)
       .add("type", type)
       .add("version", version)
       .add("tag", tag)
-      .add("timestamp", timestamp)
+      .add("selected", selected == 1)
+      .add("selectedTimeStamp", selectedTimestamp)
+      .add("created", timestamp)
       .toString();
   }
+
+  public boolean isSelected() {
+    return selected == 1;
+  }
+
+  public void setSelected(boolean selected) {
+    this.selected = selected ? 1 : 0;
+
+    // if this config is being selected, then also update the selected timestamp
+    if (selected) {
+      selectedTimestamp = System.currentTimeMillis();
+    }
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
index e56d005..6aad574 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
@@ -20,7 +20,6 @@ package org.apache.ambari.server.orm.entities;
 
 import static org.apache.commons.lang.StringUtils.defaultString;
 
-import java.util.ArrayList;
 import java.util.Collection;
 
 import javax.persistence.Basic;
@@ -131,9 +130,6 @@ public class ClusterEntity {
   private Collection<AlertDefinitionEntity> alertDefinitionEntities;
 
   @OneToMany(mappedBy = "clusterEntity", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
-  private Collection<ClusterVersionEntity> clusterVersionEntities;
-
-  @OneToMany(mappedBy = "clusterEntity", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
   private Collection<WidgetEntity> widgetEntities;
 
   @OneToMany(mappedBy = "clusterEntity", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
@@ -337,19 +333,6 @@ public class ClusterEntity {
     return alertDefinitionEntities;
   }
 
-  public Collection<ClusterVersionEntity> getClusterVersionEntities() {
-    return clusterVersionEntities;
-  }
-
-  public void setClusterVersionEntities(Collection<ClusterVersionEntity> clusterVersionEntities) { this.clusterVersionEntities = clusterVersionEntities; }
-
-  public void addClusterVersionEntity(ClusterVersionEntity clusterVersionEntity) {
-    if (clusterVersionEntities == null) {
-      clusterVersionEntities = new ArrayList<ClusterVersionEntity>();
-    }
-    clusterVersionEntities.add(clusterVersionEntity);
-  }
-
   /**
    * Get the admin resource entity.
    *

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java
deleted file mode 100644
index f1867b4..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterVersionEntity.java
+++ /dev/null
@@ -1,217 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.orm.entities;
-
-import static org.apache.commons.lang.StringUtils.defaultString;
-
-import javax.persistence.Basic;
-import javax.persistence.Column;
-import javax.persistence.Entity;
-import javax.persistence.EnumType;
-import javax.persistence.Enumerated;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
-import javax.persistence.JoinColumn;
-import javax.persistence.ManyToOne;
-import javax.persistence.NamedQueries;
-import javax.persistence.NamedQuery;
-import javax.persistence.Table;
-import javax.persistence.TableGenerator;
-
-import org.apache.ambari.server.state.RepositoryVersionState;
-
-@Table(name = "cluster_version")
-@Entity
-@TableGenerator(name = "cluster_version_id_generator",
-    table = "ambari_sequences", pkColumnName = "sequence_name", valueColumnName = "sequence_value"
-    , pkColumnValue = "cluster_version_id_seq"
-    , initialValue = 0
-)
-@NamedQueries({
-    @NamedQuery(name = "clusterVersionByClusterAndStackAndVersion", query =
-        "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion JOIN clusterVersion.clusterEntity cluster " +
-        "WHERE cluster.clusterName=:clusterName AND clusterVersion.repositoryVersion.stack.stackName=:stackName AND clusterVersion.repositoryVersion.stack.stackVersion=:stackVersion AND clusterVersion.repositoryVersion.version=:version"),
-    @NamedQuery(name = "clusterVersionByClusterAndState", query =
-        "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion JOIN clusterVersion.clusterEntity cluster " +
-        "WHERE cluster.clusterName=:clusterName AND clusterVersion.state=:state"),
-    @NamedQuery(name = "clusterVersionByCluster", query =
-        "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion JOIN clusterVersion.clusterEntity cluster " +
-        "WHERE cluster.clusterName=:clusterName"),
-    @NamedQuery(name = "clusterVersionByStackVersion", query = "SELECT clusterVersion FROM ClusterVersionEntity clusterVersion WHERE clusterVersion.repositoryVersion.stack.stackName=:stackName AND clusterVersion.repositoryVersion.stack.stackVersion=:stackVersion AND clusterVersion.repositoryVersion.version=:version"),
-})
-public class ClusterVersionEntity {
-
-  @Id
-  @Column(name = "id", nullable = false, insertable = true, updatable = false)
-  @GeneratedValue(strategy = GenerationType.TABLE, generator = "cluster_version_id_generator")
-  private Long id;
-
-  @Column(name = "cluster_id", nullable = false, insertable = false, updatable = false)
-  private Long clusterId;
-
-  @ManyToOne
-  @JoinColumn(name = "cluster_id", referencedColumnName = "cluster_id", nullable = false)
-  private ClusterEntity clusterEntity;
-
-  @ManyToOne
-  @JoinColumn(name = "repo_version_id", referencedColumnName = "repo_version_id", nullable = false)
-  private RepositoryVersionEntity repositoryVersion;
-
-  @Column(name = "state", nullable = false, insertable = true, updatable = true)
-  @Enumerated(value = EnumType.STRING)
-  private RepositoryVersionState state = RepositoryVersionState.CURRENT;
-
-  @Basic
-  @Column(name = "start_time", nullable = false, insertable = true, updatable = true)
-  private Long startTime = System.currentTimeMillis();
-
-  @Basic
-  @Column(name = "end_time", insertable = true, updatable = true)
-  private Long endTime;
-
-  @Basic
-  @Column(name = "user_name", insertable = true, updatable = true)
-  private String userName = "";
-
-  /**
-   * Empty constructor primarily used by unit tests.
-   */
-  public ClusterVersionEntity() {
-  }
-
-  /**
-   * Full constructor that doesn't have the endTime
-   * @param cluster Cluster entity
-   * @param repositoryVersion repository version
-   * @param state Cluster version state
-   * @param startTime Time the cluster version reached its first state
-   * @param userName User who performed the action
-   */
-  public ClusterVersionEntity(ClusterEntity cluster, RepositoryVersionEntity repositoryVersion, RepositoryVersionState state, long startTime, String userName) {
-    clusterId = cluster.getClusterId();
-    this.repositoryVersion = repositoryVersion;
-    clusterEntity = cluster;
-    this.state = state;
-    this.startTime = startTime;
-    this.userName = userName;
-  }
-
-  /**
-   * Full constructor that does have the endTime
-   * @param cluster Cluster entity
-   * @param repositoryVersion repository version
-   * @param state Cluster version state
-   * @param startTime Time the cluster version reached its first state
-   * @param endTime Time the cluster version finalized its state
-   * @param userName User who performed the action
-   */
-  public ClusterVersionEntity(ClusterEntity cluster, RepositoryVersionEntity repositoryVersion, RepositoryVersionState state, long startTime, long endTime, String userName) {
-    this(cluster, repositoryVersion, state, startTime, userName);
-    this.endTime = endTime;
-  }
-
-  public Long getId() {
-    return id;
-  }
-
-  public void setId(Long id) {
-    this.id = id;
-  }
-
-  public Long getClusterId() {
-    return clusterId;
-  }
-
-  public void setClusterId(Long clusterId) {
-    this.clusterId = clusterId;
-  }
-
-  public ClusterEntity getClusterEntity() {
-    return clusterEntity;
-  }
-
-  public void setClusterEntity(ClusterEntity clusterEntity) {
-    this.clusterEntity = clusterEntity;
-  }
-
-  public RepositoryVersionState getState() {
-    return state;
-  }
-
-  public void setState(RepositoryVersionState state) {
-    this.state = state;
-  }
-
-  public Long getStartTime() { return startTime; }
-
-  public void setStartTime(Long startTime) { this.startTime = startTime; }
-
-  public Long getEndTime() { return endTime; }
-
-  public void setEndTime(Long endTime) { this.endTime = endTime; }
-
-  public String getUserName() { return defaultString(userName); }
-
-  public void setUserName(String userName) { this.userName = userName; }
-
-  public void setRepositoryVersion(RepositoryVersionEntity repositoryVersion) {
-    this.repositoryVersion = repositoryVersion;
-  }
-
-  public RepositoryVersionEntity getRepositoryVersion() {
-    return repositoryVersion;
-  }
-
-  @Override
-  public boolean equals(Object o) {
-    if (this == o) {
-      return true;
-    }
-    if (o == null || getClass() != o.getClass()) {
-      return false;
-    }
-
-    ClusterVersionEntity that = (ClusterVersionEntity) o;
-
-    if (id != that.id
-        || clusterId != that.clusterId
-        || !repositoryVersion.equals(that.repositoryVersion)
-        || !state.equals(that.state)
-        || !startTime.equals(that.startTime)
-        || !endTime.equals(that.endTime)
-        || !userName.equals(that.userName)) {
-      return false;
-    }
-
-    return true;
-  }
-
-  @Override
-  public int hashCode() {
-    int result = id !=null ? id.intValue() : 0;
-    result = 31 * result + (clusterId != null ? clusterId.hashCode() : 0);
-    result = 31 * result + (repositoryVersion != null ? repositoryVersion.hashCode() : 0);
-    result = 31 * result + (state != null ? state.hashCode() : 0);
-    result = 31 * result + (startTime != null ? startTime.hashCode() : 0);
-    result = 31 * result + (endTime != null ? endTime.hashCode() : 0);
-    result = 31 * result + (userName != null ? userName.hashCode() : 0);
-    return result;
-  }
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryEntity.java
index d8d3bbd..bad8195 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryEntity.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -25,6 +25,7 @@ public class RepositoryEntity {
   private String name;
   private String baseUrl;
   private String repositoryId;
+  private String mirrorsList;
   private boolean unique;
 
   public String getName() {
@@ -73,6 +74,14 @@ public class RepositoryEntity {
     return result;
   }
 
+  public String getMirrorsList() {
+    return mirrorsList;
+  }
+
+  public void setMirrorsList(String mirrorsList) {
+    this.mirrorsList = mirrorsList;
+  }
+
   public boolean isUnique() {
     return unique;
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
index 331fe86..df15d27 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/RepositoryVersionEntity.java
@@ -115,10 +115,6 @@ public class RepositoryVersionEntity {
   @Column(name = "repositories")
   private String operatingSystems;
 
-
-  @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "repositoryVersion")
-  private Set<ClusterVersionEntity> clusterVersionEntities;
-
   @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "repositoryVersion")
   private Set<HostVersionEntity> hostVersionEntities;
 
@@ -169,13 +165,6 @@ public class RepositoryVersionEntity {
       version = version.substring(stackName.length() + 1);
     }
   }
-  /**
-   * Update one-to-many relation without rebuilding the whole entity
-   * @param entity many-to-one entity
-   */
-  public void updateClusterVersionEntityRelation(ClusterVersionEntity entity){
-    clusterVersionEntities.add(entity);
-  }
 
   /**
    * Update one-to-many relation without rebuilding the whole entity


[28/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
index 0e6f0c4..9d70546 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -20,11 +20,13 @@ package org.apache.ambari.server.serveraction.upgrades;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.text.MessageFormat;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
+import java.util.TreeSet;
 import java.util.concurrent.ConcurrentMap;
 
 import org.apache.ambari.server.AmbariException;
@@ -33,35 +35,24 @@ import org.apache.ambari.server.agent.CommandReport;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.events.StackUpgradeFinishEvent;
 import org.apache.ambari.server.events.publishers.VersionEventPublisher;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
-import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
-import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.ServiceComponentHistoryEntity;
-import org.apache.ambari.server.orm.entities.StackEntity;
-import org.apache.ambari.server.orm.entities.UpgradeEntity;
-import org.apache.ambari.server.serveraction.AbstractServerAction;
 import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ComponentInfo;
+import org.apache.ambari.server.state.RepositoryType;
 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.ServiceComponentHost;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.UpgradeContext;
-import org.apache.ambari.server.state.UpgradeContextFactory;
 import org.apache.ambari.server.state.UpgradeState;
 import org.apache.ambari.server.state.stack.upgrade.Direction;
-import org.apache.ambari.server.state.svccomphost.ServiceComponentHostSummary;
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.EqualsBuilder;
 import org.apache.commons.lang.text.StrBuilder;
 
 import com.google.inject.Inject;
@@ -69,228 +60,128 @@ import com.google.inject.Inject;
 /**
  * Action that represents finalizing the Upgrade by completing any database changes.
  */
-public class FinalizeUpgradeAction extends AbstractServerAction {
+public class FinalizeUpgradeAction extends AbstractUpgradeServerAction {
 
-  public static final String CLUSTER_NAME_KEY = "cluster_name";
-  public static final String UPGRADE_DIRECTION_KEY = "upgrade_direction";
-  public static final String VERSION_KEY = "version";
-  public static final String REQUEST_ID = "request_id";
   public static final String PREVIOUS_UPGRADE_NOT_COMPLETED_MSG = "It is possible that a previous upgrade was not finalized. " +
       "For this reason, Ambari will not remove any configs. Please ensure that all database records are correct.";
 
-  /**
-   * The original "current" stack of the cluster before the upgrade started.
-   * This is the same regardless of whether the current direction is
-   * {@link Direction#UPGRADE} or {@link Direction#DOWNGRADE}.
-   */
-  public static final String ORIGINAL_STACK_KEY = "original_stack";
-
-  /**
-   * The target upgrade stack before the upgrade started. This is the same
-   * regardless of whether the current direction is {@link Direction#UPGRADE} or
-   * {@link Direction#DOWNGRADE}.
-   */
-  public static final String TARGET_STACK_KEY = "target_stack";
-
-  /**
-   * The Cluster that this ServerAction implementation is executing on
-   */
-  @Inject
-  protected Clusters clusters;
-
-  @Inject
-  private ClusterVersionDAO clusterVersionDAO;
-
   @Inject
   private HostVersionDAO hostVersionDAO;
 
   @Inject
   private HostComponentStateDAO hostComponentStateDAO;
 
-  /**
-   * Gets {@link StackEntity} instances from {@link StackId}.
-   */
-  @Inject
-  private StackDAO stackDAO;
-
-  /**
-   * Gets desired state entities for service components.
-   */
-  @Inject
-  private ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO;
-
   @Inject
   private AmbariMetaInfo ambariMetaInfo;
 
   @Inject
-  VersionEventPublisher versionEventPublisher;
-
-  /**
-   * Used for building {@link UpgradeContext} instances.
-   */
-  @Inject
-  private UpgradeContextFactory upgradeContextFactory;
+  private VersionEventPublisher versionEventPublisher;
 
   @Override
   public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext)
       throws AmbariException, InterruptedException {
 
     String clusterName = getExecutionCommand().getClusterName();
-    Cluster cluster = clusters.getCluster(clusterName);
-    UpgradeContext context = upgradeContextFactory.create(cluster, cluster.getUpgradeInProgress());
+    Cluster cluster = m_clusters.getCluster(clusterName);
 
-    if (context.getDirection() == Direction.DOWNGRADE) {
-      return finalizeDowngrade(context);
+    UpgradeContext upgradeContext = getUpgradeContext(cluster);
+
+    if (upgradeContext.getDirection() == Direction.UPGRADE) {
+      return finalizeUpgrade(upgradeContext);
     } else {
-      return finalizeUpgrade(context);
+      return finalizeDowngrade(upgradeContext);
     }
   }
 
   /**
    * Execution path for upgrade.
-   *
-   * @param context
-   *          the upgrade context (not {@code null}).
    * @return the command report
    */
-  private CommandReport finalizeUpgrade(UpgradeContext context)
+  private CommandReport finalizeUpgrade(UpgradeContext upgradeContext)
     throws AmbariException, InterruptedException {
 
     StringBuilder outSB = new StringBuilder();
     StringBuilder errSB = new StringBuilder();
 
     try {
-      Cluster cluster = context.getCluster();
-      String clusterName = cluster.getClusterName();
-      RepositoryVersionEntity targetRepositoryVersion = context.getTargetRepositoryVersion();
-      String version = targetRepositoryVersion.getVersion();
+      Cluster cluster = upgradeContext.getCluster();
+      RepositoryVersionEntity repositoryVersion = upgradeContext.getRepositoryVersion();
+      String version = repositoryVersion.getVersion();
+
+      String message;
+      if (upgradeContext.getOrchestrationType() == RepositoryType.STANDARD) {
+        message = MessageFormat.format("Finalizing the upgrade to {0} for all cluster services.", version);
+      } else {
+        Set<String> servicesInUpgrade = upgradeContext.getSupportedServices();
 
-      outSB.append(MessageFormat.format(
-          "Begin finalizing the upgrade of cluster {0} to version {1}\n", clusterName,
-          targetRepositoryVersion.getVersion()));
+        message = MessageFormat.format(
+            "Finalizing the upgrade to {0} for the following services: {1}",
+            version, StringUtils.join(servicesInUpgrade, ','));
+      }
 
-      StackId clusterDesiredStackId = cluster.getDesiredStackVersion();
-      StackId clusterCurrentStackId = cluster.getCurrentStackVersion();
+      outSB.append(message).append(System.lineSeparator());
 
-      ClusterVersionEntity upgradingClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(
-          clusterName, clusterDesiredStackId, version);
+      // iterate through all host components and make sure that they are on the
+      // correct version; if they are not, then this will throw an exception
+      Set<InfoTuple> errors = validateComponentVersions(upgradeContext);
+      if (!errors.isEmpty()) {
+        StrBuilder messageBuff = new StrBuilder(String.format(
+            "The following %d host component(s) "
+                + "have not been upgraded to version %s. Please install and upgrade "
+                + "the Stack Version on those hosts and try again.\nHost components:",
+            errors.size(), version)).append(System.lineSeparator());
 
-      if (upgradingClusterVersion == null) {
-        throw new AmbariException(MessageFormat.format(
-            "Cluster stack version {0} not found", version));
+        for (InfoTuple error : errors) {
+          messageBuff.append(String.format("%s on host %s\n", error.componentName, error.hostName));
+        }
+
+        throw new AmbariException(messageBuff.toString());
       }
 
-      // Validate that all of the hosts with a version in the cluster have the
-      // version being upgraded to, and it is in an allowed state.
-      List<HostVersionEntity> hostVersions = hostVersionDAO.findByClusterStackAndVersion(
-          clusterName, clusterDesiredStackId, version);
+      // find every host version for this upgrade and ensure it has transitioned
+      // to CURRENT if required
+      List<HostVersionEntity> hostVersions = hostVersionDAO.findHostVersionByClusterAndRepository(
+          cluster.getClusterId(), repositoryVersion);
 
-      // Will include hosts whose state is INSTALLED
       Set<HostVersionEntity> hostVersionsAllowed = new HashSet<>();
       Set<String> hostsWithoutCorrectVersionState = new HashSet<>();
-      Set<String> hostsToUpdate = new HashSet<>();
 
-      // It is important to only iterate over the hosts with a version, as
-      // opposed to all hosts, since some hosts may only have components that do
-      // not advertise a version, such as AMBARI_METRICS.
+      // for every host version for this repository, determine if any didn't
+      // transition correctly
       for (HostVersionEntity hostVersion : hostVersions) {
-        boolean hostHasCorrectVersionState = false;
         RepositoryVersionState hostVersionState = hostVersion.getState();
-        switch( hostVersionState ){
-          case CURRENT:{
-            // if the state is correct, then do nothing
-            hostHasCorrectVersionState = true;
-            break;
-          }
-          case NOT_REQUIRED:
-          case INSTALLED:{
-            // It is possible that the host version has a state of INSTALLED and it
-            // never changed if the host only has components that do not advertise a
-            // version.
-            HostEntity host = hostVersion.getHostEntity();
-
-            ServiceComponentHostSummary hostSummary = new ServiceComponentHostSummary(ambariMetaInfo,
-                host, clusterDesiredStackId);
-
-            // if all components have finished advertising their version, then
-            // this host can be considered upgraded
-            if (hostSummary.haveAllComponentsFinishedAdvertisingVersion()) {
-              // mark this as upgraded
-              hostHasCorrectVersionState = true;
-            } else {
-              hostsWithoutCorrectVersionState.add(hostVersion.getHostName());
-            }
-
+        switch (hostVersionState) {
+          case CURRENT:
+          case NOT_REQUIRED: {
+            hostVersionsAllowed.add(hostVersion);
             break;
           }
           default: {
-            // all other states are not allowed
             hostsWithoutCorrectVersionState.add(hostVersion.getHostName());
             break;
           }
         }
-
-        // keep track of this host version in order to transition it correctly
-        if (hostHasCorrectVersionState) {
-          hostVersionsAllowed.add(hostVersion);
-          hostsToUpdate.add(hostVersion.getHostName());
-        }
       }
 
-      // throw an exception if there are hosts which are not not fully upgraded
+      // throw an exception if there are hosts which did not transition the
+      // repository to CURRENT
       if (hostsWithoutCorrectVersionState.size() > 0) {
-        String message = String.format("The following %d host(s) have not been upgraded to version %s. " +
-                "Please install and upgrade the Stack Version on those hosts and try again.\nHosts: %s\n",
-            hostsWithoutCorrectVersionState.size(),
-            version,
+        message = String.format(
+            "The following %d host(s) have not been upgraded to version %s. "
+                + "Please install and upgrade the Stack Version on those hosts and try again.\nHosts: %s",
+            hostsWithoutCorrectVersionState.size(), version,
             StringUtils.join(hostsWithoutCorrectVersionState, ", "));
         outSB.append(message);
+        outSB.append(System.lineSeparator());
         throw new AmbariException(message);
       }
 
-      // iterate through all host components and make sure that they are on the
-      // correct version; if they are not, then this will throw an exception
-      List<InfoTuple> errors = checkHostComponentVersions(cluster, version, clusterDesiredStackId);
-      if (! errors.isEmpty()) {
-        StrBuilder messageBuff = new StrBuilder(
-            String.format(
-                "The following %d host component(s) "
-                    + "have not been upgraded to version %s. Please install and upgrade "
-                    + "the Stack Version on those hosts and try again.\nHost components:\n",
-                errors.size(), version));
-
-        for (InfoTuple error : errors) {
-          messageBuff.append(String.format("%s on host %s\n", error.componentName, error.hostName));
-        }
-
-        throw new AmbariException(messageBuff.toString());
-      }
-
-
-      // we're guaranteed to be ready transition to upgraded now; ensure that
-      // the transition will be allowed if the cluster state is not upgraded
-      upgradingClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(clusterName,
-          clusterDesiredStackId, version);
-
-      if (RepositoryVersionState.INSTALLING == upgradingClusterVersion.getState()) {
-        cluster.transitionClusterVersion(clusterDesiredStackId, version, RepositoryVersionState.INSTALLED);
-
-        upgradingClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(
-            clusterName, clusterDesiredStackId, version);
-      }
-
-      // we cannot finalize since the cluster was not ready to move into the
-      // upgraded state
-      if (RepositoryVersionState.INSTALLED != upgradingClusterVersion.getState()) {
-        throw new AmbariException(String.format("The cluster stack version state %s is not allowed to transition directly into %s",
-            upgradingClusterVersion.getState(), RepositoryVersionState.CURRENT.toString()));
-      }
-
       outSB.append(
-          String.format("Finalizing the upgraded state of host components in %d host(s).\n",
-              hostVersionsAllowed.size()));
+          String.format("Finalizing the upgrade state and repository version for %d host(s).",
+              hostVersionsAllowed.size())).append(System.lineSeparator());
 
-      // Reset the upgrade state
+      // at this point, all host versions are correct - do some cleanup like
+      // resetting the upgrade state
       for (HostVersionEntity hostVersion : hostVersionsAllowed) {
         Collection<HostComponentStateEntity> hostComponentStates = hostComponentStateDAO.findByHost(hostVersion.getHostName());
         for (HostComponentStateEntity hostComponentStateEntity: hostComponentStates) {
@@ -299,28 +190,18 @@ public class FinalizeUpgradeAction extends AbstractServerAction {
         }
       }
 
-      // Impacts all hosts that have a version
-      outSB.append(
-          String.format("Finalizing the version for %d host(s).\n", hostVersionsAllowed.size()));
-      cluster.mapHostVersions(hostsToUpdate, upgradingClusterVersion, RepositoryVersionState.CURRENT);
-
-      versionEventPublisher.publish(new StackUpgradeFinishEvent(cluster));
-
-      // transitioning the cluster into CURRENT will update the current/desired
-      // stack values
-      outSB.append(String.format("Finalizing the version for cluster %s.\n", clusterName));
-      cluster.transitionClusterVersion(clusterDesiredStackId, version,
-          RepositoryVersionState.CURRENT);
-
-      UpgradeEntity upgradeEntity = cluster.getUpgradeInProgress();
-      outSB.append("Creating upgrade history.\n");
-      writeComponentHistory(cluster, upgradeEntity, clusterCurrentStackId, clusterDesiredStackId);
+      // move host versions from CURRENT to INSTALLED if their repos are no
+      // longer used
+      finalizeHostRepositoryVersions(cluster);
 
       // Reset upgrade state
       cluster.setUpgradeEntity(null);
 
-      outSB.append("Upgrade was successful!\n");
+      // the upgrade is done!
+      versionEventPublisher.publish(new StackUpgradeFinishEvent(cluster));
 
+      message = String.format("The upgrade to %s has completed.", version);
+      outSB.append(message).append(System.lineSeparator());
       return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", outSB.toString(), errSB.toString());
     } catch (Exception e) {
       errSB.append(e.getMessage());
@@ -331,223 +212,286 @@ public class FinalizeUpgradeAction extends AbstractServerAction {
   /**
    * Execution path for downgrade.
    *
-   * @param context
+   * @param upgradeContext
    *          the upgrade context (not {@code null}).
    * @return the command report
    */
-  private CommandReport finalizeDowngrade(UpgradeContext context)
+  private CommandReport finalizeDowngrade(UpgradeContext upgradeContext)
       throws AmbariException, InterruptedException {
 
-    StringBuilder out = new StringBuilder();
-    StringBuilder err = new StringBuilder();
+    StringBuilder outSB = new StringBuilder();
+    StringBuilder errSB = new StringBuilder();
 
     try {
-      Cluster cluster = context.getCluster();
-      String clusterName = cluster.getClusterName();
-
-      StackId currentClusterStackId = cluster.getCurrentStackVersion();
-      StackId sourceStackId = context.getSourceStackId();
-      StackId targetStackId = context.getTargetStackId();
-      RepositoryVersionEntity targetRepositoryVersion = context.getTargetRepositoryVersion();
-
-      // Safety check that the cluster's stack (from clusterstate's current_stack_id) is equivalent to the
-      // cluster's CURRENT repo version's stack. This is to avoid deleting configs from the target stack if the customer
-      // ended up modifying their database manually after a stack upgrade and forgot to call "Save DB State".
-      ClusterVersionEntity currentClusterVersion = cluster.getCurrentClusterVersion();
-      RepositoryVersionEntity currentRepoVersion = currentClusterVersion.getRepositoryVersion();
-      StackId currentRepoStackId = currentRepoVersion.getStackId();
-      if (!currentRepoStackId.equals(targetStackId)) {
-        String msg = String.format(
-            "The stack of %s's CURRENT repository version is %s, yet the target stack for this downgrade is %s. %s",
-            clusterName, currentRepoStackId.getStackId(), targetStackId.getStackId(),
-            PREVIOUS_UPGRADE_NOT_COMPLETED_MSG);
-        out.append(msg);
-        err.append(msg);
-        throw new AmbariException(
-            "The target stack of this downgrade doesn't match the cluster's current stack.");
-      }
+      Cluster cluster = upgradeContext.getCluster();
+      RepositoryVersionEntity downgradeFromRepositoryVersion = upgradeContext.getRepositoryVersion();
+      String downgradeFromVersion = downgradeFromRepositoryVersion.getVersion();
+      Set<String> servicesInUpgrade = upgradeContext.getSupportedServices();
 
-      // This was a cross-stack upgrade, meaning that configurations were created that now need to be removed.
-      if (!sourceStackId.equals(targetStackId)) {
-        out.append(String.format(
-            "Configurations created for stack %s will be removed since this downgrade is to stack %s.",
-            sourceStackId.getStackId(), targetStackId.getStackId()));
+      String message;
 
-        out.append(System.lineSeparator());
-        cluster.removeConfigurations(sourceStackId);
+      if (upgradeContext.getOrchestrationType() == RepositoryType.STANDARD) {
+        message = MessageFormat.format(
+            "Finalizing the downgrade from {0} for all cluster services.",
+            downgradeFromVersion);
+      } else {
+        message = MessageFormat.format(
+            "Finalizing the downgrade from {0} for the following services: {1}",
+            downgradeFromVersion, StringUtils.join(servicesInUpgrade, ','));
       }
 
-      ClusterVersionEntity clusterVersion = clusterVersionDAO.findByClusterAndStateCurrent(clusterName);
-      if (null == clusterVersion) {
-        throw new AmbariException("Could not find current cluster version");
-      }
+      outSB.append(message).append(System.lineSeparator());
 
-      out.append(String.format(
-          "Comparing downgrade target version %s-%s to current cluster version %s-%s\n",
-          targetStackId.getStackName(), targetRepositoryVersion.getVersion(),
-          clusterVersion.getRepositoryVersion().getStackId().getStackName(),
-          clusterVersion.getRepositoryVersion().getVersion()));
-
-      if (!StringUtils.equals(targetRepositoryVersion.getVersion(),
-          clusterVersion.getRepositoryVersion().getVersion())) {
-        throw new AmbariException(
-            String.format("Downgrade version %s is not the current cluster version of %s. %s",
-                targetRepositoryVersion.getVersion(),
-                clusterVersion.getRepositoryVersion().getVersion(),
-                PREVIOUS_UPGRADE_NOT_COMPLETED_MSG));
-      } else {
-        out.append(String.format(
-            "Downgrade version is the same as current. Searching "
-                + "for cluster versions that do not match %s\n",
-            targetRepositoryVersion.getVersion()));
-      }
+      // iterate through all host components and make sure that they are on the
+      // correct version; if they are not, then this will throw an exception
+      Set<InfoTuple> errors = validateComponentVersions(upgradeContext);
+      if (!errors.isEmpty()) {
+        StrBuilder messageBuff = new StrBuilder(String.format(
+            "The following %d host component(s) have not been downgraded to their desired versions:",
+            errors.size())).append(System.lineSeparator());
 
-      Set<String> badVersions = new HashSet<>();
-
-      // update the cluster version
-      for (ClusterVersionEntity cve : clusterVersionDAO.findByCluster(clusterName)) {
-        switch (cve.getState()) {
-          case INSTALL_FAILED:
-          case INSTALLED:
-          case INSTALLING: {
-              badVersions.add(cve.getRepositoryVersion().getVersion());
-              cve.setState(RepositoryVersionState.INSTALLED);
-              clusterVersionDAO.merge(cve);
-              break;
-            }
-          default:
-            break;
+        for (InfoTuple error : errors) {
+          messageBuff.append(String.format("%s: $s (current = %s, desired = %s ", error.hostName,
+              error.componentName, error.currentVersion, error.targetVersion));
+
+          messageBuff.append(System.lineSeparator());
         }
+
+        throw new AmbariException(messageBuff.toString());
       }
 
-      out.append(String.format("Found %d other version(s) not matching downgrade: %s\n",
-          badVersions.size(), StringUtils.join(badVersions, ", ")));
 
-      Set<String> badHosts = new HashSet<>();
-      for (String badVersion : badVersions) {
-        List<HostVersionEntity> hostVersions = hostVersionDAO.findByClusterStackAndVersion(
-            clusterName, sourceStackId, badVersion);
+      if (upgradeContext.isPatchRevert()) {
+        finalizeHostRepositoryVersions(cluster);
+      } else {
+        // for every repository being downgraded to, ensure the host versions are correct
+        Map<String, RepositoryVersionEntity> targetVersionsByService = upgradeContext.getTargetVersions();
+        Set<RepositoryVersionEntity> targetRepositoryVersions = new HashSet<>();
+        for (String service : targetVersionsByService.keySet()) {
+          targetRepositoryVersions.add(targetVersionsByService.get(service));
+        }
+
+        for (RepositoryVersionEntity targetRepositoryVersion : targetRepositoryVersions) {
+          // find host versions
+          List<HostVersionEntity> hostVersions = hostVersionDAO.findHostVersionByClusterAndRepository(
+              cluster.getClusterId(), targetRepositoryVersion);
+
+          outSB.append(String.format("Finalizing %d host(s) back to %s", hostVersions.size(),
+              targetRepositoryVersion.getVersion())).append(System.lineSeparator());
+
+          for (HostVersionEntity hostVersion : hostVersions) {
+            if (hostVersion.getState() != RepositoryVersionState.CURRENT) {
+              hostVersion.setState(RepositoryVersionState.CURRENT);
+              hostVersionDAO.merge(hostVersion);
+            }
+
+            List<HostComponentStateEntity> hostComponentStates = hostComponentStateDAO.findByHost(
+                hostVersion.getHostName());
 
-        for (HostVersionEntity hostVersion : hostVersions) {
-          badHosts.add(hostVersion.getHostName());
-          hostVersion.setState(RepositoryVersionState.INSTALLED);
-          hostVersionDAO.merge(hostVersion);
+            for (HostComponentStateEntity hostComponentState : hostComponentStates) {
+              hostComponentState.setUpgradeState(UpgradeState.NONE);
+              hostComponentStateDAO.merge(hostComponentState);
+            }
+          }
         }
       }
 
-      out.append(String.format("Found %d hosts not matching downgrade version: %s-%s\n",
-          badHosts.size(), targetStackId.getStackName(), targetRepositoryVersion.getVersion()));
-
-      for (String badHost : badHosts) {
-        List<HostComponentStateEntity> hostComponentStates = hostComponentStateDAO.findByHost(badHost);
-        for (HostComponentStateEntity hostComponentState : hostComponentStates) {
-          hostComponentState.setUpgradeState(UpgradeState.NONE);
-          hostComponentStateDAO.merge(hostComponentState);
+      // remove any configurations for services which crossed a stack boundary
+      for (String serviceName : servicesInUpgrade) {
+        RepositoryVersionEntity sourceRepositoryVersion = upgradeContext.getSourceRepositoryVersion(serviceName);
+        RepositoryVersionEntity targetRepositoryVersion = upgradeContext.getTargetRepositoryVersion(serviceName);
+        StackId sourceStackId = sourceRepositoryVersion.getStackId();
+        StackId targetStackId = targetRepositoryVersion.getStackId();
+        // only work with configurations when crossing stacks
+        if (!sourceStackId.equals(targetStackId)) {
+          outSB.append(
+              String.format("Removing %s configurations for %s", sourceStackId,
+                  serviceName)).append(System.lineSeparator());
+
+          cluster.removeConfigurations(sourceStackId, serviceName);
         }
       }
 
       // ensure that when downgrading, we set the desired back to the
       // original value
-      cluster.setDesiredStackVersion(currentClusterStackId);
       versionEventPublisher.publish(new StackUpgradeFinishEvent(cluster));
 
       // Reset upgrade state
       cluster.setUpgradeEntity(null);
 
-      return createCommandReport(0, HostRoleStatus.COMPLETED, "{}",
-          out.toString(), err.toString());
+      message = String.format("The downgrade from %s has completed.", downgradeFromVersion);
+      outSB.append(message).append(System.lineSeparator());
 
+      return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", outSB.toString(), errSB.toString());
     } catch (Exception e) {
       StringWriter sw = new StringWriter();
       e.printStackTrace(new PrintWriter(sw));
-      err.append(sw.toString());
+      errSB.append(sw);
 
-      return createCommandReport(-1, HostRoleStatus.FAILED, "{}",
-          out.toString(), err.toString());
+      return createCommandReport(-1, HostRoleStatus.FAILED, "{}", outSB.toString(), errSB.toString());
     }
   }
 
-
   /**
-   * Confirms that all host components that are able to provide hdp version,
-   * have been upgraded to the target version.
-   * @param cluster         the cluster the upgrade is for
-   * @param desiredVersion  the target version of the upgrade
-   * @param targetStackId     the target stack id for meta-info lookup
-   * @return the list of {@link InfoTuple} objects of host components in error
+   * Gets any host components which have not been propertly upgraded or
+   * downgraded.
+   *
+   * @param upgradeContext
+   *          the upgrade context (not {@code null}).
+   * @return a list of {@link InfoTuple} representing components which should
+   *         have been upgraded but did not.
    */
-  protected List<InfoTuple> checkHostComponentVersions(Cluster cluster, String desiredVersion, StackId targetStackId)
-          throws AmbariException {
+  protected Set<InfoTuple> validateComponentVersions(UpgradeContext upgradeContext)
+      throws AmbariException {
 
-    ArrayList<InfoTuple> errors = new ArrayList<>();
+    Set<InfoTuple> errors = new TreeSet<>();
+
+    Cluster cluster = upgradeContext.getCluster();
+    RepositoryVersionEntity repositoryVersionEntity = upgradeContext.getRepositoryVersion();
+    StackId targetStackId = repositoryVersionEntity.getStackId();
+
+    Set<String> servicesParticipating = upgradeContext.getSupportedServices();
+    for (String serviceName : servicesParticipating) {
+      Service service = cluster.getService(serviceName);
+      String targetVersion = upgradeContext.getTargetVersion(serviceName);
 
-    for (Service service : cluster.getServices().values()) {
       for (ServiceComponent serviceComponent : service.getServiceComponents().values()) {
         for (ServiceComponentHost serviceComponentHost : serviceComponent.getServiceComponentHosts().values()) {
           ComponentInfo componentInfo = ambariMetaInfo.getComponent(targetStackId.getStackName(),
                   targetStackId.getStackVersion(), service.getName(), serviceComponent.getName());
 
           if (!componentInfo.isVersionAdvertised()) {
-            StackId desired = serviceComponentHost.getDesiredStackVersion();
-            StackId actual = serviceComponentHost.getStackVersion();
-            if (!desired.equals(actual)) {
-              serviceComponentHost.setStackVersion(desired);
-            }
-          } else if (componentInfo.isVersionAdvertised()
-              && !serviceComponentHost.getVersion().equals(desiredVersion)) {
-            errors.add(new InfoTuple(
-                service.getName(), serviceComponent.getName(),
-                serviceComponentHost.getHostName(), serviceComponentHost.getVersion()));
+            continue;
+          }
+
+          if (!StringUtils.equals(targetVersion, serviceComponentHost.getVersion())) {
+            errors.add(new InfoTuple(service.getName(), serviceComponent.getName(),
+                serviceComponentHost.getHostName(), serviceComponentHost.getVersion(),
+                targetVersion));
           }
         }
       }
     }
 
+
     return errors;
   }
 
-  private void writeComponentHistory(Cluster cluster, UpgradeEntity upgradeEntity,
-      StackId fromStackId, StackId toStackId) {
+  /**
+   * Moves any {@link HostVersionEntity}s which are
+   * {@link RepositoryVersionState#CURRENT} to either
+   * {@link RepositoryVersionState#INSTALLED} or
+   * {@link RepositoryVersionState#NOT_REQUIRED} if their assocaited
+   * repositories are no longer in use.
+   *
+   * @param cluster
+   * @throws AmbariException
+   */
+  private void finalizeHostRepositoryVersions(Cluster cluster) throws AmbariException {
+    // create a set of all of the repos that the services are on
+    Set<RepositoryVersionEntity> desiredRepoVersions = new HashSet<>();
+    Set<String> serviceNames = cluster.getServices().keySet();
+    for (String serviceName : serviceNames) {
+      Service service = cluster.getService(serviceName);
+      desiredRepoVersions.add(service.getDesiredRepositoryVersion());
+    }
 
-    StackEntity fromStack = stackDAO.find(fromStackId.getStackName(), fromStackId.getStackVersion());
-    StackEntity toStack = stackDAO.find(toStackId.getStackName(), toStackId.getStackVersion());
+    // if any CURRENT host version is for a repo which is no longer desired by
+    // ANY service, move it to INSTALLED
+    List<HostVersionEntity> currentHostVersions = hostVersionDAO.findByClusterAndState(
+        cluster.getClusterName(), RepositoryVersionState.CURRENT);
 
-    // for every service component, if it was included in the upgrade then
-    // create a historical entry
-    for (Service service : cluster.getServices().values()) {
-      for (ServiceComponent serviceComponent : service.getServiceComponents().values()) {
-        if (serviceComponent.isVersionAdvertised()) {
-          // create the historical entry
-          ServiceComponentHistoryEntity historyEntity = new ServiceComponentHistoryEntity();
-          historyEntity.setUpgrade(upgradeEntity);
-          historyEntity.setFromStack(fromStack);
-          historyEntity.setToStack(toStack);
-
-          // get the service component
-          ServiceComponentDesiredStateEntity desiredStateEntity = serviceComponentDesiredStateDAO.findByName(
-              cluster.getClusterId(), serviceComponent.getServiceName(),
-              serviceComponent.getName());
-
-          // add the history to the component and save
-          desiredStateEntity.addHistory(historyEntity);
-          serviceComponentDesiredStateDAO.merge(desiredStateEntity);
-        }
+    for (HostVersionEntity hostVersion : currentHostVersions) {
+      RepositoryVersionEntity hostRepoVersion = hostVersion.getRepositoryVersion();
+      if (!desiredRepoVersions.contains(hostRepoVersion)) {
+        hostVersion.setState(RepositoryVersionState.INSTALLED);
+        hostVersion = hostVersionDAO.merge(hostVersion);
       }
     }
   }
 
-  protected static class InfoTuple {
+  protected static class InfoTuple implements Comparable<InfoTuple> {
     protected final String serviceName;
     protected final String componentName;
     protected final String hostName;
     protected final String currentVersion;
+    protected final String targetVersion;
 
-    protected InfoTuple(String service, String component, String host, String version) {
+    protected InfoTuple(String service, String component, String host, String version,
+        String desiredVersion) {
       serviceName = service;
       componentName = component;
       hostName = host;
       currentVersion = version;
+      targetVersion = desiredVersion;
     }
-  }
 
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int compareTo(InfoTuple that) {
+      int compare = hostName.compareTo(that.hostName);
+      if (compare != 0) {
+        return compare;
+      }
+
+      compare = serviceName.compareTo(that.serviceName);
+      if (compare != 0) {
+        return compare;
+      }
+
+      compare = componentName.compareTo(that.componentName);
+      if (compare != 0) {
+        return compare;
+      }
+
+      return compare;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode() {
+      return Objects.hash(hostName, serviceName, componentName, currentVersion, targetVersion);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object object) {
+      if (this == object) {
+        return true;
+      }
+
+      if (object == null || getClass() != object.getClass()) {
+        return false;
+      }
+
+      InfoTuple that = (InfoTuple) object;
+
+      EqualsBuilder equalsBuilder = new EqualsBuilder();
+      equalsBuilder.append(hostName, that.hostName);
+      equalsBuilder.append(serviceName, that.serviceName);
+      equalsBuilder.append(componentName, that.componentName);
+      equalsBuilder.append(currentVersion, that.currentVersion);
+      equalsBuilder.append(targetVersion, that.targetVersion);
+      ;
+      return equalsBuilder.isEquals();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+      return com.google.common.base.Objects.toStringHelper(this)
+          .add("host", hostName)
+          .add("component", componentName)
+          .add("current", currentVersion)
+          .add("target", targetVersion).toString();
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/UpdateDesiredStackAction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/UpdateDesiredStackAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/UpdateDesiredStackAction.java
index 498fb49..ff0becc 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/UpdateDesiredStackAction.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/UpdateDesiredStackAction.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -16,31 +16,35 @@
  * limitations under the License.
  */
 package org.apache.ambari.server.serveraction.upgrades;
-import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.VERSION;
 
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.text.MessageFormat;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentMap;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.agent.CommandReport;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.controller.AmbariServer;
-import org.apache.ambari.server.controller.internal.UpgradeResourceProvider;
+import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.orm.dao.HostVersionDAO;
+import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
-import org.apache.ambari.server.serveraction.AbstractServerAction;
+import org.apache.ambari.server.serveraction.ServerAction;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.StackInfo;
+import org.apache.ambari.server.state.RepositoryType;
+import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.UpgradeContext;
-import org.apache.ambari.server.state.UpgradeContextFactory;
 import org.apache.ambari.server.state.stack.upgrade.Direction;
+import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.inject.Inject;
+import com.google.inject.persist.Transactional;
 
 /**
  * Action that represents updating the Desired Stack Id during the middle of a stack upgrade (typically NonRolling).
@@ -48,15 +52,13 @@ import com.google.inject.Inject;
  * actually changed half-way through calculating the Actions, and this serves to update the database to make it
  * evident to the user at which point it changed.
  */
-public class UpdateDesiredStackAction extends AbstractServerAction {
+public class UpdateDesiredStackAction extends AbstractUpgradeServerAction {
 
   /**
    * Logger.
    */
   private static final Logger LOG = LoggerFactory.getLogger(UpdateDesiredStackAction.class);
 
-  public static final String COMMAND_PARAM_VERSION = VERSION;
-  public static final String COMMAND_PARAM_UPGRADE_PACK = "upgrade_pack";
 
   /**
    * The Cluster that this ServerAction implementation is executing on.
@@ -64,105 +66,122 @@ public class UpdateDesiredStackAction extends AbstractServerAction {
   @Inject
   private Clusters clusters;
 
+  /**
+   * The Ambari configuration.
+   */
   @Inject
-  private AmbariMetaInfo ambariMetaInfo;
+  private Configuration m_configuration;
 
   /**
-   * Used for building {@link UpgradeContext} instances.
+   * Used for restting host version states on downgrade.
    */
   @Inject
-  UpgradeContextFactory m_upgradeContextFactory;
+  private HostVersionDAO m_hostVersionDAO;
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
   public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext)
       throws AmbariException, InterruptedException {
+
     String clusterName = getExecutionCommand().getClusterName();
     Cluster cluster = clusters.getCluster(clusterName);
-    UpgradeContext context = m_upgradeContextFactory.create(cluster,
-        cluster.getUpgradeInProgress());
+    UpgradeContext upgradeContext = getUpgradeContext(cluster);
 
+    Map<String, String> roleParams = getExecutionCommand().getRoleParams();
 
-    CommandReport commandReport = updateDesiredStack(cluster, context);
-
-    // invalidate any cached effective ID
-    cluster.invalidateUpgradeEffectiveVersion();
+    // Make a best attempt at setting the username
+    String userName;
+    if (roleParams != null && roleParams.containsKey(ServerAction.ACTION_USER_NAME)) {
+      userName = roleParams.get(ServerAction.ACTION_USER_NAME);
+    } else {
+      userName = m_configuration.getAnonymousAuditName();
+      LOG.warn(String.format("Did not receive role parameter %s, will save configs using anonymous username %s", ServerAction.ACTION_USER_NAME, userName));
+    }
 
-    return commandReport;
+    return updateDesiredRepositoryVersion(cluster, upgradeContext, userName);
   }
 
   /**
-   * Set the cluster's Desired Stack Id during an upgrade.
+   * Sets the desired repository version for services participating in the
+   * upgrade.
    *
-   * @param context
-   *          the upgrade context (not {@code null}).
+   * @param cluster
+   *          the cluster
+   * @param upgradeContext
+   *          the upgrade context
+   * @param userName
+   *          username performing the action
    * @return the command report to return
    */
-  private CommandReport updateDesiredStack(Cluster cluster, UpgradeContext context)
+  @Transactional
+  CommandReport updateDesiredRepositoryVersion(
+      Cluster cluster, UpgradeContext upgradeContext, String userName)
       throws AmbariException, InterruptedException {
 
-    Direction direction = context.getDirection();
-    RepositoryVersionEntity fromRepositoryVersion = context.getSourceRepositoryVersion();
-    RepositoryVersionEntity toRepositoryVersion = context.getTargetRepositoryVersion();
-
-    String clusterName = cluster.getClusterName();
     StringBuilder out = new StringBuilder();
     StringBuilder err = new StringBuilder();
 
     try {
-      StackId clusterDesiredStackId = cluster.getDesiredStackVersion();
-      StackId currentClusterStackId = cluster.getCurrentStackVersion();
-      StackId targetStackId = toRepositoryVersion.getStackId();
-
-      out.append(String.format("%s %s from %s-%s to %s-%s\n",
-          direction.getVerb(true), clusterName,
-          fromRepositoryVersion.getStackId().getStackName(),
-          fromRepositoryVersion.getVersion(),
-          targetStackId.getStackName(),
-          toRepositoryVersion.getVersion()));
-
-      // Ensure that the target stack id exist
-      StackInfo desiredClusterStackInfo = ambariMetaInfo.getStack(targetStackId.getStackName(), targetStackId.getStackVersion());
-      if (null == desiredClusterStackInfo) {
-        String message = String.format("Invalid target stack of \n", targetStackId.getStackId());
-        err.append(message);
-        out.append(message);
-        return createCommandReport(-1, HostRoleStatus.FAILED, "{}", out.toString(), err.toString());
+      // the desired repository message to put in the command report - this will
+      // change based on the type of upgrade and the services participating
+      if (upgradeContext.getDirection() == Direction.UPGRADE) {
+        final String message;
+        RepositoryVersionEntity targetRepositoryVersion = upgradeContext.getRepositoryVersion();
+
+        if (upgradeContext.getOrchestrationType() == RepositoryType.STANDARD) {
+          message = MessageFormat.format(
+              "Updating the desired repository version to {0} for all cluster services.",
+              targetRepositoryVersion.getVersion());
+        } else {
+          Set<String> servicesInUpgrade = upgradeContext.getSupportedServices();
+          message = MessageFormat.format(
+              "Updating the desired repository version to {0} for the following services: {1}",
+              targetRepositoryVersion.getVersion(), StringUtils.join(servicesInUpgrade, ','));
+        }
+
+        out.append(message).append(System.lineSeparator());
       }
 
-      // Ensure that the current Stack Id coincides with the parameter that the user passed in.
-      if (direction == Direction.DOWNGRADE && !currentClusterStackId.equals(targetStackId)) {
-        String message = String.format(
-            "The cluster's current stack of %s doesn't match %s which is target stack of this downgrade",
-            currentClusterStackId, targetStackId);
+      if( upgradeContext.getDirection() == Direction.DOWNGRADE ){
+        String message = "Updating the desired repository back their original values for the following services:";
+        out.append(message).append(System.lineSeparator());
 
-        err.append(message);
-        out.append(message);
-        return createCommandReport(-1, HostRoleStatus.FAILED, "{}", out.toString(), err.toString());
+        Map<String, RepositoryVersionEntity> targetVersionsByService = upgradeContext.getTargetVersions();
+        for (String serviceName : targetVersionsByService.keySet()) {
+          RepositoryVersionEntity repositoryVersion = targetVersionsByService.get(serviceName);
+
+          message = String.format("  %s to %s", serviceName, repositoryVersion.getVersion());
+          out.append(message).append(System.lineSeparator());
+        }
       }
 
-      // Check for a no-op
-      if (clusterDesiredStackId.equals(targetStackId)) {
-        String message = String.format("The cluster's desired stack is already set to %s\n",
-            targetStackId.getStackId());
+      // move repositories to the right version and create/revert configs
+      m_upgradeHelper.updateDesiredRepositoriesAndConfigs(upgradeContext);
 
-        out.append(message);
-        return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", out.toString(), err.toString());
-      }
+      // a downgrade must force host versions back to INSTALLED for the
+      // repository which failed to be upgraded.
+      if (upgradeContext.getDirection() == Direction.DOWNGRADE) {
+        RepositoryVersionEntity downgradeFromRepositoryVersion = upgradeContext.getRepositoryVersion();
+        out.append(String.format("Setting host versions back to %s for repository version %s",
+            RepositoryVersionState.INSTALLED, downgradeFromRepositoryVersion.getVersion()));
 
-      // Create Create new configurations that are a merge between the current stack and the desired stack
-      // Also updates the desired stack version.
-      UpgradeResourceProvider upgradeResourceProvider = new UpgradeResourceProvider(AmbariServer.getController());
-      upgradeResourceProvider.applyStackAndProcessConfigurations(context);
-      String message = String.format("The cluster's desired stack was set to %s.\n",
-          targetStackId.getStackId());
+        List<HostVersionEntity> hostVersionsToReset = m_hostVersionDAO.findHostVersionByClusterAndRepository(
+            cluster.getClusterId(), downgradeFromRepositoryVersion);
 
-      out.append(message);
+        for (HostVersionEntity hostVersion : hostVersionsToReset) {
+          if( hostVersion.getState() != RepositoryVersionState.NOT_REQUIRED ){
+            hostVersion.setState(RepositoryVersionState.INSTALLED);
+          }
+        }
+      }
 
       return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", out.toString(), err.toString());
     } catch (Exception e) {
       StringWriter sw = new StringWriter();
       e.printStackTrace(new PrintWriter(sw));
-      err.append(sw.toString());
+      err.append(sw);
 
       return createCommandReport(-1, HostRoleStatus.FAILED, "{}", out.toString(), err.toString());
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/stack/MasterHostResolver.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/stack/MasterHostResolver.java b/ambari-server/src/main/java/org/apache/ambari/server/stack/MasterHostResolver.java
index f5eb007..fc657c1 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/stack/MasterHostResolver.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/stack/MasterHostResolver.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -30,13 +30,16 @@ import java.util.Map;
 import java.util.Set;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.ConfigHelper;
 import org.apache.ambari.server.state.Host;
 import org.apache.ambari.server.state.MaintenanceState;
 import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentHost;
+import org.apache.ambari.server.state.UpgradeContext;
 import org.apache.ambari.server.state.UpgradeState;
+import org.apache.ambari.server.state.stack.upgrade.Direction;
 import org.apache.ambari.server.utils.HTTPUtils;
 import org.apache.ambari.server.utils.HostAndPort;
 import org.apache.ambari.server.utils.StageUtils;
@@ -49,11 +52,11 @@ import com.google.common.reflect.TypeToken;
 
 public class MasterHostResolver {
 
-  private static Logger LOG = LoggerFactory.getLogger(MasterHostResolver.class);
+  private static final Logger LOG = LoggerFactory.getLogger(MasterHostResolver.class);
 
-  private Cluster m_cluster;
-  private String m_version;
-  private ConfigHelper m_configHelper;
+  private final UpgradeContext m_upgradeContext;
+  private final Cluster m_cluster;
+  private final ConfigHelper m_configHelper;
 
   public enum Service {
     HDFS,
@@ -71,29 +74,17 @@ public class MasterHostResolver {
   }
 
   /**
-   * Create a resolver that does not consider HostComponents' version when
-   * resolving hosts.  Common use case is creating an upgrade that should
-   * include an entire cluster.
-   * @param configHelper Configuration Helper
-   * @param cluster the cluster
-   */
-  public MasterHostResolver(ConfigHelper configHelper, Cluster cluster) {
-    this(configHelper, cluster, null);
-  }
-
-  /**
-   * Create a resolver that compares HostComponents' version when calculating
-   * hosts for the stage.  Common use case is for downgrades when only some
-   * HostComponents need to be downgraded, and HostComponents already at the
-   * correct version are skipped.
-   * @param configHelper Configuration Helper
-   * @param cluster the cluster
-   * @param version the version, or {@code null} to not compare versions
+   * Constructor.
+   *
+   * @param configHelper
+   *          Configuration Helper
+   * @param upgradeContext
+   *          the upgrade context
    */
-  public MasterHostResolver(ConfigHelper configHelper, Cluster cluster, String version) {
+  public MasterHostResolver(Cluster cluster, ConfigHelper configHelper, UpgradeContext upgradeContext) {
     m_configHelper = configHelper;
+    m_upgradeContext = upgradeContext;
     m_cluster = cluster;
-    m_version = version;
   }
 
   /**
@@ -141,7 +132,7 @@ public class MasterHostResolver {
               return filterHosts(hostsType, serviceName, componentName);
             }
 
-            Map<Status, String> pair = getNameNodePair();
+            Map<Status, String> pair = getNameNodePair(componentHosts);
             if (pair != null) {
               hostsType.master = pair.containsKey(Status.ACTIVE) ? pair.get(Status.ACTIVE) :  null;
               hostsType.secondary = pair.containsKey(Status.STANDBY) ? pair.get(Status.STANDBY) :  null;
@@ -203,8 +194,8 @@ public class MasterHostResolver {
       ServiceComponent sc = svc.getServiceComponent(component);
 
       // !!! not really a fan of passing these around
-      List<ServiceComponentHost> unhealthyHosts = new ArrayList<ServiceComponentHost>();
-      LinkedHashSet<String> upgradeHosts = new LinkedHashSet<String>();
+      List<ServiceComponentHost> unhealthyHosts = new ArrayList<>();
+      LinkedHashSet<String> upgradeHosts = new LinkedHashSet<>();
 
       for (String hostName : hostsType.hosts) {
         ServiceComponentHost sch = sc.getServiceComponentHost(hostName);
@@ -216,10 +207,29 @@ public class MasterHostResolver {
         // possible
         if (maintenanceState != MaintenanceState.OFF) {
           unhealthyHosts.add(sch);
-        } else if (null == m_version || null == sch.getVersion() ||
-            !sch.getVersion().equals(m_version) ||
-            sch.getUpgradeState() == UpgradeState.FAILED) {
+          continue;
+        }
+
+        if (sch.getUpgradeState() == UpgradeState.FAILED) {
           upgradeHosts.add(hostName);
+          continue;
+        }
+
+        if(m_upgradeContext.getDirection() == Direction.UPGRADE){
+          RepositoryVersionEntity targetRepositoryVersion = m_upgradeContext.getRepositoryVersion();
+          if (!StringUtils.equals(targetRepositoryVersion.getVersion(), sch.getVersion())) {
+            upgradeHosts.add(hostName);
+          }
+
+          continue;
+        }
+
+        // it's a downgrade ...
+        RepositoryVersionEntity downgradeToRepositoryVersion = m_upgradeContext.getTargetRepositoryVersion(service);
+        String downgradeToVersion = downgradeToRepositoryVersion.getVersion();
+        if (!StringUtils.equals(downgradeToVersion, sch.getVersion())) {
+          upgradeHosts.add(hostName);
+          continue;
         }
       }
 
@@ -263,8 +273,8 @@ public class MasterHostResolver {
    * one active and one standby host were found, otherwise, return null.
    * The hostnames are returned in lowercase.
    */
-  private Map<Status, String> getNameNodePair() {
-    Map<Status, String> stateToHost = new HashMap<Status, String>();
+  private Map<Status, String> getNameNodePair(Set<String> componentHosts) throws AmbariException {
+    Map<Status, String> stateToHost = new HashMap<>();
     Cluster cluster = getCluster();
 
     String nameService = m_configHelper.getValueFromDesiredConfigurations(cluster, ConfigHelper.HDFS_SITE, "dfs.internal.nameservices");
@@ -297,6 +307,13 @@ public class MasterHostResolver {
           throw new MalformedURLException("Could not parse host and port from " + value);
         }
 
+        if (!componentHosts.contains(hp.host)){
+          //This may happen when NN HA is configured on dual network card machines with public/private FQDNs.
+          LOG.error(
+              String.format(
+                  "Hadoop NameNode HA configuration {0} contains host {1} that does not exist in the NameNode hosts list {3}",
+                  key, hp.host, componentHosts.toString()));
+        }
         String state = queryJmxBeanValue(hp.host, hp.port, "Hadoop:service=NameNode,name=NameNodeStatus", "State", true, encrypted);
 
         if (null != state && (state.equalsIgnoreCase(Status.ACTIVE.toString()) || state.equalsIgnoreCase(Status.STANDBY.toString()))) {
@@ -323,7 +340,7 @@ public class MasterHostResolver {
    * @throws MalformedURLException
    */
   private void resolveResourceManagers(Cluster cluster, HostsType hostType) throws MalformedURLException {
-    LinkedHashSet<String> orderedHosts = new LinkedHashSet<String>(hostType.hosts);
+    LinkedHashSet<String> orderedHosts = new LinkedHashSet<>(hostType.hosts);
 
     // IMPORTANT, for RM, only the master returns jmx
     String rmWebAppAddress = m_configHelper.getValueFromDesiredConfigurations(cluster, ConfigHelper.YARN_SITE, "yarn.resourcemanager.webapp.address");

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/stack/RepoUtil.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/stack/RepoUtil.java b/ambari-server/src/main/java/org/apache/ambari/server/stack/RepoUtil.java
index 3f17fd2..d43bdfa 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/stack/RepoUtil.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/stack/RepoUtil.java
@@ -1,4 +1,4 @@
-  /**
+/*
  * 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
@@ -32,6 +32,7 @@ import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
 import org.apache.ambari.server.orm.entities.RepositoryEntity;
 import org.apache.ambari.server.state.RepositoryInfo;
 import org.apache.ambari.server.state.stack.RepositoryXml;
+import org.apache.commons.collections.CollectionUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -114,21 +115,23 @@ public class RepoUtil {
    *    service repository and will be added.
    * @param operatingSystems - A list of OperatingSystemEntity objects extracted from a RepositoryVersionEntity
    * @param stackReposByOs - Stack repositories loaded from the disk (including service repositories), grouped by os.
+   * @return {@code true} if there were added repositories
    */
-  public static void addServiceReposToOperatingSystemEntities(List<OperatingSystemEntity> operatingSystems,
+  public static boolean addServiceReposToOperatingSystemEntities(List<OperatingSystemEntity> operatingSystems,
       ListMultimap<String, RepositoryInfo> stackReposByOs) {
     Set<String> addedRepos = new HashSet<>();
     for (OperatingSystemEntity os : operatingSystems) {
       List<RepositoryInfo> serviceReposForOs = stackReposByOs.get(os.getOsType());
       ImmutableSet<String> repoNames = ImmutableSet.copyOf(Lists.transform(os.getRepositories(), REPO_ENTITY_TO_NAME));
-      for (RepositoryInfo repoInfo : serviceReposForOs) {
+      for (RepositoryInfo repoInfo : serviceReposForOs)
         if (!repoNames.contains(repoInfo.getRepoName())) {
           os.getRepositories().add(toRepositoryEntity(repoInfo));
           addedRepos.add(String.format("%s (%s)", repoInfo.getRepoId(), os.getOsType()));
         }
-      }
     }
     LOG.info("Added {} service repos: {}", addedRepos.size(),Iterables.toString(addedRepos));
+
+    return CollectionUtils.isNotEmpty(addedRepos);
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartup.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartup.java b/ambari-server/src/main/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartup.java
index 8a32a42..b7064df 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartup.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartup.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,25 +19,22 @@
 package org.apache.ambari.server.stack;
 
 import java.util.List;
-import javax.annotation.Nullable;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
+import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
 import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
-import org.apache.ambari.server.orm.entities.RepositoryEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.RepositoryInfo;
+import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.StackInfo;
 import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Function;
 import com.google.common.collect.ListMultimap;
 import com.google.inject.Inject;
 import com.google.inject.persist.Transactional;
@@ -54,26 +51,17 @@ public class UpdateActiveRepoVersionOnStartup {
 
   private static final Logger LOG = LoggerFactory.getLogger(UpdateActiveRepoVersionOnStartup.class);
 
-
   ClusterDAO clusterDao;
-  ClusterVersionDAO clusterVersionDao;
   RepositoryVersionDAO repositoryVersionDao;
   RepositoryVersionHelper repositoryVersionHelper;
   StackManager stackManager;
 
-
-  private static final Function<RepositoryEntity, String> REPO_TO_ID = new Function<RepositoryEntity, String>() {
-    @Override  public String apply(@Nullable RepositoryEntity input) { return input.getRepositoryId(); }
-  };
-
   @Inject
   public UpdateActiveRepoVersionOnStartup(ClusterDAO clusterDao,
-      ClusterVersionDAO clusterVersionDao,
       RepositoryVersionDAO repositoryVersionDao,
       RepositoryVersionHelper repositoryVersionHelper,
       AmbariMetaInfo metaInfo) {
     this.clusterDao = clusterDao;
-    this.clusterVersionDao = clusterVersionDao;
     this.repositoryVersionDao = repositoryVersionDao;
     this.repositoryVersionHelper = repositoryVersionHelper;
     this.stackManager = metaInfo.getStackManager();
@@ -86,21 +74,20 @@ public class UpdateActiveRepoVersionOnStartup {
   @Transactional
   public void process() throws AmbariException {
     LOG.info("Updating existing repo versions with service repos.");
+
     try {
+
       List<ClusterEntity> clusters = clusterDao.findAll();
       for (ClusterEntity cluster: clusters) {
-        StackInfo stack =
-            stackManager.getStack(cluster.getDesiredStack().getStackName(), cluster.getDesiredStack().getStackVersion());
-        LOG.info("Updating existing repo versions for cluster {} on stack {}-{}",
-            cluster.getClusterName(), stack.getName(), stack.getVersion());
-        ClusterVersionEntity clusterVersion = clusterVersionDao.findByClusterAndStateCurrent(cluster.getClusterName());
-        if (null != clusterVersion) {
-          RepositoryVersionEntity repoVersion = clusterVersion.getRepositoryVersion();
-          updateRepoVersion(stack, repoVersion);
-          repositoryVersionDao.merge(repoVersion);
-        }
-        else {
-          LOG.warn("Missing cluster version for cluster {}", cluster.getClusterName());
+        for (ClusterServiceEntity service : cluster.getClusterServiceEntities()) {
+          RepositoryVersionEntity repositoryVersion = service.getServiceDesiredStateEntity().getDesiredRepositoryVersion();
+
+          StackId stackId = repositoryVersion.getStackId();
+          StackInfo stack = stackManager.getStack(stackId.getStackName(), stackId.getStackVersion());
+
+          if (updateRepoVersion(stack, repositoryVersion)) {
+            repositoryVersionDao.merge(repositoryVersion);
+          }
         }
       }
     }
@@ -111,13 +98,16 @@ public class UpdateActiveRepoVersionOnStartup {
     }
   }
 
-  private void updateRepoVersion(StackInfo stackInfo, RepositoryVersionEntity repoVersion) throws Exception {
+  private boolean updateRepoVersion(StackInfo stackInfo, RepositoryVersionEntity repoVersion) throws Exception {
     ListMultimap<String, RepositoryInfo> serviceReposByOs = stackInfo.getRepositoriesByOs();
 
     // Update repos in the JSON representation
     List<OperatingSystemEntity> operatingSystems = repoVersion.getOperatingSystems();
-    RepoUtil.addServiceReposToOperatingSystemEntities(operatingSystems, serviceReposByOs);
-    repoVersion.setOperatingSystems(repositoryVersionHelper.serializeOperatingSystemEntities(operatingSystems));
+    boolean changed = RepoUtil.addServiceReposToOperatingSystemEntities(operatingSystems, serviceReposByOs);
+    if (changed) {
+      repoVersion.setOperatingSystems(repositoryVersionHelper.serializeOperatingSystemEntities(operatingSystems));
+    }
+    return changed;
   }
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java b/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
index 9dc58fe..b4f7120 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -28,14 +28,12 @@ import org.apache.ambari.server.controller.ClusterResponse;
 import org.apache.ambari.server.controller.ServiceConfigVersionResponse;
 import org.apache.ambari.server.events.ClusterConfigChangedEvent;
 import org.apache.ambari.server.metadata.RoleCommandOrder;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
-import org.apache.ambari.server.orm.entities.HostEntity;
-import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.PrivilegeEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.state.configgroup.ConfigGroup;
+import org.apache.ambari.server.state.repository.VersionDefinitionXml;
 import org.apache.ambari.server.state.scheduler.RequestExecution;
 
 import com.google.common.collect.ListMultimap;
@@ -76,6 +74,14 @@ public interface Cluster {
   Service getService(String serviceName) throws AmbariException;
 
   /**
+   * Gets a service from the given component name.
+   * @param componentName
+   * @return
+   * @throws AmbariException
+   */
+  Service getServiceByComponentName(String componentName) throws AmbariException;
+
+  /**
    * Get all services
    * @return
    */
@@ -151,49 +157,6 @@ public interface Cluster {
   void removeServiceComponentHost(ServiceComponentHost svcCompHost)
       throws AmbariException;
 
-
-  /**
-   * Get the ClusterVersionEntity object whose state is CURRENT.
-   * @return Cluster Version entity to whose state is CURRENT.
-   */
-  ClusterVersionEntity getCurrentClusterVersion();
-
-  /**
-   * Gets the current stack version associated with the cluster.
-   * <ul>
-   * <li>if there is no upgrade in progress then get the
-   * {@link ClusterVersionEntity} object whose state is
-   * {@link RepositoryVersionState#CURRENT}.
-   * <li>If an upgrade is in progress then based on the direction and the
-   * desired stack determine which version to use. Assuming upgrading from HDP
-   * 2.2.0.0-1 to 2.3.0.0-2:
-   * <ul>
-   * <li>RU Upgrade: 2.3.0.0-2 (desired stack id)
-   * <li>RU Downgrade: 2.2.0.0-1 (desired stack id)
-   * <li>EU Upgrade: while stopping services and before changing desired stack,
-   * use 2.2.0.0-1, after, use 2.3.0.0-2
-   * <li>EU Downgrade: while stopping services and before changing desired
-   * stack, use 2.3.0.0-2, after, use 2.2.0.0-1
-   * </ul>
-   * </ul>
-   *
-   * This method must take into account both a running and a suspended upgrade.
-   *
-   * @return the effective cluster stack version given the current upgrading
-   *         conditions of the cluster.
-   * 
-   * @deprecated to be removed once the cluster tracks the desired repo instead
-   *             of desired stack id
-   */
-  @Deprecated
-  ClusterVersionEntity getEffectiveClusterVersion() throws AmbariException;
-
-  /**
-   * Get all of the ClusterVersionEntity objects for the cluster.
-   * @return
-   */
-  Collection<ClusterVersionEntity> getAllClusterVersions();
-
   /**
    * Get desired stack version
    * @return
@@ -207,15 +170,6 @@ public interface Cluster {
   void setDesiredStackVersion(StackId stackVersion) throws AmbariException;
 
   /**
-   * Sets the desired stack version, optionally setting all owned services,
-   * components, and host components
-   * @param stackId the stack id
-   * @param cascade {@code true} to cascade the desired version
-   */
-  public void setDesiredStackVersion(StackId stackId, boolean cascade) throws AmbariException;
-
-
-  /**
    * Get current stack version
    * @return
    */
@@ -228,99 +182,33 @@ public interface Cluster {
   void setCurrentStackVersion(StackId stackVersion) throws AmbariException;
 
   /**
-   * Create host versions for all of the hosts that don't already have the stack version.
-   * @param hostNames Collection of host names
-   * @param currentClusterVersion Entity that contains the cluster's current stack (with its name and version)
-   * @param desiredState Desired state must be {@link RepositoryVersionState#CURRENT} or {@link RepositoryVersionState#UPGRADING}
-   * @throws AmbariException
-   */
-  void mapHostVersions(Set<String> hostNames,
-      ClusterVersionEntity currentClusterVersion,
-      RepositoryVersionState desiredState) throws AmbariException;
-
-  /**
    * Creates or updates host versions for all of the hosts within a cluster
    * based on state of cluster stack version. This is used to transition all
-   * hosts into the specified state.
+   * hosts into the correct state (which may not be
+   * {@link RepositoryVersionState#INSTALLING}).
    * <p/>
-   * The difference between this method compared to
-   * {@link Cluster#mapHostVersions} is that it affects all hosts (not only
-   * missing hosts).
-   * <p/>
-   * Hosts that are in maintenance mode will be transititioned directly into
-   * {@link RepositoryVersionState#OUT_OF_SYNC} instead.
+   * Hosts that are in maintenance mode will be transitioned directly into
+   * {@link RepositoryVersionState#OUT_OF_SYNC} instead. Hosts which do not need
+   * the version distributed to them will move into the
+   * {@link RepositoryVersionState#NOT_REQUIRED} state.
    *
-   * @param sourceClusterVersion
-   *          cluster version to be queried for a stack name/version info and
-   *          desired RepositoryVersionState. The only valid state of a cluster
-   *          version is {@link RepositoryVersionState#INSTALLING}
-   * @param state
-   *          the state to transition the cluster's hosts to.
-   * @throws AmbariException
-   */
-  void transitionHosts(ClusterVersionEntity sourceClusterVersion, RepositoryVersionState state)
-      throws AmbariException;
-
-  /**
-   * For a given host, will either either update an existing Host Version Entity for the given version, or create
-   * one if it doesn't exist
-   *
-   * @param host Host Entity object
-   * @param repositoryVersion Repository Version that the host is transitioning to
-   * @param stack Stack information with the version
-   * @return Returns either the newly created or the updated Host Version Entity.
-   * @throws AmbariException
-   */
-  HostVersionEntity transitionHostVersionState(HostEntity host,
-      final RepositoryVersionEntity repositoryVersion, final StackId stack)
-      throws AmbariException;
-
-  /**
-   * Update state of a cluster stack version for cluster based on states of host versions and stackids.
-   * @param repositoryVersion the repository version entity whose version is a value like 2.2.1.0-100)
-   * @throws AmbariException
-   */
-  void recalculateClusterVersionState(RepositoryVersionEntity repositoryVersion) throws AmbariException;
-
-  /**
-   * Update state of all cluster stack versions for cluster based on states of host versions.
-   * @throws AmbariException
-   */
-  void recalculateAllClusterVersionStates() throws AmbariException;
-
-  /**
-   * Create a cluster version for the given stack and version, whose initial
-   * state must either be either {@link RepositoryVersionState#UPGRADING} (if no
-   * other cluster version exists) or {@link RepositoryVersionState#INSTALLING}
-   * (if at exactly one CURRENT cluster version already exists) or {@link RepositoryVersionState#INIT}
-   * (if the cluster is being created using a specific repository version).
-   *
-   * @param stackId
-   *          Stack ID
-   * @param version
-   *          Stack version
-   * @param userName
-   *          User performing the operation
-   * @param state
-   *          Initial state
-   * @throws AmbariException
-   */
-  void createClusterVersion(StackId stackId, String version,
-      String userName, RepositoryVersionState state) throws AmbariException;
-
-  /**
-   * Transition an existing cluster version from one state to another.
-   *
-   * @param stackId
-   *          Stack ID
-   * @param version
-   *          Stack version
-   * @param state
-   *          Desired state
+   * @param repoVersionEntity
+   *          the repository that the hosts are being transitioned for (not
+   *          {@code null}).
+   * @param versionDefinitionXml
+   *          the VDF, or {@code null} if none.
+   * @param forceInstalled
+   *          if {@code true}, then this will transition everything directly to
+   *          {@link RepositoryVersionState#INSTALLED} instead of
+   *          {@link RepositoryVersionState#INSTALLING}. Hosts which should
+   *          received other states (like
+   *          {@link RepositoryVersionState#NOT_REQUIRED} will continue to
+   *          receive those states.
+   * @return a list of hosts which need the repository installed.
    * @throws AmbariException
    */
-  void transitionClusterVersion(StackId stackId, String version,
-      RepositoryVersionState state) throws AmbariException;
+  List<Host> transitionHostsToInstalling(RepositoryVersionEntity repoVersionEntity,
+      VersionDefinitionXml versionDefinitionXml, boolean forceInstalled) throws AmbariException;
 
   /**
    * Gets whether the cluster is still initializing or has finished with its
@@ -449,7 +337,7 @@ public interface Cluster {
    * @param serviceName service name
    * @return
    */
-  public List<ServiceConfigVersionResponse> getActiveServiceConfigVersionResponse(String serviceName);
+  List<ServiceConfigVersionResponse> getActiveServiceConfigVersionResponse(String serviceName);
 
   /**
    * Get service config version history
@@ -529,11 +417,17 @@ public interface Cluster {
 
   /**
    * Add service to the cluster
+   *
    * @param serviceName
+   *          the name of the service to add (not {@code null}).
+   * @param repositoryVersion
+   *          the repository from which the service should be installed (not
+   *          {@code null}).
    * @return
    * @throws AmbariException
    */
-  Service addService(String serviceName) throws AmbariException;
+  Service addService(String serviceName, RepositoryVersionEntity repositoryVersion)
+      throws AmbariException;
 
   /**
    * Fetch desired configs for list of hosts in cluster
@@ -654,9 +548,8 @@ public interface Cluster {
   Map<String, Object> getSessionAttributes();
 
   /**
-   * Makes the most recent configurations in the specified stack the current set
-   * of configurations. This method will first ensure that the cluster's current
-   * stack matches that of the configuration stack specified.
+   * Makes the most recent configurations for the specified stack current. This
+   * will only modify configurations for the given service.
    * <p/>
    * When completed, all other configurations for any other stack will remain,
    * but will not be marked as selected.
@@ -664,18 +557,21 @@ public interface Cluster {
    * @param stackId
    *          the stack to use when finding the latest configurations (not
    *          {@code null}).
+   * @param serviceName
+   *          the service to modify configurations for (not {@code null}).
    */
-  void applyLatestConfigurations(StackId stackId);
+  void applyLatestConfigurations(StackId stackId, String serviceName);
 
   /**
-   * Removes all cluster configurations and service configurations that belong
-   * to the specified stack.
+   * Removes all configurations for the specified service and stack.
    *
    * @param stackId
    *          the stack to use when finding the configurations to remove (not
    *          {@code null}).
+   * @param serviceName
+   *          the service to rmeove configurations for (not {@code null}).
    */
-  void removeConfigurations(StackId stackId);
+  void removeConfigurations(StackId stackId, String serviceName);
 
   /**
    * Returns whether this cluster was provisioned by a Blueprint or not.
@@ -707,8 +603,7 @@ public interface Cluster {
    * Gets whether there is an upgrade which has been suspended and not yet
    * finalized.
    *
-   * @return {@code true} if the last upgrade is in the
-   *         {@link UpgradeState#SUSPENDED}.
+   * @return {@code true} if the last upgrade is suspended
    */
   boolean isUpgradeSuspended();
 
@@ -766,11 +661,4 @@ public interface Cluster {
    */
   void addSuspendedUpgradeParameters(Map<String, String> commandParams,
       Map<String, String> roleParams);
-
-  /**
-   * Invalidates any cached effective cluster versions for upgrades.
-   *
-   * @see #getEffectiveClusterVersion()
-   */
-  void invalidateUpgradeEffectiveVersion();
 }


[35/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/KerberosHelperImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/KerberosHelperImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/KerberosHelperImpl.java
index 9c65aa6..3097a07 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/KerberosHelperImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/KerberosHelperImpl.java
@@ -299,8 +299,8 @@ public class KerberosHelperImpl implements KerberosHelper {
   public void configureServices(Cluster cluster, Map<String, Collection<String>> serviceFilter)
       throws AmbariException, KerberosInvalidConfigurationException {
     Map<String, Map<String, String>> existingConfigurations = calculateExistingConfigurations(cluster, null);
-    Map<String, Set<String>> installedServices = new HashMap<String, Set<String>>();
-    Set<String> previouslyExistingServices = new HashSet<String>();
+    Map<String, Set<String>> installedServices = new HashMap<>();
+    Set<String> previouslyExistingServices = new HashSet<>();
 
     // Calculate the map of installed services to installed components
     Map<String, Service> clusterServices = cluster.getServices();
@@ -308,7 +308,7 @@ public class KerberosHelperImpl implements KerberosHelper {
       for (Service clusterService : clusterServices.values()) {
         Set<String> installedComponents = installedServices.get(clusterService.getName());
         if (installedComponents == null) {
-          installedComponents = new HashSet<String>();
+          installedComponents = new HashSet<>();
           installedServices.put(clusterService.getName(), installedComponents);
         }
 
@@ -338,7 +338,7 @@ public class KerberosHelperImpl implements KerberosHelper {
         existingConfigurations, installedServices, serviceFilter, previouslyExistingServices, true, true);
 
     for (Map.Entry<String, Map<String, String>> entry : updates.entrySet()) {
-      configHelper.updateConfigType(cluster, ambariManagementController, entry.getKey(), entry.getValue(), null,
+      configHelper.updateConfigType(cluster, cluster.getDesiredStackVersion(), ambariManagementController, entry.getKey(), entry.getValue(), null,
           ambariManagementController.getAuthName(), "Enabling Kerberos for added components");
     }
   }
@@ -353,7 +353,7 @@ public class KerberosHelperImpl implements KerberosHelper {
                                                                          boolean applyStackAdvisorUpdates)
       throws KerberosInvalidConfigurationException, AmbariException {
 
-    Map<String, Map<String, String>> kerberosConfigurations = new HashMap<String, Map<String, String>>();
+    Map<String, Map<String, String>> kerberosConfigurations = new HashMap<>();
     KerberosDetails kerberosDetails = getKerberosDetails(cluster, null);
     KerberosDescriptor kerberosDescriptor = getKerberosDescriptor(cluster);
 
@@ -361,17 +361,17 @@ public class KerberosHelperImpl implements KerberosHelper {
     Map<String, Map<String, String>> configurations = addAdditionalConfigurations(cluster,
         deepCopy(existingConfigurations), null, kerberosDescriptorProperties);
 
-    Map<String, Set<String>> propertiesToIgnore = new HashMap<String, Set<String>>();
+    Map<String, Set<String>> propertiesToIgnore = new HashMap<>();
 
     // If Ambari is managing it own identities then add AMBARI to the set of installed servcie so
     // that its Kerberos descriptor entries will be included.
     if (createAmbariIdentities(existingConfigurations.get("kerberos-env"))) {
-      installedServices = new HashMap<String, Set<String>>(installedServices);
+      installedServices = new HashMap<>(installedServices);
       installedServices.put("AMBARI", Collections.singleton("AMBARI_SERVER"));
     }
 
     // Create the context to use for filtering Kerberos Identities based on the state of the cluster
-    Map<String, Object> filterContext = new HashMap<String, Object>();
+    Map<String, Object> filterContext = new HashMap<>();
     filterContext.put("configurations", configurations);
     filterContext.put("services", installedServices.keySet());
 
@@ -459,7 +459,7 @@ public class KerberosHelperImpl implements KerberosHelper {
         if ((properties != null) && !properties.isEmpty()) {
           Set<String> propertyNames = propertiesToIgnore.get(configType);
           if (propertyNames == null) {
-            propertyNames = new HashSet<String>();
+            propertyNames = new HashSet<>();
             propertiesToIgnore.put(configType, propertyNames);
           }
           propertyNames.addAll(properties.keySet());
@@ -479,7 +479,7 @@ public class KerberosHelperImpl implements KerberosHelper {
 
     StackId stackVersion = cluster.getCurrentStackVersion();
 
-    List<String> hostNames = new ArrayList<String>();
+    List<String> hostNames = new ArrayList<>();
     Collection<Host> hosts = cluster.getHosts();
 
     if (hosts != null) {
@@ -493,10 +493,10 @@ public class KerberosHelperImpl implements KerberosHelper {
     // This could happen when enabling Kerberos while installing a cluster via Blueprints due to the
     // way hosts are discovered during the install process.
     if (!hostNames.isEmpty()) {
-      Map<String, Map<String, Map<String, String>>> requestConfigurations = new HashMap<String, Map<String, Map<String, String>>>();
+      Map<String, Map<String, Map<String, String>>> requestConfigurations = new HashMap<>();
       if (existingConfigurations != null) {
         for (Map.Entry<String, Map<String, String>> configuration : existingConfigurations.entrySet()) {
-          Map<String, Map<String, String>> properties = new HashMap<String, Map<String, String>>();
+          Map<String, Map<String, String>> properties = new HashMap<>();
           String configType = configuration.getKey();
           Map<String, String> configurationProperties = configuration.getValue();
 
@@ -505,7 +505,7 @@ public class KerberosHelperImpl implements KerberosHelper {
           }
 
           if ("cluster-env".equals(configType)) {
-            configurationProperties = new HashMap<String, String>(configurationProperties);
+            configurationProperties = new HashMap<>(configurationProperties);
             configurationProperties.put("security_enabled", (kerberosEnabled) ? "true" : "false");
           }
 
@@ -523,15 +523,15 @@ public class KerberosHelperImpl implements KerberosHelper {
           Map<String, Map<String, String>> requestConfiguration = requestConfigurations.get(configType);
 
           if (requestConfiguration == null) {
-            requestConfiguration = new HashMap<String, Map<String, String>>();
+            requestConfiguration = new HashMap<>();
             requestConfigurations.put(configType, requestConfiguration);
           }
 
           Map<String, String> requestConfigurationProperties = requestConfiguration.get("properties");
           if (requestConfigurationProperties == null) {
-            requestConfigurationProperties = new HashMap<String, String>();
+            requestConfigurationProperties = new HashMap<>();
           } else {
-            requestConfigurationProperties = new HashMap<String, String>(requestConfigurationProperties);
+            requestConfigurationProperties = new HashMap<>(requestConfigurationProperties);
           }
 
           requestConfigurationProperties.putAll(configurationProperties);
@@ -541,7 +541,7 @@ public class KerberosHelperImpl implements KerberosHelper {
 
       StackAdvisorRequest request = StackAdvisorRequest.StackAdvisorRequestBuilder
           .forStack(stackVersion.getStackName(), stackVersion.getStackVersion())
-          .forServices(new ArrayList<String>(services))
+          .forServices(new ArrayList<>(services))
           .forHosts(hostNames)
           .withComponentHostsMap(cluster.getServiceComponentHostMap(null, services))
           .withConfigurations(requestConfigurations)
@@ -606,7 +606,7 @@ public class KerberosHelperImpl implements KerberosHelper {
                 configType, propertyName, recommendedValue);
 
             if(kerberosConfigProperties == null) {
-              kerberosConfigProperties = new HashMap<String, String>();
+              kerberosConfigProperties = new HashMap<>();
               kerberosConfigurations.put(configType, kerberosConfigProperties);
             }
 
@@ -657,13 +657,13 @@ public class KerberosHelperImpl implements KerberosHelper {
           if (propertiesToRemove != null) {
             Set<String> properties = propertiesToRemove.get(configType);
             if (properties == null) {
-              properties = new HashSet<String>();
+              properties = new HashSet<>();
               propertiesToRemove.put(configType, properties);
             }
             properties.add(propertyName);
           } else {
             if (kerberosConfigProperties == null) {
-              kerberosConfigProperties = new HashMap<String, String>();
+              kerberosConfigProperties = new HashMap<>();
               kerberosConfigurations.put(configType, kerberosConfigProperties);
             }
             kerberosConfigProperties.put(propertyName, "");
@@ -701,7 +701,7 @@ public class KerberosHelperImpl implements KerberosHelper {
       }
 
       // Create the context to use for filtering Kerberos Identities based on the state of the cluster
-      Map<String, Object> filterContext = new HashMap<String, Object>();
+      Map<String, Object> filterContext = new HashMap<>();
       filterContext.put("configurations", configurations);
       filterContext.put("services", services);
 
@@ -920,7 +920,7 @@ public class KerberosHelperImpl implements KerberosHelper {
     if (kerberosDescriptor != null && processAuthToLocalRules) {
 
       Set<String> authToLocalProperties;
-      Set<String> authToLocalPropertiesToSet = new HashSet<String>();
+      Set<String> authToLocalPropertiesToSet = new HashSet<>();
 
       // a flag to be used by the AuthToLocalBuilder marking whether the default realm rule should contain the //L option, indicating username case insensitive behaviour
       // the 'kerberos-env' structure is expected to be available here as it was previously validated
@@ -930,7 +930,7 @@ public class KerberosHelperImpl implements KerberosHelper {
       String additionalRealms = kerberosDescriptor.getProperty("additional_realms");
 
       // Create the context to use for filtering Kerberos Identities based on the state of the cluster
-      Map<String, Object> filterContext = new HashMap<String, Object>();
+      Map<String, Object> filterContext = new HashMap<>();
       filterContext.put("configurations", existingConfigurations);
       filterContext.put("services", installedServices.keySet());
 
@@ -1009,7 +1009,7 @@ public class KerberosHelperImpl implements KerberosHelper {
             if (kerberosConfiguration != null) {
               builder.addRules(kerberosConfiguration.get(propertyName));
             } else {
-              kerberosConfiguration = new HashMap<String, String>();
+              kerberosConfiguration = new HashMap<>();
               kerberosConfigurations.put(configType, kerberosConfiguration);
             }
 
@@ -1029,7 +1029,7 @@ public class KerberosHelperImpl implements KerberosHelper {
                                                                       Collection<String> hostFilter, Collection<String> identityFilter,
                                                                       Command<Boolean, ServiceComponentHost> shouldProcessCommand)
       throws AmbariException {
-    List<ServiceComponentHost> serviceComponentHostsToProcess = new ArrayList<ServiceComponentHost>();
+    List<ServiceComponentHost> serviceComponentHostsToProcess = new ArrayList<>();
     Map<String, Service> services = cluster.getServices();
 
     if ((services != null) && !services.isEmpty()) {
@@ -1083,7 +1083,7 @@ public class KerberosHelperImpl implements KerberosHelper {
   @Override
   public Set<String> getHostsWithValidKerberosClient(Cluster cluster)
       throws AmbariException {
-    Set<String> hostsWithValidKerberosClient = new HashSet<String>();
+    Set<String> hostsWithValidKerberosClient = new HashSet<>();
     List<ServiceComponentHost> schKerberosClients = cluster.getServiceComponentHosts(Service.Type.KERBEROS.name(), Role.KERBEROS_CLIENT.name());
 
     if (schKerberosClients != null) {
@@ -1130,7 +1130,7 @@ public class KerberosHelperImpl implements KerberosHelper {
     }
 
     if (evaluateWhenClauses) {
-      Set<String> services = new HashSet<String>(cluster.getServices().keySet());
+      Set<String> services = new HashSet<>(cluster.getServices().keySet());
 
       if(additionalServices != null) {
         services.addAll(additionalServices);
@@ -1138,7 +1138,7 @@ public class KerberosHelperImpl implements KerberosHelper {
 
       // Build the context needed to filter out Kerberos identities...
       // This includes the current set of configurations for the cluster and the set of installed services
-      Map<String, Object> context = new HashMap<String, Object>();
+      Map<String, Object> context = new HashMap<>();
       context.put("configurations", calculateConfigurations(cluster, null, kerberosDescriptor.getProperties()));
       context.put("services", services);
 
@@ -1183,7 +1183,7 @@ public class KerberosHelperImpl implements KerberosHelper {
 
     if ((updates != null) && !updates.isEmpty()) {
       if (configurations == null) {
-        configurations = new HashMap<String, Map<String, String>>();
+        configurations = new HashMap<>();
       }
 
       for (Map.Entry<String, KerberosConfigurationDescriptor> entry : updates.entrySet()) {
@@ -1309,7 +1309,7 @@ public class KerberosHelperImpl implements KerberosHelper {
       throw new AmbariException(String.format("The cluster object for the cluster name %s is not available", clusterName));
     }
 
-    Map<String, Collection<KerberosIdentityDescriptor>> activeIdentities = new HashMap<String, Collection<KerberosIdentityDescriptor>>();
+    Map<String, Collection<KerberosIdentityDescriptor>> activeIdentities = new HashMap<>();
 
     // Only calculate the active identities if the kerberos-env configurtaion is available.  Else
     // important information like the realm will be missing (kerberos-env/realm)
@@ -1356,12 +1356,12 @@ public class KerberosHelperImpl implements KerberosHelper {
                 kerberosDescriptorProperties);
 
             // Create the context to use for filtering Kerberos Identities based on the state of the cluster
-            Map<String, Object> filterContext = new HashMap<String, Object>();
+            Map<String, Object> filterContext = new HashMap<>();
             filterContext.put("configurations", configurations);
             filterContext.put("services", existingServices);
 
 
-            Map<String, KerberosIdentityDescriptor> hostActiveIdentities = new HashMap<String, KerberosIdentityDescriptor>();
+            Map<String, KerberosIdentityDescriptor> hostActiveIdentities = new HashMap<>();
             List<KerberosIdentityDescriptor> identities = getActiveIdentities(cluster, hostname,
                 serviceName, componentName, kerberosDescriptor, filterContext);
 
@@ -1451,7 +1451,7 @@ public class KerberosHelperImpl implements KerberosHelper {
 
   @Override
   public List<KerberosIdentityDescriptor> getAmbariServerIdentities(KerberosDescriptor kerberosDescriptor) throws AmbariException {
-    List<KerberosIdentityDescriptor> ambariIdentities = new ArrayList<KerberosIdentityDescriptor>();
+    List<KerberosIdentityDescriptor> ambariIdentities = new ArrayList<>();
 
     KerberosServiceDescriptor ambariKerberosDescriptor = kerberosDescriptor.getService("AMBARI");
     if (ambariKerberosDescriptor != null) {
@@ -1754,7 +1754,7 @@ public class KerberosHelperImpl implements KerberosHelper {
     // Gather data needed to create stages and tasks...
     Map<String, Set<String>> clusterHostInfo = StageUtils.getClusterHostInfo(cluster);
     String clusterHostInfoJson = StageUtils.getGson().toJson(clusterHostInfo);
-    Map<String, String> hostParams = customCommandExecutionHelper.createDefaultHostParams(cluster);
+    Map<String, String> hostParams = customCommandExecutionHelper.createDefaultHostParams(cluster, cluster.getDesiredStackVersion());
     String hostParamsJson = StageUtils.getGson().toJson(hostParams);
     String ambariServerHostname = StageUtils.getHostName();
     ServiceComponentHostServerActionEvent event = new ServiceComponentHostServerActionEvent(
@@ -1844,7 +1844,7 @@ public class KerberosHelperImpl implements KerberosHelper {
         throw new AmbariException("The properties map must not be null.  It is needed to store data related to the service check identity");
       }
 
-      List<ServiceComponentHost> serviceComponentHostsToProcess = new ArrayList<ServiceComponentHost>();
+      List<ServiceComponentHost> serviceComponentHostsToProcess = new ArrayList<>();
       KerberosDescriptor kerberosDescriptor = getKerberosDescriptor(cluster);
       KerberosIdentityDataFileWriter kerberosIdentityDataFileWriter = null;
 
@@ -1954,7 +1954,7 @@ public class KerberosHelperImpl implements KerberosHelper {
       // Gather data needed to create stages and tasks...
       Map<String, Set<String>> clusterHostInfo = StageUtils.getClusterHostInfo(cluster);
       String clusterHostInfoJson = StageUtils.getGson().toJson(clusterHostInfo);
-      Map<String, String> hostParams = customCommandExecutionHelper.createDefaultHostParams(cluster);
+      Map<String, String> hostParams = customCommandExecutionHelper.createDefaultHostParams(cluster, cluster.getDesiredStackVersion());
       String hostParamsJson = StageUtils.getGson().toJson(hostParams);
       String ambariServerHostname = StageUtils.getHostName();
       ServiceComponentHostServerActionEvent event = new ServiceComponentHostServerActionEvent(
@@ -2194,7 +2194,7 @@ public class KerberosHelperImpl implements KerberosHelper {
     if (updates != null) {
       Map<String, String> existingProperties = configurations.get(type);
       if (existingProperties == null) {
-        existingProperties = new HashMap<String, String>();
+        existingProperties = new HashMap<>();
         configurations.put(type, existingProperties);
       }
 
@@ -2311,8 +2311,8 @@ public class KerberosHelperImpl implements KerberosHelper {
    */
   private List<String> createUniqueHostList(Collection<ServiceComponentHost> serviceComponentHosts, Set<HostState> allowedStates)
       throws AmbariException {
-    Set<String> hostNames = new HashSet<String>();
-    Set<String> visitedHostNames = new HashSet<String>();
+    Set<String> hostNames = new HashSet<>();
+    Set<String> visitedHostNames = new HashSet<>();
 
     if (serviceComponentHosts != null) {
       for (ServiceComponentHost sch : serviceComponentHosts) {
@@ -2335,7 +2335,7 @@ public class KerberosHelperImpl implements KerberosHelper {
       }
     }
 
-    return new ArrayList<String>(hostNames);
+    return new ArrayList<>(hostNames);
   }
 
   @Override
@@ -2373,7 +2373,7 @@ public class KerberosHelperImpl implements KerberosHelper {
 
   @Override
   public Map<String, Map<String, String>> getIdentityConfigurations(List<KerberosIdentityDescriptor> identityDescriptors) {
-    Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
+    Map<String, Map<String, String>> map = new HashMap<>();
 
     if (identityDescriptors != null) {
       for (KerberosIdentityDescriptor identityDescriptor : identityDescriptors) {
@@ -2410,7 +2410,7 @@ public class KerberosHelperImpl implements KerberosHelper {
 
         Map<String, String> properties = map.get(type);
         if (properties == null) {
-          properties = new HashMap<String, String>();
+          properties = new HashMap<>();
           map.put(type, properties);
         }
 
@@ -2442,7 +2442,7 @@ public class KerberosHelperImpl implements KerberosHelper {
                                                                Map<String, Object> filterContext)
       throws AmbariException {
 
-    List<KerberosIdentityDescriptor> identities = new ArrayList<KerberosIdentityDescriptor>();
+    List<KerberosIdentityDescriptor> identities = new ArrayList<>();
 
     List<ServiceComponentHost> serviceComponentHosts = cluster.getServiceComponentHosts(hostname);
 
@@ -2490,7 +2490,7 @@ public class KerberosHelperImpl implements KerberosHelper {
     // Configurations from the tag is always expanded and then over-written by the actual
     // global:version1:{a1:A1,b1:B1,d1:D1} + global:{a1:A2,c1:C1,DELETED_d1:x} ==>
     // global:{a1:A2,b1:B1,c1:C1}
-    Map<String, Map<String, String>> configurations = new HashMap<String, Map<String, String>>();
+    Map<String, Map<String, String>> configurations = new HashMap<>();
     Map<String, Map<String, String>> configurationTags = ambariManagementController.findConfigurationTagsWithOverrides(cluster, hostname);
 
     Map<String, Map<String, String>> configProperties = configHelper.getEffectiveConfigProperties(cluster, configurationTags);
@@ -2503,7 +2503,7 @@ public class KerberosHelperImpl implements KerberosHelper {
       Map<String, String> configuration = configurations.get(type);
 
       if (configuration == null) {
-        configuration = new HashMap<String, String>(allLevelMergedConfig);
+        configuration = new HashMap<>(allLevelMergedConfig);
       } else {
         Map<String, String> mergedConfig = configHelper.getMergedConfig(allLevelMergedConfig, configuration);
         configuration.clear();
@@ -2536,7 +2536,7 @@ public class KerberosHelperImpl implements KerberosHelper {
     // and will also contain a value for the current host
     Map<String, String> generalProperties = configurations.get("");
     if (generalProperties == null) {
-      generalProperties = new HashMap<String, String>();
+      generalProperties = new HashMap<>();
       configurations.put("", generalProperties);
     }
 
@@ -2563,7 +2563,7 @@ public class KerberosHelperImpl implements KerberosHelper {
       Map<String, Set<String>> clusterHostInfo = StageUtils.getClusterHostInfo(cluster);
 
       if (clusterHostInfo != null) {
-        Map<String, String> componentHosts = new HashMap<String, String>();
+        Map<String, String> componentHosts = new HashMap<>();
 
         clusterHostInfo = StageUtils.substituteHostIndexes(clusterHostInfo);
 
@@ -2588,11 +2588,11 @@ public class KerberosHelperImpl implements KerberosHelper {
     if (map == null) {
       return null;
     } else {
-      Map<String, Map<String, String>> copy = new HashMap<String, Map<String, String>>();
+      Map<String, Map<String, String>> copy = new HashMap<>();
 
       for (Map.Entry<String, Map<String, String>> entry : map.entrySet()) {
         Map<String, String> innerMap = entry.getValue();
-        copy.put(entry.getKey(), (innerMap == null) ? null : new HashMap<String, String>(innerMap));
+        copy.put(entry.getKey(), (innerMap == null) ? null : new HashMap<>(innerMap));
       }
 
       return copy;
@@ -2607,7 +2607,7 @@ public class KerberosHelperImpl implements KerberosHelper {
    */
   private KerberosDescriptor getKerberosDescriptorUpdates(Cluster cluster) throws AmbariException {
     // find instance using name and foreign keys
-    TreeMap<String, String> foreignKeys = new TreeMap<String, String>();
+    TreeMap<String, String> foreignKeys = new TreeMap<>();
     foreignKeys.put("cluster", String.valueOf(cluster.getClusterId()));
 
     ArtifactEntity entity = artifactDAO.findByNameAndForeignKeys("kerberos_descriptor", foreignKeys);
@@ -2660,7 +2660,7 @@ public class KerberosHelperImpl implements KerberosHelper {
       for (KerberosIdentityDescriptor identity : identities) {
         if (!identity.shouldInclude(context)) {
           if (set == null) {
-            set = new HashSet<String>();
+            set = new HashSet<>();
             identitiesToRemove.put(currentPath, set);
           }
 
@@ -2959,12 +2959,12 @@ public class KerberosHelperImpl implements KerberosHelper {
           hostParamsJson);
 
       Collection<ServiceComponentHost> filteredComponents = filterServiceComponentHostsForHosts(
-          new ArrayList<ServiceComponentHost>(serviceComponentHosts), hostsWithValidKerberosClient);
+          new ArrayList<>(serviceComponentHosts), hostsWithValidKerberosClient);
 
       if (!filteredComponents.isEmpty()) {
         List<String> hostsToUpdate = createUniqueHostList(filteredComponents, Collections.singleton(HostState.HEALTHY));
-        Map<String, String> requestParams = new HashMap<String, String>();
-        List<RequestResourceFilter> requestResourceFilters = new ArrayList<RequestResourceFilter>();
+        Map<String, String> requestParams = new HashMap<>();
+        List<RequestResourceFilter> requestResourceFilters = new ArrayList<>();
         RequestResourceFilter reqResFilter = new RequestResourceFilter(Service.Type.KERBEROS.name(), Role.KERBEROS_CLIENT.name(), hostsToUpdate);
         requestResourceFilters.add(reqResFilter);
 
@@ -3100,14 +3100,14 @@ public class KerberosHelperImpl implements KerberosHelper {
           hostParamsJson);
 
       Collection<ServiceComponentHost> filteredComponents = filterServiceComponentHostsForHosts(
-          new ArrayList<ServiceComponentHost>(serviceComponentHosts), hostsWithValidKerberosClient);
+          new ArrayList<>(serviceComponentHosts), hostsWithValidKerberosClient);
 
       if (!filteredComponents.isEmpty()) {
         List<String> hostsToUpdate = createUniqueHostList(filteredComponents, Collections.singleton(HostState.HEALTHY));
 
         if (!hostsToUpdate.isEmpty()) {
-          Map<String, String> requestParams = new HashMap<String, String>();
-          List<RequestResourceFilter> requestResourceFilters = new ArrayList<RequestResourceFilter>();
+          Map<String, String> requestParams = new HashMap<>();
+          List<RequestResourceFilter> requestResourceFilters = new ArrayList<>();
           RequestResourceFilter reqResFilter = new RequestResourceFilter("KERBEROS", "KERBEROS_CLIENT", hostsToUpdate);
           requestResourceFilters.add(reqResFilter);
 
@@ -3160,7 +3160,7 @@ public class KerberosHelperImpl implements KerberosHelper {
         throws AmbariException {
 
       // Add the finalize stage...
-      Map<String, String> commandParameters = new HashMap<String, String>();
+      Map<String, String> commandParameters = new HashMap<>();
       commandParameters.put(KerberosServerAction.DEFAULT_REALM, kerberosDetails.getDefaultRealm());
       commandParameters.put(KerberosServerAction.KDC_TYPE, kerberosDetails.getKdcType().name());
       commandParameters.put(KerberosServerAction.AUTHENTICATED_USER_NAME, ambariManagementController.getAuthName());
@@ -3274,7 +3274,7 @@ public class KerberosHelperImpl implements KerberosHelper {
             actionManager);
       }
 
-      Map<String, String> commandParameters = new HashMap<String, String>();
+      Map<String, String> commandParameters = new HashMap<>();
       commandParameters.put(KerberosServerAction.AUTHENTICATED_USER_NAME, ambariManagementController.getAuthName());
       commandParameters.put(KerberosServerAction.UPDATE_CONFIGURATION_NOTE, "Enabling Kerberos");
       commandParameters.put(KerberosServerAction.UPDATE_CONFIGURATIONS, "true");
@@ -3392,7 +3392,7 @@ public class KerberosHelperImpl implements KerberosHelper {
             actionManager);
       }
 
-      Map<String, String> commandParameters = new HashMap<String, String>();
+      Map<String, String> commandParameters = new HashMap<>();
       commandParameters.put(KerberosServerAction.AUTHENTICATED_USER_NAME, ambariManagementController.getAuthName());
       commandParameters.put(KerberosServerAction.UPDATE_CONFIGURATION_NOTE, "Disabling Kerberos");
       commandParameters.put(KerberosServerAction.UPDATE_CONFIGURATIONS, "true");
@@ -3546,7 +3546,7 @@ public class KerberosHelperImpl implements KerberosHelper {
       }
 
 
-      Map<String, String> commandParameters = new HashMap<String, String>();
+      Map<String, String> commandParameters = new HashMap<>();
       commandParameters.put(KerberosServerAction.AUTHENTICATED_USER_NAME, ambariManagementController.getAuthName());
       commandParameters.put(KerberosServerAction.DEFAULT_REALM, kerberosDetails.getDefaultRealm());
       if (dataDirectory != null) {
@@ -3669,7 +3669,7 @@ public class KerberosHelperImpl implements KerberosHelper {
         //  2) delete principals
         //  3) delete keytab files
 
-        Map<String, String> commandParameters = new HashMap<String, String>();
+        Map<String, String> commandParameters = new HashMap<>();
         commandParameters.put(KerberosServerAction.AUTHENTICATED_USER_NAME, ambariManagementController.getAuthName());
         commandParameters.put(KerberosServerAction.DEFAULT_REALM, kerberosDetails.getDefaultRealm());
         if (dataDirectory != null) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/PrereqCheckRequest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/PrereqCheckRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/PrereqCheckRequest.java
index 8584ef6..f80c16a 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/PrereqCheckRequest.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/PrereqCheckRequest.java
@@ -38,8 +38,8 @@ public class PrereqCheckRequest {
 
   private UpgradeType m_upgradeType;
 
-  private Map<CheckDescription, PrereqCheckStatus> m_results =
-      new HashMap<CheckDescription, PrereqCheckStatus>();
+  private Map<CheckDescription, PrereqCheckStatus> m_results = new HashMap<>();
+  private boolean m_revert = false;
 
 
   public PrereqCheckRequest(String clusterName, UpgradeType upgradeType) {
@@ -132,11 +132,30 @@ public class PrereqCheckRequest {
    * Gets the prerequisite check config
    * @return the prereqCheckConfig
    */
-  public PrerequisiteCheckConfig getPrerequisiteCheckConfig() { return m_prereqCheckConfig; }
+  public PrerequisiteCheckConfig getPrerequisiteCheckConfig() {
+    return m_prereqCheckConfig;
+  }
 
   /**
    * Sets the prerequisite check config obtained from the upgrade pack
    * @param prereqCheckConfig The prereqCheckConfig
    */
-  public void setPrerequisiteCheckConfig(PrerequisiteCheckConfig prereqCheckConfig) { m_prereqCheckConfig = prereqCheckConfig;}
+  public void setPrerequisiteCheckConfig(PrerequisiteCheckConfig prereqCheckConfig) {
+    m_prereqCheckConfig = prereqCheckConfig;
+  }
+
+  /**
+   * @param revert
+   *          {@code true} if the check is for a patch reversion
+   */
+  public void setRevert(boolean revert) {
+    m_revert = revert;
+  }
+
+  /**
+   * @return if the check is for a patch reversion
+   */
+  public boolean isRevert() {
+    return m_revert;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceComponentResponse.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceComponentResponse.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceComponentResponse.java
index 4845c78..f9274be 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceComponentResponse.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceComponentResponse.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -15,34 +15,30 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
-
 package org.apache.ambari.server.controller;
 
 import java.util.Map;
+import java.util.Objects;
 
 public class RootServiceComponentResponse {
 
-  private String serviceName;
-  private String componentName;
-  private Map<String, String> properties;
-  private String componentVersion;
+  private final String serviceName;
+  private final String componentName;
+  private final Map<String, String> properties;
+  private final String componentVersion;
+  private final long serverClock = System.currentTimeMillis() / 1000L;
 
-  public RootServiceComponentResponse(String componentName, String componentVersion, Map<String, String> properties) {
+  public RootServiceComponentResponse(String serviceName, String componentName, String componentVersion, Map<String, String> properties) {
+    this.serviceName = serviceName;
     this.componentName = componentName;
-    this.setComponentVersion(componentVersion); 
-    this.setProperties(properties);
-    
+    this.componentVersion = componentVersion;
+    this.properties = properties;
   }
 
   public String getServiceName() {
     return serviceName;
   }
 
-  public void setServiceName(String serviceName) {
-    this.serviceName = serviceName;
-  }
-
   public String getComponentName() {
     return componentName;
   }
@@ -51,35 +47,30 @@ public class RootServiceComponentResponse {
     return properties;
   }
 
-  public void setProperties(Map<String, String> properties) {
-    this.properties = properties;
-  }
-  
   public String getComponentVersion() {
     return componentVersion;
   }
 
-  public void setComponentVersion(String componentVersion) {
-    this.componentVersion = componentVersion;
+  public long getServerClock() {
+    return serverClock;
   }
-  
+
   @Override
   public boolean equals(Object o) {
     if (this == o) return true;
     if (o == null || getClass() != o.getClass()) return false;
 
-    RootServiceComponentResponse that = (RootServiceComponentResponse) o;
-
-    return !(componentName != null ? !componentName.equals(that.componentName) : that.componentName != null) &&
-        !(componentVersion != null ? !componentVersion.equals(that.componentVersion) : that.componentVersion != null) &&
-        !(properties != null ? !properties.equals(that.properties) : that.properties != null);
+    RootServiceComponentResponse other = (RootServiceComponentResponse) o;
 
+    return Objects.equals(serviceName, other.serviceName) &&
+      Objects.equals(componentName, other.componentName) &&
+      Objects.equals(componentVersion, other.componentVersion) &&
+      Objects.equals(properties, other.properties);
   }
 
   @Override
   public int hashCode() {
-    int result = 31 + (componentName != null ? componentName.hashCode() : 0);
-    result += (componentVersion != null ? componentVersion.hashCode() : 0);
-    return result;
+    return Objects.hash(serviceName, componentName, componentVersion);
   }
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceHostComponentResponse.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceHostComponentResponse.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceHostComponentResponse.java
index 6cbc30c..725c2aa 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceHostComponentResponse.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceHostComponentResponse.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,20 +19,21 @@
 package org.apache.ambari.server.controller;
 
 import java.util.Map;
+import java.util.Objects;
 
 public class RootServiceHostComponentResponse {
 
-  private String serviceName;
-  private String hostName;
-  private String componentName;
-  private String componentState;
-  private String componentVersion;
-  private Map<String, String> properties;
+  private final String serviceName;
+  private final String hostName;
+  private final String componentName;
+  private final String componentState;
+  private final String componentVersion;
+  private final Map<String, String> properties;
 
-
-  public RootServiceHostComponentResponse(String hostName, String componentName, String componentState,
+  public RootServiceHostComponentResponse(String serviceName, String hostName, String componentName, String componentState,
       String componentVersion,
       Map<String, String> properties) {
+    this.serviceName = serviceName;
     this.hostName = hostName;
     this.componentName = componentName;
     this.componentState = componentState;
@@ -44,10 +45,6 @@ public class RootServiceHostComponentResponse {
     return serviceName;
   }
 
-  public void setServiceName(String serviceName) {
-    this.serviceName = serviceName;
-  }
-
   public String getHostName() {
     return hostName;
   }
@@ -56,47 +53,36 @@ public class RootServiceHostComponentResponse {
     return componentName;
   }
 
-  public void setComponentName(String componentName) {
-    this.componentName = componentName;
+  public String getComponentState() {
+    return componentState;
+  }
+
+  public String getComponentVersion() {
+    return componentVersion;
   }
 
   public Map<String, String> getProperties() {
     return properties;
   }
 
-  public void setProperties(Map<String, String> properties) {
-    this.properties = properties;
-  }
-  
   @Override
   public boolean equals(Object o) {
     if (this == o) return true;
     if (o == null || getClass() != o.getClass()) return false;
 
-    RootServiceHostComponentResponse that = (RootServiceHostComponentResponse) o;
-
-    return !(hostName != null ? !hostName.equals(that.hostName) : that.hostName != null) &&
-        !(componentName != null ? !componentName.equals(that.componentName) : that.componentName != null) &&
-        !(componentState != null ? !componentState.equals(that.componentState) : that.componentState != null) &&
-        !(componentVersion != null ? !componentVersion.equals(that.componentVersion) : that.componentVersion != null) &&
-        !(properties != null ? !properties.equals(that.properties) : that.properties != null);
+    RootServiceHostComponentResponse other = (RootServiceHostComponentResponse) o;
 
+    return Objects.equals(serviceName, other.serviceName) &&
+      Objects.equals(hostName, other.hostName) &&
+      Objects.equals(componentName, other.componentName) &&
+      Objects.equals(componentState, other.componentState) &&
+      Objects.equals(componentVersion, other.componentVersion) &&
+      Objects.equals(properties, other.properties);
   }
 
   @Override
   public int hashCode() {
-    int result = 31 + (hostName != null ? hostName.hashCode() : 0);
-    result = result + (componentName != null ? componentName.hashCode() : 0);
-    result = result + (componentState != null ? componentState.hashCode() : 0);
-    result = result + (componentVersion != null ? componentVersion.hashCode() : 0);
-    return result;
+    return Objects.hash(serviceName, hostName, componentName, componentVersion, componentState);
   }
 
-  public String getComponentState() {
-    return componentState;
-  }
-
-  public String getComponentVersion() {
-    return componentVersion;
-  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceResponseFactory.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceResponseFactory.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceResponseFactory.java
index 6bb68b6..c207e2b 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceResponseFactory.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/RootServiceResponseFactory.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -18,13 +18,13 @@
 
 package org.apache.ambari.server.controller;
 
+import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.JDK_LOCATION;
+
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
-import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.JDK_LOCATION;
-
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.ObjectNotFoundException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
@@ -43,21 +43,21 @@ public class RootServiceResponseFactory extends
   public static final String NOT_APPLICABLE = "NOT_APPLICABLE";
   @Inject
   private Configuration configs;
-  
+
   @Inject
   private AmbariMetaInfo ambariMetaInfo;
 
   @Inject
   private AmbariManagementController managementController;
-  
-  
+
+
   @Override
   public Set<RootServiceResponse> getRootServices(RootServiceRequest request) throws ObjectNotFoundException {
-    
+
     Set<RootServiceResponse> response;
-    
+
     String serviceName = null;
-    
+
     if (request != null)
       serviceName = request.getServiceName();
 
@@ -69,22 +69,22 @@ public class RootServiceResponseFactory extends
       catch (IllegalArgumentException ex) {
         throw new ObjectNotFoundException("Root service name: " + serviceName);
       }
-      
+
       response = Collections.singleton(new RootServiceResponse(service.toString()));
     } else {
-      response = new HashSet<RootServiceResponse>();
-      
-      for (Services service: Services.values())    
+      response = new HashSet<>();
+
+      for (Services service: Services.values())
         response.add(new RootServiceResponse(service.toString()));
-    }    
+    }
     return response;
   }
-  
+
   @Override
   public Set<RootServiceComponentResponse> getRootServiceComponents(
       RootServiceComponentRequest request) throws ObjectNotFoundException {
-    Set<RootServiceComponentResponse> response = new HashSet<RootServiceComponentResponse>();
-    
+    Set<RootServiceComponentResponse> response = new HashSet<>();
+
     String serviceName = request.getServiceName();
     String componentName = request.getComponentName();
     Services service;
@@ -98,7 +98,7 @@ public class RootServiceResponseFactory extends
     catch (NullPointerException np) {
       throw new ObjectNotFoundException("Root service name: null");
     }
-    
+
     if (componentName != null) {
       Components component;
       try {
@@ -109,13 +109,13 @@ public class RootServiceResponseFactory extends
       catch (IllegalArgumentException ex) {
         throw new ObjectNotFoundException("Component name: " + componentName);
       }
-      response = Collections.singleton(new RootServiceComponentResponse(component.toString(),
+      response = Collections.singleton(new RootServiceComponentResponse(serviceName, component.toString(),
                                        getComponentVersion(componentName, null),
                                        getComponentProperties(componentName)));
     } else {
-    
-      for (Components component: service.getComponents())    
-        response.add(new RootServiceComponentResponse(component.toString(),
+
+      for (Components component: service.getComponents())
+        response.add(new RootServiceComponentResponse(serviceName, component.toString(),
                      getComponentVersion(component.name(), null),
                      getComponentProperties(component.name())));
       }
@@ -125,14 +125,14 @@ public class RootServiceResponseFactory extends
   private String getComponentVersion(String componentName, HostResponse host) {
     Components component = Components.valueOf(componentName);
     String componentVersion;
-      
+
     switch (component) {
       case AMBARI_SERVER:
         componentVersion = ambariMetaInfo.getServerVersion();
         break;
       case AMBARI_AGENT:
         //Could be defined on level of host component
-        
+
         if (host == null)
           componentVersion = NOT_APPLICABLE;
         else
@@ -142,18 +142,18 @@ public class RootServiceResponseFactory extends
       default:
         componentVersion = null;
       }
-      
+
     return componentVersion;
   }
-  
+
   private Map<String, String> getComponentProperties(String componentName){
-    
+
     Map<String, String> response;
     Components component = null;
 
     if (componentName != null) {
       component = Components.valueOf(componentName);
-      
+
       switch (component) {
       case AMBARI_SERVER:
         response = configs.getAmbariProperties();
@@ -171,7 +171,7 @@ public class RootServiceResponseFactory extends
     return response;
   }
 
-  
+
   public enum Services {
     AMBARI(Components.values());
     private Components[] components;
@@ -184,25 +184,26 @@ public class RootServiceResponseFactory extends
       return components;
     }
   }
-  
+
   public enum Components {
     AMBARI_SERVER, AMBARI_AGENT
   }
 
   @Override
   public Set<RootServiceHostComponentResponse> getRootServiceHostComponent(RootServiceHostComponentRequest request, Set<HostResponse> hosts) throws AmbariException {
-    Set<RootServiceHostComponentResponse> response = new HashSet<RootServiceHostComponentResponse>();
+    Set<RootServiceHostComponentResponse> response = new HashSet<>();
 
-    Set<RootServiceComponentResponse> rootServiceComponents = 
-        getRootServiceComponents(new RootServiceComponentRequest(request.getServiceName(), request.getComponentName()));
+    String serviceName = request.getServiceName();
+    Set<RootServiceComponentResponse> rootServiceComponents =
+        getRootServiceComponents(new RootServiceComponentRequest(serviceName, request.getComponentName()));
 
     //Cartesian product with hosts and components
     for (RootServiceComponentResponse component : rootServiceComponents) {
-      
-      Set<HostResponse> filteredHosts = new HashSet<HostResponse>(hosts);      
-      
+
+      Set<HostResponse> filteredHosts = new HashSet<>(hosts);
+
       //Make some filtering of hosts if need
-      if (component.getComponentName().equals(Components.AMBARI_SERVER.name()))
+      if (component.getComponentName().equals(Components.AMBARI_SERVER.name())) {
         CollectionUtils.filter(filteredHosts, new Predicate() {
           @Override
           public boolean evaluate(Object arg0) {
@@ -210,18 +211,20 @@ public class RootServiceResponseFactory extends
             return hostResponse.getHostname().equals(StageUtils.getHostName());
           }
         });
-      
+      }
+
       for (HostResponse host : filteredHosts) {
-        
-        if (component.getComponentName().equals(Components.AMBARI_SERVER.name()))
-          response.add(new RootServiceHostComponentResponse(host.getHostname(), component.getComponentName(),
-            RUNNING_STATE, getComponentVersion(component.getComponentName(), host), component.getProperties()));
-        else
-          response.add(new RootServiceHostComponentResponse(host.getHostname(), component.getComponentName(),
-            host.getHostState(), getComponentVersion(component.getComponentName(), host), component.getProperties()));
+        String state;
+        if (component.getComponentName().equals(Components.AMBARI_SERVER.name())) {
+          state = RUNNING_STATE;
+        } else {
+          state = host.getHostState().toString();
+        }
+        String componentVersion = getComponentVersion(component.getComponentName(), host);
+        response.add(new RootServiceHostComponentResponse(serviceName, host.getHostname(), component.getComponentName(), state, componentVersion, component.getProperties()));
       }
     }
-    
+
     return response;
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceComponentHostResponse.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceComponentHostResponse.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceComponentHostResponse.java
index f6993f8..7b75e06 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceComponentHostResponse.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceComponentHostResponse.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -35,21 +35,19 @@ public class ServiceComponentHostResponse {
   // type -> desired config
   private Map<String, HostConfig> actualConfigs;
   private String liveState;
-  private String stackVersion;
+  private String version;
   private String desiredStackVersion;
+  private String desiredRepositoryVersion;
   private String desiredState;
   private boolean staleConfig = false;
   private String adminState = null;
   private String maintenanceState = null;
   private UpgradeState upgradeState = UpgradeState.NONE;
 
-
-  public ServiceComponentHostResponse(String clusterName, String serviceName,
-                                      String componentName, String displayName,
-                                      String hostname, String publicHostname,
-                                      String liveState, String stackVersion,
-                                      String desiredState, String desiredStackVersion,
-                                      HostComponentAdminState adminState) {
+  public ServiceComponentHostResponse(String clusterName, String serviceName, String componentName,
+      String displayName, String hostname, String publicHostname, String liveState, String version,
+      String desiredState, String desiredStackVersion, String desiredRepositoryVersion,
+      HostComponentAdminState adminState) {
     this.clusterName = clusterName;
     this.serviceName = serviceName;
     this.componentName = componentName;
@@ -57,9 +55,10 @@ public class ServiceComponentHostResponse {
     this.hostname = hostname;
     this.publicHostname = publicHostname;
     this.liveState = liveState;
-    this.stackVersion = stackVersion;
+    this.version = version;
     this.desiredState = desiredState;
     this.desiredStackVersion = desiredStackVersion;
+    this.desiredRepositoryVersion = desiredRepositoryVersion;
     if (adminState != null) {
       this.adminState = adminState.name();
     }
@@ -143,17 +142,10 @@ public class ServiceComponentHostResponse {
   }
 
   /**
-   * @return the stackVersion
-   */
-  public String getStackVersion() {
-    return stackVersion;
-  }
-
-  /**
-   * @param stackVersion the stackVersion to set
+   * @return the version
    */
-  public void setStackVersion(String stackVersion) {
-    this.stackVersion = stackVersion;
+  public String getVersion() {
+    return version;
   }
 
   /**
@@ -185,6 +177,15 @@ public class ServiceComponentHostResponse {
   }
 
   /**
+   * Gets the desired repository of the component.
+   *
+   * @return the desired repository.
+   */
+  public String getDesiredRepositoryVersion() {
+    return desiredRepositoryVersion;
+  }
+
+  /**
    * @return the clusterName
    */
   public String getClusterName() {

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceComponentResponse.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceComponentResponse.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceComponentResponse.java
index 9dbda20..9bd3e44 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceComponentResponse.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceComponentResponse.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -21,46 +21,40 @@ package org.apache.ambari.server.controller;
 
 import java.util.Map;
 
+import org.apache.ambari.server.state.RepositoryVersionState;
+import org.apache.ambari.server.state.StackId;
+
 public class ServiceComponentResponse {
 
   private Long clusterId; // REF
-
   private String clusterName; // REF
-
   private String serviceName;
-
   private String componentName;
-
   private String displayName;
-
-  private String desiredStackVersion;
-
+  private String desiredStackId;
   private String desiredState;
-
   private String category;
-
-  Map<String, Integer> serviceComponentStateCount;
-
+  private Map<String, Integer> serviceComponentStateCount;
   private boolean recoveryEnabled;
+  private String desiredVersion;
+  private RepositoryVersionState repoState;
+
+  public ServiceComponentResponse(Long clusterId, String clusterName, String serviceName,
+      String componentName, StackId desiredStackId, String desiredState,
+      Map<String, Integer> serviceComponentStateCount, boolean recoveryEnabled, String displayName,
+      String desiredVersion, RepositoryVersionState repoState) {
 
-  public ServiceComponentResponse(Long clusterId, String clusterName,
-                                  String serviceName,
-                                  String componentName,
-                                  String desiredStackVersion,
-                                  String desiredState,
-                                  Map<String, Integer> serviceComponentStateCount,
-                                  boolean recoveryEnabled,
-                                  String displayName) {
-    super();
     this.clusterId = clusterId;
     this.clusterName = clusterName;
     this.serviceName = serviceName;
     this.componentName = componentName;
     this.displayName = displayName;
-    this.desiredStackVersion = desiredStackVersion;
+    this.desiredStackId = desiredStackId.getStackId();
     this.desiredState = desiredState;
     this.serviceComponentStateCount = serviceComponentStateCount;
     this.recoveryEnabled = recoveryEnabled;
+    this.desiredVersion = desiredVersion;
+    this.repoState = repoState;
   }
 
   /**
@@ -141,17 +135,12 @@ public class ServiceComponentResponse {
   }
 
   /**
+   * Gets the desired stack ID.
+   *
    * @return the desiredStackVersion
    */
-  public String getDesiredStackVersion() {
-    return desiredStackVersion;
-  }
-
-  /**
-   * @param desiredStackVersion the desiredStackVersion to set
-   */
-  public void setDesiredStackVersion(String desiredStackVersion) {
-    this.desiredStackVersion = desiredStackVersion;
+  public String getDesiredStackId() {
+    return desiredStackId;
   }
 
   /**
@@ -196,10 +185,29 @@ public class ServiceComponentResponse {
     this.recoveryEnabled = recoveryEnabled;
   }
 
+  /**
+   * @return the desired version of the component
+   */
+  public String getDesiredVersion() {
+    return desiredVersion;
+  }
+
+  /**
+   * @return the state of the repository against the desired version
+   */
+  public RepositoryVersionState getRepositoryState() {
+    return repoState;
+  }
+
+
   @Override
   public boolean equals(Object o) {
-    if (this == o) return true;
-    if (o == null || getClass() != o.getClass()) return false;
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
 
     ServiceComponentResponse that =
         (ServiceComponentResponse) o;

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceRequest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceRequest.java
index a8e6315..0fbb63f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceRequest.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceRequest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -17,7 +17,7 @@
  */
 package org.apache.ambari.server.controller;
 
-
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 
 public class ServiceRequest {
 
@@ -28,17 +28,25 @@ public class ServiceRequest {
   private String credentialStoreEnabled; // CREATE/UPDATE/GET
   private String credentialStoreSupported; //GET
 
+  private Long desiredRepositoryVersionId;
+  /**
+   * Short-lived object that gets set while validating a request
+   */
+  private RepositoryVersionEntity resolvedRepository;
+
   public ServiceRequest(String clusterName, String serviceName,
-                        String desiredState) {
-    this(clusterName, serviceName, desiredState, null);
+      Long desiredRepositoryVersionId, String desiredState) {
+    this(clusterName, serviceName, desiredRepositoryVersionId, desiredState, null);
   }
 
   public ServiceRequest(String clusterName, String serviceName,
-                        String desiredState,
-                        String credentialStoreEnabled) {
+      Long desiredRepositoryVersionId, String desiredState, String credentialStoreEnabled) {
     this.clusterName = clusterName;
     this.serviceName = serviceName;
     this.desiredState = desiredState;
+
+    this.desiredRepositoryVersionId = desiredRepositoryVersionId;
+
     this.credentialStoreEnabled = credentialStoreEnabled;
     // Credential store supported cannot be changed after
     // creation since it comes from the stack definition.
@@ -73,6 +81,10 @@ public class ServiceRequest {
     this.desiredState = desiredState;
   }
 
+  public Long getDesiredRepositoryVersionId() {
+    return desiredRepositoryVersionId;
+  }
+
   /**
    * @return the clusterName
    */
@@ -130,13 +142,25 @@ public class ServiceRequest {
     this.credentialStoreSupported = credentialStoreSupported;
   }
 
+  @Override
   public String toString() {
     StringBuilder sb = new StringBuilder();
-    sb.append("clusterName=" + clusterName
-        + ", serviceName=" + serviceName
-        + ", desiredState=" + desiredState
-        + ", credentialStoreEnabled=" + credentialStoreEnabled
-        + ", credentialStoreSupported=" + credentialStoreSupported);
+    sb.append("clusterName=").append(clusterName)
+      .append(", serviceName=").append(serviceName)
+      .append(", desiredState=").append(desiredState)
+      .append(", credentialStoreEnabled=").append(credentialStoreEnabled)
+      .append(", credentialStoreSupported=").append(credentialStoreSupported);
     return sb.toString();
   }
+
+  /**
+   * @param repositoryVersion
+   */
+  public void setResolvedRepository(RepositoryVersionEntity repositoryVersion) {
+    resolvedRepository = repositoryVersion;
+  }
+
+  public RepositoryVersionEntity getResolvedRepository() {
+    return resolvedRepository;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceResponse.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceResponse.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceResponse.java
index 3e35c0c..dea95a6 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceResponse.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceResponse.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -18,32 +18,39 @@
 
 package org.apache.ambari.server.controller;
 
+import org.apache.ambari.server.state.RepositoryVersionState;
+import org.apache.ambari.server.state.StackId;
 
 public class ServiceResponse {
 
   private Long clusterId;
   private String clusterName;
   private String serviceName;
-  private String desiredStackVersion;
+  private StackId desiredStackId;
+  private String desiredRepositoryVersion;
+  private Long desiredRepositoryVersionId;
+  private RepositoryVersionState repositoryVersionState;
   private String desiredState;
   private String maintenanceState;
   private boolean credentialStoreSupported;
   private boolean credentialStoreEnabled;
 
-  public ServiceResponse(Long clusterId, String clusterName,
-                         String serviceName,
-                         String desiredStackVersion, String desiredState,
-                         boolean credentialStoreSupported, boolean credentialStoreEnabled) {
+  public ServiceResponse(Long clusterId, String clusterName, String serviceName,
+      StackId desiredStackId, String desiredRepositoryVersion,
+      RepositoryVersionState repositoryVersionState, String desiredState,
+      boolean credentialStoreSupported, boolean credentialStoreEnabled) {
     this.clusterId = clusterId;
     this.clusterName = clusterName;
     this.serviceName = serviceName;
-    this.setDesiredStackVersion(desiredStackVersion);
-    this.setDesiredState(desiredState);
+    this.desiredStackId = desiredStackId;
+    this.repositoryVersionState = repositoryVersionState;
+    setDesiredState(desiredState);
+    this.desiredRepositoryVersion = desiredRepositoryVersion;
     this.credentialStoreSupported = credentialStoreSupported;
     this.credentialStoreEnabled = credentialStoreEnabled;
   }
-  
-  
+
+
 
   /**
    * @return the serviceName
@@ -102,23 +109,40 @@ public class ServiceResponse {
   }
 
   /**
-   * @return the desiredStackVersion
+   * @return the desired stack ID.
    */
-  public String getDesiredStackVersion() {
-    return desiredStackVersion;
+  public String getDesiredStackId() {
+    return desiredStackId.getStackId();
+
   }
 
   /**
-   * @param desiredStackVersion the desiredStackVersion to set
+   * Gets the desired repository version.
+   *
+   * @return the desired repository version.
    */
-  public void setDesiredStackVersion(String desiredStackVersion) {
-    this.desiredStackVersion = desiredStackVersion;
+  public String getDesiredRepositoryVersion() {
+    return desiredRepositoryVersion;
+  }
+
+  /**
+   * Gets the calculated repository version state from the components of this
+   * service.
+   *
+   * @return the desired repository version state
+   */
+  public RepositoryVersionState getRepositoryVersionState() {
+    return repositoryVersionState;
   }
 
   @Override
   public boolean equals(Object o) {
-    if (this == o) return true;
-    if (o == null || getClass() != o.getClass()) return false;
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
 
     ServiceResponse that = (ServiceResponse) o;
 
@@ -137,11 +161,11 @@ public class ServiceResponse {
 
     return true;
   }
-  
+
   public void setMaintenanceState(String state) {
     maintenanceState = state;
   }
-  
+
   public String getMaintenanceState() {
     return maintenanceState;
   }
@@ -194,4 +218,18 @@ public class ServiceResponse {
     return result;
   }
 
+  /**
+   * @param id
+   */
+  public void setDesiredRepositoryVersionId(Long id) {
+    desiredRepositoryVersionId = id;
+  }
+
+  /**
+   * @param id
+   */
+  public Long getDesiredRepositoryVersionId() {
+    return desiredRepositoryVersionId;
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java
index b26814a..5036bf4 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java
@@ -243,7 +243,7 @@ public abstract class AbstractControllerResourceProvider extends AbstractAuthori
    *
    * @return resource provider for the specified type
    */
-  ResourceProvider getResourceProvider(Resource.Type type) {
+  public static ResourceProvider getResourceProvider(Resource.Type type) {
     return ((ClusterControllerImpl) ClusterControllerHelper.getClusterController()).
         ensureResourceProvider(type);
   }


[34/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
index 9ea6083..93c02be 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProvider.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -20,7 +20,6 @@ package org.apache.ambari.server.controller.internal;
 import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.JDK_LOCATION;
 
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.EnumSet;
 import java.util.HashMap;
@@ -30,19 +29,15 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.ambari.annotations.Experimental;
+import org.apache.ambari.annotations.ExperimentalFeature;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.Role;
 import org.apache.ambari.server.StaticallyInject;
 import org.apache.ambari.server.actionmanager.ActionManager;
-import org.apache.ambari.server.actionmanager.HostRoleCommand;
-import org.apache.ambari.server.actionmanager.HostRoleCommandFactory;
-import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.actionmanager.RequestFactory;
 import org.apache.ambari.server.actionmanager.Stage;
 import org.apache.ambari.server.actionmanager.StageFactory;
-import org.apache.ambari.server.agent.CommandReport;
-import org.apache.ambari.server.agent.ExecutionCommand;
-import org.apache.ambari.server.agent.ExecutionCommand.KeyNames;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.ActionExecutionContext;
@@ -60,11 +55,8 @@ import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
 import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
-import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
 import org.apache.ambari.server.orm.entities.RepositoryEntity;
@@ -72,30 +64,26 @@ import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.security.authorization.RoleAuthorization;
-import org.apache.ambari.server.serveraction.upgrades.FinalizeUpgradeAction;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ComponentInfo;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.ConfigHelper;
 import org.apache.ambari.server.state.Host;
-import org.apache.ambari.server.state.MaintenanceState;
+import org.apache.ambari.server.state.RepositoryType;
 import org.apache.ambari.server.state.RepositoryVersionState;
+import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponentHost;
-import org.apache.ambari.server.state.ServiceInfo;
-import org.apache.ambari.server.state.ServiceOsSpecific;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.repository.VersionDefinitionXml;
+import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
 import org.apache.ambari.server.utils.StageUtils;
 import org.apache.ambari.server.utils.VersionUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.math.NumberUtils;
 
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
-import com.google.gson.Gson;
 import com.google.inject.Inject;
-import com.google.inject.Injector;
 import com.google.inject.Provider;
 import com.google.inject.persist.Transactional;
 
@@ -115,7 +103,6 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
   protected static final String CLUSTER_STACK_VERSION_HOST_STATES_PROPERTY_ID = PropertyHelper.getPropertyId("ClusterStackVersions", "host_states");
   protected static final String CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID  = PropertyHelper.getPropertyId("ClusterStackVersions", "repository_version");
   protected static final String CLUSTER_STACK_VERSION_STAGE_SUCCESS_FACTOR  = PropertyHelper.getPropertyId("ClusterStackVersions", "success_factor");
-  protected static final String CLUSTER_STACK_VERSION_IGNORE_PACKAGE_DEPENDENCIES = PropertyHelper.getPropertyId("ClusterStackVersions", KeyNames.IGNORE_PACKAGE_DEPENDENCIES);
 
   /**
    * Forces the {@link HostVersionEntity}s to a specific
@@ -129,7 +116,7 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
   protected static final String CLUSTER_STACK_VERSION_FORCE = "ClusterStackVersions/force";
 
   protected static final String INSTALL_PACKAGES_ACTION = "install_packages";
-  protected static final String INSTALL_PACKAGES_FULL_NAME = "Install version";
+  protected static final String INSTALL_PACKAGES_FULL_NAME = "Install Version";
 
   /**
    * The default success factor that will be used when determining if a stage's
@@ -153,7 +140,7 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
       CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID, CLUSTER_STACK_VERSION_STACK_PROPERTY_ID,
       CLUSTER_STACK_VERSION_VERSION_PROPERTY_ID, CLUSTER_STACK_VERSION_HOST_STATES_PROPERTY_ID,
       CLUSTER_STACK_VERSION_STATE_PROPERTY_ID, CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID,
-      CLUSTER_STACK_VERSION_STAGE_SUCCESS_FACTOR, CLUSTER_STACK_VERSION_IGNORE_PACKAGE_DEPENDENCIES, CLUSTER_STACK_VERSION_FORCE);
+      CLUSTER_STACK_VERSION_STAGE_SUCCESS_FACTOR, CLUSTER_STACK_VERSION_FORCE);
 
   private static Map<Type, String> keyPropertyIds = ImmutableMap.<Type, String> builder()
       .put(Type.Cluster, CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID)
@@ -164,21 +151,12 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
       .build();
 
   @Inject
-  private static ClusterVersionDAO clusterVersionDAO;
-
-  @Inject
   private static HostVersionDAO hostVersionDAO;
 
   @Inject
   private static RepositoryVersionDAO repositoryVersionDAO;
 
   @Inject
-  private static HostRoleCommandFactory hostRoleCommandFactory;
-
-  @Inject
-  private static Gson gson;
-
-  @Inject
   private static Provider<AmbariActionExecutionHelper> actionExecutionHelper;
 
   @Inject
@@ -191,24 +169,12 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
   private static Configuration configuration;
 
   @Inject
-  private static Injector injector;
+  private static RepositoryVersionHelper repoVersionHelper;
 
-  @Inject
-  private static HostComponentStateDAO hostComponentStateDAO;
 
-  /**
-   * Used for updating the existing stack tools with those of the stack being
-   * distributed.
-   */
-  @Inject
-  private static Provider<ConfigHelper> configHelperProvider;
 
-  /**
-   * We have to include such a hack here, because if we
-   * make finalizeUpgradeAction field static and request injection
-   * for it, there will be a circle dependency error
-   */
-  private FinalizeUpgradeAction finalizeUpgradeAction = injector.getInstance(FinalizeUpgradeAction.class);
+  @Inject
+  private static Provider<Clusters> clusters;
 
   /**
    * Constructor.
@@ -223,70 +189,87 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
   }
 
   @Override
+  @Experimental(feature = ExperimentalFeature.PATCH_UPGRADES,
+    comment = "this is a fake response until the UI no longer uses the endpoint")
   public Set<Resource> getResourcesAuthorized(Request request, Predicate predicate) throws
       SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
     final Set<Resource> resources = new HashSet<>();
+
     final Set<String> requestedIds = getRequestPropertyIds(request, predicate);
     final Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate);
 
-    List<ClusterVersionEntity> requestedEntities = new ArrayList<>();
-    for (Map<String, Object> propertyMap: propertyMaps) {
-      final String clusterName = propertyMap.get(CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID).toString();
-      final Long id;
-      if (propertyMap.get(CLUSTER_STACK_VERSION_ID_PROPERTY_ID) == null && propertyMaps.size() == 1) {
-        requestedEntities = clusterVersionDAO.findByCluster(clusterName);
-      } else {
-        try {
-          id = Long.parseLong(propertyMap.get(CLUSTER_STACK_VERSION_ID_PROPERTY_ID).toString());
-        } catch (Exception ex) {
-          throw new SystemException("Stack version should have numerical id");
-        }
-        final ClusterVersionEntity entity = clusterVersionDAO.findByPK(id);
-        if (entity == null) {
-          throw new NoSuchResourceException("There is no stack version with id " + id);
-        } else {
-          requestedEntities.add(entity);
-        }
+    if (1 != propertyMaps.size()) {
+      throw new SystemException("Cannot request more than one resource");
+    }
+
+    Map<String, Object> propertyMap = propertyMaps.iterator().next();
+
+    String clusterName = propertyMap.get(CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID).toString();
+    final Cluster cluster;
+    try {
+      cluster = clusters.get().getCluster(clusterName);
+    } catch (AmbariException e) {
+      throw new SystemException(e.getMessage(), e);
+    }
+
+    Set<Long> requestedEntities = new HashSet<>();
+
+    if (propertyMap.containsKey(CLUSTER_STACK_VERSION_ID_PROPERTY_ID)) {
+      Long id = Long.parseLong(propertyMap.get(CLUSTER_STACK_VERSION_ID_PROPERTY_ID).toString());
+      requestedEntities.add(id);
+    } else {
+      List<RepositoryVersionEntity> entities = repositoryVersionDAO.findAll();
+
+      for (RepositoryVersionEntity entity : entities) {
+        requestedEntities.add(entity.getId());
       }
     }
 
-    for (ClusterVersionEntity entity: requestedEntities) {
+    if (requestedEntities.isEmpty()) {
+      throw new SystemException("Could not find any repositories to show");
+    }
+
+
+    for (Long repositoryVersionId : requestedEntities) {
       final Resource resource = new ResourceImpl(Resource.Type.ClusterStackVersion);
 
-      final Map<String, List<String>> hostStates = new HashMap<>();
+      RepositoryVersionEntity repositoryVersion = repositoryVersionDAO.findByPK(repositoryVersionId);
+
+      final List<RepositoryVersionState> allStates = new ArrayList<>();
+      final Map<RepositoryVersionState, List<String>> hostStates = new HashMap<>();
       for (RepositoryVersionState state: RepositoryVersionState.values()) {
-        hostStates.put(state.name(), new ArrayList<String>());
+        hostStates.put(state, new ArrayList<String>());
       }
 
-      StackEntity repoVersionStackEntity = entity.getRepositoryVersion().getStack();
+      StackEntity repoVersionStackEntity = repositoryVersion.getStack();
       StackId repoVersionStackId = new StackId(repoVersionStackEntity);
 
-      for (HostVersionEntity hostVersionEntity : hostVersionDAO.findByClusterStackAndVersion(
-          entity.getClusterEntity().getClusterName(), repoVersionStackId,
-          entity.getRepositoryVersion().getVersion())) {
+      List<HostVersionEntity> hostVersionsForRepository = hostVersionDAO.findHostVersionByClusterAndRepository(
+          cluster.getClusterId(), repositoryVersion);
 
-        hostStates.get(hostVersionEntity.getState().name()).add(hostVersionEntity.getHostName());
+      // create the in-memory structures
+      for (HostVersionEntity hostVersionEntity : hostVersionsForRepository) {
+        hostStates.get(hostVersionEntity.getState()).add(hostVersionEntity.getHostName());
+        allStates.add(hostVersionEntity.getState());
       }
 
-      StackId stackId = new StackId(entity.getRepositoryVersion().getStack());
-      RepositoryVersionEntity repoVerEntity = repositoryVersionDAO.findByStackAndVersion(
-          stackId, entity.getRepositoryVersion().getVersion());
-
-      setResourceProperty(resource, CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID, entity.getClusterEntity().getClusterName(), requestedIds);
+      setResourceProperty(resource, CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID, clusterName, requestedIds);
       setResourceProperty(resource, CLUSTER_STACK_VERSION_HOST_STATES_PROPERTY_ID, hostStates, requestedIds);
-      setResourceProperty(resource, CLUSTER_STACK_VERSION_ID_PROPERTY_ID, entity.getId(), requestedIds);
-      setResourceProperty(resource, CLUSTER_STACK_VERSION_STACK_PROPERTY_ID, stackId.getStackName(), requestedIds);
-      setResourceProperty(resource, CLUSTER_STACK_VERSION_STATE_PROPERTY_ID, entity.getState().name(), requestedIds);
-      setResourceProperty(resource, CLUSTER_STACK_VERSION_VERSION_PROPERTY_ID, stackId.getStackVersion(), requestedIds);
-      if (repoVerEntity!=null) {
-        Long repoVersionId = repoVerEntity.getId();
-        setResourceProperty(resource, CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID, repoVersionId, requestedIds);
-      }
+      setResourceProperty(resource, CLUSTER_STACK_VERSION_ID_PROPERTY_ID, repositoryVersion.getId(), requestedIds);
+      setResourceProperty(resource, CLUSTER_STACK_VERSION_STACK_PROPERTY_ID, repoVersionStackId.getStackName(), requestedIds);
+      setResourceProperty(resource, CLUSTER_STACK_VERSION_VERSION_PROPERTY_ID, repoVersionStackId.getStackVersion(), requestedIds);
+      setResourceProperty(resource, CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID, repositoryVersion.getId(), requestedIds);
+
+      @Experimental(feature = ExperimentalFeature.PATCH_UPGRADES,
+          comment = "this is a fake status until the UI can handle services that are on their own")
+      RepositoryVersionState aggregateState = RepositoryVersionState.getAggregateState(allStates);
+      setResourceProperty(resource, CLUSTER_STACK_VERSION_STATE_PROPERTY_ID, aggregateState, requestedIds);
 
       if (predicate == null || predicate.evaluate(resource)) {
         resources.add(resource);
       }
     }
+
     return resources;
   }
 
@@ -304,6 +287,8 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
 
     String clName;
     final String desiredRepoVersion;
+    String stackName;
+    String stackVersion;
 
     Map<String, Object> propertyMap = iterator.next();
 
@@ -342,35 +327,35 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
           cluster.getClusterName(), entity.getDirection().getText(false)));
     }
 
-    String stackName = (String) propertyMap.get(CLUSTER_STACK_VERSION_STACK_PROPERTY_ID);
-    String stackVersion = (String) propertyMap.get(CLUSTER_STACK_VERSION_VERSION_PROPERTY_ID);
-    if (StringUtils.isBlank(stackName) || StringUtils.isBlank(stackVersion)) {
-      String message = String.format(
-          "Both the %s and %s properties are required when distributing a new stack",
-          CLUSTER_STACK_VERSION_STACK_PROPERTY_ID, CLUSTER_STACK_VERSION_VERSION_PROPERTY_ID);
-
-      throw new SystemException(message);
+    Set<StackId> stackIds = new HashSet<>();
+    if (propertyMap.containsKey(CLUSTER_STACK_VERSION_STACK_PROPERTY_ID) &&
+            propertyMap.containsKey(CLUSTER_STACK_VERSION_VERSION_PROPERTY_ID)) {
+      stackName = (String) propertyMap.get(CLUSTER_STACK_VERSION_STACK_PROPERTY_ID);
+      stackVersion = (String) propertyMap.get(CLUSTER_STACK_VERSION_VERSION_PROPERTY_ID);
+      StackId stackId = new StackId(stackName, stackVersion);
+      if (! ami.isSupportedStack(stackName, stackVersion)) {
+        throw new NoSuchParentResourceException(String.format("Stack %s is not supported",
+                stackId));
+      }
+      stackIds.add(stackId);
+    } else { // Using stack that is current for cluster
+      for (Service service : cluster.getServices().values()) {
+        stackIds.add(service.getDesiredStackId());
+      }
     }
 
-    final StackId stackId = new StackId(stackName, stackVersion);
-
-    if (!ami.isSupportedStack(stackName, stackVersion)) {
-      throw new NoSuchParentResourceException(String.format("Stack %s is not supported", stackId));
+    if (stackIds.size() > 1) {
+      throw new SystemException("Could not determine stack to add out of " + StringUtils.join(stackIds, ','));
     }
 
-    // bootstrap the stack tools if necessary for the stack which is being
-    // distributed
-    try {
-      bootstrapStackTools(stackId, cluster);
-    } catch (AmbariException ambariException) {
-      throw new SystemException("Unable to modify stack tools for new stack being distributed",
-          ambariException);
-    }
+    StackId stackId = stackIds.iterator().next();
+    stackName = stackId.getStackName();
+    stackVersion = stackId.getStackVersion();
 
-    RepositoryVersionEntity repoVersionEnt = repositoryVersionDAO.findByStackAndVersion(
+    RepositoryVersionEntity repoVersionEntity = repositoryVersionDAO.findByStackAndVersion(
         stackId, desiredRepoVersion);
 
-    if (repoVersionEnt == null) {
+    if (repoVersionEntity == null) {
       throw new IllegalArgumentException(String.format(
               "Repo version %s is not available for stack %s",
               desiredRepoVersion, stackId));
@@ -378,130 +363,87 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
 
     VersionDefinitionXml desiredVersionDefinition = null;
     try {
-      desiredVersionDefinition = repoVersionEnt.getRepositoryXml();
+      desiredVersionDefinition = repoVersionEntity.getRepositoryXml();
     } catch (Exception e) {
       throw new IllegalArgumentException(
           String.format("Version %s is backed by a version definition, but it could not be parsed", desiredRepoVersion), e);
     }
 
-    // get all of the host eligible for stack distribution
-    List<Host> hosts = getHostsForStackDistribution(cluster);
+    // if true, then we need to force all new host versions into the INSTALLED state
+    boolean forceInstalled = Boolean.parseBoolean((String)propertyMap.get(
+        CLUSTER_STACK_VERSION_FORCE));
 
-    /*
-    If there is a repository that is already ATTEMPTED to be installed and the version
-    is GREATER than the one trying to install, we must fail (until we can support that via Patch Upgrades)
+    try {
+      // either create the necessary host version entries, or set them to INSTALLING when attempting to re-distribute an existing version
+      return createOrUpdateHostVersions(cluster, repoVersionEntity, desiredVersionDefinition,
+          stackId, forceInstalled, propertyMap);
+    } catch (AmbariException e) {
+      throw new SystemException("Can not persist request", e);
+    }
+  }
 
-    For example:
+  @Transactional
+  RequestStatus createOrUpdateHostVersions(Cluster cluster,
+      RepositoryVersionEntity repoVersionEntity, VersionDefinitionXml versionDefinitionXml,
+      StackId stackId, boolean forceInstalled, Map<String, Object> propertyMap)
+      throws AmbariException, SystemException {
 
-    1. Install 2.3.0.0
-    2. Register and Install 2.5.0.0 (with or without package-version; it gets computed correctly)
-    3. Register 2.4 (without package-version)
+    final String desiredRepoVersion = repoVersionEntity.getVersion();
 
-    Installation of 2.4 will fail because the way agents invoke installation is to
-    install by name.  if the package-version is not known, then the 'newest' is ALWAYS installed.
-    In this case, 2.5.0.0.  2.4 is never picked up.
-    */
-    for (ClusterVersionEntity clusterVersion : clusterVersionDAO.findByCluster(clName)) {
-      RepositoryVersionEntity clusterRepoVersion = clusterVersion.getRepositoryVersion();
+    // get all of the hosts eligible for stack distribution
+    List<Host> hosts = Lists.newArrayList(cluster.getHosts());
 
-      int compare = compareVersions(clusterRepoVersion.getVersion(), desiredRepoVersion);
 
-      // ignore earlier versions
-      if (compare <= 0) {
-        continue;
-      }
+    for (Host host : hosts) {
+      for (HostVersionEntity hostVersion : host.getAllHostVersions()) {
+        RepositoryVersionEntity hostRepoVersion = hostVersion.getRepositoryVersion();
 
-      // !!! the version is greater to the one to install
+        // !!! ignore stack differences
+        if (!hostRepoVersion.getStackName().equals(repoVersionEntity.getStackName())) {
+          continue;
+        }
 
-      // if the stacks are different, then don't fail (further check same-stack version strings)
-      if (!StringUtils.equals(clusterRepoVersion.getStackName(), repoVersionEnt.getStackName())) {
-        continue;
-      }
+        int compare = compareVersions(hostRepoVersion.getVersion(), desiredRepoVersion);
 
-      // if there is no backing VDF for the desired version, allow the operation (legacy behavior)
-      if (null == desiredVersionDefinition) {
-        continue;
-      }
+        // ignore earlier versions
+        if (compare <= 0) {
+          continue;
+        }
+
+        // !!! the version is greater to the one to install
+
+        // if there is no backing VDF for the desired version, allow the operation (legacy behavior)
+        if (null == versionDefinitionXml) {
+          continue;
+        }
 
-      // backing VDF does not define the package version for any of the hosts, cannot install (allows a VDF with package-version)
-      for (Host host : hosts) {
-        if (StringUtils.isBlank(desiredVersionDefinition.getPackageVersion(host.getOsFamily()))) {
+        if (StringUtils.isBlank(versionDefinitionXml.getPackageVersion(host.getOsFamily()))) {
           String msg = String.format("Ambari cannot install version %s.  Version %s is already installed.",
-            desiredRepoVersion, clusterRepoVersion.getVersion());
+            desiredRepoVersion, hostRepoVersion.getVersion());
           throw new IllegalArgumentException(msg);
         }
       }
     }
 
-    // if true, then we need to force all new host versions into the INSTALLED state
-    boolean forceInstalled = Boolean.parseBoolean((String)propertyMap.get(
-        CLUSTER_STACK_VERSION_FORCE));
-
-    final RequestStatusResponse response;
 
-    try {
-      if (forceInstalled) {
-        createHostVersions(cluster, hosts, stackId, desiredRepoVersion, RepositoryVersionState.INSTALLED);
-        response = null;
-      } else {
-        createHostVersions(cluster, hosts, stackId, desiredRepoVersion,
-            RepositoryVersionState.INSTALLING);
+    // the cluster will create/update all of the host versions to the correct state
+    List<Host> hostsNeedingInstallCommands = cluster.transitionHostsToInstalling(
+        repoVersionEntity, versionDefinitionXml, forceInstalled);
 
-        RequestStageContainer installRequest = createOrchestration(cluster, stackId, hosts,
-            repoVersionEnt, propertyMap);
+    RequestStatusResponse response = null;
+    if (!forceInstalled) {
+      RequestStageContainer installRequest = createOrchestration(cluster, stackId,
+          hostsNeedingInstallCommands, repoVersionEntity, versionDefinitionXml, propertyMap);
 
-        response = installRequest.getRequestStatusResponse();
-      }
-    } catch (AmbariException e) {
-      throw new SystemException("Can not persist request", e);
+      response = installRequest.getRequestStatusResponse();
     }
 
     return getRequestStatus(response);
   }
 
   @Transactional
-  void createHostVersions(Cluster cluster, List<Host> hosts, StackId stackId,
-      String desiredRepoVersion, RepositoryVersionState repoState)
-      throws AmbariException, SystemException {
-    final String clusterName = cluster.getClusterName();
-    final String authName = getManagementController().getAuthName();
-
-    ClusterVersionEntity clusterVersionEntity = clusterVersionDAO.findByClusterAndStackAndVersion(
-        clusterName, stackId, desiredRepoVersion);
-
-    if (clusterVersionEntity == null) {
-      try {
-        // Create/persist new cluster stack version
-        cluster.createClusterVersion(stackId, desiredRepoVersion, authName, repoState);
-
-        clusterVersionEntity = clusterVersionDAO.findByClusterAndStackAndVersion(clusterName,
-            stackId, desiredRepoVersion);
-      } catch (AmbariException e) {
-        throw new SystemException(
-            String.format("Can not create cluster stack version %s for cluster %s",
-                desiredRepoVersion, clusterName), e);
-      }
-    } else {
-      // Move cluster version into the specified state (retry installation)
-      cluster.transitionClusterVersion(stackId, desiredRepoVersion, repoState);
-    }
-
-    // Will also initialize all Host Versions to the specified state state.
-    cluster.transitionHosts(clusterVersionEntity, repoState);
-
-    // Directly transition host versions to NOT_REQUIRED for hosts that don't
-    // have versionable components
-    for (Host host : hosts) {
-      if (!host.hasComponentsAdvertisingVersions(stackId)) {
-        transitionHostVersionToNotRequired(host, cluster,
-            clusterVersionEntity.getRepositoryVersion());
-      }
-    }
-  }
-
-  @Transactional
   RequestStageContainer createOrchestration(Cluster cluster, StackId stackId,
-      List<Host> hosts, RepositoryVersionEntity repoVersionEnt, Map<String, Object> propertyMap)
+      List<Host> hosts, RepositoryVersionEntity repoVersionEnt, VersionDefinitionXml desiredVersionDefinition, Map<String, Object> propertyMap)
       throws AmbariException, SystemException {
     final AmbariManagementController managementController = getManagementController();
     final AmbariMetaInfo ami = managementController.getAmbariMetaInfo();
@@ -550,9 +492,6 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
       successFactor = Float.valueOf(successFactorProperty);
     }
 
-    Object ignorePackageDependenciesProperty = propertyMap.get(CLUSTER_STACK_VERSION_IGNORE_PACKAGE_DEPENDENCIES);
-    String ignorePackageDependencies = Boolean.valueOf(String.valueOf(ignorePackageDependenciesProperty)).toString();
-
     boolean hasStage = false;
 
     ArrayList<Stage> stages = new ArrayList<>(batchCount);
@@ -585,8 +524,11 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
       // determine services for the repo
       Set<String> serviceNames = new HashSet<>();
 
-      // !!! TODO for patch upgrades, we need to limit the serviceNames to those
-      // that are detailed for the repository
+      // !!! limit the serviceNames to those that are detailed for the repository.
+      // TODO packages don't have component granularity
+      if (RepositoryType.STANDARD != repoVersionEnt.getType()) {
+        serviceNames.addAll(desiredVersionDefinition.getAvailableServiceNames());
+      }
 
       // Populate with commands for host
       for (int i = 0; i < maxTasks && hostIterator.hasNext(); i++) {
@@ -595,7 +537,6 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
           ActionExecutionContext actionContext = getHostVersionInstallCommand(repoVersionEnt,
                   cluster, managementController, ami, stackId, serviceNames, perOsRepos, stage, host);
           if (null != actionContext) {
-            actionContext.getParameters().put(KeyNames.IGNORE_PACKAGE_DEPENDENCIES, ignorePackageDependencies);
             try {
               actionExecutionHelper.get().addExecutionCommandsToStage(actionContext, stage, null);
               hasStage = true;
@@ -626,6 +567,7 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
           throws SystemException {
     // Determine repositories for host
     String osFamily = host.getOsFamily();
+
     final List<RepositoryEntity> repoInfo = perOsRepos.get(osFamily);
     if (repoInfo == null) {
       throw new SystemException(String.format("Repositories for os type %s are " +
@@ -633,9 +575,11 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
         osFamily, repoVersion.getVersion(), stackId));
     }
 
+    if (repoInfo.isEmpty()){
+      LOG.error(String.format("Repository list is empty. Ambari may not be managing the repositories for %s", osFamily));
+    }
 
     // determine packages for all services that are installed on host
-    List<ServiceOsSpecific.Package> packages = new ArrayList<>();
     Set<String> servicesOnHost = new HashSet<>();
     List<ServiceComponentHost> components = cluster.getServiceComponentHosts(host.getHostName());
     for (ServiceComponentHost component : components) {
@@ -647,76 +591,25 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
     if (servicesOnHost.isEmpty()) {
       return null;
     }
-    List<String> blacklistedPackagePrefixes = configuration.getRollingUpgradeSkipPackagesPrefixes();
-    for (String serviceName : servicesOnHost) {
-      try{
-        if(ami.isServiceRemovedInStack(stackId.getStackName(), stackId.getStackVersion(), serviceName)){
-          LOG.info(String.format("%s has been removed from stack %s-%s. Skip calculating its installation packages", stackId.getStackName(), stackId.getStackVersion(), serviceName));
-          continue; //No need to calculate install packages for removed services
-        }
-      } catch (AmbariException e1) {
-        throw new SystemException(String.format("Cannot obtain stack information for %s-%s", stackId.getStackName(), stackId.getStackVersion()), e1);
-      }
 
-      ServiceInfo info;
-      try {
-        info = ami.getService(stackId.getStackName(), stackId.getStackVersion(), serviceName);
-      } catch (AmbariException e) {
-        throw new SystemException("Cannot enumerate services", e);
-      }
 
-      List<ServiceOsSpecific.Package> packagesForService = managementController.getPackagesForServiceHost(info,
-              new HashMap<String, String>(), // Contents are ignored
-        osFamily);
-      for (ServiceOsSpecific.Package aPackage : packagesForService) {
-        if (! aPackage.getSkipUpgrade()) {
-          boolean blacklisted = false;
-          for(String prefix : blacklistedPackagePrefixes) {
-            if (aPackage.getName().startsWith(prefix)) {
-              blacklisted = true;
-              break;
-            }
-          }
-          if (! blacklisted) {
-            packages.add(aPackage);
-          }
-        }
-      }
-    }
-
-    final String packageList = gson.toJson(packages);
-    final String repoList = gson.toJson(repoInfo);
-
-    Map<String, String> params = new HashMap<>();
-    params.put("stack_id", stackId.getStackId());
-    params.put("repository_version", repoVersion.getVersion());
-    params.put("base_urls", repoList);
-    params.put(KeyNames.PACKAGE_LIST, packageList);
-    params.put(KeyNames.REPO_VERSION_ID, repoVersion.getId().toString());
-
-    VersionDefinitionXml xml = null;
-    try {
-      xml = repoVersion.getRepositoryXml();
-    } catch (Exception e) {
-      throw new SystemException(String.format("Could not load xml from repo version %s",
-          repoVersion.getVersion()));
-    }
-
-    if (null != xml && StringUtils.isNotBlank(xml.getPackageVersion(osFamily))) {
-      params.put(KeyNames.PACKAGE_VERSION, xml.getPackageVersion(osFamily));
-    }
+    Map<String, String> roleParams = repoVersionHelper.buildRoleParams(managementController, repoVersion,
+        osFamily, servicesOnHost);
 
     // add host to this stage
     RequestResourceFilter filter = new RequestResourceFilter(null, null,
             Collections.singletonList(host.getHostName()));
 
-    ActionExecutionContext actionContext = new ActionExecutionContext(cluster.getClusterName(),
-        INSTALL_PACKAGES_ACTION, Collections.singletonList(filter), params);
-
-    actionContext.setStackId(stackId);
+    ActionExecutionContext actionContext = new ActionExecutionContext(
+            cluster.getClusterName(), INSTALL_PACKAGES_ACTION,
+            Collections.singletonList(filter),
+            roleParams);
     actionContext.setTimeout(Short.valueOf(configuration.getDefaultAgentTaskTimeout(true)));
 
+    repoVersionHelper.addCommandRepository(actionContext, osFamily, repoVersion, repoInfo);
+
     return actionContext;
+
   }
 
 
@@ -752,29 +645,6 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
     return false;
   }
 
-
-  /**
-   *  Sets host versions states to not-required.
-   *
-   *  Transitioning host version to NOT_REQUIRED state manually is ok since
-   *  other completion handlers set success/fail states correctly during heartbeat.
-   *  The number of NOT_REQUIRED components for a cluster will be low.
-   */
-  private void transitionHostVersionToNotRequired(Host host, Cluster cluster, RepositoryVersionEntity repoVersion) {
-    LOG.info(String.format("Transitioning version %s on host %s directly to %s" +
-                    " without distributing bits to host since it has no versionable components.",
-            repoVersion.getVersion(), host.getHostName(), RepositoryVersionState.NOT_REQUIRED));
-
-    for (HostVersionEntity hve : host.getAllHostVersions()) {
-      if (hve.getRepositoryVersion().equals(repoVersion)) {
-        hve.setState(RepositoryVersionState.NOT_REQUIRED);
-        hostVersionDAO.merge(hve);
-      }
-    }
-
-  }
-
-
   private RequestStageContainer createRequest() {
     ActionManager actionManager = getManagementController().getActionManager();
 
@@ -785,131 +655,6 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
     return requestStages;
   }
 
-  /**
-   * The only appliance of this method is triggering Finalize during
-   * manual Stack Upgrade
-   */
-  @Override
-  public RequestStatus updateResourcesAuthorized(Request request, Predicate predicate)
-      throws SystemException, UnsupportedPropertyException,
-      NoSuchResourceException, NoSuchParentResourceException {
-    try {
-      Iterator<Map<String, Object>> iterator = request.getProperties().iterator();
-      String clName;
-      final String desiredRepoVersion;
-      if (request.getProperties().size() != 1) {
-        throw new UnsupportedOperationException("Multiple requests cannot be executed at the same time.");
-      }
-      Map<String, Object> propertyMap = iterator.next();
-
-      Set<String> requiredProperties = new HashSet<>();
-      requiredProperties.add(CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID);
-      requiredProperties.add(CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID);
-      requiredProperties.add(CLUSTER_STACK_VERSION_STATE_PROPERTY_ID);
-
-      for (String requiredProperty : requiredProperties) {
-        if (!propertyMap.containsKey(requiredProperty)) {
-          throw new IllegalArgumentException(
-                  String.format("The required property %s is not defined",
-                          requiredProperty));
-        }
-      }
-
-      clName = (String) propertyMap.get(CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID);
-      String desiredDisplayRepoVersion = (String) propertyMap.get(CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID);
-      RepositoryVersionEntity rve = repositoryVersionDAO.findByDisplayName(desiredDisplayRepoVersion);
-      if (rve == null) {
-        throw new IllegalArgumentException(
-                  String.format("Repository version with display name %s does not exist",
-                          desiredDisplayRepoVersion));
-      }
-      desiredRepoVersion = rve.getVersion();
-      String newStateStr = (String) propertyMap.get(CLUSTER_STACK_VERSION_STATE_PROPERTY_ID);
-
-      LOG.info("Initiating finalization for manual upgrade to version {} for cluster {}",
-              desiredRepoVersion, clName);
-
-      // First, set desired cluster stack version to enable cross-stack upgrade
-      StackId stackId = rve.getStackId();
-      Cluster cluster = getManagementController().getClusters().getCluster(clName);
-      cluster.setDesiredStackVersion(stackId);
-
-      String forceCurrent = (String) propertyMap.get(CLUSTER_STACK_VERSION_FORCE);
-      boolean force = false;
-      if (null != forceCurrent) {
-        force = Boolean.parseBoolean(forceCurrent);
-      }
-
-      if (!force) {
-        Map<String, String> args = new HashMap<>();
-        if (newStateStr.equals(RepositoryVersionState.CURRENT.toString())) {
-          // Finalize upgrade workflow
-          args.put(FinalizeUpgradeAction.UPGRADE_DIRECTION_KEY, "upgrade");
-        } else if (newStateStr.equals(RepositoryVersionState.INSTALLED.toString())) {
-          // Finalize downgrade workflow
-          args.put(FinalizeUpgradeAction.UPGRADE_DIRECTION_KEY, "downgrade");
-        } else {
-          throw new IllegalArgumentException(
-            String.format("Invalid desired state %s. Should be either CURRENT or INSTALLED",
-                    newStateStr));
-        }
-
-        // Get a host name to populate the hostrolecommand table's hostEntity.
-        String defaultHostName;
-        ArrayList<Host> hosts = new ArrayList<>(cluster.getHosts());
-        if (!hosts.isEmpty()) {
-          Collections.sort(hosts);
-          defaultHostName = hosts.get(0).getHostName();
-        } else {
-          throw new AmbariException("Could not find at least one host to set the command for");
-        }
-
-        args.put(FinalizeUpgradeAction.VERSION_KEY, desiredRepoVersion);
-        args.put(FinalizeUpgradeAction.CLUSTER_NAME_KEY, clName);
-
-        ExecutionCommand command = new ExecutionCommand();
-        command.setCommandParams(args);
-        command.setClusterName(clName);
-        finalizeUpgradeAction.setExecutionCommand(command);
-
-        HostRoleCommand hostRoleCommand = hostRoleCommandFactory.create(defaultHostName,
-                Role.AMBARI_SERVER_ACTION, null, null);
-        finalizeUpgradeAction.setHostRoleCommand(hostRoleCommand);
-
-        CommandReport report = finalizeUpgradeAction.execute(null);
-
-        LOG.info("Finalize output:");
-        LOG.info("STDOUT: {}", report.getStdOut());
-        LOG.info("STDERR: {}", report.getStdErr());
-
-        if (report.getStatus().equals(HostRoleStatus.COMPLETED.toString())) {
-          return getRequestStatus(null);
-        } else {
-          String detailedOutput = "Finalization failed. More details: \n" +
-                  "STDOUT: " + report.getStdOut() + "\n" +
-                  "STDERR: " + report.getStdErr();
-          throw new SystemException(detailedOutput);
-        }
-      } else {
-        // !!! revisit for PU
-        // If forcing to become CURRENT, get the Cluster Version whose state is CURRENT and make sure that
-        // the Host Version records for the same Repo Version are also marked as CURRENT.
-        ClusterVersionEntity current = cluster.getCurrentClusterVersion();
-
-        if (!current.getRepositoryVersion().equals(rve)) {
-          updateVersionStates(current.getClusterId(), current.getRepositoryVersion(), rve);
-        }
-
-
-        return getRequestStatus(null);
-      }
-    } catch (AmbariException e) {
-      throw new SystemException("Cannot perform request", e);
-    } catch (InterruptedException e) {
-      throw new SystemException("Cannot perform request", e);
-    }
-  }
-
   @Override
   public RequestStatus deleteResourcesAuthorized(Request request, Predicate predicate)
       throws SystemException, UnsupportedPropertyException,
@@ -923,44 +668,6 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
   }
 
   /**
-   * Gets all of the hosts in a cluster which are not in "maintenance mode" and
-   * are considered to be healthy. In the case of stack distribution, a host
-   * must be explicitely marked as being in maintenance mode for it to be
-   * considered as unhealthy.
-   *
-   * @param cluster
-   *          the cluster (not {@code null}).
-   * @return the list of hosts that are not in maintenance mode and are
-   *         elidgable to have a stack distributed to them.
-   */
-  private List<Host> getHostsForStackDistribution(Cluster cluster) {
-    Collection<Host> hosts = cluster.getHosts();
-    List<Host> healthyHosts = new ArrayList<>(hosts.size());
-    for (Host host : hosts) {
-      if (host.getMaintenanceState(cluster.getClusterId()) == MaintenanceState.OFF) {
-        healthyHosts.add(host);
-      }
-    }
-
-    return healthyHosts;
-  }
-
-  /**
-   * Updates the version states.  Transactional to ensure only one transaction for all updates
-   * @param clusterId the cluster
-   * @param current   the repository that is current for the cluster
-   * @param target    the target repository
-   */
-  @Transactional
-  protected void updateVersionStates(Long clusterId, RepositoryVersionEntity current,
-      RepositoryVersionEntity target) {
-
-    hostComponentStateDAO.updateVersions(target.getVersion());
-    hostVersionDAO.updateVersions(target, current);
-    clusterVersionDAO.updateVersions(clusterId, target, current);
-  }
-
-  /**
    * Additional check over {@link VersionUtils#compareVersions(String, String)} that
    * compares build numbers
    */
@@ -991,101 +698,4 @@ public class ClusterStackVersionResourceProvider extends AbstractControllerResou
   }
 
 
-  /**
-   * Ensures that the stack tools and stack features are set on
-   * {@link ConfigHelper#CLUSTER_ENV} for the stack of the repository being
-   * distributed. This step ensures that the new repository can be distributed
-   * with the correct tools.
-   * <p/>
-   * If the cluster's current stack name matches that of the new stack or the
-   * new stack's tools are already added in the configuration, then this method
-   * will not change anything.
-   *
-   * @param stackId
-   *          the stack of the repository being distributed (not {@code null}).
-   * @param cluster
-   *          the cluster the new stack/repo is being distributed for (not
-   *          {@code null}).
-   * @throws AmbariException
-   */
-  private void bootstrapStackTools(StackId stackId, Cluster cluster) throws AmbariException {
-    // if the stack name is the same as the cluster's current stack name, then
-    // there's no work to do
-    if (StringUtils.equals(stackId.getStackName(),
-        cluster.getCurrentStackVersion().getStackName())) {
-      return;
-    }
-
-    ConfigHelper configHelper = configHelperProvider.get();
-
-    // get the stack tools/features for the stack being distributed
-    Map<String, Map<String, String>> defaultStackConfigurationsByType = configHelper.getDefaultProperties(
-        stackId, cluster);
-
-    Map<String, String> clusterEnvDefaults = defaultStackConfigurationsByType.get(
-        ConfigHelper.CLUSTER_ENV);
-
-    Config clusterEnv = cluster.getDesiredConfigByType(ConfigHelper.CLUSTER_ENV);
-    Map<String, String> clusterEnvProperties = clusterEnv.getProperties();
-
-    // the 3 properties we need to check and update
-    Set<String> properties = Sets.newHashSet(ConfigHelper.CLUSTER_ENV_STACK_ROOT_PROPERTY,
-        ConfigHelper.CLUSTER_ENV_STACK_TOOLS_PROPERTY,
-        ConfigHelper.CLUSTER_ENV_STACK_FEATURES_PROPERTY);
-
-    // any updates are stored here and merged into the existing config type
-    Map<String, String> updatedProperties = new HashMap<>();
-
-    for (String property : properties) {
-      // determine if the property exists in the stack being distributed (it
-      // kind of has to, but we'll be safe if it's not found)
-      String newStackDefaultJson = clusterEnvDefaults.get(property);
-      if (StringUtils.isBlank(newStackDefaultJson)) {
-        continue;
-      }
-
-      String existingPropertyJson = clusterEnvProperties.get(property);
-
-      // if the stack tools/features property doesn't exist, then just set the
-      // one from the new stack
-      if (StringUtils.isBlank(existingPropertyJson)) {
-        updatedProperties.put(property, newStackDefaultJson);
-        continue;
-      }
-
-      // now is the hard part - we need to check to see if the new stack tools
-      // exists alongside the current tools and if it doesn't, then add the new
-      // tools in
-      final Map<String, Object> existingJson;
-      final Map<String, ?> newStackJsonAsObject;
-      if (StringUtils.equals(property, ConfigHelper.CLUSTER_ENV_STACK_ROOT_PROPERTY)) {
-        existingJson = gson.<Map<String, Object>> fromJson(existingPropertyJson, Map.class);
-        newStackJsonAsObject = gson.<Map<String, String>> fromJson(newStackDefaultJson, Map.class);
-      } else {
-        existingJson = gson.<Map<String, Object>> fromJson(existingPropertyJson,
-            Map.class);
-
-        newStackJsonAsObject = gson.<Map<String, Map<Object, Object>>> fromJson(newStackDefaultJson,
-            Map.class);
-      }
-
-      if (existingJson.keySet().contains(stackId.getStackName())) {
-        continue;
-      }
-
-      existingJson.put(stackId.getStackName(), newStackJsonAsObject.get(stackId.getStackName()));
-
-      String newJson = gson.toJson(existingJson);
-      updatedProperties.put(property, newJson);
-    }
-
-    if (!updatedProperties.isEmpty()) {
-      AmbariManagementController amc = getManagementController();
-      String serviceNote = String.format(
-          "Adding stack tools for %s while distributing a new repository", stackId.toString());
-
-      configHelper.updateConfigType(cluster, amc, clusterEnv.getType(), updatedProperties, null,
-          amc.getAuthName(), serviceNote);
-    }
-  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ComponentResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ComponentResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ComponentResourceProvider.java
index 65cfcaa..b35903b 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ComponentResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ComponentResourceProvider.java
@@ -89,6 +89,9 @@ public class ComponentResourceProvider extends AbstractControllerResourceProvide
   protected static final String COMPONENT_UNKNOWN_COUNT_PROPERTY_ID   = "ServiceComponentInfo/unknown_count";
   protected static final String COMPONENT_INSTALL_FAILED_COUNT_PROPERTY_ID = "ServiceComponentInfo/install_failed_count";
   protected static final String COMPONENT_RECOVERY_ENABLED_ID         = "ServiceComponentInfo/recovery_enabled";
+  protected static final String COMPONENT_DESIRED_STACK               = "ServiceComponentInfo/desired_stack";
+  protected static final String COMPONENT_DESIRED_VERSION             = "ServiceComponentInfo/desired_version";
+  protected static final String COMPONENT_REPOSITORY_STATE            = "ServiceComponentInfo/repository_state";
 
   private static final String TRUE = "true";
 
@@ -324,7 +327,7 @@ public class ComponentResourceProvider extends AbstractControllerResourceProvide
         // Expected
       }
 
-      StackId stackId = s.getDesiredStackVersion();
+      StackId stackId = s.getDesiredStackId();
       if (!ambariMetaInfo.isValidServiceComponent(stackId.getStackName(),
           stackId.getStackVersion(), s.getName(), request.getComponentName())) {
         throw new IllegalArgumentException("Unsupported or invalid component"
@@ -349,7 +352,7 @@ public class ComponentResourceProvider extends AbstractControllerResourceProvide
       Cluster cluster = clusters.getCluster(request.getClusterName());
       Service s = cluster.getService(request.getServiceName());
       ServiceComponent sc = serviceComponentFactory.createNew(s, request.getComponentName());
-      sc.setDesiredStackVersion(s.getDesiredStackVersion());
+      sc.setDesiredRepositoryVersion(s.getDesiredRepositoryVersion());
 
       if (StringUtils.isNotEmpty(request.getDesiredState())) {
         State state = State.valueOf(request.getDesiredState());
@@ -367,7 +370,7 @@ public class ComponentResourceProvider extends AbstractControllerResourceProvide
         sc.setRecoveryEnabled(recoveryEnabled);
         LOG.info("Component: {}, recovery_enabled from request: {}", request.getComponentName(), recoveryEnabled);
       } else {
-        StackId stackId = s.getDesiredStackVersion();
+        StackId stackId = s.getDesiredStackId();
         ComponentInfo componentInfo = ambariMetaInfo.getComponent(stackId.getStackName(),
                 stackId.getStackVersion(), s.getName(), request.getComponentName());
         if (componentInfo == null) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostComponentResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostComponentResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostComponentResourceProvider.java
index c8ec08b..aaf4656 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostComponentResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostComponentResourceProvider.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -50,7 +50,6 @@ import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.security.authorization.AuthorizationHelper;
 import org.apache.ambari.server.security.authorization.ResourceType;
@@ -97,10 +96,12 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
       = PropertyHelper.getPropertyId("HostRoles", "state");
   public static final String HOST_COMPONENT_DESIRED_STATE_PROPERTY_ID
       = PropertyHelper.getPropertyId("HostRoles", "desired_state");
-  public static final String HOST_COMPONENT_STACK_ID_PROPERTY_ID
-      = PropertyHelper.getPropertyId("HostRoles", "stack_id");
+  public static final String HOST_COMPONENT_VERSION_PROPERTY_ID
+      = PropertyHelper.getPropertyId("HostRoles", "version");
   public static final String HOST_COMPONENT_DESIRED_STACK_ID_PROPERTY_ID
       = PropertyHelper.getPropertyId("HostRoles", "desired_stack_id");
+  public static final String HOST_COMPONENT_DESIRED_REPOSITORY_VERSION
+    = PropertyHelper.getPropertyId("HostRoles", "desired_repository_version");
   public static final String HOST_COMPONENT_ACTUAL_CONFIGS_PROPERTY_ID
       = PropertyHelper.getPropertyId("HostRoles", "actual_configs");
   public static final String HOST_COMPONENT_STALE_CONFIGS_PROPERTY_ID
@@ -109,18 +110,16 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
       = PropertyHelper.getPropertyId("HostRoles", "desired_admin_state");
   public static final String HOST_COMPONENT_MAINTENANCE_STATE_PROPERTY_ID
       = "HostRoles/maintenance_state";
-  public static final String HOST_COMPONENT_HDP_VERSION_PROPERTY_ID
-      = PropertyHelper.getPropertyId("HostRoles", "hdp_version");
   public static final String HOST_COMPONENT_UPGRADE_STATE_PROPERTY_ID = "HostRoles/upgrade_state";
 
   //Parameters from the predicate
   private static final String QUERY_PARAMETERS_RUN_SMOKE_TEST_ID = "params/run_smoke_test";
   private static Set<String> pkPropertyIds =
-      new HashSet<String>(Arrays.asList(new String[]{
-          HOST_COMPONENT_CLUSTER_NAME_PROPERTY_ID,
-          HOST_COMPONENT_SERVICE_NAME_PROPERTY_ID,
-          HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID,
-          HOST_COMPONENT_HOST_NAME_PROPERTY_ID}));
+    new HashSet<>(Arrays.asList(new String[]{
+      HOST_COMPONENT_CLUSTER_NAME_PROPERTY_ID,
+      HOST_COMPONENT_SERVICE_NAME_PROPERTY_ID,
+      HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID,
+      HOST_COMPONENT_HOST_NAME_PROPERTY_ID}));
 
   /**
    * maintenance state helper
@@ -160,7 +159,7 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
       ResourceAlreadyExistsException,
       NoSuchParentResourceException {
 
-    final Set<ServiceComponentHostRequest> requests = new HashSet<ServiceComponentHostRequest>();
+    final Set<ServiceComponentHostRequest> requests = new HashSet<>();
     for (Map<String, Object> propertyMap : request.getProperties()) {
       requests.add(changeRequest(propertyMap));
     }
@@ -182,7 +181,7 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
   public Set<Resource> getResources(Request request, Predicate predicate)
       throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
 
-    final Set<ServiceComponentHostRequest> requests = new HashSet<ServiceComponentHostRequest>();
+    final Set<ServiceComponentHostRequest> requests = new HashSet<>();
 
     for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) {
       requests.add(getRequest(propertyMap));
@@ -194,7 +193,7 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
   private Set<Resource> getResourcesForUpdate(Request request, Predicate predicate)
     throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
 
-    final Set<ServiceComponentHostRequest> requests = new HashSet<ServiceComponentHostRequest>();
+    final Set<ServiceComponentHostRequest> requests = new HashSet<>();
 
     for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) {
       requests.add(getRequest(propertyMap));
@@ -207,7 +206,7 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
   private Set<Resource> findResources(Request request, final Predicate predicate,
                                       final Set<ServiceComponentHostRequest> requests)
           throws SystemException, NoSuchResourceException, NoSuchParentResourceException {
-    Set<Resource> resources = new HashSet<Resource>();
+    Set<Resource> resources = new HashSet<>();
     Set<String> requestedIds = getRequestPropertyIds(request, predicate);
     // We always need host_name for sch
     requestedIds.add(HOST_COMPONENT_HOST_NAME_PROPERTY_ID);
@@ -237,8 +236,8 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
               response.getLiveState(), requestedIds);
       setResourceProperty(resource, HOST_COMPONENT_DESIRED_STATE_PROPERTY_ID,
               response.getDesiredState(), requestedIds);
-      setResourceProperty(resource, HOST_COMPONENT_STACK_ID_PROPERTY_ID,
-              response.getStackVersion(), requestedIds);
+      setResourceProperty(resource, HOST_COMPONENT_VERSION_PROPERTY_ID, response.getVersion(),
+          requestedIds);
       setResourceProperty(resource, HOST_COMPONENT_DESIRED_STACK_ID_PROPERTY_ID,
               response.getDesiredStackVersion(), requestedIds);
       setResourceProperty(resource, HOST_COMPONENT_ACTUAL_CONFIGS_PROPERTY_ID,
@@ -247,15 +246,8 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
               response.isStaleConfig(), requestedIds);
       setResourceProperty(resource, HOST_COMPONENT_UPGRADE_STATE_PROPERTY_ID,
               response.getUpgradeState(), requestedIds);
-
-      if (requestedIds.contains(HOST_COMPONENT_HDP_VERSION_PROPERTY_ID)) {
-        HostVersionEntity versionEntity = hostVersionDAO.
-            findByHostAndStateCurrent(response.getClusterName(), response.getHostname());
-        if (versionEntity != null) {
-          setResourceProperty(resource, HOST_COMPONENT_HDP_VERSION_PROPERTY_ID,
-              versionEntity.getRepositoryVersion().getDisplayName(), requestedIds);
-        }
-      }
+      setResourceProperty(resource, HOST_COMPONENT_DESIRED_REPOSITORY_VERSION,
+          response.getDesiredRepositoryVersion(), requestedIds);
 
       if (response.getAdminState() != null) {
         setResourceProperty(resource, HOST_COMPONENT_DESIRED_ADMIN_STATE_PROPERTY_ID,
@@ -322,7 +314,7 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
     if (propertyIds.isEmpty()) {
       return propertyIds;
     }
-    Set<String> unsupportedProperties = new HashSet<String>();
+    Set<String> unsupportedProperties = new HashSet<>();
 
     for (String propertyId : propertyIds) {
       if (!propertyId.equals("config")) {
@@ -341,7 +333,7 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
     RequestStageContainer requestStages;
     //for (String host : hosts) {
 
-    Map<String, Object> installProperties = new HashMap<String, Object>();
+    Map<String, Object> installProperties = new HashMap<>();
 
     installProperties.put(HOST_COMPONENT_DESIRED_STATE_PROPERTY_ID, "INSTALLED");
     Map<String, String> requestInfo = new HashMap<>();
@@ -402,14 +394,14 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
 
     RequestStageContainer requestStages;
     try {
-      Map<String, Object> startProperties = new HashMap<String, Object>();
+      Map<String, Object> startProperties = new HashMap<>();
       startProperties.put(HOST_COMPONENT_DESIRED_STATE_PROPERTY_ID, "STARTED");
       Request startRequest = PropertyHelper.getUpdateRequest(startProperties, requestInfo);
       // Important to query against desired_state as this has been updated when install stage was created
       // If I query against state, then the getRequest compares predicate prop against desired_state and then when the predicate
       // is later applied explicitly, it gets compared to live_state. Since live_state == INSTALLED == INIT at this point and
       // desired_state == INSTALLED, we will always get 0 matches since both comparisons can't be true :(
-      Predicate installedStatePredicate = new EqualsPredicate<String>(HOST_COMPONENT_DESIRED_STATE_PROPERTY_ID, "INSTALLED");
+      Predicate installedStatePredicate = new EqualsPredicate<>(HOST_COMPONENT_DESIRED_STATE_PROPERTY_ID, "INSTALLED");
       Predicate notClientPredicate = new NotPredicate(new ClientComponentPredicate());
       Predicate clusterAndClientPredicate = new AndPredicate(clusterPredicate, notClientPredicate);
       Predicate hostAndStatePredicate = new AndPredicate(installedStatePredicate, hostPredicate);
@@ -422,10 +414,10 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
       } else {
         // any INSTALL_ONLY components should not be started
         List<Predicate> listOfComponentPredicates =
-          new ArrayList<Predicate>();
+          new ArrayList<>();
 
         for (String installOnlyComponent : installOnlyComponents) {
-          Predicate componentNameEquals = new EqualsPredicate<String>(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, installOnlyComponent);
+          Predicate componentNameEquals = new EqualsPredicate<>(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, installOnlyComponent);
           // create predicate to filter out the install only component
           listOfComponentPredicates.add(new NotPredicate(componentNameEquals));
         }
@@ -482,11 +474,11 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
     Clusters clusters = getManagementController().getClusters();
 
 
-    Map<String, Map<State, List<ServiceComponentHost>>> changedScHosts = new HashMap<String, Map<State, List<ServiceComponentHost>>>();
-    Collection<ServiceComponentHost> ignoredScHosts = new ArrayList<ServiceComponentHost>();
-    Set<String> clusterNames = new HashSet<String>();
-    Map<String, Map<String, Map<String, Set<String>>>> requestClusters = new HashMap<String, Map<String, Map<String, Set<String>>>>();
-    Map<ServiceComponentHost, State> directTransitionScHosts = new HashMap<ServiceComponentHost, State>();
+    Map<String, Map<State, List<ServiceComponentHost>>> changedScHosts = new HashMap<>();
+    Collection<ServiceComponentHost> ignoredScHosts = new ArrayList<>();
+    Set<String> clusterNames = new HashSet<>();
+    Map<String, Map<String, Map<String, Set<String>>>> requestClusters = new HashMap<>();
+    Map<ServiceComponentHost, State> directTransitionScHosts = new HashMap<>();
 
     Resource.Type reqOpLvl = determineOperationLevel(requestProperties);
 
@@ -529,19 +521,19 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
       // maps of cluster->services, services->components, components->hosts
       Map<String, Map<String, Set<String>>> clusterServices = requestClusters.get(request.getClusterName());
       if (clusterServices == null) {
-        clusterServices = new HashMap<String, Map<String, Set<String>>>();
+        clusterServices = new HashMap<>();
         requestClusters.put(request.getClusterName(), clusterServices);
       }
 
       Map<String, Set<String>> serviceComponents = clusterServices.get(request.getServiceName());
       if (serviceComponents == null) {
-        serviceComponents = new HashMap<String, Set<String>>();
+        serviceComponents = new HashMap<>();
         clusterServices.put(request.getServiceName(), serviceComponents);
       }
 
       Set<String> componentHosts = serviceComponents.get(request.getComponentName());
       if (componentHosts == null) {
-        componentHosts = new HashSet<String>();
+        componentHosts = new HashSet<>();
         serviceComponents.put(request.getComponentName(), componentHosts) ;
       }
 
@@ -562,7 +554,7 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
         // throw exception if desired state isn't a valid desired state (static check)
         if (!newState.isValidDesiredState()) {
           throw new IllegalArgumentException("Invalid arguments, invalid"
-              + " desired state, desiredState=" + newState.toString());
+              + " desired state, desiredState=" + newState);
         }
       }
 
@@ -684,7 +676,6 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
         (String) properties.get(HOST_COMPONENT_HOST_NAME_PROPERTY_ID),
         (String) properties.get(HOST_COMPONENT_DESIRED_STATE_PROPERTY_ID));
     serviceComponentHostRequest.setState((String) properties.get(HOST_COMPONENT_STATE_PROPERTY_ID));
-    serviceComponentHostRequest.setDesiredStackId((String) properties.get(HOST_COMPONENT_STACK_ID_PROPERTY_ID));
     if (properties.get(HOST_COMPONENT_STALE_CONFIGS_PROPERTY_ID) != null) {
       serviceComponentHostRequest.setStaleConfig(
           properties.get(HOST_COMPONENT_STALE_CONFIGS_PROPERTY_ID).toString().toLowerCase());
@@ -724,8 +715,6 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
     if (properties.get(HOST_COMPONENT_DESIRED_STATE_PROPERTY_ID) != null) {
       serviceComponentHostRequest.setDesiredState((String)properties.get(HOST_COMPONENT_DESIRED_STATE_PROPERTY_ID));
     }
-    serviceComponentHostRequest.setDesiredStackId(
-            (String) properties.get(HOST_COMPONENT_STACK_ID_PROPERTY_ID));
     if (properties.get(HOST_COMPONENT_STALE_CONFIGS_PROPERTY_ID) != null) {
       serviceComponentHostRequest.setStaleConfig(
               properties.get(HOST_COMPONENT_STALE_CONFIGS_PROPERTY_ID).toString().toLowerCase());
@@ -764,7 +753,7 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
                                                          NoSuchResourceException,
                                                          NoSuchParentResourceException {
 
-    final Set<ServiceComponentHostRequest> requests = new HashSet<ServiceComponentHostRequest>();
+    final Set<ServiceComponentHostRequest> requests = new HashSet<>();
 
     final boolean runSmokeTest = "true".equals(getQueryParameterValue(
         QUERY_PARAMETERS_RUN_SMOKE_TEST_ID, predicate));
@@ -782,7 +771,7 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
       //todo: and then this predicate evaluation should always be performed and the
       //todo: temporary performQueryEvaluation flag hack should be removed.
       if (! performQueryEvaluation || predicate.evaluate(queryResource)) {
-        Map<String, Object> updateRequestProperties = new HashMap<String, Object>();
+        Map<String, Object> updateRequestProperties = new HashMap<>();
 
         // add props from query resource
         updateRequestProperties.putAll(PropertyHelper.getProperties(queryResource));


[31/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
index 1130026..5d02de3 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -23,13 +23,10 @@ import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.SERVICE_P
 import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -65,32 +62,24 @@ import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
 import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
-import org.apache.ambari.server.controller.utilities.PredicateBuilder;
-import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
 import org.apache.ambari.server.orm.dao.HostRoleCommandStatusSummaryDTO;
-import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.RequestDAO;
 import org.apache.ambari.server.orm.dao.UpgradeDAO;
 import org.apache.ambari.server.orm.entities.HostRoleCommandEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.RequestEntity;
-import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.orm.entities.UpgradeGroupEntity;
+import org.apache.ambari.server.orm.entities.UpgradeHistoryEntity;
 import org.apache.ambari.server.orm.entities.UpgradeItemEntity;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.security.authorization.AuthorizationHelper;
 import org.apache.ambari.server.security.authorization.ResourceType;
 import org.apache.ambari.server.security.authorization.RoleAuthorization;
-import org.apache.ambari.server.serveraction.upgrades.UpdateDesiredStackAction;
-import org.apache.ambari.server.stack.MasterHostResolver;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.ConfigHelper;
-import org.apache.ambari.server.state.DesiredConfig;
-import org.apache.ambari.server.state.Host;
 import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceInfo;
@@ -101,32 +90,26 @@ import org.apache.ambari.server.state.UpgradeContextFactory;
 import org.apache.ambari.server.state.UpgradeHelper;
 import org.apache.ambari.server.state.UpgradeHelper.UpgradeGroupHolder;
 import org.apache.ambari.server.state.stack.ConfigUpgradePack;
-import org.apache.ambari.server.state.stack.PrereqCheckStatus;
 import org.apache.ambari.server.state.stack.UpgradePack;
 import org.apache.ambari.server.state.stack.upgrade.ConfigureTask;
 import org.apache.ambari.server.state.stack.upgrade.Direction;
-import org.apache.ambari.server.state.stack.upgrade.Grouping;
-import org.apache.ambari.server.state.stack.upgrade.HostOrderGrouping;
-import org.apache.ambari.server.state.stack.upgrade.HostOrderItem;
-import org.apache.ambari.server.state.stack.upgrade.HostOrderItem.HostOrderActionType;
 import org.apache.ambari.server.state.stack.upgrade.ManualTask;
 import org.apache.ambari.server.state.stack.upgrade.ServerSideActionTask;
 import org.apache.ambari.server.state.stack.upgrade.StageWrapper;
 import org.apache.ambari.server.state.stack.upgrade.Task;
 import org.apache.ambari.server.state.stack.upgrade.TaskWrapper;
 import org.apache.ambari.server.state.stack.upgrade.UpdateStackGrouping;
-import org.apache.ambari.server.state.stack.upgrade.UpgradeScope;
 import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
 import org.apache.ambari.server.state.svccomphost.ServiceComponentHostServerActionEvent;
 import org.apache.ambari.server.utils.StageUtils;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang.StringUtils;
+import org.codehaus.jackson.annotate.JsonProperty;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
-import com.google.gson.Gson;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import com.google.inject.Inject;
@@ -141,14 +124,13 @@ import com.google.inject.persist.Transactional;
 public class UpgradeResourceProvider extends AbstractControllerResourceProvider {
 
   public static final String UPGRADE_CLUSTER_NAME = "Upgrade/cluster_name";
-  public static final String UPGRADE_REPO_ID = "Upgrade/repository_id";
-  public static final String UPGRADE_REPO_NAME = "Upgrade/repository_name";
-  public static final String UPGRADE_REPO_VERSION = "Upgrade/repository_version";
+  public static final String UPGRADE_REPO_VERSION_ID = "Upgrade/repository_version_id";
   public static final String UPGRADE_TYPE = "Upgrade/upgrade_type";
   public static final String UPGRADE_PACK = "Upgrade/pack";
+  public static final String UPGRADE_ID = "Upgrade/upgrade_id";
   public static final String UPGRADE_REQUEST_ID = "Upgrade/request_id";
-  public static final String UPGRADE_FROM_VERSION = "Upgrade/from_version";
-  public static final String UPGRADE_TO_VERSION = "Upgrade/to_version";
+  public static final String UPGRADE_ASSOCIATED_VERSION = "Upgrade/associated_version";
+  public static final String UPGRADE_VERSIONS = "Upgrade/versions";
   public static final String UPGRADE_DIRECTION = "Upgrade/direction";
   public static final String UPGRADE_DOWNGRADE_ALLOWED = "Upgrade/downgrade_allowed";
   public static final String UPGRADE_REQUEST_STATUS = "Upgrade/request_status";
@@ -157,32 +139,20 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
   public static final String UPGRADE_SKIP_PREREQUISITE_CHECKS = "Upgrade/skip_prerequisite_checks";
   public static final String UPGRADE_FAIL_ON_CHECK_WARNINGS = "Upgrade/fail_on_check_warnings";
 
-
-  /**
-   * Names that appear in the Upgrade Packs that are used by
-   * {@link org.apache.ambari.server.state.cluster.ClusterImpl#isNonRollingUpgradePastUpgradingStack}
-   * to determine if an upgrade has already changed the version to use.
-   * For this reason, DO NOT CHANGE the name of these since they represent historic values.
-   */
-  public static final String CONST_UPGRADE_GROUP_NAME = "UPDATE_DESIRED_STACK_ID";
-  public static final String CONST_UPGRADE_ITEM_TEXT = "Update Target Stack";
-  public static final String CONST_CUSTOM_COMMAND_NAME = UpdateDesiredStackAction.class.getName();
-
-
   /**
    * Skip slave/client component failures if the tasks are skippable.
    */
-  protected static final String UPGRADE_SKIP_FAILURES = "Upgrade/skip_failures";
+  public static final String UPGRADE_SKIP_FAILURES = "Upgrade/skip_failures";
 
   /**
    * Skip service check failures if the tasks are skippable.
    */
-  protected static final String UPGRADE_SKIP_SC_FAILURES = "Upgrade/skip_service_check_failures";
+  public static final String UPGRADE_SKIP_SC_FAILURES = "Upgrade/skip_service_check_failures";
 
   /**
    * Skip manual verification tasks for hands-free upgrade/downgrade experience.
    */
-  protected static final String UPGRADE_SKIP_MANUAL_VERIFICATION = "Upgrade/skip_manual_verification";
+  public static final String UPGRADE_SKIP_MANUAL_VERIFICATION = "Upgrade/skip_manual_verification";
 
   /**
    * When creating an upgrade of type {@link UpgradeType#HOST_ORDERED}, this
@@ -203,7 +173,12 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
    * </pre>
    *
    */
-  protected static final String UPGRADE_HOST_ORDERED_HOSTS = "Upgrade/host_order";
+  public static final String UPGRADE_HOST_ORDERED_HOSTS = "Upgrade/host_order";
+
+  /**
+   * Allows reversion of a successful upgrade of a patch.
+   */
+  public static final String UPGRADE_REVERT_UPGRADE_ID = "Upgrade/revert_upgrade_id";
 
   /**
    * The role that will be used when creating HRC's for the type
@@ -226,8 +201,8 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
 
   private static final Set<String> PK_PROPERTY_IDS = new HashSet<>(
       Arrays.asList(UPGRADE_REQUEST_ID, UPGRADE_CLUSTER_NAME));
-  private static final Set<String> PROPERTY_IDS = new HashSet<>();
 
+  private static final Set<String> PROPERTY_IDS = new HashSet<>();
 
   private static final String DEFAULT_REASON_TEMPLATE = "Aborting upgrade %s";
 
@@ -240,9 +215,6 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
   private static Provider<AmbariMetaInfo> s_metaProvider = null;
 
   @Inject
-  private static RepositoryVersionDAO s_repoVersionDAO = null;
-
-  @Inject
   private static Provider<RequestFactory> s_requestFactory;
 
   @Inject
@@ -272,9 +244,6 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
   @Inject
   private static Configuration s_configuration;
 
-  @Inject
-  private static Gson s_gson;
-
   /**
    * Used to create instances of {@link UpgradeContext} with injected
    * dependencies.
@@ -285,14 +254,13 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
   static {
     // properties
     PROPERTY_IDS.add(UPGRADE_CLUSTER_NAME);
-    PROPERTY_IDS.add(UPGRADE_REPO_ID);
-    PROPERTY_IDS.add(UPGRADE_REPO_NAME);
-    PROPERTY_IDS.add(UPGRADE_REPO_VERSION);
+    PROPERTY_IDS.add(UPGRADE_REPO_VERSION_ID);
     PROPERTY_IDS.add(UPGRADE_TYPE);
     PROPERTY_IDS.add(UPGRADE_PACK);
+    PROPERTY_IDS.add(UPGRADE_ID);
     PROPERTY_IDS.add(UPGRADE_REQUEST_ID);
-    PROPERTY_IDS.add(UPGRADE_FROM_VERSION);
-    PROPERTY_IDS.add(UPGRADE_TO_VERSION);
+    PROPERTY_IDS.add(UPGRADE_ASSOCIATED_VERSION);
+    PROPERTY_IDS.add(UPGRADE_VERSIONS);
     PROPERTY_IDS.add(UPGRADE_DIRECTION);
     PROPERTY_IDS.add(UPGRADE_DOWNGRADE_ALLOWED);
     PROPERTY_IDS.add(UPGRADE_SUSPENDED);
@@ -302,6 +270,7 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
     PROPERTY_IDS.add(UPGRADE_SKIP_PREREQUISITE_CHECKS);
     PROPERTY_IDS.add(UPGRADE_FAIL_ON_CHECK_WARNINGS);
     PROPERTY_IDS.add(UPGRADE_HOST_ORDERED_HOSTS);
+    PROPERTY_IDS.add(UPGRADE_REVERT_UPGRADE_ID);
 
     PROPERTY_IDS.add(REQUEST_CONTEXT_ID);
     PROPERTY_IDS.add(REQUEST_CREATE_TIME_ID);
@@ -362,75 +331,8 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
       @Override
       public UpgradeEntity invoke() throws AmbariException, AuthorizationException {
 
-        final String directionProperty = (String) requestMap.get(UPGRADE_DIRECTION);
-        if (StringUtils.isEmpty(directionProperty)) {
-          throw new AmbariException(String.format("%s is required", UPGRADE_DIRECTION));
-        }
-
-        final Direction direction = Direction.valueOf(directionProperty);
-
-        // Default to ROLLING upgrade, but attempt to read from properties.
-        UpgradeType upgradeType = UpgradeType.ROLLING;
-        if (requestMap.containsKey(UPGRADE_TYPE)) {
-          try {
-            upgradeType = UpgradeType.valueOf(requestMap.get(UPGRADE_TYPE).toString());
-          } catch (Exception e) {
-            throw new AmbariException(String.format("Property %s has an incorrect value of %s.",
-                UPGRADE_TYPE, requestMap.get(UPGRADE_TYPE)));
-          }
-        }
-
-        // use a combination of the supplied information to find the target
-        // repository
-        final String repositoryId = (String) requestMap.get(UPGRADE_REPO_ID);
-        final String repositoryName = (String) requestMap.get(UPGRADE_REPO_NAME);
-        final String repositoryVersion = (String) requestMap.get(UPGRADE_REPO_VERSION);
-
-        RepositoryVersionEntity toRepositoryVersion = null;
-
-        if (null != repositoryId) {
-          toRepositoryVersion = s_repoVersionDAO.findByPK(Long.valueOf(repositoryId));
-        } else {
-          if (StringUtils.isNotBlank(repositoryName)) {
-            toRepositoryVersion = s_repoVersionDAO.findByStackNameAndVersion(repositoryName,
-                repositoryVersion);
-          } else {
-            toRepositoryVersion = s_repoVersionDAO.findByVersion(repositoryVersion);
-          }
-        }
-
-        if (null == toRepositoryVersion) {
-          throw new AmbariException(
-              "Unable to find repository by any combination of id, name, or version");
-        }
-
-        // determine the correct "from" repo version which is based on upgrade
-        // direction
-        RepositoryVersionEntity fromRepositoryVersion = cluster.getCurrentClusterVersion().getRepositoryVersion();
-
-        if (direction.isDowngrade()) {
-          String downgradeFromVersion = (String) requestMap.get(UPGRADE_FROM_VERSION);
-
-          UpgradeEntity lastUpgradeItemForCluster = s_upgradeDAO.findLastUpgradeForCluster(
-              cluster.getClusterId(), Direction.UPGRADE);
-
-          RepositoryVersionEntity lastUpgradeTargetVersion = lastUpgradeItemForCluster.getToRepositoryVersion();
-          String lastUpgradeVersion = lastUpgradeTargetVersion.getVersion();
-          if (null != downgradeFromVersion
-              && !StringUtils.equals(downgradeFromVersion, lastUpgradeVersion)) {
-            throw new AmbariException(
-                "Unable to perform downgrade to " + toRepositoryVersion.getVersion()
-                    + " since there is no prior failed upgrade to " + downgradeFromVersion);
-          }
-
-          fromRepositoryVersion = lastUpgradeTargetVersion;
-        }
-
-        final UpgradeContext upgradeContext = s_upgradeContextFactory.create(cluster, upgradeType,
-            direction, fromRepositoryVersion, toRepositoryVersion, requestMap);
-
-        UpgradePack upgradePack = validateRequest(upgradeContext);
-        upgradeContext.setUpgradePack(upgradePack);
+        // create the context, validating the properties in the process
+        final UpgradeContext upgradeContext = s_upgradeContextFactory.create(cluster, requestMap);
 
         try {
           return createUpgrade(upgradeContext);
@@ -644,53 +546,38 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
   private Resource toResource(UpgradeEntity entity, String clusterName, Set<String> requestedIds) {
     ResourceImpl resource = new ResourceImpl(Resource.Type.Upgrade);
 
+    setResourceProperty(resource, UPGRADE_ID, entity.getId(), requestedIds);
     setResourceProperty(resource, UPGRADE_CLUSTER_NAME, clusterName, requestedIds);
     setResourceProperty(resource, UPGRADE_TYPE, entity.getUpgradeType(), requestedIds);
     setResourceProperty(resource, UPGRADE_PACK, entity.getUpgradePackage(), requestedIds);
     setResourceProperty(resource, UPGRADE_REQUEST_ID, entity.getRequestId(), requestedIds);
-    setResourceProperty(resource, UPGRADE_FROM_VERSION, entity.getFromRepositoryVersion().getVersion(), requestedIds);
-    setResourceProperty(resource, UPGRADE_TO_VERSION, entity.getToRepositoryVersion().getVersion(), requestedIds);
     setResourceProperty(resource, UPGRADE_DIRECTION, entity.getDirection(), requestedIds);
     setResourceProperty(resource, UPGRADE_SUSPENDED, entity.isSuspended(), requestedIds);
     setResourceProperty(resource, UPGRADE_DOWNGRADE_ALLOWED, entity.isDowngradeAllowed(), requestedIds);
     setResourceProperty(resource, UPGRADE_SKIP_FAILURES, entity.isComponentFailureAutoSkipped(), requestedIds);
     setResourceProperty(resource, UPGRADE_SKIP_SC_FAILURES, entity.isServiceCheckFailureAutoSkipped(), requestedIds);
 
-    return resource;
-  }
-
-  /**
-   * Validates a singular API request.
-   *
-   * @param upgradeContext details about the upgrade such as direction and type
-   * @return the validated upgrade pack
-   * @throws AmbariException
-   */
-  private UpgradePack validateRequest(UpgradeContext upgradeContext) throws AmbariException {
-    Cluster cluster = upgradeContext.getCluster();
-    Direction direction = upgradeContext.getDirection();
-    String targetStackName = upgradeContext.getTargetStackId().getStackName();
-    Map<String, Object> requestMap = upgradeContext.getUpgradeRequest();
-    UpgradeType upgradeType = upgradeContext.getType();
-    RepositoryVersionEntity targetRepositoryVersion = upgradeContext.getTargetRepositoryVersion();
+    // set the assocaited to/from version (to/from is dictated by direction)
+    RepositoryVersionEntity repositoryVersion = entity.getRepositoryVersion();
+    setResourceProperty(resource, UPGRADE_ASSOCIATED_VERSION, repositoryVersion.getVersion(), requestedIds);
 
-    /**
-     * For the unit tests tests, there are multiple upgrade packs for the same type, so
-     * allow picking one of them. In prod, this is empty.
-     */
-    String preferredUpgradePackName = (String) requestMap.get(UPGRADE_PACK);
+    // now set the target verison for all services in the upgrade
+    Map<String, RepositoryVersions> repositoryVersions = new HashMap<>();
+    for( UpgradeHistoryEntity history : entity.getHistory() ){
+      RepositoryVersions serviceVersions = repositoryVersions.get(history.getServiceName());
+      if (null != serviceVersions) {
+        continue;
+      }
 
-    String version = targetRepositoryVersion.getVersion();
-    String versionForUpgradePack = (String) requestMap.get(UPGRADE_FROM_VERSION);
+      serviceVersions = new RepositoryVersions(history.getFromReposistoryVersion(),
+          history.getTargetRepositoryVersion());
 
-    UpgradePack pack = s_upgradeHelper.suggestUpgradePack(cluster.getClusterName(),
-        versionForUpgradePack, version, direction, upgradeType, targetStackName, preferredUpgradePackName);
+      repositoryVersions.put(history.getServiceName(), serviceVersions);
+    }
 
-    // the validator will throw an exception if the upgrade request is not valid
-    UpgradeRequestValidator upgradeRequestValidator = buildValidator(upgradeType);
-    upgradeRequestValidator.validate(upgradeContext, pack);
+    setResourceProperty(resource, UPGRADE_VERSIONS, repositoryVersions, requestedIds);
 
-    return pack;
+    return resource;
   }
 
   /**
@@ -750,95 +637,52 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
     UpgradePack pack = upgradeContext.getUpgradePack();
     Cluster cluster = upgradeContext.getCluster();
     Direction direction = upgradeContext.getDirection();
-    Map<String, Object> requestMap = upgradeContext.getUpgradeRequest();
-    ConfigHelper configHelper = getManagementController().getConfigHelper();
-
-    // the version being upgraded to or downgraded from (ie 2.2.1.0-1234)
-    final RepositoryVersionEntity repositoryVersion = upgradeContext.getTargetRepositoryVersion();
-
-    MasterHostResolver resolver = null;
-    if (direction.isUpgrade()) {
-      resolver = new MasterHostResolver(configHelper, cluster);
-    } else {
-      resolver = new MasterHostResolver(configHelper, cluster,
-          upgradeContext.getTargetRepositoryVersion().getVersion());
-    }
 
-    Set<String> supportedServices = new HashSet<>();
-    UpgradeScope scope = UpgradeScope.COMPLETE;
-
-    upgradeContext.setResolver(resolver);
-    upgradeContext.setSupportedServices(supportedServices);
-    upgradeContext.setScope(scope);
-
-    if (direction.isDowngrade()) {
-      String downgradeFromVersion = (String) requestMap.get(UPGRADE_FROM_VERSION);
-
-      UpgradeEntity lastUpgradeItemForCluster = s_upgradeDAO.findLastUpgradeForCluster(
-          cluster.getClusterId(), Direction.UPGRADE);
-
-      String lastUpgradeVersion = lastUpgradeItemForCluster.getToRepositoryVersion().getVersion();
-      if (null != downgradeFromVersion
-          && !StringUtils.equals(downgradeFromVersion, lastUpgradeVersion)) {
-        throw new AmbariException("Unable to perform downgrade to " + repositoryVersion.getVersion()
-            + " since there is no prior failed upgrade to " + downgradeFromVersion);
-      }
-    }
-
-    // optionally skip failures - this can be supplied on either the request or
-    // in the upgrade pack explicitely, however the request will always override
-    // the upgrade pack if explicitely specified
-    boolean skipComponentFailures = pack.isComponentFailureAutoSkipped();
-    boolean skipServiceCheckFailures = pack.isServiceCheckFailureAutoSkipped();
-
-    // only override the upgrade pack if set on the request
-    if (requestMap.containsKey(UPGRADE_SKIP_FAILURES)) {
-      skipComponentFailures = Boolean.parseBoolean((String) requestMap.get(UPGRADE_SKIP_FAILURES));
-    }
-
-    // only override the upgrade pack if set on the request
-    if (requestMap.containsKey(UPGRADE_SKIP_SC_FAILURES)) {
-      skipServiceCheckFailures = Boolean.parseBoolean(
-          (String) requestMap.get(UPGRADE_SKIP_SC_FAILURES));
-    }
-
-    boolean skipManualVerification = false;
-    if(requestMap.containsKey(UPGRADE_SKIP_MANUAL_VERIFICATION)) {
-      skipManualVerification = Boolean.parseBoolean((String) requestMap.get(UPGRADE_SKIP_MANUAL_VERIFICATION));
-    }
-
-    upgradeContext.setAutoSkipComponentFailures(skipComponentFailures);
-    upgradeContext.setAutoSkipServiceCheckFailures(skipServiceCheckFailures);
-    upgradeContext.setAutoSkipManualVerification(skipManualVerification);
+    ConfigHelper configHelper = getManagementController().getConfigHelper();
 
     List<UpgradeGroupHolder> groups = s_upgradeHelper.createSequence(pack, upgradeContext);
 
-    if (groups.isEmpty()) {
+    if (1 == 1 || groups.isEmpty()) {
       throw new AmbariException("There are no groupings available");
     }
 
     // Non Rolling Upgrades require a group with name "UPDATE_DESIRED_STACK_ID".
     // This is needed as a marker to indicate which version to use when an upgrade is paused.
     if (pack.getType() == UpgradeType.NON_ROLLING) {
-      boolean foundGroupWithNameUPDATE_DESIRED_STACK_ID = false;
+      boolean foundUpdateDesiredRepositoryIdGrouping = false;
       for (UpgradeGroupHolder group : groups) {
-        if (group.name.equalsIgnoreCase(CONST_UPGRADE_GROUP_NAME)) {
-          foundGroupWithNameUPDATE_DESIRED_STACK_ID = true;
+        if (group.groupClass == UpdateStackGrouping.class) {
+          foundUpdateDesiredRepositoryIdGrouping = true;
           break;
         }
       }
 
-      if (foundGroupWithNameUPDATE_DESIRED_STACK_ID == false) {
-        throw new AmbariException(String.format("NonRolling Upgrade Pack %s requires a Group with name %s",
-            pack.getName(), CONST_UPGRADE_GROUP_NAME));
+      if (!foundUpdateDesiredRepositoryIdGrouping) {
+        throw new AmbariException(String.format(
+            "Express upgrade packs are required to have a group of type %s. The upgrade pack %s is missing this grouping.",
+            "update-stack", pack.getName()));
       }
     }
 
     List<UpgradeGroupEntity> groupEntities = new ArrayList<>();
-    RequestStageContainer req = createRequest(cluster, direction,
-        upgradeContext.getTargetRepositoryVersion().getStackId());
-
-    /**
+    RequestStageContainer req = createRequest(upgradeContext);
+
+    UpgradeEntity upgrade = new UpgradeEntity();
+    upgrade.setRepositoryVersion(upgradeContext.getRepositoryVersion());
+    upgrade.setClusterId(cluster.getClusterId());
+    upgrade.setDirection(direction);
+    upgrade.setUpgradePackage(pack.getName());
+    upgrade.setUpgradeType(pack.getType());
+    upgrade.setAutoSkipComponentFailures(upgradeContext.isComponentFailureAutoSkipped());
+    upgrade.setAutoSkipServiceCheckFailures(upgradeContext.isServiceCheckFailureAutoSkipped());
+    upgrade.setDowngradeAllowed(upgradeContext.isDowngradeAllowed());
+    upgrade.setOrchestration(upgradeContext.getOrchestrationType());
+
+    // create to/from history for this upgrade - this should be done before any
+    // possible changes to the desired version for components
+    addComponentHistoryToUpgrade(cluster, upgrade, upgradeContext);
+
+    /*
     During a Rolling Upgrade, change the desired Stack Id if jumping across
     major stack versions (e.g., HDP 2.2 -> 2.3), and then set config changes
     so they are applied on the newer stack.
@@ -847,59 +691,29 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
     stopping all services), and the configs are applied immediately before starting the services.
     The Upgrade Pack is responsible for calling {@link org.apache.ambari.server.serveraction.upgrades.UpdateDesiredStackAction}
     at the appropriate moment during the orchestration.
-    **/
+    */
     if (pack.getType() == UpgradeType.ROLLING) {
-      // Desired configs must be set before creating stages because the config tag
-      // names are read and set on the command for filling in later
-      applyStackAndProcessConfigurations(upgradeContext);
+      s_upgradeHelper.updateDesiredRepositoriesAndConfigs(upgradeContext);
     }
 
+    @Experimental(feature = ExperimentalFeature.PATCH_UPGRADES, comment = "This is wrong")
+    StackId configurationPackSourceStackId = upgradeContext.getSourceVersions().values().iterator().next().getStackId();
+
     // resolve or build a proper config upgrade pack - always start out with the config pack
     // for the current stack and merge into that
     //
     // HDP 2.2 to 2.3 should start with the config-upgrade.xml from HDP 2.2
     // HDP 2.2 to 2.4 should start with HDP 2.2 and merge in HDP 2.3's config-upgrade.xml
     ConfigUpgradePack configUpgradePack = ConfigurationPackBuilder.build(pack,
-        upgradeContext.getOriginalStackId());
-
-    // TODO: for now, all service components are transitioned to upgrading state
-    // TODO: When performing patch upgrade, we should only target supported services/components
-    // from upgrade pack
-    @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES)
-    Set<Service> services = new HashSet<>(cluster.getServices().values());
-    Map<Service, Set<ServiceComponent>> targetComponents = new HashMap<>();
-    for (Service service: services) {
-      Set<ServiceComponent> serviceComponents =
-        new HashSet<>(service.getServiceComponents().values());
-      targetComponents.put(service, serviceComponents);
-    }
-
-    // !!! determine which stack to check for component isAdvertised
-    StackId componentStack = upgradeContext.getTargetStackId();
-
-    s_upgradeHelper.putComponentsToUpgradingState(repositoryVersion.getStackId(),
-        repositoryVersion.getVersion(), targetComponents, componentStack);
-
-    // keep track of which stack to use when building commands - an express
-    // upgrade switches the stack half way through while other types move it in
-    // the beginning
-    StackId effectiveStackId = upgradeContext.getTargetStackId();
-    if(upgradeContext.getType() == UpgradeType.NON_ROLLING ) {
-      effectiveStackId = upgradeContext.getSourceStackId();
-    }
+        configurationPackSourceStackId);
 
+    // create the upgrade and request
     for (UpgradeGroupHolder group : groups) {
-      boolean skippable = group.skippable;
-      boolean supportsAutoSkipOnFailure = group.supportsAutoSkipOnFailure;
-      boolean allowRetry = group.allowRetry;
-
-      if (upgradeContext.getType() == UpgradeType.NON_ROLLING
-          && UpdateStackGrouping.class.equals(group.groupClass)) {
-        effectiveStackId = upgradeContext.getTargetStackId();
-      }
-
       List<UpgradeItemEntity> itemEntities = new ArrayList<>();
+
       for (StageWrapper wrapper : group.items) {
+        RepositoryVersionEntity effectiveRepositoryVersion = upgradeContext.getRepositoryVersion();
+
         if (wrapper.getType() == StageWrapper.Type.SERVER_SIDE_ACTION) {
           // !!! each stage is guaranteed to be of one type. but because there
           // is a bug that prevents one stage with multiple tasks assigned for
@@ -919,9 +733,8 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
               itemEntities.add(itemEntity);
 
               injectVariables(configHelper, cluster, itemEntity);
-              makeServerSideStage(upgradeContext, req, effectiveStackId, itemEntity,
-                  (ServerSideActionTask) task, skippable, supportsAutoSkipOnFailure, allowRetry,
-                  pack, configUpgradePack);
+              makeServerSideStage(group, upgradeContext, effectiveRepositoryVersion, req,
+                  itemEntity, (ServerSideActionTask) task, configUpgradePack);
             }
           }
         } else {
@@ -934,8 +747,7 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
           injectVariables(configHelper, cluster, itemEntity);
 
           // upgrade items match a stage
-          createStage(upgradeContext, req, effectiveStackId, itemEntity, wrapper, skippable,
-              supportsAutoSkipOnFailure, allowRetry);
+          createStage(group, upgradeContext, effectiveRepositoryVersion, req, itemEntity, wrapper);
         }
       }
 
@@ -948,20 +760,11 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
       }
     }
 
-    UpgradeEntity entity = new UpgradeEntity();
-    entity.setFromRepositoryVersion(upgradeContext.getSourceRepositoryVersion());
-    entity.setToRepositoryVersion(upgradeContext.getTargetRepositoryVersion());
-    entity.setUpgradeGroups(groupEntities);
-    entity.setClusterId(cluster.getClusterId());
-    entity.setDirection(direction);
-    entity.setUpgradePackage(pack.getName());
-    entity.setUpgradeType(pack.getType());
-    entity.setAutoSkipComponentFailures(skipComponentFailures);
-    entity.setAutoSkipServiceCheckFailures(skipServiceCheckFailures);
-    entity.setDowngradeAllowed(pack.isDowngradeAllowed());
+    // set all of the groups we just created
+    upgrade.setUpgradeGroups(groupEntities);
 
     req.getRequestStatusResponse();
-    return createUpgradeInsideTransaction(cluster, req, entity);
+    return createUpgradeInsideTransaction(cluster, req, upgrade);
   }
 
   /**
@@ -998,269 +801,19 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
     return upgradeEntity;
   }
 
-  /**
-   * Handles the creation or resetting of configurations based on whether an
-   * upgrade or downgrade is occurring. This method will not do anything when
-   * the target stack version is the same as the cluster's current stack version
-   * since, by definition, no new configurations are automatically created when
-   * upgrading with the same stack (ie HDP 2.2.0.0 -> HDP 2.2.1.0).
-   * <p/>
-   * When upgrading or downgrade between stacks (HDP 2.2.0.0 -> HDP 2.3.0.0)
-   * then this will perform the following:
-   * <ul>
-   * <li>Upgrade: Create new configurations that are a merge between the current
-   * stack and the desired stack. If a value has changed between stacks, then
-   * the target stack value should be taken unless the cluster's value differs
-   * from the old stack. This can occur if a property has been customized after
-   * installation.</li>
-   * <li>Downgrade: Reset the latest configurations from the cluster's original
-   * stack. The new configurations that were created on upgrade must be left
-   * intact until all components have been reverted, otherwise heartbeats will
-   * fail due to missing configurations.</li>
-   * </ul>
-   *
-   *
-   * @param context
-   *          the upgrade context (not {@code null}).
-   * @throws AmbariException
-   */
-  public void applyStackAndProcessConfigurations(UpgradeContext context) throws AmbariException {
-    Cluster cluster = context.getCluster();
-    Direction direction = context.getDirection();
-    UpgradePack upgradePack = context.getUpgradePack();
-    RepositoryVersionEntity targetRve = context.getTargetRepositoryVersion();
-    String userName = getManagementController().getAuthName();
-
-    // if the current and target stacks are the same (ie HDP 2.2.0.0 -> 2.2.1.0)
-    // then we should never do anything with configs on either upgrade or
-    // downgrade; however if we are going across stacks, we have to do the stack
-    // checks differently depending on whether this is an upgrade or downgrade
-    StackEntity targetStack = targetRve.getStack();
-    StackId currentStackId = cluster.getCurrentStackVersion();
-    StackId desiredStackId = cluster.getDesiredStackVersion();
-    StackId targetStackId = new StackId(targetStack);
-    // Only change configs if moving to a different stack.
-    switch (direction) {
-      case UPGRADE:
-        if (currentStackId.equals(targetStackId)) {
-          return;
-        }
-        break;
-      case DOWNGRADE:
-        if (desiredStackId.equals(targetStackId)) {
-          return;
-        }
-        break;
-    }
-
-    Map<String, Map<String, String>> newConfigurationsByType = null;
-    ConfigHelper configHelper = getManagementController().getConfigHelper();
-
-    if (direction == Direction.UPGRADE) {
-      // populate a map of default configurations for the old stack (this is
-      // used when determining if a property has been customized and should be
-      // overriden with the new stack value)
-      Map<String, Map<String, String>> oldStackDefaultConfigurationsByType = configHelper.getDefaultProperties(
-          currentStackId, cluster);
-
-      // populate a map with default configurations from the new stack
-      newConfigurationsByType = configHelper.getDefaultProperties(targetStackId, cluster);
-
-      // We want to skip updating config-types of services that are not in the upgrade pack.
-      // Care should be taken as some config-types could be in services that are in and out
-      // of the upgrade pack. We should never ignore config-types of services in upgrade pack.
-      Set<String> skipConfigTypes = new HashSet<>();
-      Set<String> upgradePackServices = new HashSet<>();
-      Set<String> upgradePackConfigTypes = new HashSet<>();
-      AmbariMetaInfo ambariMetaInfo = s_metaProvider.get();
-
-      // ensure that we get the service info from the target stack
-      // (since it could include new configuration types for a service)
-      Map<String, ServiceInfo> stackServicesMap = ambariMetaInfo.getServices(
-          targetStack.getStackName(), targetStack.getStackVersion());
-
-      for (Grouping group : upgradePack.getGroups(direction)) {
-        for (UpgradePack.OrderService service : group.services) {
-          if (service.serviceName == null || upgradePackServices.contains(service.serviceName)) {
-            // No need to re-process service that has already been looked at
-            continue;
-          }
-
-          upgradePackServices.add(service.serviceName);
-          ServiceInfo serviceInfo = stackServicesMap.get(service.serviceName);
-          if (serviceInfo == null) {
-            continue;
-          }
-
-          // add every configuration type for all services defined in the
-          // upgrade pack
-          Set<String> serviceConfigTypes = serviceInfo.getConfigTypeAttributes().keySet();
-          for (String serviceConfigType : serviceConfigTypes) {
-            if (!upgradePackConfigTypes.contains(serviceConfigType)) {
-              upgradePackConfigTypes.add(serviceConfigType);
-            }
-          }
-        }
-      }
-
-      // build a set of configurations that should not be merged since their
-      // services are not installed
-      Set<String> servicesNotInUpgradePack = new HashSet<>(stackServicesMap.keySet());
-      servicesNotInUpgradePack.removeAll(upgradePackServices);
-      for (String serviceNotInUpgradePack : servicesNotInUpgradePack) {
-        ServiceInfo serviceInfo = stackServicesMap.get(serviceNotInUpgradePack);
-        Set<String> configTypesOfServiceNotInUpgradePack = serviceInfo.getConfigTypeAttributes().keySet();
-        for (String configType : configTypesOfServiceNotInUpgradePack) {
-          if (!upgradePackConfigTypes.contains(configType) && !skipConfigTypes.contains(configType)) {
-            skipConfigTypes.add(configType);
-          }
-        }
-      }
-
-      // remove any configurations from the target stack that are not used
-      // because the services are not installed
-      Iterator<String> iterator = newConfigurationsByType.keySet().iterator();
-      while (iterator.hasNext()) {
-        String configType = iterator.next();
-        if (skipConfigTypes.contains(configType)) {
-          LOG.info("Stack Upgrade: Removing configs for config-type {}", configType);
-          iterator.remove();
-        }
-      }
-
-      // now that the map has been populated with the default configurations
-      // from the stack/service, overlay the existing configurations on top
-      Map<String, DesiredConfig> existingDesiredConfigurationsByType = cluster.getDesiredConfigs();
-      for (Map.Entry<String, DesiredConfig> existingEntry : existingDesiredConfigurationsByType.entrySet()) {
-        String configurationType = existingEntry.getKey();
-        if(skipConfigTypes.contains(configurationType)) {
-          LOG.info("Stack Upgrade: Skipping config-type {} as upgrade-pack contains no updates to its service", configurationType);
-          continue;
-        }
-
-        // NPE sanity, although shouldn't even happen since we are iterating
-        // over the desired configs to start with
-        Config currentClusterConfig = cluster.getDesiredConfigByType(configurationType);
-        if (null == currentClusterConfig) {
-          continue;
-        }
-
-        // get current stack default configurations on install
-        Map<String, String> configurationTypeDefaultConfigurations = oldStackDefaultConfigurationsByType.get(
-            configurationType);
-
-        // NPE sanity for current stack defaults
-        if (null == configurationTypeDefaultConfigurations) {
-          configurationTypeDefaultConfigurations = Collections.emptyMap();
-        }
-
-        // get the existing configurations
-        Map<String, String> existingConfigurations = currentClusterConfig.getProperties();
-
-        // if the new stack configurations don't have the type, then simply add
-        // all of the existing in
-        Map<String, String> newDefaultConfigurations = newConfigurationsByType.get(
-            configurationType);
-
-        if (null == newDefaultConfigurations) {
-          newConfigurationsByType.put(configurationType, existingConfigurations);
-          continue;
-        } else {
-          // TODO, should we remove existing configs whose value is NULL even though they don't have a value in the new stack?
-
-          // Remove any configs in the new stack whose value is NULL, unless they currently exist and the value is not NULL.
-          Iterator<Map.Entry<String, String>> iter = newDefaultConfigurations.entrySet().iterator();
-          while (iter.hasNext()) {
-            Map.Entry<String, String> entry = iter.next();
-            if (entry.getValue() == null) {
-              iter.remove();
-            }
-          }
-        }
-
-        // for every existing configuration, see if an entry exists; if it does
-        // not exist, then put it in the map, otherwise we'll have to compare
-        // the existing value to the original stack value to see if its been
-        // customized
-        for (Map.Entry<String, String> existingConfigurationEntry : existingConfigurations.entrySet()) {
-          String existingConfigurationKey = existingConfigurationEntry.getKey();
-          String existingConfigurationValue = existingConfigurationEntry.getValue();
-
-          // if there is already an entry, we now have to try to determine if
-          // the value was customized after stack installation
-          if (newDefaultConfigurations.containsKey(existingConfigurationKey)) {
-            String newDefaultConfigurationValue = newDefaultConfigurations.get(
-                existingConfigurationKey);
-
-            if (!StringUtils.equals(existingConfigurationValue, newDefaultConfigurationValue)) {
-              // the new default is different from the existing cluster value;
-              // only override the default value if the existing value differs
-              // from the original stack
-              String oldDefaultValue = configurationTypeDefaultConfigurations.get(
-                  existingConfigurationKey);
-
-              if (!StringUtils.equals(existingConfigurationValue, oldDefaultValue)) {
-                // at this point, we've determined that there is a difference
-                // between default values between stacks, but the value was
-                // also customized, so keep the customized value
-                newDefaultConfigurations.put(existingConfigurationKey, existingConfigurationValue);
-              }
-            }
-          } else {
-            // there is no entry in the map, so add the existing key/value pair
-            newDefaultConfigurations.put(existingConfigurationKey, existingConfigurationValue);
-          }
-        }
-
-        /*
-        for every new configuration which does not exist in the existing
-        configurations, see if it was present in the current stack
-
-        stack 2.x has foo-site/property (on-ambari-upgrade is false)
-        stack 2.y has foo-site/property
-        the current cluster (on 2.x) does not have it
-
-        In this case, we should NOT add it back as clearly stack advisor has removed it
-        */
-        Iterator<Map.Entry<String, String>> newDefaultConfigurationsIterator = newDefaultConfigurations.entrySet().iterator();
-        while( newDefaultConfigurationsIterator.hasNext() ){
-          Map.Entry<String, String> newConfigurationEntry = newDefaultConfigurationsIterator.next();
-          String newConfigurationPropertyName = newConfigurationEntry.getKey();
-          if (configurationTypeDefaultConfigurations.containsKey(newConfigurationPropertyName)
-              && !existingConfigurations.containsKey(newConfigurationPropertyName)) {
-            LOG.info(
-                "The property {}/{} exists in both {} and {} but is not part of the current set of configurations and will therefore not be included in the configuration merge",
-                configurationType, newConfigurationPropertyName, currentStackId, targetStackId);
-
-            // remove the property so it doesn't get merged in
-            newDefaultConfigurationsIterator.remove();
-          }
-        }
-      }
-    } else {
-      // downgrade
-      cluster.applyLatestConfigurations(cluster.getCurrentStackVersion());
-    }
-
-    // !!! update the stack
-    cluster.setDesiredStackVersion(
-        new StackId(targetStack.getStackName(), targetStack.getStackVersion()), true);
-
-    // !!! configs must be created after setting the stack version
-    if (null != newConfigurationsByType) {
-      configHelper.createConfigTypes(cluster, getManagementController(), newConfigurationsByType,
-          userName, "Configuration created for Upgrade");
-    }
-  }
-
-  private RequestStageContainer createRequest(Cluster cluster, Direction direction, StackId stackId)
-      throws AmbariException {
+  private RequestStageContainer createRequest(UpgradeContext upgradeContext) throws AmbariException {
     ActionManager actionManager = getManagementController().getActionManager();
 
     RequestStageContainer requestStages = new RequestStageContainer(
         actionManager.getNextRequestId(), null, s_requestFactory.get(), actionManager);
-    requestStages.setRequestContext(String.format("%s to %s", direction.getVerb(true), stackId));
 
+    Direction direction = upgradeContext.getDirection();
+    RepositoryVersionEntity repositoryVersion = upgradeContext.getRepositoryVersion();
+
+    requestStages.setRequestContext(String.format("%s %s %s", direction.getVerb(true),
+        direction.getPreposition(), repositoryVersion.getVersion()));
+
+    Cluster cluster = upgradeContext.getCluster();
     Map<String, Set<String>> clusterHostInfo = StageUtils.getClusterHostInfo(cluster);
     String clusterHostInfoJson = StageUtils.getGson().toJson(clusterHostInfo);
     requestStages.setClusterHostInfo(clusterHostInfoJson);
@@ -1268,50 +821,30 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
     return requestStages;
   }
 
-  /**
-   * Builds a stage's commands.
-   *
-   * @param context
-   *          the upgrade context (not {@code null}).
-   * @param request
-   *          the request to add the new stage to (not {@code null}).
-   * @param effectiveStackId
-   *          the stack ID to use when building the command. This will determine
-   *          things like stack tools and version information added to the
-   *          command (not {@code null}).
-   * @param entity
-   *          the upgrade entity to add the new items to (not {@code null}).
-   * @param wrapper
-   *          the encapsulation of a stage (not {@code null}).
-   * @param skippable
-   *          {@code true} if the stage is skippable.
-   * @param supportsAutoSkipOnFailure
-   *          {@code true} if a failure will skip the stage automatically.
-   * @param allowRetry
-   *          {@code true} if the stage can be retried on failure.
-   * @throws AmbariException
-   */
-  private void createStage(UpgradeContext context, RequestStageContainer request,
-      StackId effectiveStackId, UpgradeItemEntity entity, StageWrapper wrapper, boolean skippable,
-      boolean supportsAutoSkipOnFailure, boolean allowRetry) throws AmbariException {
+  private void createStage(UpgradeGroupHolder group, UpgradeContext context,
+      RepositoryVersionEntity effectiveRepositoryVersion,
+      RequestStageContainer request, UpgradeItemEntity entity, StageWrapper wrapper)
+          throws AmbariException {
+
+    boolean skippable = group.skippable;
+    boolean supportsAutoSkipOnFailure = group.supportsAutoSkipOnFailure;
+    boolean allowRetry = group.allowRetry;
 
     switch (wrapper.getType()) {
       case CONFIGURE:
       case START:
       case STOP:
       case RESTART:
-        makeCommandStage(context, request, effectiveStackId, entity, wrapper, skippable,
-            supportsAutoSkipOnFailure,
-            allowRetry);
+        makeCommandStage(context, request, effectiveRepositoryVersion, entity, wrapper, skippable,
+            supportsAutoSkipOnFailure, allowRetry);
         break;
-      case RU_TASKS:
-        makeActionStage(context, request, effectiveStackId, entity, wrapper, skippable,
-            supportsAutoSkipOnFailure,
-            allowRetry);
+      case UPGRADE_TASKS:
+        makeActionStage(context, request, effectiveRepositoryVersion, entity, wrapper, skippable,
+            supportsAutoSkipOnFailure, allowRetry);
         break;
       case SERVICE_CHECK:
-        makeServiceCheckStage(context, request, effectiveStackId, entity, wrapper, skippable,
-            supportsAutoSkipOnFailure, allowRetry);
+        makeServiceCheckStage(context, request, effectiveRepositoryVersion, entity, wrapper,
+            skippable, supportsAutoSkipOnFailure, allowRetry);
         break;
       default:
         break;
@@ -1333,35 +866,10 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
     }
   }
 
-  /**
-   * Creates an action stage using the {@link #EXECUTE_TASK_ROLE} custom action
-   * to execute some Python command.
-   *
-   * @param context
-   *          the upgrade context.
-   * @param request
-   *          the request object to add the stage to.
-   * @param effectiveStackId
-   *          the stack ID to use when generating content for the command. On
-   *          some upgrade types, this may change during the course of the
-   *          upgrade orchestration. An express upgrade changes this after
-   *          stopping all services.
-   * @param entity
-   *          the upgrade entity to set the stage information on
-   * @param wrapper
-   *          the stage wrapper containing information to generate the stage.
-   * @param skippable
-   *          {@code true} to mark the stage as being skippable if a failure
-   *          occurs.
-   * @param supportsAutoSkipOnFailure
-   *          {@code true} to automatically skip on a failure.
-   * @param allowRetry
-   *          {@code true} to be able to retry the failed stage.
-   * @throws AmbariException
-   */
   private void makeActionStage(UpgradeContext context, RequestStageContainer request,
-      StackId effectiveStackId, UpgradeItemEntity entity, StageWrapper wrapper, boolean skippable,
-      boolean supportsAutoSkipOnFailure, boolean allowRetry) throws AmbariException {
+      RepositoryVersionEntity effectiveRepositoryVersion, UpgradeItemEntity entity,
+      StageWrapper wrapper, boolean skippable, boolean supportsAutoSkipOnFailure,
+      boolean allowRetry) throws AmbariException {
 
     if (0 == wrapper.getHosts().size()) {
       throw new AmbariException(
@@ -1370,10 +878,6 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
 
     Cluster cluster = context.getCluster();
 
-    // add each host to this stage
-    RequestResourceFilter filter = new RequestResourceFilter("", "",
-        new ArrayList<>(wrapper.getHosts()));
-
     LOG.debug("Analyzing upgrade item {} with tasks: {}.", entity.getText(), entity.getTasks());
     Map<String, String> params = getNewParameterMap(request, context);
     params.put(UpgradeContext.COMMAND_PARAM_TASKS, entity.getTasks());
@@ -1381,30 +885,40 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
     // Apply additional parameters to the command that come from the stage.
     applyAdditionalParameters(wrapper, params);
 
-    // the ru_execute_tasks invokes scripts - it needs information about where
-    // the scripts live and for that it should always use the target repository
-    // stack
-    if (CollectionUtils.isNotEmpty(wrapper.getTasks())
-        && wrapper.getTasks().get(0).getService() != null) {
+    // Because custom task may end up calling a script/function inside a
+    // service, it is necessary to set the
+    // service_package_folder and hooks_folder params.
+    AmbariMetaInfo ambariMetaInfo = s_metaProvider.get();
+    StackId stackId = effectiveRepositoryVersion.getStackId();
+
+    StackInfo stackInfo = ambariMetaInfo.getStack(stackId.getStackName(),
+        stackId.getStackVersion());
 
-      AmbariMetaInfo ambariMetaInfo = s_metaProvider.get();
+    // if the service/component are specified, then make sure to grab them off
+    // of the wrapper so they can be stored on the command for use later
+    String serviceName = null;
+    String componentName = null;
 
-      StackInfo stackInfo = ambariMetaInfo.getStack(effectiveStackId.getStackName(),
-          effectiveStackId.getStackVersion());
+    if (wrapper.getTasks() != null && wrapper.getTasks().size() > 0
+        && wrapper.getTasks().get(0).getService() != null) {
+      TaskWrapper taskWrapper = wrapper.getTasks().get(0);
+      serviceName = taskWrapper.getService();
+      componentName = taskWrapper.getComponent();
 
-      String serviceName = wrapper.getTasks().get(0).getService();
-      ServiceInfo serviceInfo = ambariMetaInfo.getService(effectiveStackId.getStackName(),
-          effectiveStackId.getStackVersion(), serviceName);
+      ServiceInfo serviceInfo = ambariMetaInfo.getService(stackId.getStackName(),
+          stackId.getStackVersion(), serviceName);
 
       params.put(SERVICE_PACKAGE_FOLDER, serviceInfo.getServicePackageFolder());
       params.put(HOOKS_FOLDER, stackInfo.getStackHooksFolder());
     }
 
+    // add each host to this stage
+    RequestResourceFilter filter = new RequestResourceFilter(serviceName, componentName,
+        new ArrayList<>(wrapper.getHosts()));
+
     ActionExecutionContext actionContext = new ActionExecutionContext(cluster.getClusterName(),
         EXECUTE_TASK_ROLE, Collections.singletonList(filter), params);
 
-    actionContext.setStackId(effectiveStackId);
-
     // hosts in maintenance mode are excluded from the upgrade
     actionContext.setMaintenanceModeHostExcluded(true);
 
@@ -1413,7 +927,7 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
     actionContext.setAutoSkipFailures(context.isComponentFailureAutoSkipped());
 
     ExecuteCommandJson jsons = s_commandExecutionHelper.get().getCommandJson(actionContext,
-        cluster, effectiveStackId);
+        cluster, effectiveRepositoryVersion);
 
     Stage stage = s_stageFactory.get().createNew(request.getId().longValue(), "/tmp/ambari",
         cluster.getClusterName(), cluster.getClusterId(), entity.getText(), jsons.getCommandParamsForStage(),
@@ -1444,28 +958,18 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
 
   /**
    * Used to create a stage for restart, start, or stop.
-   *
-   * @param context
-   *          Upgrade Context
-   * @param request
-   *          Container for stage
-   * @param effectiveStackId
-   *          the stack ID to use when building the command. This will determine
-   *          things like stack tools and version information added to the
-   *          command (not {@code null}).
-   * @param entity
-   *          Upgrade Item
-   * @param wrapper
-   *          Stage
-   * @param skippable
-   *          Whether the item can be skipped
-   * @param allowRetry
-   *          Whether the item is allowed to be retried
+   * @param context Upgrade Context
+   * @param request Container for stage
+   * @param entity Upgrade Item
+   * @param wrapper Stage
+   * @param skippable Whether the item can be skipped
+   * @param allowRetry Whether the item is allowed to be retried
    * @throws AmbariException
    */
   private void makeCommandStage(UpgradeContext context, RequestStageContainer request,
-      StackId effectiveStackId, UpgradeItemEntity entity, StageWrapper wrapper, boolean skippable,
-      boolean supportsAutoSkipOnFailure, boolean allowRetry) throws AmbariException {
+      RepositoryVersionEntity effectiveRepositoryVersion, UpgradeItemEntity entity,
+      StageWrapper wrapper, boolean skippable, boolean supportsAutoSkipOnFailure,
+      boolean allowRetry) throws AmbariException {
 
     Cluster cluster = context.getCluster();
 
@@ -1497,9 +1001,6 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
 
     ActionExecutionContext actionContext = new ActionExecutionContext(cluster.getClusterName(),
         function, filters, commandParams);
-
-    actionContext.setStackId(effectiveStackId);
-
     actionContext.setTimeout(wrapper.getMaxTimeout(s_configuration));
     actionContext.setRetryAllowed(allowRetry);
     actionContext.setAutoSkipFailures(context.isComponentFailureAutoSkipped());
@@ -1508,7 +1009,7 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
     actionContext.setMaintenanceModeHostExcluded(true);
 
     ExecuteCommandJson jsons = s_commandExecutionHelper.get().getCommandJson(actionContext,
-        cluster, effectiveStackId);
+        cluster, effectiveRepositoryVersion);
 
     Stage stage = s_stageFactory.get().createNew(request.getId().longValue(), "/tmp/ambari",
         cluster.getClusterName(), cluster.getClusterId(), entity.getText(), jsons.getCommandParamsForStage(),
@@ -1540,8 +1041,9 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
   }
 
   private void makeServiceCheckStage(UpgradeContext context, RequestStageContainer request,
-      StackId effectiveStackId, UpgradeItemEntity entity, StageWrapper wrapper, boolean skippable,
-      boolean supportsAutoSkipOnFailure, boolean allowRetry) throws AmbariException {
+      RepositoryVersionEntity effectiveRepositoryVersion, UpgradeItemEntity entity,
+      StageWrapper wrapper, boolean skippable, boolean supportsAutoSkipOnFailure,
+      boolean allowRetry) throws AmbariException {
 
     List<RequestResourceFilter> filters = new ArrayList<>();
 
@@ -1559,7 +1061,6 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
     ActionExecutionContext actionContext = new ActionExecutionContext(cluster.getClusterName(),
         "SERVICE_CHECK", filters, commandParams);
 
-    actionContext.setStackId(effectiveStackId);
     actionContext.setTimeout(wrapper.getMaxTimeout(s_configuration));
     actionContext.setRetryAllowed(allowRetry);
     actionContext.setAutoSkipFailures(context.isServiceCheckFailureAutoSkipped());
@@ -1569,7 +1070,7 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
     actionContext.setMaintenanceModeHostExcluded(true);
 
     ExecuteCommandJson jsons = s_commandExecutionHelper.get().getCommandJson(actionContext,
-        cluster, effectiveStackId);
+        cluster, effectiveRepositoryVersion);
 
     Stage stage = s_stageFactory.get().createNew(request.getId().longValue(), "/tmp/ambari",
         cluster.getClusterName(), cluster.getClusterId(), entity.getText(), jsons.getCommandParamsForStage(),
@@ -1594,35 +1095,22 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
 
   /**
    * Creates a stage consisting of server side actions
-   *
-   * @param context
-   *          upgrade context
-   * @param request
-   *          upgrade request
-   * @param effectiveStackId
-   *          the stack ID to use when building the command. This will determine
-   *          things like stack tools and version information added to the
-   *          command (not {@code null}).
-   * @param entity
-   *          a single of upgrade
-   * @param task
-   *          server-side task (if any)
-   * @param skippable
-   *          if user can skip stage on failure
-   * @param allowRetry
-   *          if user can retry running stage on failure
-   * @param configUpgradePack
-   *          a runtime-generated config upgrade pack that contains all config
-   *          change definitions from all stacks involved into upgrade
+   * @param context upgrade context
+   * @param request upgrade request
+   * @param entity a single of upgrade
+   * @param task server-side task (if any)
+   * @param configUpgradePack a runtime-generated config upgrade pack that
+   * contains all config change definitions from all stacks involved into
+   * upgrade
    * @throws AmbariException
    */
-  private void makeServerSideStage(UpgradeContext context, RequestStageContainer request,
-      StackId effectiveStackId, UpgradeItemEntity entity, ServerSideActionTask task,
-      boolean skippable, boolean supportsAutoSkipOnFailure, boolean allowRetry,
-      UpgradePack upgradePack, ConfigUpgradePack configUpgradePack)
-          throws AmbariException {
+  private void makeServerSideStage(UpgradeGroupHolder group, UpgradeContext context,
+      RepositoryVersionEntity effectiveRepositoryVersion, RequestStageContainer request,
+      UpgradeItemEntity entity, ServerSideActionTask task, ConfigUpgradePack configUpgradePack)
+      throws AmbariException {
 
     Cluster cluster = context.getCluster();
+    UpgradePack upgradePack = context.getUpgradePack();
 
     Map<String, String> commandParams = getNewParameterMap(request, context);
     commandParams.put(UpgradeContext.COMMAND_PARAM_UPGRADE_PACK, upgradePack.getName());
@@ -1702,23 +1190,22 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
         Role.AMBARI_SERVER_ACTION.toString(), Collections.<RequestResourceFilter> emptyList(),
         commandParams);
 
-    actionContext.setStackId(effectiveStackId);
     actionContext.setTimeout(Short.valueOf((short) -1));
-    actionContext.setRetryAllowed(allowRetry);
+    actionContext.setRetryAllowed(group.allowRetry);
     actionContext.setAutoSkipFailures(context.isComponentFailureAutoSkipped());
 
     // hosts in maintenance mode are excluded from the upgrade
     actionContext.setMaintenanceModeHostExcluded(true);
 
     ExecuteCommandJson jsons = s_commandExecutionHelper.get().getCommandJson(actionContext,
-        cluster, effectiveStackId);
+        cluster, context.getRepositoryVersion());
 
     Stage stage = s_stageFactory.get().createNew(request.getId().longValue(), "/tmp/ambari",
         cluster.getClusterName(), cluster.getClusterId(), stageText, jsons.getCommandParamsForStage(),
       jsons.getHostParamsForStage());
 
-    stage.setSkippable(skippable);
-    stage.setAutoSkipFailureSupported(supportsAutoSkipOnFailure);
+    stage.setSkippable(group.skippable);
+    stage.setAutoSkipFailureSupported(group.supportsAutoSkipOnFailure);
 
     long stageId = request.getLastStageId() + 1;
     if (0L == stageId) {
@@ -1732,7 +1219,7 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
         getManagementController().getAuthName(), Role.AMBARI_SERVER_ACTION, RoleCommand.EXECUTE,
         cluster.getClusterName(),
         new ServiceComponentHostServerActionEvent(null, System.currentTimeMillis()), commandParams,
-        itemDetail, null, s_configuration.getDefaultServerTaskTimeout(), allowRetry,
+        itemDetail, null, s_configuration.getDefaultServerTaskTimeout(), group.allowRetry,
         context.isComponentFailureAutoSkipped());
 
     request.addStages(Collections.singletonList(stage));
@@ -1743,11 +1230,7 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
    * following properties are already set:
    * <ul>
    * <li>{@link UpgradeContext#COMMAND_PARAM_CLUSTER_NAME}
-   * <li>{@link UpgradeContext#COMMAND_PARAM_VERSION}
    * <li>{@link UpgradeContext#COMMAND_PARAM_DIRECTION}
-   * <li>{@link UpgradeContext#COMMAND_PARAM_ORIGINAL_STACK}
-   * <li>{@link UpgradeContext#COMMAND_PARAM_TARGET_STACK}
-   * <li>{@link UpgradeContext#COMMAND_DOWNGRADE_FROM_VERSION}
    * <li>{@link UpgradeContext#COMMAND_PARAM_UPGRADE_TYPE}
    * <li>{@link KeyNames#REFRESH_CONFIG_TAGS_BEFORE_EXECUTION} - necessary in
    * order to have the commands contain the correct configurations. Otherwise,
@@ -1755,7 +1238,7 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
    * command was created. For upgrades, this is problematic since the commands
    * are all created ahead of time, but the upgrade may change configs as part
    * of the upgrade pack.</li>
-   * <li>{@link #COMMAND_PARAM_REQUEST_ID}</li> the ID of the request.
+   * <li>{@link UpgradeContext#COMMAND_PARAM_REQUEST_ID}</li> the ID of the request.
    * <ul>
    *
    * @return the initialized parameter map.
@@ -1858,54 +1341,47 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
   }
 
   /**
-   * Builds the list of {@link HostOrderItem}s from the upgrade request. If the
-   * upgrade request does not contain the hosts
+   * Creates the {@link UpgradeHistoryEntity} instances for this upgrade for
+   * every component participating.
    *
-   * @param requestMap
-   *          the map of properties from the request (not {@code null}).
-   * @return the ordered list of actions to orchestrate for the
-   *         {@link UpgradeType#HOST_ORDERED} upgrade.
-   * @throws AmbariException
-   *           if the request properties are not valid.
+   * @param cluster
+   *          the cluster (not {@code null}).
+   * @param upgrade
+   *          the upgrade to add the entities to (not {@code null}).
+   * @param upgradeContext
+   *          the upgrade context for this upgrade (not {@code null}).
    */
-  @SuppressWarnings("unchecked")
-  private List<HostOrderItem> extractHostOrderItemsFromRequest(Map<String, Object> requestMap)
-      throws AmbariException {
-    // ewwww
-    Set<Map<String, List<String>>> hostsOrder = (Set<Map<String, List<String>>>) requestMap.get(
-        UPGRADE_HOST_ORDERED_HOSTS);
-
-    if (CollectionUtils.isEmpty(hostsOrder)) {
-      throw new AmbariException(
-          String.format("The %s property must be specified when using a %s upgrade type.",
-              UPGRADE_HOST_ORDERED_HOSTS, UpgradeType.HOST_ORDERED));
-    }
-
-    List<HostOrderItem> hostOrderItems = new ArrayList<>();
-
-    // extract all of the hosts so that we can ensure they are all accounted for
-    Iterator<Map<String, List<String>>> iterator = hostsOrder.iterator();
-    while (iterator.hasNext()) {
-      Map<String, List<String>> grouping = iterator.next();
-      List<String> hosts = grouping.get("hosts");
-      List<String> serviceChecks = grouping.get("service_checks");
-
-      if (CollectionUtils.isEmpty(hosts) && CollectionUtils.isEmpty(serviceChecks)) {
-        throw new AmbariException(String.format(
-            "The %s property must contain at least one object with either a %s or %s key",
-            UPGRADE_HOST_ORDERED_HOSTS, "hosts", "service_checks"));
-      }
+  private void addComponentHistoryToUpgrade(Cluster cluster, UpgradeEntity upgrade,
+      UpgradeContext upgradeContext) throws AmbariException {
+    Set<String> services = upgradeContext.getSupportedServices();
+    for (String serviceName : services) {
+      Service service = cluster.getService(serviceName);
+      Map<String, ServiceComponent> componentMap = service.getServiceComponents();
+      for (ServiceComponent component : componentMap.values()) {
+        UpgradeHistoryEntity history = new UpgradeHistoryEntity();
+        history.setUpgrade(upgrade);
+        history.setServiceName(serviceName);
+        history.setComponentName(component.getName());
+
+        // depending on whether this is an upgrade or a downgrade, the history
+        // will be different
+        if (upgradeContext.getDirection() == Direction.UPGRADE || upgradeContext.isPatchRevert()) {
+          history.setFromRepositoryVersion(component.getDesiredRepositoryVersion());
+          history.setTargetRepositoryVersion(upgradeContext.getRepositoryVersion());
+        } else {
+          // the target version on a downgrade is the original version that the
+          // service was on in the failed upgrade
+          RepositoryVersionEntity targetRepositoryVersion = upgradeContext.getTargetRepositoryVersion(
+              serviceName);
 
-      if (CollectionUtils.isNotEmpty(hosts)) {
-        hostOrderItems.add(new HostOrderItem(HostOrderActionType.HOST_UPGRADE, hosts));
-      }
+          history.setFromRepositoryVersion(upgradeContext.getRepositoryVersion());
+          history.setTargetRepositoryVersion(targetRepositoryVersion);
+        }
 
-      if (CollectionUtils.isNotEmpty(serviceChecks)) {
-        hostOrderItems.add(new HostOrderItem(HostOrderActionType.SERVICE_CHECK, serviceChecks));
+        // add the history
+        upgrade.addHistory(history);
       }
     }
-
-    return hostOrderItems;
   }
 
   /**
@@ -1967,269 +1443,31 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
   }
 
   /**
-   * Builds a chain of {@link UpgradeRequestValidator}s to ensure that the
-   * incoming request to create a new upgrade is valid.
-   *
-   * @param upgradeType
-   *          the type of upgrade to build the validator for.
-   * @return the validator which can check to ensure that the properties are
-   *         valid.
+   * The {@link RepositoryVersions} class is used to represent to/from versions
+   * of a service during an upgrade or downgrade.
    */
-  private UpgradeRequestValidator buildValidator(UpgradeType upgradeType){
-    UpgradeRequestValidator validator = new BasicUpgradePropertiesValidator();
-    UpgradeRequestValidator preReqValidator = new PreReqCheckValidator();
-    validator.setNextValidator(preReqValidator);
-
-    final UpgradeRequestValidator upgradeTypeValidator;
-    switch( upgradeType ){
-      case HOST_ORDERED:
-        upgradeTypeValidator = new HostOrderedUpgradeValidator();
-        break;
-      case NON_ROLLING:
-      case ROLLING:
-      default:
-        upgradeTypeValidator = null;
-        break;
-    }
+  final static class RepositoryVersions {
+    @JsonProperty("from_repository_id")
+    final long fromRepositoryId;
 
-    preReqValidator.setNextValidator(upgradeTypeValidator);
-    return validator;
-  }
+    @JsonProperty("from_repository_version")
+    final String fromRepositoryVersion;
 
-  /**
-   * The {@link UpgradeRequestValidator} contains the logic to check for correct
-   * upgrade request properties and then pass the responsibility onto the next
-   * validator in the chain.
-   */
-  private abstract class UpgradeRequestValidator {
-    /**
-     * The next validator.
-     */
-    UpgradeRequestValidator m_nextValidator;
+    @JsonProperty("to_repository_id")
+    final long toRepositoryId;
 
-    /**
-     * Sets the next validator in the chain.
-     *
-     * @param nextValidator
-     *          the next validator to run, or {@code null} for none.
-     */
-    void setNextValidator(UpgradeRequestValidator nextValidator) {
-      m_nextValidator = nextValidator;
-    }
-
-    /**
-     * Validates the upgrade request from this point in the chain.
-     *
-     * @param upgradeContext
-     * @param upgradePack
-     * @throws AmbariException
-     */
-    final void validate(UpgradeContext upgradeContext, UpgradePack upgradePack)
-        throws AmbariException {
-
-      // run this instance's check
-      check(upgradeContext, upgradePack);
-
-      // pass along to the next
-      if( null != m_nextValidator ) {
-        m_nextValidator.validate(upgradeContext, upgradePack);
-      }
-    }
-
-    /**
-     * Checks to ensure that upgrade request is valid given the specific
-     * arguments.
-     *
-     * @param upgradeContext
-     * @param upgradePack
-     *
-     * @throws AmbariException
-     */
-    abstract void check(UpgradeContext upgradeContext, UpgradePack upgradePack)
-        throws AmbariException;
-  }
-
-  /**
-   * The {@link BasicUpgradePropertiesValidator} ensures that the basic required
-   * properties are present on the upgrade request.
-   */
-  private final class BasicUpgradePropertiesValidator extends UpgradeRequestValidator {
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void check(UpgradeContext upgradeContext, UpgradePack upgradePack)
-        throws AmbariException {
-      Map<String, Object> requestMap = upgradeContext.getUpgradeRequest();
-
-      String clusterName = (String) requestMap.get(UPGRADE_CLUSTER_NAME);
-      String versionId = (String) requestMap.get(UPGRADE_REPO_ID);
-      String version = (String) requestMap.get(UPGRADE_REPO_VERSION);
-      String direction = (String) requestMap.get(UPGRADE_DIRECTION);
-
-      if (StringUtils.isBlank(clusterName)) {
-        throw new AmbariException(String.format("%s is required", UPGRADE_CLUSTER_NAME));
-      }
-
-      if (StringUtils.isBlank(versionId) && StringUtils.isBlank(version)) {
-        throw new AmbariException(
-            String.format("Either %s or %s is required", UPGRADE_REPO_ID, UPGRADE_REPO_VERSION));
-      }
-
-      if (StringUtils.isBlank(direction)) {
-        throw new AmbariException(String.format("%s is required", UPGRADE_DIRECTION));
-      }
-    }
-  }
-
-  /**
-   * The {@link PreReqCheckValidator} ensures that the upgrade pre-requisite
-   * checks have passed.
-   */
-  private final class PreReqCheckValidator extends UpgradeRequestValidator {
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    void check(UpgradeContext upgradeContext, UpgradePack upgradePack) throws AmbariException {
-      Cluster cluster = upgradeContext.getCluster();
-      Direction direction = upgradeContext.getDirection();
-      Map<String, Object> requestMap = upgradeContext.getUpgradeRequest();
-      UpgradeType upgradeType = upgradeContext.getType();
-
-      String version = (String) requestMap.get(UPGRADE_REPO_VERSION);
-      boolean skipPrereqChecks = Boolean.parseBoolean((String) requestMap.get(UPGRADE_SKIP_PREREQUISITE_CHECKS));
-      boolean failOnCheckWarnings = Boolean.parseBoolean((String) requestMap.get(UPGRADE_FAIL_ON_CHECK_WARNINGS));
-      String preferredUpgradePack = requestMap.containsKey(UPGRADE_PACK) ? (String) requestMap.get(UPGRADE_PACK) : null;
-
-      // verify that there is not an upgrade or downgrade that is in progress or suspended
-      UpgradeEntity existingUpgrade = cluster.getUpgradeInProgress();
-      if( null != existingUpgrade ){
-        throw new AmbariException(
-            String.format("Unable to perform %s as another %s (request ID %s) is in progress.",
-                direction.getText(false), existingUpgrade.getDirection().getText(false),
-                existingUpgrade.getRequestId()));
-      }
-
-      // skip this check if it's a downgrade or we are instructed to skip it
-      if( direction.isDowngrade() || skipPrereqChecks ){
-        return;
-      }
-
-      // Validate pre-req checks pass
-      PreUpgradeCheckResourceProvider preUpgradeCheckResourceProvider = (PreUpgradeCheckResourceProvider)
-          getResourceProvider(Resource.Type.PreUpgradeCheck);
-
-      Predicate preUpgradeCheckPredicate = new PredicateBuilder().property(
-          PreUpgradeCheckResourceProvider.UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID).equals(cluster.getClusterName()).and().property(
-          PreUpgradeCheckResourceProvider.UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID).equals(version).and().property(
-          PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID).equals(upgradeType).and().property(
-          PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_PACK_PROPERTY_ID).equals(preferredUpgradePack).toPredicate();
-
-      Request preUpgradeCheckRequest = PropertyHelper.getReadRequest();
-
-      Set<Resource> preUpgradeCheckResources;
-      try {
-        preUpgradeCheckResources = preUpgradeCheckResourceProvider.getResources(
-            preUpgradeCheckRequest, preUpgradeCheckPredicate);
-      } catch (NoSuchResourceException|SystemException|UnsupportedPropertyException|NoSuchParentResourceException e) {
-        throw new AmbariException(
-            String.format("Unable to perform %s. Prerequisite checks could not be run",
-                direction.getText(false), e));
-      }
-
-      List<Resource> failedResources = new LinkedList<>();
-      if (preUpgradeCheckResources != null) {
-        for (Resource res : preUpgradeCheckResources) {
-          PrereqCheckStatus prereqCheckStatus = (PrereqCheckStatus) res.getPropertyValue(
-              PreUpgradeCheckResourceProvider.UPGRADE_CHECK_STATUS_PROPERTY_ID);
-
-          if (prereqCheckStatus == PrereqCheckStatus.FAIL
-              || (failOnCheckWarnings && prereqCheckStatus == PrereqCheckStatus.WARNING)) {
-            failedResources.add(res);
-          }
-        }
-      }
-
-      if (!failedResources.isEmpty()) {
-        throw new AmbariException(
-            String.format("Unable to perform %s. Prerequisite checks failed %s",
-                direction.getText(false), s_gson.toJson(failedResources)));
-      }
-    }
-  }
-
-  /**
-   * Ensures that for {@link UpgradeType#HOST_ORDERED}, the properties supplied
-   * are valid.
-   */
-  @SuppressWarnings("unchecked")
-  private final class HostOrderedUpgradeValidator extends UpgradeRequestValidator {
+    @JsonProperty("to_repository_version")
+    final String toRepositoryVersion;
 
     /**
-     * {@inheritDoc}
+     * Constructor.
      */
-    @Override
-    void check(UpgradeContext upgradeContext, UpgradePack upgradePack) throws AmbariException {
-      Cluster cluster = upgradeContext.getCluster();
-      Direction direction = upgradeContext.getDirection();
-      Map<String, Object> requestMap = upgradeContext.getUpgradeRequest();
-
-      String skipFailuresRequestProperty = (String) requestMap.get(UPGRADE_SKIP_FAILURES);
-      if (Boolean.parseBoolean(skipFailuresRequestProperty)) {
-        throw new AmbariException(
-            String.format("The %s property is not valid when creating a %s upgrade.",
-                UPGRADE_SKIP_FAILURES, UpgradeType.HOST_ORDERED));
-      }
-
-      String skipManualVerification = (String) requestMap.get(UPGRADE_SKIP_MANUAL_VERIFICATION);
-      if (Boolean.parseBoolean(skipManualVerification)) {
-        throw new AmbariException(
-            String.format("The %s property is not valid when creating a %s upgrade.",
-                UPGRADE_SKIP_MANUAL_VERIFICATION, UpgradeType.HOST_ORDERED));
-      }
-
-      if (!requestMap.containsKey(UPGRADE_HOST_ORDERED_HOSTS)) {
-        throw new AmbariException(
-            String.format("The %s property is required when creating a %s upgrade.",
-                UPGRADE_HOST_ORDERED_HOSTS, UpgradeType.HOST_ORDERED));
-      }
-
-      List<HostOrderItem> hostOrderItems = extractHostOrderItemsFromRequest(requestMap);
-      List<String> hostsFromRequest = new ArrayList<>(hostOrderItems.size());
-      for (HostOrderItem hostOrderItem : hostOrderItems) {
-        if (hostOrderItem.getType() == HostOrderActionType.HOST_UPGRADE) {
-          hostsFromRequest.addAll(hostOrderItem.getActionItems());
-        }
-      }
-
-      // ensure that all hosts for this cluster are accounted for
-      Collection<Host> hosts = cluster.getHosts();
-      Set<String> clusterHostNames = new HashSet<>(hosts.size());
-      for (Host host : hosts) {
-        clusterHostNames.add(host.getHostName());
-      }
+    public RepositoryVersions(RepositoryVersionEntity from, RepositoryVersionEntity to) {
+      fromRepositoryId = from.getId();
+      fromRepositoryVersion = from.getVersion();
 
-      Collection<String> disjunction = CollectionUtils.disjunction(hostsFromRequest,
-          clusterHostNames);
-
-      if (CollectionUtils.isNotEmpty(disjunction)) {
-        throw new AmbariException(String.format(
-            "The supplied list of hosts must match the cluster hosts in an upgrade of type %s. The following hosts are either missing or invalid: %s",
-            UpgradeType.HOST_ORDERED, StringUtils.join(disjunction, ", ")));
-      }
-
-      // verify that the upgradepack has the required grouping and set the
-      // action items on it
-      HostOrderGrouping hostOrderGrouping = null;
-      List<Grouping> groupings = upgradePack.getGroups(direction);
-      for (Grouping grouping : groupings) {
-        if (grouping instanceof HostOrderGrouping) {
-          hostOrderGrouping = (HostOrderGrouping) grouping;
-          hostOrderGrouping.setHostOrderItems(hostOrderItems);
-        }
-      }
+      toRepositoryId = to.getId();
+      toRepositoryVersion = to.getVersion();
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/PropertyHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/PropertyHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/PropertyHelper.java
index 4ded10e..455d569 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/PropertyHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/PropertyHelper.java
@@ -20,7 +20,6 @@ package org.apache.ambari.server.controller.utilities;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.EnumMap;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -31,13 +30,11 @@ import java.util.regex.Pattern;
 
 import org.apache.ambari.server.controller.internal.PropertyInfo;
 import org.apache.ambari.server.controller.internal.RequestImpl;
-import org.apache.ambari.server.controller.jmx.JMXPropertyProvider;
 import org.apache.ambari.server.controller.spi.PageRequest;
 import org.apache.ambari.server.controller.spi.Request;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.SortRequest;
 import org.apache.ambari.server.controller.spi.TemporalInfo;
-import org.apache.ambari.server.state.stack.Metric;
 import org.apache.commons.lang.StringUtils;
 import org.codehaus.jackson.map.ObjectMapper;
 import org.codehaus.jackson.type.TypeReference;
@@ -52,7 +49,7 @@ public class PropertyHelper {
   private static final String SQLSERVER_PROPERTIES_FILE = "sqlserver_properties.json";
   private static final String JMX_PROPERTIES_FILE = "jmx_properties.json";
   private static final String KEY_PROPERTIES_FILE = "key_properties.json";
-  private static final char EXTERNAL_PATH_SEP = '/';
+  public static final char EXTERNAL_PATH_SEP = '/';
 
   /**
    * Aggregate functions implicitly supported by the Metrics Service
@@ -126,7 +123,7 @@ public class PropertyHelper {
    * @return the set of property ids
    */
   public static Set<String> getPropertyIds(Map<String, Map<String, PropertyInfo>> componentPropertyInfoMap ) {
-    Set<String> propertyIds = new HashSet<String>();
+    Set<String> propertyIds = new HashSet<>();
 
     for (Map.Entry<String, Map<String, PropertyInfo>> entry : componentPropertyInfoMap.entrySet()) {
       propertyIds.addAll(entry.getValue().keySet());
@@ -250,7 +247,7 @@ public class PropertyHelper {
    * @return the set of categories
    */
   public static Set<String> getCategories(Set<String> propertyIds) {
-    Set<String> categories = new HashSet<String>();
+    Set<String> categories = new HashSet<>();
     for (String property : propertyIds) {
       String category = PropertyHelper.getPropertyCategory(property);
       while (category != null) {
@@ -314,9 +311,9 @@ public class PropertyHelper {
     Set<String> ids = request.getPropertyIds();
 
     if (ids != null) {
-      ids = new HashSet<String>(ids);
+      ids = new HashSet<>(ids);
     } else {
-      ids = new HashSet<String>();
+      ids = new HashSet<>();
     }
 
     Set<Map<String, Object>> properties = request.getProperties();
@@ -336,7 +333,7 @@ public class PropertyHelper {
    * @return the map of properties for the given resource
    */
   public static Map<String, Object> getProperties(Resource resource) {
-    Map<String, Object> properties = new HashMap<String, Object>();
+    Map<String, Object> properties = new HashMap<>();
 
     Map<String, Map<String, Object>> categories = resource.getPropertiesMap();
 
@@ -416,7 +413,7 @@ public class PropertyHelper {
    * @param propertyIds  the property ids associated with the request; may be null
    */
   public static Request getReadRequest(String ... propertyIds) {
-    return PropertyHelper.getReadRequest(new HashSet<String>(
+    return PropertyHelper.getReadRequest(new HashSet<>(
         Arrays.asList(propertyIds)));
   }
 
@@ -441,13 +438,13 @@ public class PropertyHelper {
               new TypeReference<Map<Resource.InternalType, Map<String, Map<String, Metric>>>>() {});
 
       Map<Resource.InternalType, Map<String, Map<String, PropertyInfo>>> resourceMetrics =
-          new HashMap<Resource.InternalType, Map<String, Map<String, PropertyInfo>>>();
+          new HashMap<>();
 
       for (Map.Entry<Resource.InternalType, Map<String, Map<String, Metric>>> resourceEntry : resourceMetricMap.entrySet()) {
-        Map<String, Map<String, PropertyInfo>> componentMetrics = new HashMap<String, Map<String, PropertyInfo>>();
+        Map<String, Map<String, PropertyInfo>> componentMetrics = new HashMap<>();
 
         for (Map.Entry<String, Map<String, Metric>> componentEntry : resourceEntry.getValue().entrySet()) {
-          Map<String, PropertyInfo> metrics = new HashMap<String, PropertyInfo>();
+          Map<String, PropertyInfo> metrics = new HashMap<>();
 
           for (Map.Entry<String, Metric> metricEntry : componentEntry.getValue().entrySet()) {
             String property = metricEntry.getKey();
@@ -492,11 +489,11 @@ public class PropertyHelper {
               new TypeReference<Map<Resource.InternalType, Map<Resource.InternalType, String>>>() {});
 
       Map<Resource.InternalType, Map<Resource.Type, String>> returnMap =
-          new HashMap<Resource.InternalType, Map<Resource.Type, String>>();
+          new HashMap<>();
 
       // convert inner maps from InternalType to Type
       for (Map.Entry<Resource.InternalType, Map<Resource.InternalType, String>> entry : map.entrySet()) {
-        Map<Resource.Type, String> innerMap = new HashMap<Resource.Type, String>();
+        Map<Resource.Type, String> innerMap = new HashMap<>();
 
         for (Map.Entry<Resource.InternalType, String> entry1 : entry.getValue().entrySet()) {
           innerMap.put(Resource.Type.valueOf(entry1.getKey().name()), entry1.getValue());
@@ -596,7 +593,7 @@ public class PropertyHelper {
     for (Map.Entry<String, Map<String, PropertyInfo>> metricInfoEntry : metrics.entrySet()) {
       Map<String, PropertyInfo> metricInfo = metricInfoEntry.getValue();
 
-      Map<String, PropertyInfo> aggregateMetrics = new HashMap<String, PropertyInfo>();
+      Map<String, PropertyInfo> aggregateMetrics = new HashMap<>();
       // For every metric
       for (Map.Entry<String, PropertyInfo> metricEntry : metricInfo.entrySet()) {
         // For each aggregate function id


[26/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeContext.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeContext.java b/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeContext.java
index b97dc80..d2c0ea2 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeContext.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeContext.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -17,31 +17,70 @@
  */
 package org.apache.ambari.server.state;
 
-import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.VERSION;
+import static org.apache.ambari.server.controller.internal.UpgradeResourceProvider.UPGRADE_DIRECTION;
+import static org.apache.ambari.server.controller.internal.UpgradeResourceProvider.UPGRADE_FAIL_ON_CHECK_WARNINGS;
+import static org.apache.ambari.server.controller.internal.UpgradeResourceProvider.UPGRADE_HOST_ORDERED_HOSTS;
+import static org.apache.ambari.server.controller.internal.UpgradeResourceProvider.UPGRADE_PACK;
+import static org.apache.ambari.server.controller.internal.UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID;
+import static org.apache.ambari.server.controller.internal.UpgradeResourceProvider.UPGRADE_REVERT_UPGRADE_ID;
+import static org.apache.ambari.server.controller.internal.UpgradeResourceProvider.UPGRADE_SKIP_FAILURES;
+import static org.apache.ambari.server.controller.internal.UpgradeResourceProvider.UPGRADE_SKIP_MANUAL_VERIFICATION;
+import static org.apache.ambari.server.controller.internal.UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS;
+import static org.apache.ambari.server.controller.internal.UpgradeResourceProvider.UPGRADE_SKIP_SC_FAILURES;
+import static org.apache.ambari.server.controller.internal.UpgradeResourceProvider.UPGRADE_TYPE;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import org.apache.ambari.annotations.Experimental;
 import org.apache.ambari.annotations.ExperimentalFeature;
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.ServiceNotFoundException;
 import org.apache.ambari.server.actionmanager.HostRoleCommandFactory;
 import org.apache.ambari.server.agent.ExecutionCommand.KeyNames;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.controller.internal.AbstractControllerResourceProvider;
+import org.apache.ambari.server.controller.internal.PreUpgradeCheckResourceProvider;
+import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
+import org.apache.ambari.server.controller.spi.NoSuchResourceException;
+import org.apache.ambari.server.controller.spi.Predicate;
+import org.apache.ambari.server.controller.spi.Request;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.controller.spi.SystemException;
+import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
+import org.apache.ambari.server.controller.utilities.PredicateBuilder;
+import org.apache.ambari.server.controller.utilities.PropertyHelper;
+import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
+import org.apache.ambari.server.orm.dao.UpgradeDAO;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
+import org.apache.ambari.server.orm.entities.UpgradeHistoryEntity;
 import org.apache.ambari.server.stack.MasterHostResolver;
 import org.apache.ambari.server.stageplanner.RoleGraphFactory;
+import org.apache.ambari.server.state.repository.VersionDefinitionXml;
+import org.apache.ambari.server.state.stack.PrereqCheckStatus;
 import org.apache.ambari.server.state.stack.UpgradePack;
 import org.apache.ambari.server.state.stack.upgrade.Direction;
 import org.apache.ambari.server.state.stack.upgrade.Grouping;
+import org.apache.ambari.server.state.stack.upgrade.HostOrderGrouping;
+import org.apache.ambari.server.state.stack.upgrade.HostOrderItem;
+import org.apache.ambari.server.state.stack.upgrade.HostOrderItem.HostOrderActionType;
 import org.apache.ambari.server.state.stack.upgrade.UpgradeScope;
 import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Objects;
 import com.google.gson.Gson;
 import com.google.gson.JsonElement;
 import com.google.inject.Inject;
@@ -49,11 +88,14 @@ import com.google.inject.assistedinject.Assisted;
 import com.google.inject.assistedinject.AssistedInject;
 
 /**
- * Used to hold various helper objects required to process an upgrade pack.
+ * The {@link UpgradeContext} is used to hold all information pertaining to an
+ * upgrade. It is initialized directly from an existing {@link UpgradeEntity} or
+ * from a request to create an upgrade/downgrade.
  */
 public class UpgradeContext {
 
-  public static final String COMMAND_PARAM_VERSION = VERSION;
+  private static final Logger LOG = LoggerFactory.getLogger(UpgradeContext.class);
+
   public static final String COMMAND_PARAM_CLUSTER_NAME = "clusterName";
   public static final String COMMAND_PARAM_DIRECTION = "upgrade_direction";
   public static final String COMMAND_PARAM_UPGRADE_PACK = "upgrade_pack";
@@ -62,21 +104,6 @@ public class UpgradeContext {
   public static final String COMMAND_PARAM_UPGRADE_TYPE = "upgrade_type";
   public static final String COMMAND_PARAM_TASKS = "tasks";
   public static final String COMMAND_PARAM_STRUCT_OUT = "structured_out";
-  public static final String COMMAND_DOWNGRADE_FROM_VERSION = "downgrade_from_version";
-
-  /**
-   * The original "current" stack of the cluster before the upgrade started.
-   * This is the same regardless of whether the current direction is
-   * {@link Direction#UPGRADE} or {@link Direction#DOWNGRADE}.
-   */
-  public static final String COMMAND_PARAM_ORIGINAL_STACK = "original_stack";
-
-  /**
-   * The target upgrade stack before the upgrade started. This is the same
-   * regardless of whether the current direction is {@link Direction#UPGRADE} or
-   * {@link Direction#DOWNGRADE}.
-   */
-  public static final String COMMAND_PARAM_TARGET_STACK = "target_stack";
 
   /**
    * The cluster that the upgrade is for.
@@ -94,31 +121,42 @@ public class UpgradeContext {
   final private UpgradeType m_type;
 
   /**
-   * The request parameters from the REST API for creating this upgrade.
+   * The upgrade pack for this upgrade.
    */
-  final private Map<String, Object> m_upgradeRequestMap;
+  private UpgradePack m_upgradePack;
 
   /**
-   * The upgrade pack for this upgrade.
+   * Upgrades will always have a single version being upgraded to and downgrades
+   * will have a single version being downgraded from. This repository
+   * represents that version.
+   * <p/>
+   * When the direction is {@link Direction#UPGRADE}, this represents the target
+   * repository. <br/>
+   * When the direction is {@link Direction#DOWNGRADE}, this represents the
+   * repository being downgraded from.
    */
-  private UpgradePack m_upgradePack;
+  private final RepositoryVersionEntity m_repositoryVersion;
 
   /**
-   * The source of the upgrade/downgrade.
+   * Resolves master components on hosts.
    */
-  private final RepositoryVersionEntity m_fromRepositoryVersion;
+  private final MasterHostResolver m_resolver;
 
   /**
-   * The target of the upgrade/downgrade.
+   * A collection of hosts in the cluster which are unhealthy and will not
+   * participate in the upgrade.
    */
-  private final RepositoryVersionEntity m_toRepositoryVersion;
+  private final List<ServiceComponentHost> m_unhealthy = new ArrayList<>();
 
+  /**
+   * Mapping of service name to display name.
+   */
+  private final Map<String, String> m_serviceNames = new HashMap<>();
 
-  private MasterHostResolver m_resolver;
-  private AmbariMetaInfo m_metaInfo;
-  private List<ServiceComponentHost> m_unhealthy = new ArrayList<>();
-  private Map<String, String> m_serviceNames = new HashMap<>();
-  private Map<String, String> m_componentNames = new HashMap<>();
+  /**
+   * Mapping of component name to display name.
+   */
+  private final Map<String, String> m_componentNames = new HashMap<>();
 
   /**
    * {@code true} if slave/client component failures should be automatically
@@ -139,9 +177,27 @@ public class UpgradeContext {
    */
   private boolean m_autoSkipManualVerification = false;
 
-  private Set<String> m_supported = new HashSet<>();
+  /**
+   * A set of services which are included in this upgrade. If this is empty,
+   * then all cluster services are included.
+   */
+  private Set<String> m_services = new HashSet<>();
 
-  private UpgradeScope m_scope = UpgradeScope.ANY;
+  /**
+   * A mapping of service to target repository. On an upgrade, this will be the
+   * same for all services. On a downgrade, this may be different for each
+   * service depending on which repository the service was on before the failed
+   * upgrade.
+   */
+  private final Map<String, RepositoryVersionEntity> m_targetRepositoryMap = new HashMap<>();
+
+  /**
+   * A mapping of service to source (from) repository. On an upgrade, this will
+   * be the current desired repository of every service. When downgrading, this
+   * will be the same for all components and will represent the value returned
+   * from {@link #getRepositoryVersion()}.
+   */
+  private final Map<String, RepositoryVersionEntity> m_sourceRepositoryMap = new HashMap<>();
 
   /**
    * Used by some {@link Grouping}s to generate commands. It is exposed here
@@ -164,29 +220,249 @@ public class UpgradeContext {
   private Gson m_gson;
 
   /**
-   * Constructor.
-   *
-   * @param cluster
-   *          the cluster that the upgrade is for
-   * @param type
-   *          the type of upgrade, either rolling or non_rolling
-   * @param direction
-   *          the direction for the upgrade
-   * @param upgradeRequestMap
-   *          the original map of paramters used to create the upgrade
+   * Used for looking up information about components and services.
+   */
+  @Inject
+  private AmbariMetaInfo m_metaInfo;
+
+  /**
+   * Used to suggest upgrade packs during creation of an upgrade context.
+   */
+  @Inject
+  private UpgradeHelper m_upgradeHelper;
+
+  /**
+   * Used to lookup the repository version from an ID.
+   */
+  @Inject
+  private RepositoryVersionDAO m_repoVersionDAO;
+
+  /**
+   * Used to lookup a prior upgrade by ID.
    */
+  @Inject
+  private UpgradeDAO m_upgradeDAO;
+
+  /**
+   * Used as a quick way to tell if the upgrade is to revert a patch.
+   */
+  private boolean m_isRevert = false;
+
+  /**
+   * Defines orchestration type.  This is not the repository type when reverting a patch.
+   */
+  private RepositoryType m_orchestration = RepositoryType.STANDARD;
+
   @AssistedInject
-  public UpgradeContext(@Assisted Cluster cluster, @Assisted UpgradeType type,
-      @Assisted Direction direction,
-      @Assisted("fromRepositoryVersion") RepositoryVersionEntity fromRepositoryVersion,
-      @Assisted("toRepositoryVersion") RepositoryVersionEntity toRepositoryVersion,
-      @Assisted Map<String, Object> upgradeRequestMap) {
+  public UpgradeContext(@Assisted Cluster cluster,
+      @Assisted Map<String, Object> upgradeRequestMap, Gson gson, UpgradeHelper upgradeHelper,
+      UpgradeDAO upgradeDAO, RepositoryVersionDAO repoVersionDAO, ConfigHelper configHelper)
+      throws AmbariException {
+    // injected constructor dependencies
+    m_gson = gson;
+    m_upgradeHelper = upgradeHelper;
+    m_upgradeDAO = upgradeDAO;
+    m_repoVersionDAO = repoVersionDAO;
+
     m_cluster = cluster;
-    m_type = type;
-    m_direction = direction;
-    m_fromRepositoryVersion = fromRepositoryVersion;
-    m_toRepositoryVersion = toRepositoryVersion;
-    m_upgradeRequestMap = upgradeRequestMap;
+
+    // determine upgrade type (default is ROLLING)
+    String upgradeTypeProperty = (String) upgradeRequestMap.get(UPGRADE_TYPE);
+    if (StringUtils.isNotBlank(upgradeTypeProperty)) {
+      try {
+        m_type = UpgradeType.valueOf(upgradeRequestMap.get(UPGRADE_TYPE).toString());
+      } catch (Exception e) {
+        throw new AmbariException(String.format("Property %s has an incorrect value of %s.",
+            UPGRADE_TYPE, upgradeTypeProperty));
+      }
+    } else {
+      // default type
+      m_type= UpgradeType.ROLLING;
+    }
+
+    m_isRevert = upgradeRequestMap.containsKey(UPGRADE_REVERT_UPGRADE_ID);
+
+    if (m_isRevert) {
+      Long revertUpgradeId = Long.valueOf(upgradeRequestMap.get(UPGRADE_REVERT_UPGRADE_ID).toString());
+      UpgradeEntity revertUpgrade = m_upgradeDAO.findUpgrade(revertUpgradeId);
+
+      if (null == revertUpgrade) {
+          throw new AmbariException(String.format("Could not find Upgrade with id %s to revert.", revertUpgradeId));
+      }      
+      
+      if (revertUpgrade.getOrchestration() != RepositoryType.PATCH) {
+        throw new AmbariException("Can only revert upgrades that have been done as a patch.");
+      }
+
+      if (revertUpgrade.getDirection() != Direction.UPGRADE) {
+        throw new AmbariException("Can only revert successful upgrades, not downgrades.");
+      }
+
+      Set<RepositoryVersionEntity> priors = new HashSet<>();
+      for (UpgradeHistoryEntity history : revertUpgrade.getHistory()) {
+        priors.add(history.getFromReposistoryVersion());
+
+        // !!! build all service-specific
+        m_services.add(history.getServiceName());
+        m_sourceRepositoryMap.put(history.getServiceName(), history.getTargetRepositoryVersion());
+        m_targetRepositoryMap.put(history.getServiceName(), history.getFromReposistoryVersion());
+      }
+
+      if (priors.size() != 1) {
+        String message = String.format("Upgrade from %s could not be reverted as there is no single "
+            + " repository across services.", revertUpgrade.getRepositoryVersion().getVersion());
+
+        throw new AmbariException(message);
+      }
+
+      m_repositoryVersion = priors.iterator().next();
+
+      // !!! the version is used later in validators
+      upgradeRequestMap.put(UPGRADE_REPO_VERSION_ID, m_repositoryVersion.getId().toString());
+      // !!! use the same upgrade pack that was used in the upgrade being reverted
+      upgradeRequestMap.put(UPGRADE_PACK, revertUpgrade.getUpgradePackage());
+
+      // !!! direction can ONLY be an downgrade on revert
+      m_direction = Direction.DOWNGRADE;
+      m_orchestration = RepositoryType.PATCH;
+    } else {
+
+      // determine direction
+      String directionProperty = (String) upgradeRequestMap.get(UPGRADE_DIRECTION);
+      if (StringUtils.isEmpty(directionProperty)) {
+        throw new AmbariException(String.format("%s is required", UPGRADE_DIRECTION));
+      }
+
+      m_direction = Direction.valueOf(directionProperty);
+
+      // depending on the direction, we must either have a target repository or an upgrade we are downgrading from
+      switch(m_direction){
+        case UPGRADE:{
+          String repositoryVersionId = (String) upgradeRequestMap.get(UPGRADE_REPO_VERSION_ID);
+          if (null == repositoryVersionId) {
+            throw new AmbariException(
+                String.format("The property %s is required when the upgrade direction is %s",
+                    UPGRADE_REPO_VERSION_ID, m_direction));
+          }
+
+          // depending on the repository, add services
+          m_repositoryVersion = m_repoVersionDAO.findByPK(Long.valueOf(repositoryVersionId));
+          m_orchestration = m_repositoryVersion.getType();
+
+          if (m_orchestration == RepositoryType.STANDARD) {
+            m_services.addAll(cluster.getServices().keySet());
+          } else {
+            try {
+              VersionDefinitionXml vdf = m_repositoryVersion.getRepositoryXml();
+              m_services.addAll(vdf.getAvailableServiceNames());
+
+              // if this is every true, then just stop the upgrade attempt and
+              // throw an exception
+              if (m_services.isEmpty()) {
+                String message = String.format(
+                    "When using a VDF of type %s, the available services must be defined in the VDF",
+                    m_repositoryVersion.getType());
+
+                throw new AmbariException(message);
+              }
+
+            } catch (Exception e) {
+              String msg = String.format(
+                  "Could not parse version definition for %s.  Upgrade will not proceed.",
+                  m_repositoryVersion.getVersion());
+
+              throw new AmbariException(msg);
+            }
+          }
+
+          Set<String> installedServices = new HashSet<>();
+          // populate the target repository map for all services in the upgrade
+          for (String serviceName : m_services) {
+            try {
+              Service service = cluster.getService(serviceName);
+              m_sourceRepositoryMap.put(serviceName, service.getDesiredRepositoryVersion());
+              m_targetRepositoryMap.put(serviceName, m_repositoryVersion);
+              installedServices.add(serviceName);
+            } catch (ServiceNotFoundException e) {
+              LOG.warn("Skipping orchestraction for service {}, as it was defined to upgrade, but is not installed in cluster {}",
+                  serviceName, cluster.getClusterName());
+            }
+          }
+
+          m_services = installedServices;
+
+          break;
+        }
+        case DOWNGRADE:{
+          UpgradeEntity upgrade = m_upgradeDAO.findLastUpgradeForCluster(
+              cluster.getClusterId(), Direction.UPGRADE);
+
+          m_repositoryVersion = upgrade.getRepositoryVersion();
+
+          // populate the repository maps for all services in the upgrade
+          for (UpgradeHistoryEntity history : upgrade.getHistory()) {
+            m_services.add(history.getServiceName());
+            m_sourceRepositoryMap.put(history.getServiceName(), m_repositoryVersion);
+            m_targetRepositoryMap.put(history.getServiceName(), history.getFromReposistoryVersion());
+          }
+
+          break;
+        }
+        default:
+          m_repositoryVersion = null;
+          break;
+      }
+    }
+
+
+    /**
+     * For the unit tests tests, there are multiple upgrade packs for the same
+     * type, so allow picking one of them. In prod, this is empty.
+     */
+    String preferredUpgradePackName = (String) upgradeRequestMap.get(UPGRADE_PACK);
+
+    @Experimental(feature = ExperimentalFeature.PATCH_UPGRADES, comment="This is wrong")
+    String upgradePackFromVersion = cluster.getService(
+        m_services.iterator().next()).getDesiredRepositoryVersion().getVersion();
+
+    m_upgradePack = m_upgradeHelper.suggestUpgradePack(m_cluster.getClusterName(),
+        upgradePackFromVersion, m_repositoryVersion.getVersion(), m_direction, m_type,
+        preferredUpgradePackName);
+
+    // the validator will throw an exception if the upgrade request is not valid
+    UpgradeRequestValidator upgradeRequestValidator = buildValidator(m_type);
+    upgradeRequestValidator.validate(cluster, m_direction, m_type, m_upgradePack,
+        upgradeRequestMap);
+
+    // optionally skip failures - this can be supplied on either the request or
+    // in the upgrade pack explicitely, however the request will always override
+    // the upgrade pack if explicitely specified
+    boolean skipComponentFailures = m_upgradePack.isComponentFailureAutoSkipped();
+    boolean skipServiceCheckFailures = m_upgradePack.isServiceCheckFailureAutoSkipped();
+
+    // only override the upgrade pack if set on the request
+    if (upgradeRequestMap.containsKey(UPGRADE_SKIP_FAILURES)) {
+      skipComponentFailures = Boolean.parseBoolean(
+          (String) upgradeRequestMap.get(UPGRADE_SKIP_FAILURES));
+    }
+
+    // only override the upgrade pack if set on the request
+    if (upgradeRequestMap.containsKey(UPGRADE_SKIP_SC_FAILURES)) {
+      skipServiceCheckFailures = Boolean.parseBoolean(
+          (String) upgradeRequestMap.get(UPGRADE_SKIP_SC_FAILURES));
+    }
+
+    boolean skipManualVerification = false;
+    if (upgradeRequestMap.containsKey(UPGRADE_SKIP_MANUAL_VERIFICATION)) {
+      skipManualVerification = Boolean.parseBoolean(
+          (String) upgradeRequestMap.get(UPGRADE_SKIP_MANUAL_VERIFICATION));
+    }
+
+    m_autoSkipComponentFailures = skipComponentFailures;
+    m_autoSkipServiceCheckFailures = skipServiceCheckFailures;
+    m_autoSkipManualVerification = skipManualVerification;
+
+    m_resolver = new MasterHostResolver(m_cluster, configHelper, this);
   }
 
   /**
@@ -199,42 +475,38 @@ public class UpgradeContext {
    */
   @AssistedInject
   public UpgradeContext(@Assisted Cluster cluster, @Assisted UpgradeEntity upgradeEntity,
-      AmbariMetaInfo ambariMetaInfo) {
-
+      AmbariMetaInfo ambariMetaInfo, ConfigHelper configHelper) {
     m_metaInfo = ambariMetaInfo;
+
     m_cluster = cluster;
     m_type = upgradeEntity.getUpgradeType();
     m_direction = upgradeEntity.getDirection();
-
-    m_fromRepositoryVersion = upgradeEntity.getFromRepositoryVersion();
-    m_toRepositoryVersion = upgradeEntity.getToRepositoryVersion();
-
-    String upgradePackage = upgradeEntity.getUpgradePackage();
-
-    StackId originalStackId = m_fromRepositoryVersion.getStackId();
-    if (m_direction == Direction.DOWNGRADE) {
-      originalStackId = m_toRepositoryVersion.getStackId();
+    m_repositoryVersion = upgradeEntity.getRepositoryVersion();
+
+    m_autoSkipComponentFailures = upgradeEntity.isComponentFailureAutoSkipped();
+    m_autoSkipServiceCheckFailures = upgradeEntity.isServiceCheckFailureAutoSkipped();
+
+    List<UpgradeHistoryEntity> allHistory = upgradeEntity.getHistory();
+    for (UpgradeHistoryEntity history : allHistory) {
+      String serviceName = history.getServiceName();
+      RepositoryVersionEntity sourceRepositoryVersion = history.getFromReposistoryVersion();
+      RepositoryVersionEntity targetRepositoryVersion = history.getTargetRepositoryVersion();
+      m_sourceRepositoryMap.put(serviceName, sourceRepositoryVersion);
+      m_targetRepositoryMap.put(serviceName, targetRepositoryVersion);
+      m_services.add(serviceName);
     }
 
-    Map<String, UpgradePack> packs = m_metaInfo.getUpgradePacks(originalStackId.getStackName(),
-        originalStackId.getStackVersion());
-
+    @Experimental(feature = ExperimentalFeature.PATCH_UPGRADES, comment = "This is wrong")
+    String upgradePackage = upgradeEntity.getUpgradePackage();
+    StackId stackId = m_repositoryVersion.getStackId();
+    Map<String, UpgradePack> packs = m_metaInfo.getUpgradePacks(stackId.getStackName(), stackId.getStackVersion());
     m_upgradePack = packs.get(upgradePackage);
 
-    // since this constructor is initialized from an entity, then this map is
-    // not present
-    m_upgradeRequestMap = Collections.emptyMap();
-  }
+    m_resolver = new MasterHostResolver(m_cluster, configHelper, this);
+    m_orchestration = upgradeEntity.getOrchestration();
 
-  /**
-   * Gets the original mapping of key/value pairs from the request which created
-   * the upgrade.
-   *
-   * @return the original mapping of key/value pairs from the request which
-   *         created the upgrade.
-   */
-  public Map<String, Object> getUpgradeRequest() {
-    return m_upgradeRequestMap;
+    m_isRevert = upgradeEntity.getOrchestration() == RepositoryType.PATCH &&
+        upgradeEntity.getDirection() == Direction.DOWNGRADE;
   }
 
   /**
@@ -266,104 +538,146 @@ public class UpgradeContext {
   }
 
   /**
-   * @return the target version for the upgrade
+   * Gets the version that components are being considered to be "coming from".
+   * <p/>
+   * With a {@link Direction#UPGRADE}, this value represent the services'
+   * desired repository. However, {@link Direction#DOWNGRADE} will use the same
+   * value for all services which is the version that the downgrade is coming
+   * from.
+   *
+   * @return the source version for the upgrade
    */
-  public RepositoryVersionEntity getTargetRepositoryVersion() {
-    return m_toRepositoryVersion;
+  public Map<String, RepositoryVersionEntity> getSourceVersions() {
+    return new HashMap<>(m_sourceRepositoryMap);
   }
 
   /**
-   * @return the source version for the upgrade
+   * Gets the version that service is being considered to be "coming from".
+   * <p/>
+   * With a {@link Direction#UPGRADE}, this value represent the services'
+   * desired repository. However, {@link Direction#DOWNGRADE} will use the same
+   * value for all services which is the version that the downgrade is coming
+   * from.
+   *
+   * @return the source repository for the upgrade
    */
-  public RepositoryVersionEntity getSourceRepositoryVersion() {
-    return m_fromRepositoryVersion;
+  public RepositoryVersionEntity getSourceRepositoryVersion(String serviceName) {
+    return m_sourceRepositoryMap.get(serviceName);
   }
 
   /**
-   * @return the direction of the upgrade
+   * Gets the version that service is being considered to be "coming from".
+   * <p/>
+   * With a {@link Direction#UPGRADE}, this value represent the services'
+   * desired repository. However, {@link Direction#DOWNGRADE} will use the same
+   * value for all services which is the version that the downgrade is coming
+   * from.
+   *
+   * @return the source repository for the upgrade
+   * @see #getSourceRepositoryVersion(String)
    */
-  public Direction getDirection() {
-    return m_direction;
+  public String getSourceVersion(String serviceName) {
+    RepositoryVersionEntity serviceSourceVersion = m_sourceRepositoryMap.get(serviceName);
+    return serviceSourceVersion.getVersion();
   }
 
   /**
-   * @return the type of upgrade.
+   * Gets the version being upgraded to or downgraded to for all services
+   * participating. This is the version that the service will be on if the
+   * upgrade or downgrade succeeds.
+   * <p/>
+   * With a {@link Direction#UPGRADE}, all services should be targetting the
+   * same repository version. However, {@link Direction#DOWNGRADE} will target
+   * the original repository that the service was on.
+   *
+   * @return the target version for the upgrade
    */
-  public UpgradeType getType() {
-    return m_type;
+  public Map<String, RepositoryVersionEntity> getTargetVersions() {
+    return new HashMap<>(m_targetRepositoryMap);
   }
 
   /**
-   * Sets the host resolver.
+   * Gets the repository being upgraded to or downgraded to for the given
+   * service. This is the version that the service will be on if the upgrade or
+   * downgrade succeeds.
+   * <p/>
+   * With a {@link Direction#UPGRADE}, all services should be targeting the
+   * same repository version. However, {@link Direction#DOWNGRADE} will target
+   * the original repository that the service was on.
    *
-   * @param resolver
-   *          the resolver that also references the required cluster
+   * @return the target repository for the upgrade
    */
-  public void setResolver(MasterHostResolver resolver) {
-    m_resolver = resolver;
+  public RepositoryVersionEntity getTargetRepositoryVersion(String serviceName) {
+    return m_targetRepositoryMap.get(serviceName);
   }
 
   /**
-   * @return the resolver
+   * Gets the version being upgraded to or downgraded to for the given service.
+   * This is the version that the service will be on if the upgrade or downgrade
+   * succeeds.
+   * <p/>
+   * With a {@link Direction#UPGRADE}, all services should be targetting the
+   * same repository version. However, {@link Direction#DOWNGRADE} will target
+   * the original repository that the service was on.
+   *
+   * @return the target version for the upgrade
+   * @see #getTargetRepositoryVersion(String)
    */
-  public MasterHostResolver getResolver() {
-    return m_resolver;
+  public String getTargetVersion(String serviceName) {
+    RepositoryVersionEntity serviceTargetVersion = m_targetRepositoryMap.get(serviceName);
+    return serviceTargetVersion.getVersion();
   }
 
   /**
-   * @return the metainfo for access to service definitions
+   * @return the direction of the upgrade
    */
-  public AmbariMetaInfo getAmbariMetaInfo() {
-    return m_metaInfo;
+  public Direction getDirection() {
+    return m_direction;
   }
 
   /**
-   * @param metaInfo the metainfo for access to service definitions
+   * @return the type of upgrade.
    */
-  public void setAmbariMetaInfo(AmbariMetaInfo metaInfo) {
-    m_metaInfo = metaInfo;
+  public UpgradeType getType() {
+    return m_type;
   }
 
   /**
-   * @param unhealthy a list of unhealthy host components
+   * @return the resolver
    */
-  public void addUnhealthy(List<ServiceComponentHost> unhealthy) {
-    m_unhealthy.addAll(unhealthy);
+  public MasterHostResolver getResolver() {
+    return m_resolver;
   }
 
   /**
-   * Gets the target stack of the upgrade based on the target repository which
-   * finalization will set.
-   *
-   * @return the target stack (never {@code null}).
+   * @return the metainfo for access to service definitions
    */
-  public StackId getTargetStackId() {
-    return m_toRepositoryVersion.getStackId();
+  public AmbariMetaInfo getAmbariMetaInfo() {
+    return m_metaInfo;
   }
 
   /**
-   * Gets the current stack of the components participating in the upgrade.
-   *
-   * @return the source stack (never {@code null}).
+   * @param unhealthy a list of unhealthy host components
    */
-  public StackId getSourceStackId() {
-    return m_fromRepositoryVersion.getStackId();
+  public void addUnhealthy(List<ServiceComponentHost> unhealthy) {
+    m_unhealthy.addAll(unhealthy);
   }
 
   /**
-   * Gets the original stack ID that the cluster was on. For an upgrade, this
-   * returns the source stack ID. For a downgrade, this will return the target
-   * stack ID.
+   * Gets the single repository version for the upgrade depending on the
+   * direction.
+   * <p/>
+   * If the direction is {@link Direction#UPGRADE} then this will return the
+   * target repository which every service will be on if the upgrade is
+   * finalized. <br/>
+   * If the direction is {@link Direction#DOWNGRADE} then this will return the
+   * repository from which the downgrade is coming from.
    *
-   * @return the original stack ID.
+   * @return the target repository version for this upgrade (never
+   *         {@code null}).
    */
-  public StackId getOriginalStackId() {
-    StackId originalStackId = getSourceStackId();
-    if (m_direction == Direction.DOWNGRADE) {
-      originalStackId = getTargetStackId();
-    }
-
-    return originalStackId;
+  public RepositoryVersionEntity getRepositoryVersion() {
+    return m_repositoryVersion;
   }
 
   /**
@@ -417,17 +731,6 @@ public class UpgradeContext {
   }
 
   /**
-   * Sets whether skippable components that failed are automatically skipped.
-   *
-   * @param autoSkipComponentFailures
-   *          {@code true} to automatically skip component failures which are
-   *          marked as skippable.
-   */
-  public void setAutoSkipComponentFailures(boolean autoSkipComponentFailures) {
-    m_autoSkipComponentFailures = autoSkipComponentFailures;
-  }
-
-  /**
    * Gets whether skippable service checks that failed are automatically
    * skipped.
    *
@@ -438,18 +741,6 @@ public class UpgradeContext {
   }
 
   /**
-   * Sets whether skippable service checks that failed are automatically
-   * skipped.
-   *
-   * @param autoSkipServiceCheckFailures
-   *          {@code true} to automatically skip service check failures which
-   *          are marked as being skippable.
-   */
-  public void setAutoSkipServiceCheckFailures(boolean autoSkipServiceCheckFailures) {
-    m_autoSkipServiceCheckFailures = autoSkipServiceCheckFailures;
-  }
-
-  /**
    * Gets whether manual verification tasks can be automatically skipped.
    *
    * @return the skipManualVerification
@@ -459,49 +750,43 @@ public class UpgradeContext {
   }
 
   /**
-   * Sets whether manual verification checks are automatically skipped.
+   * Gets the services participating in the upgrade.
    *
-   * @param autoSkipManualVerification
-   *          {@code true} to automatically skip manual verification tasks.
-   */
-  public void setAutoSkipManualVerification(boolean autoSkipManualVerification) {
-    m_autoSkipManualVerification = autoSkipManualVerification;
-  }
-
-  /**
-   * Sets the service names that are supported by an upgrade.  This is used for
-   * {@link RepositoryType#PATCH} and {@link RepositoryType#SERVICE}.
-   *
-   * @param services  the set of specific services
+   * @return the set of supported services. This collection should never be
+   *         empty.
    */
   @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES)
-  public void setSupportedServices(Set<String> services) {
-    m_supported = services;
+  public Set<String> getSupportedServices() {
+    return Collections.unmodifiableSet(m_services);
   }
 
   /**
-   * Gets if a service is supported.  If there are no services marked for the context,
-   * then ALL services are supported
-   * @param serviceName the service name to check.
+   * Gets if a service is supported.
+   *
+   * @param serviceName
+   *          the service name to check.
    * @return {@code true} when the service is supported
    */
   @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES)
   public boolean isServiceSupported(String serviceName) {
-    if (m_supported.isEmpty() || m_supported.contains(serviceName)) {
+    return m_services.contains(serviceName);
+  }
+
+  @Experimental(feature = ExperimentalFeature.PATCH_UPGRADES)
+  public boolean isScoped(UpgradeScope scope) {
+    if (scope == UpgradeScope.ANY) {
       return true;
     }
 
-    return false;
-  }
-
-  @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES)
-  public void setScope(UpgradeScope scope) {
-    m_scope = scope;
-  }
+    switch (m_orchestration) {
+      case PATCH:
+      case SERVICE:
+        return scope == UpgradeScope.PARTIAL;
+      case STANDARD:
+        return scope == UpgradeScope.COMPLETE;
+    }
 
-  @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES)
-  public boolean isScoped(UpgradeScope scope) {
-    return m_scope.isScoped(scope);
+    return false;
   }
 
   /**
@@ -523,6 +808,18 @@ public class UpgradeContext {
   }
 
   /**
+   * Gets the repository type to determine if this upgrade is a complete upgrade
+   * or a service/patch.  This value is not always the same as the repository version.  In
+   * the case of a revert of a patch, the target repository may be of type STANDARD, but orchestration
+   * must be "like a patch".
+   *
+   * @return the orchestration type.
+   */
+  public RepositoryType getOrchestrationType() {
+    return m_orchestration;
+  }
+
+  /**
    * Gets a map initialized with parameters required for upgrades to work. The
    * following properties are already set:
    * <ul>
@@ -557,4 +854,350 @@ public class UpgradeContext {
     parameters.put(KeyNames.REFRESH_CONFIG_TAGS_BEFORE_EXECUTION, "true");
     return parameters;
   }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String toString() {
+    return Objects.toStringHelper(this)
+        .add("direction", m_direction)
+        .add("type", m_type)
+        .add("target", m_repositoryVersion).toString();
+  }
+
+  /**
+   * Gets whether a downgrade is allowed for this upgrade. If the direction is
+   * {@link Direction#DOWNGRADE}, then this method always returns false.
+   * Otherwise it will consule {@link UpgradePack#isDowngradeAllowed()}.
+   *
+   * @return {@code true} of a downgrade is allowed for this upgrade,
+   *         {@code false} otherwise.
+   */
+  public boolean isDowngradeAllowed() {
+    if (m_direction == Direction.DOWNGRADE) {
+      return false;
+    }
+
+    return m_upgradePack.isDowngradeAllowed();
+  }
+
+  /**
+   * @return
+   */
+  public boolean isPatchRevert() {
+    return m_isRevert;
+  }
+
+  /**
+   * Builds a chain of {@link UpgradeRequestValidator}s to ensure that the
+   * incoming request to create a new upgrade is valid.
+   *
+   * @param upgradeType
+   *          the type of upgrade to build the validator for.
+   * @return the validator which can check to ensure that the properties are
+   *         valid.
+   */
+  private UpgradeRequestValidator buildValidator(UpgradeType upgradeType){
+    UpgradeRequestValidator validator = new BasicUpgradePropertiesValidator();
+    UpgradeRequestValidator preReqValidator = new PreReqCheckValidator();
+    validator.setNextValidator(preReqValidator);
+
+    final UpgradeRequestValidator upgradeTypeValidator;
+    switch (upgradeType) {
+      case HOST_ORDERED:
+        upgradeTypeValidator = new HostOrderedUpgradeValidator();
+        break;
+      case NON_ROLLING:
+      case ROLLING:
+      default:
+        upgradeTypeValidator = null;
+        break;
+    }
+
+    preReqValidator.setNextValidator(upgradeTypeValidator);
+    return validator;
+  }
+
+  /**
+   * The {@link UpgradeRequestValidator} contains the logic to check for correct
+   * upgrade request properties and then pass the responsibility onto the next
+   * validator in the chain.
+   */
+  private abstract class UpgradeRequestValidator {
+    /**
+     * The next validator.
+     */
+    UpgradeRequestValidator m_nextValidator;
+
+    /**
+     * Sets the next validator in the chain.
+     *
+     * @param nextValidator
+     *          the next validator to run, or {@code null} for none.
+     */
+    void setNextValidator(UpgradeRequestValidator nextValidator) {
+      m_nextValidator = nextValidator;
+    }
+
+    /**
+     * Validates the upgrade request from this point in the chain.
+     *
+     * @param cluster
+     * @param direction
+     * @param type
+     * @param upgradePack
+     * @param requestMap
+     * @throws AmbariException
+     */
+    final void validate(Cluster cluster, Direction direction, UpgradeType type,
+        UpgradePack upgradePack, Map<String, Object> requestMap) throws AmbariException {
+
+      // run this instance's check
+      check(cluster, direction, type, upgradePack, requestMap);
+
+      // pass along to the next
+      if (null != m_nextValidator) {
+        m_nextValidator.validate(cluster, direction, type, upgradePack, requestMap);
+      }
+    }
+
+    /**
+     * Checks to ensure that upgrade request is valid given the specific
+     * arguments.
+     *
+     * @param cluster
+     * @param direction
+     * @param type
+     * @param upgradePack
+     * @param requestMap
+     * @throws AmbariException
+     */
+    abstract void check(Cluster cluster, Direction direction, UpgradeType type,
+        UpgradePack upgradePack, Map<String, Object> requestMap) throws AmbariException;
+  }
+
+  /**
+   * The {@link BasicUpgradePropertiesValidator} ensures that the basic required
+   * properties are present on the upgrade request.
+   */
+  private final class BasicUpgradePropertiesValidator extends UpgradeRequestValidator {
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void check(Cluster cluster, Direction direction, UpgradeType type,
+        UpgradePack upgradePack, Map<String, Object> requestMap) throws AmbariException {
+
+      if (direction == Direction.UPGRADE) {
+        String repositoryVersionId = (String) requestMap.get(UPGRADE_REPO_VERSION_ID);
+        if (StringUtils.isBlank(repositoryVersionId)) {
+          throw new AmbariException(
+              String.format("%s is required for upgrades", UPGRADE_REPO_VERSION_ID));
+        }
+      }
+    }
+  }
+
+  /**
+   * The {@link PreReqCheckValidator} ensures that the upgrade pre-requisite
+   * checks have passed.
+   */
+  private final class PreReqCheckValidator extends UpgradeRequestValidator {
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    void check(Cluster cluster, Direction direction, UpgradeType type, UpgradePack upgradePack,
+        Map<String, Object> requestMap) throws AmbariException {
+
+      String repositoryVersionId = (String) requestMap.get(UPGRADE_REPO_VERSION_ID);
+      boolean skipPrereqChecks = Boolean.parseBoolean((String) requestMap.get(UPGRADE_SKIP_PREREQUISITE_CHECKS));
+      boolean failOnCheckWarnings = Boolean.parseBoolean((String) requestMap.get(UPGRADE_FAIL_ON_CHECK_WARNINGS));
+      String preferredUpgradePack = requestMap.containsKey(UPGRADE_PACK) ? (String) requestMap.get(UPGRADE_PACK) : null;
+
+      // verify that there is not an upgrade or downgrade that is in progress or suspended
+      UpgradeEntity existingUpgrade = cluster.getUpgradeInProgress();
+      if (null != existingUpgrade) {
+        throw new AmbariException(
+            String.format("Unable to perform %s as another %s (request ID %s) is in progress.",
+                direction.getText(false), existingUpgrade.getDirection().getText(false),
+                existingUpgrade.getRequestId()));
+      }
+
+      // skip this check if it's a downgrade or we are instructed to skip it
+      if (direction.isDowngrade() || skipPrereqChecks) {
+        return;
+      }
+
+      RepositoryVersionEntity repositoryVersion = m_repoVersionDAO.findByPK(
+          Long.valueOf(repositoryVersionId));
+
+      // Validate pre-req checks pass
+      PreUpgradeCheckResourceProvider provider = (PreUpgradeCheckResourceProvider) AbstractControllerResourceProvider.getResourceProvider(
+          Resource.Type.PreUpgradeCheck);
+
+      Predicate preUpgradeCheckPredicate = new PredicateBuilder().property(
+          PreUpgradeCheckResourceProvider.UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID).equals(cluster.getClusterName()).and().property(
+          PreUpgradeCheckResourceProvider.UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID).equals(repositoryVersion.getVersion()).and().property(
+          PreUpgradeCheckResourceProvider.UPGRADE_CHECK_FOR_REVERT_PROPERTY_ID).equals(m_isRevert).and().property(
+          PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID).equals(type).and().property(
+          PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_PACK_PROPERTY_ID).equals(preferredUpgradePack).toPredicate();
+
+      Request preUpgradeCheckRequest = PropertyHelper.getReadRequest();
+
+      Set<Resource> preUpgradeCheckResources;
+      try {
+        preUpgradeCheckResources = provider.getResources(
+            preUpgradeCheckRequest, preUpgradeCheckPredicate);
+      } catch (NoSuchResourceException|SystemException|UnsupportedPropertyException|NoSuchParentResourceException e) {
+        throw new AmbariException(
+            String.format("Unable to perform %s. Prerequisite checks could not be run",
+                direction.getText(false), e));
+      }
+
+      List<Resource> failedResources = new LinkedList<>();
+      if (preUpgradeCheckResources != null) {
+        for (Resource res : preUpgradeCheckResources) {
+          PrereqCheckStatus prereqCheckStatus = (PrereqCheckStatus) res.getPropertyValue(
+              PreUpgradeCheckResourceProvider.UPGRADE_CHECK_STATUS_PROPERTY_ID);
+
+          if (prereqCheckStatus == PrereqCheckStatus.FAIL
+              || (failOnCheckWarnings && prereqCheckStatus == PrereqCheckStatus.WARNING)) {
+            failedResources.add(res);
+          }
+        }
+      }
+
+      if (!failedResources.isEmpty()) {
+        throw new AmbariException(
+            String.format("Unable to perform %s. Prerequisite checks failed %s",
+                direction.getText(false), m_gson.toJson(failedResources)));
+      }
+    }
+  }
+
+  /**
+   * Ensures that for {@link UpgradeType#HOST_ORDERED}, the properties supplied
+   * are valid.
+   */
+  @SuppressWarnings("unchecked")
+  private final class HostOrderedUpgradeValidator extends UpgradeRequestValidator {
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    void check(Cluster cluster, Direction direction, UpgradeType type, UpgradePack upgradePack,
+        Map<String, Object> requestMap) throws AmbariException {
+
+      String skipFailuresRequestProperty = (String) requestMap.get(UPGRADE_SKIP_FAILURES);
+      if (Boolean.parseBoolean(skipFailuresRequestProperty)) {
+        throw new AmbariException(
+            String.format("The %s property is not valid when creating a %s upgrade.",
+                UPGRADE_SKIP_FAILURES, UpgradeType.HOST_ORDERED));
+      }
+
+      String skipManualVerification = (String) requestMap.get(UPGRADE_SKIP_MANUAL_VERIFICATION);
+      if (Boolean.parseBoolean(skipManualVerification)) {
+        throw new AmbariException(
+            String.format("The %s property is not valid when creating a %s upgrade.",
+                UPGRADE_SKIP_MANUAL_VERIFICATION, UpgradeType.HOST_ORDERED));
+      }
+
+      if (!requestMap.containsKey(UPGRADE_HOST_ORDERED_HOSTS)) {
+        throw new AmbariException(
+            String.format("The %s property is required when creating a %s upgrade.",
+                UPGRADE_HOST_ORDERED_HOSTS, UpgradeType.HOST_ORDERED));
+      }
+
+      List<HostOrderItem> hostOrderItems = extractHostOrderItemsFromRequest(requestMap);
+      List<String> hostsFromRequest = new ArrayList<>(hostOrderItems.size());
+      for (HostOrderItem hostOrderItem : hostOrderItems) {
+        if (hostOrderItem.getType() == HostOrderActionType.HOST_UPGRADE) {
+          hostsFromRequest.addAll(hostOrderItem.getActionItems());
+        }
+      }
+
+      // ensure that all hosts for this cluster are accounted for
+      Collection<Host> hosts = cluster.getHosts();
+      Set<String> clusterHostNames = new HashSet<>(hosts.size());
+      for (Host host : hosts) {
+        clusterHostNames.add(host.getHostName());
+      }
+
+      Collection<String> disjunction = CollectionUtils.disjunction(hostsFromRequest,
+          clusterHostNames);
+
+      if (CollectionUtils.isNotEmpty(disjunction)) {
+        throw new AmbariException(String.format(
+            "The supplied list of hosts must match the cluster hosts in an upgrade of type %s. The following hosts are either missing or invalid: %s",
+            UpgradeType.HOST_ORDERED, StringUtils.join(disjunction, ", ")));
+      }
+
+      // verify that the upgradepack has the required grouping and set the
+      // action items on it
+      HostOrderGrouping hostOrderGrouping = null;
+      List<Grouping> groupings = upgradePack.getGroups(direction);
+      for (Grouping grouping : groupings) {
+        if (grouping instanceof HostOrderGrouping) {
+          hostOrderGrouping = (HostOrderGrouping) grouping;
+          hostOrderGrouping.setHostOrderItems(hostOrderItems);
+        }
+      }
+    }
+
+    /**
+     * Builds the list of {@link HostOrderItem}s from the upgrade request. If
+     * the upgrade request does not contain the hosts
+     *
+     * @param requestMap
+     *          the map of properties from the request (not {@code null}).
+     * @return the ordered list of actions to orchestrate for the
+     *         {@link UpgradeType#HOST_ORDERED} upgrade.
+     * @throws AmbariException
+     *           if the request properties are not valid.
+     */
+    private List<HostOrderItem> extractHostOrderItemsFromRequest(Map<String, Object> requestMap)
+        throws AmbariException {
+      // ewwww
+      Set<Map<String, List<String>>> hostsOrder = (Set<Map<String, List<String>>>) requestMap.get(
+          UPGRADE_HOST_ORDERED_HOSTS);
+
+      if (CollectionUtils.isEmpty(hostsOrder)) {
+        throw new AmbariException(
+            String.format("The %s property must be specified when using a %s upgrade type.",
+                UPGRADE_HOST_ORDERED_HOSTS, UpgradeType.HOST_ORDERED));
+      }
+
+      List<HostOrderItem> hostOrderItems = new ArrayList<>();
+
+      // extract all of the hosts so that we can ensure they are all accounted
+      // for
+      Iterator<Map<String, List<String>>> iterator = hostsOrder.iterator();
+      while (iterator.hasNext()) {
+        Map<String, List<String>> grouping = iterator.next();
+        List<String> hosts = grouping.get("hosts");
+        List<String> serviceChecks = grouping.get("service_checks");
+
+        if (CollectionUtils.isEmpty(hosts) && CollectionUtils.isEmpty(serviceChecks)) {
+          throw new AmbariException(String.format(
+              "The %s property must contain at least one object with either a %s or %s key",
+              UPGRADE_HOST_ORDERED_HOSTS, "hosts", "service_checks"));
+        }
+
+        if (CollectionUtils.isNotEmpty(hosts)) {
+          hostOrderItems.add(new HostOrderItem(HostOrderActionType.HOST_UPGRADE, hosts));
+        }
+
+        if (CollectionUtils.isNotEmpty(serviceChecks)) {
+          hostOrderItems.add(new HostOrderItem(HostOrderActionType.SERVICE_CHECK, serviceChecks));
+        }
+      }
+
+      return hostOrderItems;
+    }
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeContextFactory.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeContextFactory.java b/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeContextFactory.java
index d482b8a..833c0b5 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeContextFactory.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeContextFactory.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,12 +19,8 @@ package org.apache.ambari.server.state;
 
 import java.util.Map;
 
-import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
+import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
-import org.apache.ambari.server.state.stack.upgrade.Direction;
-import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
-
-import com.google.inject.assistedinject.Assisted;
 
 /**
  * The {@link UpgradeContextFactory} is used to create dependency-injected
@@ -37,27 +33,14 @@ public interface UpgradeContextFactory {
    *
    * @param cluster
    *          the cluster that the upgrade is for (not {@code null}).
-   * @param type
-   *          the type of upgrade, either rolling or non_rolling (not
-   *          {@code null}).
-   * @param direction
-   *          the direction for the upgrade
-   * @param fromRepositoryVersion
-   *          the repository where any existing services are coming from
-   *          {@code null}).
-   * @param toRepositoryVersion
-   *          the repository which is the target of the finalized
-   *          upgrade/downgrade {@code null}).
    * @param upgradeRequestMap
    *          the original map of parameters used to create the upgrade (not
    *          {@code null}).
    *
    * @return an initialized {@link UpgradeContext}.
    */
-  UpgradeContext create(Cluster cluster, UpgradeType type, Direction direction,
-      @Assisted("fromRepositoryVersion") RepositoryVersionEntity fromRepositoryVersion,
-      @Assisted("toRepositoryVersion") RepositoryVersionEntity toRepositoryVersion,
-      Map<String, Object> upgradeRequestMap);
+  UpgradeContext create(Cluster cluster, Map<String, Object> upgradeRequestMap)
+      throws AmbariException;
 
   /**
    * Creates an {@link UpgradeContext} which is injected with dependencies.

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeHelper.java
index fe6532d..5fdcd66 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeHelper.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,8 +19,10 @@ package org.apache.ambari.server.state;
 
 import java.text.MessageFormat;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
@@ -28,8 +30,11 @@ import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.ambari.annotations.Experimental;
+import org.apache.ambari.annotations.ExperimentalFeature;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.internal.TaskResourceProvider;
 import org.apache.ambari.server.controller.predicate.AndPredicate;
 import org.apache.ambari.server.controller.spi.ClusterController;
@@ -46,7 +51,10 @@ import org.apache.ambari.server.controller.utilities.PredicateBuilder;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.events.listeners.upgrade.StackVersionListener;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
+import org.apache.ambari.server.orm.dao.ServiceConfigDAO;
+import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
+import org.apache.ambari.server.orm.entities.ServiceConfigEntity;
 import org.apache.ambari.server.stack.HostsType;
 import org.apache.ambari.server.stack.MasterHostResolver;
 import org.apache.ambari.server.state.stack.UpgradePack;
@@ -98,7 +106,7 @@ public class UpgradeHelper {
   /**
    * Enum used to define placeholder text for replacement
    */
-  private static enum Placeholder {
+  private enum Placeholder {
     /**
      * No placeholder defined
      */
@@ -154,7 +162,7 @@ public class UpgradeHelper {
     DIRECTION_VERB_PROPER("direction.verb.proper");
 
     private String pattern;
-    private Placeholder(String key) {
+    Placeholder(String key) {
       pattern = "{{" + key + "}}";
     }
 
@@ -174,35 +182,55 @@ public class UpgradeHelper {
    * {@link StageWrapperBuilder} has finished building out all of the stages.
    */
   @Inject
-  private Provider<ConfigHelper> m_configHelper;
+  Provider<ConfigHelper> m_configHelperProvider;
+
+  @Inject
+  private Provider<AmbariMetaInfo> m_ambariMetaInfoProvider;
+
+  @Inject
+  private Provider<Clusters> m_clusters;
 
   @Inject
-  private Provider<AmbariMetaInfo> m_ambariMetaInfo;
+  private Provider<RepositoryVersionDAO> m_repoVersionProvider;
 
+  /**
+   * Used to update the configuration properties.
+   */
   @Inject
-  private Provider<Clusters> clusters;
+  private Provider<AmbariManagementController> m_controllerProvider;
 
+  /**
+   * Used to get configurations by service name.
+   */
   @Inject
-  private Provider<RepositoryVersionDAO> s_repoVersionDAO;
+  ServiceConfigDAO m_serviceConfigDAO;
 
   /**
-   * Get right Upgrade Pack, depends on stack, direction and upgrade type information
-   * @param clusterName The name of the cluster
-   * @param upgradeFromVersion Current stack version
-   * @param upgradeToVersion Target stack version
-   * @param direction {@code Direction} of the upgrade
-   * @param upgradeType The {@code UpgradeType}
-   * @param targetStackName The destination target stack name.
-   * @param preferredUpgradePackName For unit test, need to prefer an upgrade pack since multiple matches can be found.
+   * Get right Upgrade Pack, depends on stack, direction and upgrade type
+   * information
+   *
+   * @param clusterName
+   *          The name of the cluster
+   * @param upgradeFromVersion
+   *          Current stack version
+   * @param upgradeToVersion
+   *          Target stack version
+   * @param direction
+   *          {@code Direction} of the upgrade
+   * @param upgradeType
+   *          The {@code UpgradeType}
+   * @param preferredUpgradePackName
+   *          For unit test, need to prefer an upgrade pack since multiple
+   *          matches can be found.
    * @return {@code UpgradeType} object
    * @throws AmbariException
    */
   public UpgradePack suggestUpgradePack(String clusterName, String upgradeFromVersion, String upgradeToVersion,
-    Direction direction, UpgradeType upgradeType, String targetStackName, String preferredUpgradePackName) throws AmbariException {
+    Direction direction, UpgradeType upgradeType, String preferredUpgradePackName) throws AmbariException {
 
     // Find upgrade packs based on current stack. This is where to upgrade from
-    Cluster cluster = clusters.get().getCluster(clusterName);
-    StackId currentStack =  cluster.getCurrentStackVersion();
+    Cluster cluster = m_clusters.get().getCluster(clusterName);
+    StackId stack =  cluster.getCurrentStackVersion();
 
     String repoVersion = upgradeToVersion;
 
@@ -211,25 +239,19 @@ public class UpgradeHelper {
       repoVersion = upgradeFromVersion;
     }
 
-    Map<String, UpgradePack> packs = m_ambariMetaInfo.get().getUpgradePacks(currentStack.getStackName(), currentStack.getStackVersion());
-    UpgradePack pack = null;
-
-    if (StringUtils.isNotEmpty(preferredUpgradePackName) && packs.containsKey(preferredUpgradePackName)) {
-      pack = packs.get(preferredUpgradePackName);
-    }
-
-    RepositoryVersionEntity versionEntity = s_repoVersionDAO.get().findByStackNameAndVersion(targetStackName, repoVersion);
-    if (null == versionEntity) {
-      versionEntity = s_repoVersionDAO.get().findByVersion(repoVersion);
-    }
+    RepositoryVersionEntity versionEntity = m_repoVersionProvider.get().findByStackNameAndVersion(
+        stack.getStackName(), repoVersion);
 
     if (versionEntity == null) {
-      throw new AmbariException(String.format("Repository version %s was not found for target stack %s", repoVersion, targetStackName));
+      throw new AmbariException(String.format("Repository version %s was not found", repoVersion));
     }
 
-    // Best-attempt at picking an upgrade pack assuming within the same stack whose target stack version matches.
-    // If multiple candidates are found, raise an exception.
-    if (null == pack) {
+    Map<String, UpgradePack> packs = m_ambariMetaInfoProvider.get().getUpgradePacks(stack.getStackName(), stack.getStackVersion());
+    UpgradePack pack = null;
+
+    if (StringUtils.isNotEmpty(preferredUpgradePackName) && packs.containsKey(preferredUpgradePackName)) {
+      pack = packs.get(preferredUpgradePackName);
+    } else {
       String repoStackId = versionEntity.getStackId().getStackId();
       for (UpgradePack upgradePack : packs.values()) {
         if (null != upgradePack.getTargetStack() && upgradePack.getTargetStack().equals(repoStackId) &&
@@ -247,8 +269,8 @@ public class UpgradeHelper {
     }
 
     if (null == pack) {
-      throw new AmbariException(String.format("Unable to perform %s. Could not locate %s upgrade pack for stack %s and version %s",
-          direction.getText(false), upgradeType.toString(), targetStackName, repoVersion));
+      throw new AmbariException(String.format("Unable to perform %s. Could not locate %s upgrade pack for version %s",
+          direction.getText(false), upgradeType.toString(), repoVersion));
     }
 
    return pack;
@@ -268,7 +290,6 @@ public class UpgradeHelper {
   public List<UpgradeGroupHolder> createSequence(UpgradePack upgradePack,
       UpgradeContext context) throws AmbariException {
 
-    context.setAmbariMetaInfo(m_ambariMetaInfo.get());
     Cluster cluster = context.getCluster();
     MasterHostResolver mhr = context.getResolver();
 
@@ -546,7 +567,6 @@ public class UpgradeHelper {
   private String tokenReplace(UpgradeContext ctx, String source, String service, String component) {
     Cluster cluster = ctx.getCluster();
     MasterHostResolver mhr = ctx.getResolver();
-    String version = ctx.getTargetRepositoryVersion().getVersion();
 
     String result = source;
 
@@ -583,7 +603,7 @@ public class UpgradeHelper {
           break;
         }
         case VERSION:
-          value = version;
+          value = ctx.getRepositoryVersion().getVersion();
           break;
         case DIRECTION_VERB:
         case DIRECTION_VERB_PROPER:
@@ -602,7 +622,7 @@ public class UpgradeHelper {
           value = ctx.getDirection().getText(p == Placeholder.DIRECTION_TEXT_PROPER);
           break;
         default:
-          value = m_configHelper.get().getPlaceholderValueFromDesiredConfigurations(
+          value = m_configHelperProvider.get().getPlaceholderValueFromDesiredConfigurations(
               cluster, token);
           break;
       }
@@ -708,7 +728,7 @@ public class UpgradeHelper {
   private void setDisplayNames(UpgradeContext context, String service, String component) {
     StackId stackId = context.getCluster().getDesiredStackVersion();
     try {
-      ServiceInfo serviceInfo = m_ambariMetaInfo.get().getService(stackId.getStackName(),
+      ServiceInfo serviceInfo = m_ambariMetaInfoProvider.get().getService(stackId.getStackName(),
           stackId.getStackVersion(), service);
       context.setServiceDisplay(service, serviceInfo.getDisplayName());
 
@@ -721,6 +741,32 @@ public class UpgradeHelper {
   }
 
   /**
+   * Updates the various repositories and configurations for services
+   * participating in the upgrade or downgrade. The following actions are
+   * performed in order:
+   * <ul>
+   * <li>The desired repository for every service and component is changed<
+   * <li>The {@link UpgradeState} of every component host is moved to either
+   * {@link UpgradeState#IN_PROGRESS} or {@link UpgradeState#NONE}.
+   * <li>In the case of an upgrade, new configurations and service
+   * configurations are created if necessary. In the case of a downgrade, any
+   * configurations created by the upgrade are reverted.
+   * </ul>
+   *
+   * @param upgradeContext
+   *          the upgrade context holding all relevent upgrade information (not
+   *          {@code null}).
+   * @throws AmbariException
+   */
+  @Transactional
+  @Experimental(feature = ExperimentalFeature.PATCH_UPGRADES)
+  public void updateDesiredRepositoriesAndConfigs(UpgradeContext upgradeContext)
+      throws AmbariException {
+    setDesiredRepositories(upgradeContext);
+    processConfigurationsIfRequired(upgradeContext);
+  }
+
+  /**
    * Transitions all affected components to {@link UpgradeState#IN_PROGRESS}.
    * Transition is performed only for components that advertise their version.
    * Additionally sets the service component desired version to the specified
@@ -730,27 +776,25 @@ public class UpgradeHelper {
    * the upgrade state individually, we wrap this method inside of a transaction
    * to prevent 1000's of transactions from being opened and committed.
    *
-   * @param stackId
-   *          the desired stack ID for the upgrade
-   * @param version
-   *          desired version (like 2.2.1.0-1234) for upgrade
-   * @param targetServices
-   *          targets for upgrade
-   * @param targetStack
-   *          the target stack for the components. Express and Rolling upgrades
-   *          determine the "correct" stack differently, so the component's
-   *          desired stack id is not a reliable indicator.
+   * @param upgradeContext
+   *          the upgrade context (not {@code null}).
    */
-  @Transactional
-  public void putComponentsToUpgradingState(StackId stackId, String version,
-      Map<Service, Set<ServiceComponent>> targetServices, StackId targetStack) throws AmbariException {
-
-    for (Map.Entry<Service, Set<ServiceComponent>> entry: targetServices.entrySet()) {
-      for (ServiceComponent serviceComponent: entry.getValue()) {
-
+  @Experimental(feature = ExperimentalFeature.PATCH_UPGRADES)
+  private void setDesiredRepositories(UpgradeContext upgradeContext) throws AmbariException {
+    Cluster cluster = upgradeContext.getCluster();
+    Set<String> services = upgradeContext.getSupportedServices();
+
+    for (String serviceName : services) {
+      Service service = cluster.getService(serviceName);
+      RepositoryVersionEntity targetRepositoryVersion = upgradeContext.getTargetRepositoryVersion(serviceName);
+      StackId targetStack = targetRepositoryVersion.getStackId();
+      service.setDesiredRepositoryVersion(targetRepositoryVersion);
+
+      Collection<ServiceComponent> components = service.getServiceComponents().values();
+      for (ServiceComponent serviceComponent : components) {
         boolean versionAdvertised = false;
         try {
-          ComponentInfo ci = m_ambariMetaInfo.get().getComponent(targetStack.getStackName(),
+          ComponentInfo ci = m_ambariMetaInfoProvider.get().getComponent(targetStack.getStackName(),
               targetStack.getStackVersion(), serviceComponent.getServiceName(),
               serviceComponent.getName());
 
@@ -761,27 +805,224 @@ public class UpgradeHelper {
               StackVersionListener.UNKNOWN_VERSION);
         }
 
-        UpgradeState upgradeState = UpgradeState.IN_PROGRESS;
-        String desiredVersion = version;
-
+        UpgradeState upgradeStateToSet = UpgradeState.IN_PROGRESS;
         if (!versionAdvertised) {
-          upgradeState = UpgradeState.NONE;
-          desiredVersion = StackVersionListener.UNKNOWN_VERSION;
+          upgradeStateToSet = UpgradeState.NONE;
         }
 
-        for (ServiceComponentHost serviceComponentHost: serviceComponent.getServiceComponentHosts().values()) {
-          serviceComponentHost.setUpgradeState(upgradeState);
+        for (ServiceComponentHost serviceComponentHost : serviceComponent.getServiceComponentHosts().values()) {
+          if (serviceComponentHost.getUpgradeState() != upgradeStateToSet) {
+            serviceComponentHost.setUpgradeState(upgradeStateToSet);
+          }
 
-          // !!! if we aren't version advertised, but there IS a version, set it.
-          if (!versionAdvertised &&
-              !serviceComponentHost.getVersion().equals(StackVersionListener.UNKNOWN_VERSION)) {
+          // !!! if we aren't version advertised, but there IS a version, set
+          // it.
+          if (!versionAdvertised && !StringUtils.equals(StackVersionListener.UNKNOWN_VERSION,
+              serviceComponentHost.getVersion())) {
             serviceComponentHost.setVersion(StackVersionListener.UNKNOWN_VERSION);
           }
         }
 
-        serviceComponent.setDesiredStackVersion(stackId);
-        serviceComponent.setDesiredVersion(desiredVersion);
+        // set component desired repo
+        serviceComponent.setDesiredRepositoryVersion(targetRepositoryVersion);
+      }
+    }
+  }
+
+  /**
+   * Handles the creation or resetting of configurations based on whether an
+   * upgrade or downgrade is occurring. This method will not do anything when
+   * the service is not crossing major stack versions, since, by definition, no
+   * new configurations are automatically created when upgrading with the same
+   * stack (ie HDP 2.2.0.0 -> HDP 2.2.1.0).
+   * <p/>
+   * When upgrading or downgrade between stacks (HDP 2.2.0.0 -> HDP 2.3.0.0)
+   * then this will perform the following:
+   * <ul>
+   * <li>Upgrade: Create new configurations that are a merge between the source
+   * stack and the target stack. If a value has changed between stacks, then the
+   * target stack value should be taken unless the cluster's value differs from
+   * the old stack. This can occur if a property has been customized after
+   * installation.</li>
+   * <li>Downgrade: Reset the latest configurations from the service's original
+   * stack. The new configurations that were created on upgrade must be left
+   * intact until all components have been reverted, otherwise heartbeats will
+   * fail due to missing configurations.</li>
+   * </ul>
+   *
+   * @param upgradeContext
+   *          the upgrade context (not {@code null}).
+   * @throws AmbariException
+   */
+  private void processConfigurationsIfRequired(UpgradeContext upgradeContext)
+      throws AmbariException {
+
+    AmbariManagementController controller = m_controllerProvider.get();
+
+    Cluster cluster = upgradeContext.getCluster();
+    Direction direction = upgradeContext.getDirection();
+    String userName = controller.getAuthName();
+    Set<String> servicesInUpgrade = upgradeContext.getSupportedServices();
+
+    // merge or revert configurations for any service that needs it
+    for( String serviceName : servicesInUpgrade ){
+      RepositoryVersionEntity sourceRepositoryVersion = upgradeContext.getSourceRepositoryVersion(serviceName);
+      RepositoryVersionEntity targetRepositoryVersion = upgradeContext.getTargetRepositoryVersion(serviceName);
+      StackId sourceStackId = sourceRepositoryVersion.getStackId();
+      StackId targetStackId = targetRepositoryVersion.getStackId();
+
+      // only work with configurations when crossing stacks
+      if (sourceStackId.equals(targetStackId)) {
+        RepositoryVersionEntity associatedRepositoryVersion = upgradeContext.getRepositoryVersion();
+        LOG.info(
+            "The {} {} {} will not change stack configurations for {} since the source and target are both {}",
+            direction.getText(false), direction.getPreposition(),
+            associatedRepositoryVersion.getVersion(), serviceName, targetStackId);
+
+        continue;
+      }
+
+      ConfigHelper configHelper = m_configHelperProvider.get();
+
+      // downgrade is easy - just remove the new and make the old current
+      if (direction == Direction.DOWNGRADE) {
+        cluster.applyLatestConfigurations(targetStackId, serviceName);
+        return;
+      }
+
+      // upgrade is a bit harder - we have to merge new stack configurations in
+
+      // populate a map of default configurations for the service on the old
+      // stack (this is used when determining if a property has been
+      // customized and should be overriden with the new stack value)
+      Map<String, Map<String, String>> oldServiceDefaultConfigsByType = configHelper.getDefaultProperties(
+          sourceStackId, serviceName);
+
+      // populate a map with default configurations from the new stack
+      Map<String, Map<String, String>> newServiceDefaultConfigsByType = configHelper.getDefaultProperties(
+          targetStackId, serviceName);
+
+      // find the current, existing configurations for the service
+      List<Config> existingServiceConfigs = new ArrayList<>();
+      List<ServiceConfigEntity> latestServiceConfigs = m_serviceConfigDAO.getLastServiceConfigsForService(
+          cluster.getClusterId(), serviceName);
+
+      for (ServiceConfigEntity serviceConfig : latestServiceConfigs) {
+        List<ClusterConfigEntity> existingConfigurations = serviceConfig.getClusterConfigEntities();
+        for (ClusterConfigEntity currentServiceConfig : existingConfigurations) {
+          String configurationType = currentServiceConfig.getType();
+          Config currentClusterConfigForService = cluster.getDesiredConfigByType(configurationType);
+          existingServiceConfigs.add(currentClusterConfigForService);
+        }
+      }
+
+      // now that we have found, old, new, and existing confgs, overlay the
+      // existing on top of the new
+      for (Config existingServiceConfig : existingServiceConfigs) {
+        String configurationType = existingServiceConfig.getType();
+
+        // get current stack default configurations on install
+        Map<String, String> oldServiceDefaultConfigs = oldServiceDefaultConfigsByType.get(
+            configurationType);
+
+        // NPE sanity for current stack defaults
+        if (null == oldServiceDefaultConfigs) {
+          oldServiceDefaultConfigs = Collections.emptyMap();
+        }
+
+        // get the existing configurations
+        Map<String, String> existingConfigurations = existingServiceConfig.getProperties();
+
+        // get the new configurations
+        Map<String, String> newDefaultConfigurations = newServiceDefaultConfigsByType.get(
+            configurationType);
+
+        // if the new stack configurations don't have the type, then simply add
+        // all of the existing in
+        if (null == newDefaultConfigurations) {
+          newServiceDefaultConfigsByType.put(configurationType, existingConfigurations);
+          continue;
+        } else {
+          // Remove any configs in the new stack whose value is NULL, unless
+          // they currently exist and the value is not NULL.
+          Iterator<Map.Entry<String, String>> iter = newDefaultConfigurations.entrySet().iterator();
+          while (iter.hasNext()) {
+            Map.Entry<String, String> entry = iter.next();
+            if (entry.getValue() == null) {
+              iter.remove();
+            }
+          }
+        }
+
+        // process every existing configuration property for this configuration
+        // type
+        for (Map.Entry<String, String> existingConfigurationEntry : existingConfigurations.entrySet()) {
+          String existingConfigurationKey = existingConfigurationEntry.getKey();
+          String existingConfigurationValue = existingConfigurationEntry.getValue();
+
+          // if there is already an entry, we now have to try to determine if
+          // the value was customized after stack installation
+          if (newDefaultConfigurations.containsKey(existingConfigurationKey)) {
+            String newDefaultConfigurationValue = newDefaultConfigurations.get(
+                existingConfigurationKey);
+
+            if (!StringUtils.equals(existingConfigurationValue, newDefaultConfigurationValue)) {
+              // the new default is different from the existing cluster value;
+              // only override the default value if the existing value differs
+              // from the original stack
+              String oldDefaultValue = oldServiceDefaultConfigs.get(existingConfigurationKey);
+
+              if (!StringUtils.equals(existingConfigurationValue, oldDefaultValue)) {
+                // at this point, we've determined that there is a
+                // difference
+                // between default values between stacks, but the value was
+                // also customized, so keep the customized value
+                newDefaultConfigurations.put(existingConfigurationKey, existingConfigurationValue);
+              }
+            }
+          } else {
+            // there is no entry in the map, so add the existing key/value
+            // pair
+            newDefaultConfigurations.put(existingConfigurationKey, existingConfigurationValue);
+          }
+        }
+
+        /*
+        for every new configuration which does not exist in the existing
+        configurations, see if it was present in the current stack
+
+        stack 2.x has foo-site/property (on-ambari-upgrade is false)
+        stack 2.y has foo-site/property
+        the current cluster (on 2.x) does not have it
+
+        In this case, we should NOT add it back as clearly stack advisor has removed it
+        */
+        Iterator<Map.Entry<String, String>> newDefaultConfigurationsIterator = newDefaultConfigurations.entrySet().iterator();
+        while (newDefaultConfigurationsIterator.hasNext()) {
+          Map.Entry<String, String> newConfigurationEntry = newDefaultConfigurationsIterator.next();
+          String newConfigurationPropertyName = newConfigurationEntry.getKey();
+          if (oldServiceDefaultConfigs.containsKey(newConfigurationPropertyName)
+              && !existingConfigurations.containsKey(newConfigurationPropertyName)) {
+            LOG.info(
+                "The property {}/{} exists in both {} and {} but is not part of the current set of configurations and will therefore not be included in the configuration merge",
+                configurationType, newConfigurationPropertyName, sourceStackId, targetStackId);
+
+            // remove the property so it doesn't get merged in
+            newDefaultConfigurationsIterator.remove();
+          }
+        }
+      }
+
+      if (null != newServiceDefaultConfigsByType) {
+        Set<String> configTypes = newServiceDefaultConfigsByType.keySet();
+        LOG.info("The upgrade will create the following configurations for stack {}: {}",
+            targetStackId, StringUtils.join(configTypes, ','));
+
+        String serviceVersionNote = String.format("%s %s %s", direction.getText(true),
+            direction.getPreposition(), upgradeContext.getRepositoryVersion().getVersion());
 
+        configHelper.createConfigTypes(cluster, targetStackId, controller,
+            newServiceDefaultConfigsByType, userName, serviceVersionNote);
       }
     }
   }


[33/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostResourceProvider.java
index 71e105e..c526b6e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostResourceProvider.java
@@ -35,11 +35,11 @@ import org.apache.ambari.server.DuplicateResourceException;
 import org.apache.ambari.server.HostNotFoundException;
 import org.apache.ambari.server.ObjectNotFoundException;
 import org.apache.ambari.server.ParentObjectNotFoundException;
+import org.apache.ambari.server.StaticallyInject;
 import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.ConfigurationRequest;
 import org.apache.ambari.server.controller.HostRequest;
 import org.apache.ambari.server.controller.HostResponse;
-import org.apache.ambari.server.controller.MaintenanceStateHelper;
 import org.apache.ambari.server.controller.RequestStatusResponse;
 import org.apache.ambari.server.controller.ServiceComponentHostRequest;
 import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
@@ -74,6 +74,7 @@ import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.collect.ImmutableSet;
 import com.google.inject.Inject;
 import com.google.inject.assistedinject.Assisted;
 import com.google.inject.assistedinject.AssistedInject;
@@ -83,86 +84,75 @@ import com.google.inject.persist.Transactional;
 /**
  * Resource provider for host resources.
  */
+@StaticallyInject
 public class HostResourceProvider extends AbstractControllerResourceProvider {
 
   // ----- Property ID constants ---------------------------------------------
 
   // Hosts
-  public static final String HOST_CLUSTER_NAME_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "cluster_name");
-  public static final String HOST_NAME_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "host_name");
-  public static final String HOST_PUBLIC_NAME_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "public_host_name");
-  public static final String HOST_IP_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "ip");
-  public static final String HOST_TOTAL_MEM_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "total_mem");
-  public static final String HOST_CPU_COUNT_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "cpu_count");
-  public static final String HOST_PHYSICAL_CPU_COUNT_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "ph_cpu_count");
-  public static final String HOST_OS_ARCH_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "os_arch");
-  public static final String HOST_OS_TYPE_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "os_type");
-  public static final String HOST_OS_FAMILY_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "os_family");
-  public static final String HOST_RACK_INFO_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "rack_info");
-  public static final String HOST_LAST_HEARTBEAT_TIME_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "last_heartbeat_time");
-  public static final String HOST_LAST_REGISTRATION_TIME_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "last_registration_time");
-  public static final String HOST_DISK_INFO_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "disk_info");
-
-
-  public static final String HOST_HOST_STATUS_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "host_status");
-  public static final String HOST_MAINTENANCE_STATE_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "maintenance_state");
-
-  public static final String HOST_HOST_HEALTH_REPORT_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "host_health_report");
-  public static final String HOST_RECOVERY_REPORT_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "recovery_report");
-  public static final String HOST_RECOVERY_SUMMARY_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "recovery_summary");
-  public static final String HOST_STATE_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "host_state");
-  public static final String HOST_LAST_AGENT_ENV_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "last_agent_env");
-  public static final String HOST_DESIRED_CONFIGS_PROPERTY_ID =
-      PropertyHelper.getPropertyId("Hosts", "desired_configs");
-
-  public static final String BLUEPRINT_PROPERTY_ID =
-      PropertyHelper.getPropertyId(null, "blueprint");
-  public static final String HOSTGROUP_PROPERTY_ID =
-      PropertyHelper.getPropertyId(null, "host_group");
-  public static final String HOST_NAME_NO_CATEGORY_PROPERTY_ID =
-      PropertyHelper.getPropertyId(null, "host_name");
-  public static final String HOST_COUNT_PROPERTY_ID =
-      PropertyHelper.getPropertyId(null, "host_count");
-  public static final String HOST_PREDICATE_PROPERTY_ID =
-      PropertyHelper.getPropertyId(null, "host_predicate");
+  public static final String RESPONSE_KEY = "Hosts";
+  public static final String ALL_PROPERTIES = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + "*";
+
+  public static final String CLUSTER_NAME_PROPERTY_ID = "cluster_name";
+  public static final String CPU_COUNT_PROPERTY_ID = "cpu_count";
+  public static final String DESIRED_CONFIGS_PROPERTY_ID = "desired_configs";
+  public static final String DISK_INFO_PROPERTY_ID = "disk_info";
+  public static final String HOST_HEALTH_REPORT_PROPERTY_ID = "host_health_report";
+  public static final String HOST_NAME_PROPERTY_ID = "host_name";
+  public static final String HOST_STATUS_PROPERTY_ID = "host_status";
+  public static final String IP_PROPERTY_ID = "ip";
+  public static final String LAST_AGENT_ENV_PROPERTY_ID = "last_agent_env";
+  public static final String LAST_HEARTBEAT_TIME_PROPERTY_ID = "last_heartbeat_time";
+  public static final String LAST_REGISTRATION_TIME_PROPERTY_ID = "last_registration_time";
+  public static final String MAINTENANCE_STATE_PROPERTY_ID = "maintenance_state";
+  public static final String OS_ARCH_PROPERTY_ID = "os_arch";
+  public static final String OS_FAMILY_PROPERTY_ID = "os_family";
+  public static final String OS_TYPE_PROPERTY_ID = "os_type";
+  public static final String PHYSICAL_CPU_COUNT_PROPERTY_ID = "ph_cpu_count";
+  public static final String PUBLIC_NAME_PROPERTY_ID = "public_host_name";
+  public static final String RACK_INFO_PROPERTY_ID = "rack_info";
+  public static final String RECOVERY_REPORT_PROPERTY_ID = "recovery_report";
+  public static final String RECOVERY_SUMMARY_PROPERTY_ID = "recovery_summary";
+  public static final String STATE_PROPERTY_ID = "host_state";
+  public static final String TOTAL_MEM_PROPERTY_ID = "total_mem";
+
+  public static final String HOST_CLUSTER_NAME_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + CLUSTER_NAME_PROPERTY_ID;
+  public static final String HOST_CPU_COUNT_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + CPU_COUNT_PROPERTY_ID;
+  public static final String HOST_DESIRED_CONFIGS_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + DESIRED_CONFIGS_PROPERTY_ID;
+  public static final String HOST_DISK_INFO_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + DISK_INFO_PROPERTY_ID;
+  public static final String HOST_HOST_HEALTH_REPORT_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + HOST_HEALTH_REPORT_PROPERTY_ID;
+  public static final String HOST_HOST_STATUS_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + HOST_STATUS_PROPERTY_ID;
+  public static final String HOST_IP_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + IP_PROPERTY_ID;
+  public static final String HOST_LAST_AGENT_ENV_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + LAST_AGENT_ENV_PROPERTY_ID;
+  public static final String HOST_LAST_HEARTBEAT_TIME_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + LAST_HEARTBEAT_TIME_PROPERTY_ID;
+  public static final String HOST_LAST_REGISTRATION_TIME_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + LAST_REGISTRATION_TIME_PROPERTY_ID;
+  public static final String HOST_MAINTENANCE_STATE_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + MAINTENANCE_STATE_PROPERTY_ID;
+  public static final String HOST_HOST_NAME_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + HOST_NAME_PROPERTY_ID;
+  public static final String HOST_OS_ARCH_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + OS_ARCH_PROPERTY_ID;
+  public static final String HOST_OS_FAMILY_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + OS_FAMILY_PROPERTY_ID;
+  public static final String HOST_OS_TYPE_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + OS_TYPE_PROPERTY_ID;
+  public static final String HOST_PHYSICAL_CPU_COUNT_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + PHYSICAL_CPU_COUNT_PROPERTY_ID;
+  public static final String HOST_PUBLIC_NAME_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + PUBLIC_NAME_PROPERTY_ID;
+  public static final String HOST_RACK_INFO_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + RACK_INFO_PROPERTY_ID;
+  public static final String HOST_RECOVERY_REPORT_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + RECOVERY_REPORT_PROPERTY_ID;
+  public static final String HOST_RECOVERY_SUMMARY_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + RECOVERY_SUMMARY_PROPERTY_ID;
+  public static final String HOST_STATE_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + STATE_PROPERTY_ID;
+  public static final String HOST_TOTAL_MEM_PROPERTY_ID = RESPONSE_KEY + PropertyHelper.EXTERNAL_PATH_SEP + TOTAL_MEM_PROPERTY_ID;
+
+  public static final String BLUEPRINT_PROPERTY_ID = "blueprint";
+  public static final String HOST_GROUP_PROPERTY_ID = "host_group";
+  public static final String HOST_COUNT_PROPERTY_ID = "host_count";
+  public static final String HOST_PREDICATE_PROPERTY_ID = "host_predicate";
 
   //todo use the same json structure for cluster host addition (cluster template and upscale)
-  public static final String HOST_RACK_INFO_NO_CATEGORY_PROPERTY_ID =
-      PropertyHelper.getPropertyId(null, "rack_info");
 
   protected static final String FORCE_DELETE_COMPONENTS = "force_delete_components";
 
 
-  private static Set<String> pkPropertyIds =
-      new HashSet<String>(Arrays.asList(new String[]{
-          HOST_NAME_PROPERTY_ID}));
+  private static final Set<String> PK_PROPERTY_IDS = ImmutableSet.of(HOST_HOST_NAME_PROPERTY_ID);
 
   @Inject
-  private MaintenanceStateHelper maintenanceStateHelper;
-
-  @Inject
-  private OsFamily osFamily;
+  private static OsFamily osFamily;
 
   @Inject
   private static TopologyManager topologyManager;
@@ -220,7 +210,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
   protected Set<Resource> getResourcesAuthorized(Request request, Predicate predicate)
       throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
 
-    final Set<HostRequest> requests = new HashSet<HostRequest>();
+    final Set<HostRequest> requests = new HashSet<>();
 
     if (predicate == null) {
       requests.add(getRequest(null));
@@ -239,7 +229,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
     });
 
     Set<String>   requestedIds = getRequestPropertyIds(request, predicate);
-    Set<Resource> resources    = new HashSet<Resource>();
+    Set<Resource> resources    = new HashSet<>();
 
     for (HostResponse response : responses) {
       Resource resource = new ResourceImpl(Resource.Type.Host);
@@ -250,7 +240,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
         setResourceProperty(resource, HOST_CLUSTER_NAME_PROPERTY_ID,
             response.getClusterName(), requestedIds);
       }
-      setResourceProperty(resource, HOST_NAME_PROPERTY_ID,
+      setResourceProperty(resource, HOST_HOST_NAME_PROPERTY_ID,
           response.getHostname(), requestedIds);
       setResourceProperty(resource, HOST_PUBLIC_NAME_PROPERTY_ID,
           response.getPublicHostName(), requestedIds);
@@ -259,22 +249,15 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
       setResourceProperty(resource, HOST_TOTAL_MEM_PROPERTY_ID,
           response.getTotalMemBytes(), requestedIds);
       setResourceProperty(resource, HOST_CPU_COUNT_PROPERTY_ID,
-          (long) response.getCpuCount(), requestedIds);
+          response.getCpuCount(), requestedIds);
       setResourceProperty(resource, HOST_PHYSICAL_CPU_COUNT_PROPERTY_ID,
-          (long) response.getPhCpuCount(), requestedIds);
+          response.getPhCpuCount(), requestedIds);
       setResourceProperty(resource, HOST_OS_ARCH_PROPERTY_ID,
           response.getOsArch(), requestedIds);
       setResourceProperty(resource, HOST_OS_TYPE_PROPERTY_ID,
           response.getOsType(), requestedIds);
-
-      String hostOsFamily = osFamily.find(response.getOsType());
-      if (hostOsFamily == null) {
-        LOG.error("Can not find host OS family. For OS type = '{}' and host name = '{}'",
-            response.getOsType(), response.getHostname());
-      }
       setResourceProperty(resource, HOST_OS_FAMILY_PROPERTY_ID,
-          hostOsFamily, requestedIds);
-
+          response.getOsFamily(), requestedIds);
       setResourceProperty(resource, HOST_RACK_INFO_PROPERTY_ID,
           response.getRackInfo(), requestedIds);
       setResourceProperty(resource, HOST_LAST_HEARTBEAT_TIME_PROPERTY_ID,
@@ -286,7 +269,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
       setResourceProperty(resource, HOST_HOST_STATUS_PROPERTY_ID,
           response.getStatus(),requestedIds);
       setResourceProperty(resource, HOST_HOST_HEALTH_REPORT_PROPERTY_ID,
-          response.getHealthStatus().getHealthReport(), requestedIds);
+          response.getHealthReport(), requestedIds);
       setResourceProperty(resource, HOST_RECOVERY_REPORT_PROPERTY_ID,
           response.getRecoveryReport(), requestedIds);
       setResourceProperty(resource, HOST_RECOVERY_SUMMARY_PROPERTY_ID,
@@ -313,7 +296,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
   protected RequestStatus updateResourcesAuthorized(final Request request, Predicate predicate)
       throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
 
-    final Set<HostRequest> requests = new HashSet<HostRequest>();
+    final Set<HostRequest> requests = new HashSet<>();
     for (Map<String, Object> propertyMap : getPropertyMaps(request.getProperties().iterator().next(), predicate)) {
       requests.add(getRequest(propertyMap));
     }
@@ -363,12 +346,11 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
     Set<String> baseUnsupported = super.checkPropertyIds(propertyIds);
 
     baseUnsupported.remove(BLUEPRINT_PROPERTY_ID);
-    baseUnsupported.remove(HOSTGROUP_PROPERTY_ID);
-    baseUnsupported.remove(HOST_NAME_NO_CATEGORY_PROPERTY_ID);
-    //todo: constants
+    baseUnsupported.remove(HOST_GROUP_PROPERTY_ID);
+    baseUnsupported.remove(HOST_NAME_PROPERTY_ID);
     baseUnsupported.remove(HOST_COUNT_PROPERTY_ID);
     baseUnsupported.remove(HOST_PREDICATE_PROPERTY_ID);
-    baseUnsupported.remove(HOST_RACK_INFO_NO_CATEGORY_PROPERTY_ID);
+    baseUnsupported.remove(RACK_INFO_PROPERTY_ID);
 
     return checkConfigPropertyIds(baseUnsupported, "Hosts");
   }
@@ -378,7 +360,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
 
   @Override
   protected Set<String> getPKPropertyIds() {
-    return pkPropertyIds;
+    return PK_PROPERTY_IDS;
   }
 
 
@@ -397,7 +379,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
     Set<Map<String, Object>> properties = request.getProperties();
     if (properties != null && ! properties.isEmpty()) {
       //todo: for now, either all or none of the hosts need to specify a hg.  Unable to mix.
-      String hgName = (String) properties.iterator().next().get(HOSTGROUP_PROPERTY_ID);
+      String hgName = (String) properties.iterator().next().get(HOST_GROUP_PROPERTY_ID);
       isHostGroupRequest = hgName != null && ! hgName.isEmpty();
     }
     return isHostGroupRequest;
@@ -413,21 +395,21 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
   private HostRequest getRequest(Map<String, Object> properties) {
 
     if (properties == null) {
-      return  new HostRequest(null, null, null);
+      return new HostRequest(null, null);
     }
 
     HostRequest hostRequest = new HostRequest(
         getHostNameFromProperties(properties),
-        (String) properties.get(HOST_CLUSTER_NAME_PROPERTY_ID),
-        null);
+        (String) properties.get(HOST_CLUSTER_NAME_PROPERTY_ID)
+    );
     hostRequest.setPublicHostName((String) properties.get(HOST_PUBLIC_NAME_PROPERTY_ID));
 
     String rackInfo = (String) ((null != properties.get(HOST_RACK_INFO_PROPERTY_ID))? properties.get(HOST_RACK_INFO_PROPERTY_ID):
-            properties.get(HOST_RACK_INFO_NO_CATEGORY_PROPERTY_ID));
+            properties.get(RACK_INFO_PROPERTY_ID));
 
     hostRequest.setRackInfo(rackInfo);
     hostRequest.setBlueprintName((String) properties.get(BLUEPRINT_PROPERTY_ID));
-    hostRequest.setHostGroupName((String) properties.get(HOSTGROUP_PROPERTY_ID));
+    hostRequest.setHostGroupName((String) properties.get(HOST_GROUP_PROPERTY_ID));
 
     Object o = properties.get(HOST_MAINTENANCE_STATE_PROPERTY_ID);
     if (null != o) {
@@ -446,7 +428,6 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
    * Accepts a request with registered hosts and if the request contains a cluster name then will map all of the
    * hosts onto that cluster.
    * @param request Request that must contain registered hosts, and optionally a cluster.
-   * @throws AmbariException
    */
   public synchronized void createHosts(Request request)
       throws AmbariException {
@@ -460,16 +441,16 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
     AmbariManagementController controller = getManagementController();
     Clusters                   clusters   = controller.getClusters();
 
-    Set<String> duplicates = new HashSet<String>();
-    Set<String> unknowns = new HashSet<String>();
-    Set<String> allHosts = new HashSet<String>();
+    Set<String> duplicates = new HashSet<>();
+    Set<String> unknowns = new HashSet<>();
+    Set<String> allHosts = new HashSet<>();
 
 
-    Set<HostRequest> hostRequests = new HashSet<HostRequest>();
+    Set<HostRequest> hostRequests = new HashSet<>();
     for (Map<String, Object> propertyMap : propertySet) {
       HostRequest hostRequest = getRequest(propertyMap);
       hostRequests.add(hostRequest);
-      if (! propertyMap.containsKey(HOSTGROUP_PROPERTY_ID)) {
+      if (! propertyMap.containsKey(HOST_GROUP_PROPERTY_ID)) {
         createHostResource(clusters, duplicates, unknowns, allHosts, hostRequest);
       }
     }
@@ -486,7 +467,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
       }
       throw new IllegalArgumentException("Invalid request contains"
           + " duplicate hostnames"
-          + ", hostnames=" + names.toString());
+          + ", hostnames=" + names);
     }
 
     if (!unknowns.isEmpty()) {
@@ -501,12 +482,12 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
       }
 
       throw new IllegalArgumentException("Attempted to add unknown hosts to a cluster.  " +
-          "These hosts have not been registered with the server: " + names.toString());
+          "These hosts have not been registered with the server: " + names);
     }
 
-    Map<String, Set<String>> hostClustersMap = new HashMap<String, Set<String>>();
-    Map<String, Map<String, String>> hostAttributes = new HashMap<String, Map<String, String>>();
-    Set<String> allClusterSet = new HashSet<String>();
+    Map<String, Set<String>> hostClustersMap = new HashMap<>();
+    Map<String, Map<String, String>> hostAttributes = new HashMap<>();
+    Set<String> allClusterSet = new HashSet<>();
 
     for (HostRequest hostRequest : hostRequests) {
       if (hostRequest.getHostname() != null &&
@@ -514,20 +495,14 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
           hostRequest.getClusterName() != null &&
           !hostRequest.getClusterName().isEmpty()){
 
-        Set<String> clusterSet = new HashSet<String>();
+        Set<String> clusterSet = new HashSet<>();
         clusterSet.add(hostRequest.getClusterName());
         allClusterSet.add(hostRequest.getClusterName());
         hostClustersMap.put(hostRequest.getHostname(), clusterSet);
-        if (hostRequest.getHostAttributes() != null) {
-          hostAttributes.put(hostRequest.getHostname(), hostRequest.getHostAttributes());
-        }
       }
     }
     clusters.updateHostWithClusterAndAttributes(hostClustersMap, hostAttributes);
 
-    for (String clusterName : allClusterSet) {
-      clusters.getCluster(clusterName).recalculateAllClusterVersionStates();
-    }
   }
 
   private void createHostResource(Clusters clusters, Set<String> duplicates,
@@ -543,9 +518,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
     }
 
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Received a createHost request"
-          + ", hostname=" + request.getHostname()
-          + ", request=" + request);
+      LOG.debug("Received a createHost request, hostname={}, request={}", request.getHostname(), request);
     }
 
     if (allHosts.contains(request.getHostname())) {
@@ -597,7 +570,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
   }
 
   protected Set<HostResponse> getHosts(Set<HostRequest> requests) throws AmbariException {
-    Set<HostResponse> response = new HashSet<HostResponse>();
+    Set<HostResponse> response = new HashSet<>();
 
     AmbariManagementController controller = getManagementController();
 
@@ -615,6 +588,9 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
     return response;
   }
 
+  /**
+   * @param osFamily provides OS to OS family lookup; may be null if OS family is ignored anyway (eg. for liveness check)
+   */
   protected static Set<HostResponse> getHosts(AmbariManagementController controller, HostRequest request)
       throws AmbariException {
 
@@ -622,7 +598,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
     //TODO/FIXME what is the requirement for filtering on host attributes?
 
     List<Host> hosts;
-    Set<HostResponse> response = new HashSet<HostResponse>();
+    Set<HostResponse> response = new HashSet<>();
     Cluster           cluster  = null;
 
     Clusters                   clusters   = controller.getClusters();
@@ -642,7 +618,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
     if (hostName == null) {
       hosts = clusters.getHosts();
     } else {
-      hosts = new ArrayList<Host>();
+      hosts = new ArrayList<>();
       try {
         hosts.add(clusters.getHost(request.getHostname()));
       } catch (HostNotFoundException e) {
@@ -654,7 +630,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
     // retrieve the cluster desired configs once instead of per host
     Map<String, DesiredConfig> desiredConfigs = null;
     if (null != cluster) {
-      cluster.getDesiredConfigs();
+      desiredConfigs = cluster.getDesiredConfigs();
     }
 
     for (Host h : hosts) {
@@ -665,6 +641,13 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
           r.setClusterName(clusterName);
           r.setDesiredHostConfigs(h.getDesiredHostConfigs(cluster, desiredConfigs));
           r.setMaintenanceState(h.getMaintenanceState(cluster.getClusterId()));
+          if (osFamily != null) {
+            String hostOsFamily = osFamily.find(r.getOsType());
+            if (hostOsFamily == null) {
+              LOG.error("Can not find host OS family. For OS type = '{}' and host name = '{}'", r.getOsType(), r.getHostname());
+            }
+            r.setOsFamily(hostOsFamily);
+          }
 
           response.add(r);
         } else if (hostName != null) {
@@ -678,7 +661,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
         if (clustersForHost != null && clustersForHost.size() != 0) {
           Cluster clusterForHost = clustersForHost.iterator().next();
           r.setClusterName(clusterForHost.getClusterName());
-          r.setDesiredHostConfigs(h.getDesiredHostConfigs(clusterForHost, desiredConfigs));
+          r.setDesiredHostConfigs(h.getDesiredHostConfigs(clusterForHost, null));
           r.setMaintenanceState(h.getMaintenanceState(clusterForHost.getClusterId()));
         }
 
@@ -706,9 +689,7 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
 
     for (HostRequest request : requests) {
       if (LOG.isDebugEnabled()) {
-        LOG.debug("Received an updateHost request"
-            + ", hostname=" + request.getHostname()
-            + ", request=" + request);
+        LOG.debug("Received an updateHost request, hostname={}, request={}", request.getHostname(), request);
       }
 
       Host host = clusters.getHost(request.getHostname());
@@ -726,13 +707,6 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
         // do nothing
       }
 
-      if (null != request.getHostAttributes()) {
-        if(!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, resourceId, RoleAuthorization.HOST_ADD_DELETE_HOSTS)) {
-          throw new AuthorizationException("The authenticated user is not authorized to update host attributes");
-        }
-        host.setHostAttributes(request.getHostAttributes());
-      }
-
       String  rackInfo        = host.getRackInfo();
       String  requestRackInfo = request.getRackInfo();
       boolean rackChange      = requestRackInfo != null && !requestRackInfo.equals(rackInfo);
@@ -803,12 +777,9 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
         }
       }
 
-      if (clusterName != null && !clusterName.isEmpty()) {
-        clusters.getCluster(clusterName).recalculateAllClusterVersionStates();
-        if (rackChange) {
-          // Authorization check for this update was performed before we got to this point.
-          controller.registerRackChange(clusterName);
-        }
+      if (StringUtils.isNotBlank(clusterName) && rackChange) {
+        // Authorization check for this update was performed before we got to this point.
+        controller.registerRackChange(clusterName);
       }
 
       //todo: if attempt was made to update a property other than those
@@ -931,9 +902,6 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
       }
     }
     clusters.publishHostsDeletion(allClustersWithHosts, hostNames);
-    for (String clustername : hostsClusters) {
-      clusters.getCluster(clustername).recalculateAllClusterVersionStates();
-    }
   }
 
   private void validateHostInDeleteFriendlyState(HostRequest hostRequest, Clusters clusters, boolean forceDelete) throws AmbariException {
@@ -999,9 +967,6 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
 
   /**
    * Removes hostname from the stateful cluster topology
-   * @param clusters
-   * @param hostRequest
-   * @throws AmbariException
    */
   private void removeHostFromClusterTopology(Clusters clusters, HostRequest hostRequest) throws AmbariException{
     if (hostRequest.getClusterName() == null) {
@@ -1032,10 +997,10 @@ public class HostResourceProvider extends AbstractControllerResourceProvider {
    * @return the host name for the host request
    */
   private String getHostNameFromProperties(Map<String, Object> properties) {
-    String hostname = (String) properties.get(HOST_NAME_PROPERTY_ID);
+    String hostname = (String) properties.get(HOST_HOST_NAME_PROPERTY_ID);
 
     return hostname != null ? hostname :
-        (String) properties.get(HOST_NAME_NO_CATEGORY_PROPERTY_ID);
+        (String) properties.get(HOST_NAME_PROPERTY_ID);
   }
 
   //todo: for api/v1/hosts we also end up here so we need to ensure proper 400 response

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProvider.java
index 5fc3652..fb4831e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProvider.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,7 +19,6 @@ package org.apache.ambari.server.controller.internal;
 
 import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.JDK_LOCATION;
 
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -34,7 +33,6 @@ import org.apache.ambari.server.actionmanager.ActionManager;
 import org.apache.ambari.server.actionmanager.RequestFactory;
 import org.apache.ambari.server.actionmanager.Stage;
 import org.apache.ambari.server.actionmanager.StageFactory;
-import org.apache.ambari.server.agent.ExecutionCommand.KeyNames;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.ActionExecutionContext;
@@ -61,17 +59,14 @@ import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Host;
 import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.ServiceComponentHost;
-import org.apache.ambari.server.state.ServiceInfo;
-import org.apache.ambari.server.state.ServiceOsSpecific;
 import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.repository.VersionDefinitionXml;
+import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
 import org.apache.ambari.server.utils.StageUtils;
 import org.apache.commons.lang.BooleanUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.Validate;
 
 import com.google.common.collect.Sets;
-import com.google.gson.Gson;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 
@@ -107,7 +102,7 @@ public class HostStackVersionResourceProvider extends AbstractControllerResource
 
   protected static final String INSTALL_PACKAGES_ACTION = "install_packages";
   protected static final String STACK_SELECT_ACTION = "ru_set_all";
-  protected static final String INSTALL_PACKAGES_FULL_NAME = "Install version";
+  protected static final String INSTALL_PACKAGES_FULL_NAME = "Install Version";
 
 
   private static Set<String> pkPropertyIds = Sets.newHashSet(
@@ -147,8 +142,6 @@ public class HostStackVersionResourceProvider extends AbstractControllerResource
   @Inject
   private static RepositoryVersionDAO repositoryVersionDAO;
 
-  private static Gson gson = StageUtils.getGson();
-
   @Inject
   private static StageFactory stageFactory;
 
@@ -161,6 +154,8 @@ public class HostStackVersionResourceProvider extends AbstractControllerResource
   @Inject
   private static Configuration configuration;
 
+  @Inject
+  private static RepositoryVersionHelper repoVersionHelper;
 
   /**
    * Constructor.
@@ -398,8 +393,11 @@ public class HostStackVersionResourceProvider extends AbstractControllerResource
                       "not defined. Repo version=%s, stackId=%s",
         osFamily, desiredRepoVersion, stackId));
     }
-    // For every host at cluster, determine packages for all installed services
-    List<ServiceOsSpecific.Package> packages = new ArrayList<>();
+
+    if (repoInfo.isEmpty()){
+      LOG.error(String.format("Repository list is empty. Ambari may not be managing the repositories for %s", osFamily));
+    }
+
     Set<String> servicesOnHost = new HashSet<>();
 
     if (forceInstallOnNonMemberHost) {
@@ -421,7 +419,6 @@ public class HostStackVersionResourceProvider extends AbstractControllerResource
           LOG.error("Service not found for component {}!", componentName, e);
           throw new IllegalArgumentException("Service not found for component : " + componentName);
         }
-
       }
 
     } else {
@@ -430,53 +427,9 @@ public class HostStackVersionResourceProvider extends AbstractControllerResource
         servicesOnHost.add(component.getServiceName());
       }
     }
-    List<String> blacklistedPackagePrefixes = configuration.getRollingUpgradeSkipPackagesPrefixes();
-    for (String serviceName : servicesOnHost) {
-      ServiceInfo info;
-      try {
-        info = ami.getService(stackName, stackVersion, serviceName);
-      } catch (AmbariException e) {
-        throw new SystemException("Can not enumerate services", e);
-      }
-      List<ServiceOsSpecific.Package> packagesForService = managementController.getPackagesForServiceHost(info,
-              new HashMap<String, String>(), // Contents are ignored
-        osFamily);
-      for (ServiceOsSpecific.Package aPackage : packagesForService) {
-        if (! aPackage.getSkipUpgrade()) {
-          boolean blacklisted = false;
-          for(String prefix : blacklistedPackagePrefixes) {
-            if (aPackage.getName().startsWith(prefix)) {
-              blacklisted = true;
-              break;
-            }
-          }
-          if (! blacklisted) {
-            packages.add(aPackage);
-          }
-        }
-      }
-    }
-    final String packageList = gson.toJson(packages);
-    final String repoList = gson.toJson(repoInfo);
-
-    Map<String, String> params = new HashMap<String, String>(){{
-      put("stack_id", stackId.getStackId());
-      put("repository_version", desiredRepoVersion);
-      put("base_urls", repoList);
-      put(KeyNames.PACKAGE_LIST, packageList);
-    }};
 
-    VersionDefinitionXml xml = null;
-    try {
-      xml = repoVersionEnt.getRepositoryXml();
-    } catch (Exception e) {
-      throw new SystemException(String.format("Could not load xml from repo version %s",
-          repoVersionEnt.getVersion()));
-    }
-
-    if (null != xml && StringUtils.isNotBlank(xml.getPackageVersion(osFamily))) {
-      params.put(KeyNames.PACKAGE_VERSION, xml.getPackageVersion(osFamily));
-    }
+    Map<String, String> roleParams = repoVersionHelper.buildRoleParams(managementController, repoVersionEnt,
+        osFamily, servicesOnHost);
 
     // Create custom action
     RequestResourceFilter filter = new RequestResourceFilter(null, null,
@@ -485,9 +438,11 @@ public class HostStackVersionResourceProvider extends AbstractControllerResource
     ActionExecutionContext actionContext = new ActionExecutionContext(
             cluster.getClusterName(), INSTALL_PACKAGES_ACTION,
             Collections.singletonList(filter),
-            params);
+            roleParams);
     actionContext.setTimeout(Short.valueOf(configuration.getDefaultAgentTaskTimeout(true)));
 
+    repoVersionHelper.addCommandRepository(actionContext, osFamily, repoVersionEnt, repoInfo);
+
     String caption = String.format(INSTALL_PACKAGES_FULL_NAME + " on host %s", hostName);
     RequestStageContainer req = createRequest(caption);
 
@@ -534,7 +489,6 @@ public class HostStackVersionResourceProvider extends AbstractControllerResource
       if (!forceInstallOnNonMemberHost) {
         hostVersEntity.setState(RepositoryVersionState.INSTALLING);
         hostVersionDAO.merge(hostVersEntity);
-        cluster.recalculateClusterVersionState(repoVersionEnt);
       }
       req.persist();
     } catch (AmbariException e) {
@@ -548,7 +502,7 @@ public class HostStackVersionResourceProvider extends AbstractControllerResource
     Stage stage;
     long stageId;
     ActionExecutionContext actionContext;
-    Map<String, String> commandParams = new HashMap();
+    Map<String, String> commandParams = new HashMap<>();
     commandParams.put("version", desiredRepoVersion);
 
     stage = stageFactory.createNew(req.getId(),

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStatusHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStatusHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStatusHelper.java
index 69f1c44..981c039 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStatusHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostStatusHelper.java
@@ -18,6 +18,9 @@
 
 package org.apache.ambari.server.controller.internal;
 
+import java.util.Collections;
+import java.util.Set;
+
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.HostRequest;
@@ -29,9 +32,6 @@ import org.apache.ambari.server.state.State;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.Collections;
-import java.util.Set;
-
 public class HostStatusHelper {
 
   protected final static Logger LOG =
@@ -72,8 +72,7 @@ public class HostStatusHelper {
     HostResponse hostResponse;
 
     try {
-      HostRequest hostRequest = new HostRequest(hostName, clusterName,
-        Collections.<String, String>emptyMap());
+      HostRequest hostRequest = new HostRequest(hostName, clusterName);
       Set<HostResponse> hosts = HostResourceProvider.getHosts(managementController, hostRequest);
 
       hostResponse = hosts.size() == 1 ? hosts.iterator().next() : null;

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProvider.java
index 689942d..7b03912 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProvider.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -47,13 +47,12 @@ import org.apache.ambari.server.state.CheckHelper;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ServiceInfo;
-import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.UpgradeHelper;
 import org.apache.ambari.server.state.stack.PrerequisiteCheck;
 import org.apache.ambari.server.state.stack.UpgradePack;
 import org.apache.ambari.server.state.stack.upgrade.Direction;
 import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
-
+import org.apache.commons.lang.BooleanUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -66,7 +65,7 @@ import com.google.inject.Provider;
  */
 @StaticallyInject
 public class PreUpgradeCheckResourceProvider extends ReadOnlyResourceProvider {
-  private static Logger LOG = LoggerFactory.getLogger(PreUpgradeCheckResourceProvider.class);
+  private static final Logger LOG = LoggerFactory.getLogger(PreUpgradeCheckResourceProvider.class);
 
   //----- Property ID constants ---------------------------------------------
 
@@ -79,12 +78,12 @@ public class PreUpgradeCheckResourceProvider extends ReadOnlyResourceProvider {
   public static final String UPGRADE_CHECK_CHECK_TYPE_PROPERTY_ID         = PropertyHelper.getPropertyId("UpgradeChecks", "check_type");
   public static final String UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID       = PropertyHelper.getPropertyId("UpgradeChecks", "cluster_name");
   public static final String UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID       = PropertyHelper.getPropertyId("UpgradeChecks", "upgrade_type");
-  public static final String UPGRADE_CHECK_TARGET_STACK_ID                = PropertyHelper.getPropertyId("UpgradeChecks", "target_stack");
   /**
    * Optional parameter to specify the preferred Upgrade Pack to use.
    */
   public static final String UPGRADE_CHECK_UPGRADE_PACK_PROPERTY_ID       = PropertyHelper.getPropertyId("UpgradeChecks", "upgrade_pack");
   public static final String UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID = PropertyHelper.getPropertyId("UpgradeChecks", "repository_version");
+  public static final String UPGRADE_CHECK_FOR_REVERT_PROPERTY_ID = PropertyHelper.getPropertyId("UpgradeChecks", "for_revert");
 
   @Inject
   private static Provider<Clusters> clustersProvider;
@@ -116,9 +115,9 @@ public class PreUpgradeCheckResourceProvider extends ReadOnlyResourceProvider {
       UPGRADE_CHECK_CHECK_TYPE_PROPERTY_ID,
       UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID,
       UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID,
-      UPGRADE_CHECK_TARGET_STACK_ID,
       UPGRADE_CHECK_UPGRADE_PACK_PROPERTY_ID,
-      UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID);
+      UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID,
+      UPGRADE_CHECK_FOR_REVERT_PROPERTY_ID);
 
 
   @SuppressWarnings("serial")
@@ -138,16 +137,16 @@ public class PreUpgradeCheckResourceProvider extends ReadOnlyResourceProvider {
     super(propertyIds, keyPropertyIds, managementController);
   }
 
+  @Override
   public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException,
     NoSuchResourceException, NoSuchParentResourceException {
 
-    final Set<Resource> resources = new HashSet<Resource>();
+    final Set<Resource> resources = new HashSet<>();
     final Set<String> requestedIds = getRequestPropertyIds(request, predicate);
     final Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate);
 
     for (Map<String, Object> propertyMap: propertyMaps) {
       final String clusterName = propertyMap.get(UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID).toString();
-      StackId targetStack = new StackId(propertyMap.get(UPGRADE_CHECK_TARGET_STACK_ID).toString());
 
       UpgradeType upgradeType = UpgradeType.ROLLING;
       if (propertyMap.containsKey(UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID)) {
@@ -174,12 +173,17 @@ public class PreUpgradeCheckResourceProvider extends ReadOnlyResourceProvider {
 
       if (propertyMap.containsKey(UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID)) {
         String repositoryVersionId = propertyMap.get(UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID).toString();
-        RepositoryVersionEntity repositoryVersionEntity = repositoryVersionDAO.findByStackNameAndVersion(targetStack.getStackName(), repositoryVersionId);
+        RepositoryVersionEntity repositoryVersionEntity = repositoryVersionDAO.findByStackNameAndVersion(stackName, repositoryVersionId);
         // set some required properties on the check request
         upgradeCheckRequest.setRepositoryVersion(repositoryVersionId);
         upgradeCheckRequest.setTargetStackId(repositoryVersionEntity.getStackId());
       }
 
+      if (propertyMap.containsKey(UPGRADE_CHECK_FOR_REVERT_PROPERTY_ID)) {
+        Boolean forRevert = BooleanUtils.toBooleanObject(propertyMap.get(UPGRADE_CHECK_FOR_REVERT_PROPERTY_ID).toString());
+        upgradeCheckRequest.setRevert(forRevert);
+      }
+
       //ambariMetaInfo.getStack(stackName, cluster.getCurrentStackVersion().getStackVersion()).getUpgradePacks()
       // TODO AMBARI-12698, filter the upgrade checks to run based on the stack and upgrade type, or the upgrade pack.
       UpgradePack upgradePack = null;
@@ -188,7 +192,7 @@ public class PreUpgradeCheckResourceProvider extends ReadOnlyResourceProvider {
       try{
         // Hint: PreChecks currently executing only before UPGRADE direction
         upgradePack = upgradeHelper.get().suggestUpgradePack(clusterName, sourceStackVersion,
-            upgradeCheckRequest.getRepositoryVersion(), Direction.UPGRADE, upgradeType, targetStack.getStackName(), preferredUpgradePackName);
+            upgradeCheckRequest.getRepositoryVersion(), Direction.UPGRADE, upgradeType, preferredUpgradePackName);
       } catch (AmbariException e) {
         throw new SystemException(e.getMessage(), e);
       }
@@ -226,7 +230,6 @@ public class PreUpgradeCheckResourceProvider extends ReadOnlyResourceProvider {
         setResourceProperty(resource, UPGRADE_CHECK_CHECK_TYPE_PROPERTY_ID, prerequisiteCheck.getType(), requestedIds);
         setResourceProperty(resource, UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID, prerequisiteCheck.getClusterName(), requestedIds);
         setResourceProperty(resource, UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID, upgradeType, requestedIds);
-        setResourceProperty(resource, UPGRADE_CHECK_TARGET_STACK_ID, targetStack.getStackId(), requestedIds);
         if (upgradeCheckRequest.getRepositoryVersion() != null) {
           setResourceProperty(resource, UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID, upgradeCheckRequest.getRepositoryVersion(), requestedIds);
         }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProvider.java
index 46b0cb7..34b7b92 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProvider.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -25,6 +25,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 
 import org.apache.ambari.server.AmbariException;
@@ -43,10 +44,10 @@ import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
 import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
+import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
+import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
 import org.apache.ambari.server.orm.entities.RepositoryEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
@@ -55,7 +56,6 @@ import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.security.authorization.AuthorizationHelper;
 import org.apache.ambari.server.security.authorization.ResourceType;
 import org.apache.ambari.server.security.authorization.RoleAuthorization;
-import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.OperatingSystemInfo;
 import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.ServiceInfo;
@@ -64,14 +64,14 @@ import org.apache.ambari.server.state.StackInfo;
 import org.apache.ambari.server.state.repository.ManifestServiceInfo;
 import org.apache.ambari.server.state.repository.VersionDefinitionXml;
 import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang.ObjectUtils;
 import org.apache.commons.lang.StringUtils;
 
-import com.google.common.collect.Lists;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Sets;
 import com.google.gson.Gson;
 import com.google.inject.Inject;
-import com.google.inject.Provider;
 import com.google.inject.persist.Transactional;
 
 /**
@@ -101,11 +101,7 @@ public class RepositoryVersionResourceProvider extends AbstractAuthorizedResourc
   public static final String REPOSITORY_VERSION_HAS_CHILDREN                   = "RepositoryVersions/has_children";
 
   @SuppressWarnings("serial")
-  private static Set<String> pkPropertyIds = new HashSet<String>() {
-    {
-      add(REPOSITORY_VERSION_ID_PROPERTY_ID);
-    }
-  };
+  private static Set<String> pkPropertyIds = Sets.newHashSet(REPOSITORY_VERSION_ID_PROPERTY_ID);
 
   @SuppressWarnings("serial")
   public static Set<String> propertyIds = Sets.newHashSet(
@@ -126,13 +122,11 @@ public class RepositoryVersionResourceProvider extends AbstractAuthorizedResourc
       REPOSITORY_VERSION_STACK_SERVICES);
 
   @SuppressWarnings("serial")
-  public static Map<Type, String> keyPropertyIds = new HashMap<Type, String>() {
-    {
-      put(Type.Stack, REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID);
-      put(Type.StackVersion, REPOSITORY_VERSION_STACK_VERSION_PROPERTY_ID);
-      put(Type.RepositoryVersion, REPOSITORY_VERSION_ID_PROPERTY_ID);
-    }
-  };
+  public static Map<Type, String> keyPropertyIds = new ImmutableMap.Builder<Type, String>()
+      .put(Type.Stack, REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID)
+      .put(Type.StackVersion, REPOSITORY_VERSION_STACK_VERSION_PROPERTY_ID)
+      .put(Type.RepositoryVersion, REPOSITORY_VERSION_ID_PROPERTY_ID)
+      .build();
 
   @Inject
   private Gson gson;
@@ -141,23 +135,20 @@ public class RepositoryVersionResourceProvider extends AbstractAuthorizedResourc
   private RepositoryVersionDAO repositoryVersionDAO;
 
   @Inject
-  private ClusterVersionDAO clusterVersionDAO;
-
-  @Inject
   private AmbariMetaInfo ambariMetaInfo;
 
   @Inject
   private RepositoryVersionHelper repositoryVersionHelper;
 
-  @Inject
-  private Provider<Clusters> clusters;
-
   /**
    * Data access object used for lookup up stacks.
    */
   @Inject
   private StackDAO stackDAO;
 
+  @Inject
+  HostVersionDAO hostVersionDAO;
+
   /**
    * Create a new resource provider.
    *
@@ -226,12 +217,12 @@ public class RepositoryVersionResourceProvider extends AbstractAuthorizedResourc
   @Override
   protected Set<Resource> getResourcesAuthorized(Request request, Predicate predicate)
       throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
-    final Set<Resource> resources = new HashSet<Resource>();
+    final Set<Resource> resources = new HashSet<>();
     final Set<String> requestedIds = getRequestPropertyIds(request, predicate);
     final Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate);
 
 
-    List<RepositoryVersionEntity> requestedEntities = new ArrayList<RepositoryVersionEntity>();
+    List<RepositoryVersionEntity> requestedEntities = new ArrayList<>();
     for (Map<String, Object> propertyMap: propertyMaps) {
       final StackId stackId = getStackInformationFromUrl(propertyMap);
 
@@ -358,19 +349,6 @@ public class RepositoryVersionResourceProvider extends AbstractAuthorizedResourc
 
           repositoryVersionDAO.merge(entity);
 
-          //
-          // Update metaInfo table as well
-          //
-          if (operatingSystemEntities != null) {
-            String entityStackName = entity.getStackName();
-            String entityStackVersion = entity.getStackVersion();
-            for (OperatingSystemEntity osEntity : operatingSystemEntities) {
-              List<RepositoryEntity> repositories = osEntity.getRepositories();
-              for (RepositoryEntity repository : repositories) {
-                ambariMetaInfo.updateRepoBaseURL(entityStackName, entityStackVersion, osEntity.getOsType(), repository.getRepositoryId(), repository.getBaseUrl());
-              }
-            }
-          }
         }
         return null;
       }
@@ -384,7 +362,7 @@ public class RepositoryVersionResourceProvider extends AbstractAuthorizedResourc
       throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
     final Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate);
 
-    final List<RepositoryVersionEntity> entitiesToBeRemoved = new ArrayList<RepositoryVersionEntity>();
+    final List<RepositoryVersionEntity> entitiesToBeRemoved = new ArrayList<>();
     for (Map<String, Object> propertyMap : propertyMaps) {
       final Long id;
       try {
@@ -398,22 +376,34 @@ public class RepositoryVersionResourceProvider extends AbstractAuthorizedResourc
         throw new NoSuchResourceException("There is no repository version with id " + id);
       }
 
-      StackEntity stackEntity = entity.getStack();
-      String stackName = stackEntity.getStackName();
-      String stackVersion = stackEntity.getStackVersion();
-
-      final List<ClusterVersionEntity> clusterVersionEntities = clusterVersionDAO.findByStackAndVersion(
-          stackName, stackVersion, entity.getVersion());
-
-      final List<RepositoryVersionState> forbiddenToDeleteStates = Lists.newArrayList(
+      final Set<RepositoryVersionState> forbiddenToDeleteStates = Sets.newHashSet(
           RepositoryVersionState.CURRENT,
           RepositoryVersionState.INSTALLED,
           RepositoryVersionState.INSTALLING);
-      for (ClusterVersionEntity clusterVersionEntity : clusterVersionEntities) {
-        if (clusterVersionEntity.getRepositoryVersion().getId().equals(id) && forbiddenToDeleteStates.contains(clusterVersionEntity.getState())) {
-          throw new SystemException("Repository version can't be deleted as it is " +
-              clusterVersionEntity.getState().name() + " on cluster " + clusterVersionEntity.getClusterEntity().getClusterName());
+
+      List<HostVersionEntity> hostVersions = hostVersionDAO.findByRepositoryAndStates(
+          entity, forbiddenToDeleteStates);
+
+      if (CollectionUtils.isNotEmpty(hostVersions)) {
+        Map<RepositoryVersionState, Set<String>> hostsInUse = new HashMap<>();
+
+        for (HostVersionEntity hostVersion : hostVersions) {
+          if (!hostsInUse.containsKey(hostVersion.getState())) {
+            hostsInUse.put(hostVersion.getState(), new HashSet<String>());
+          }
+
+          hostsInUse.get(hostVersion.getState()).add(hostVersion.getHostName());
+        }
+
+        Set<String> errors = new HashSet<>();
+        for (Entry<RepositoryVersionState, Set<String>> entry : hostsInUse.entrySet()) {
+          errors.add(String.format("%s on %s", entry.getKey(), StringUtils.join(entry.getValue(), ", ")));
         }
+
+
+        throw new SystemException(
+            String.format("Repository version can't be deleted as it is used by the following hosts: %s",
+                StringUtils.join(errors, ';')));
       }
 
       entitiesToBeRemoved.add(entity);
@@ -456,7 +446,7 @@ public class RepositoryVersionResourceProvider extends AbstractAuthorizedResourc
     final String requiredStackId = requiredStack.getStackId();
 
     // List of all repo urls that are already added at stack
-    Set<String> existingRepoUrls = new HashSet<String>();
+    Set<String> existingRepoUrls = new HashSet<>();
     List<RepositoryVersionEntity> existingRepoVersions = dao.findByStack(requiredStack);
     for (RepositoryVersionEntity existingRepoVersion : existingRepoVersions) {
       for (OperatingSystemEntity operatingSystemEntity : existingRepoVersion.getOperatingSystems()) {
@@ -469,12 +459,12 @@ public class RepositoryVersionResourceProvider extends AbstractAuthorizedResourc
     }
 
     // check that repositories contain only supported operating systems
-    final Set<String> osSupported = new HashSet<String>();
+    final Set<String> osSupported = new HashSet<>();
     for (OperatingSystemInfo osInfo: metaInfo.getOperatingSystems(requiredStackName, requiredStackVersion)) {
       osSupported.add(osInfo.getOsType());
     }
 
-    final Set<String> osRepositoryVersion = new HashSet<String>();
+    final Set<String> osRepositoryVersion = new HashSet<>();
 
     for (OperatingSystemEntity os: repositoryVersion.getOperatingSystems()) {
       osRepositoryVersion.add(os.getOsType());

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RootServiceHostComponentResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RootServiceHostComponentResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RootServiceHostComponentResourceProvider.java
index 51aaeec..7617e61 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RootServiceHostComponentResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RootServiceHostComponentResourceProvider.java
@@ -35,14 +35,14 @@ import org.apache.ambari.server.controller.spi.NoSuchResourceException;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.Request;
 import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.controller.spi.Resource.Type;
 import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
-import org.apache.ambari.server.controller.spi.Resource.Type;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 
 public class RootServiceHostComponentResourceProvider extends
     ReadOnlyResourceProvider {
-  
+
   public static final String SERVICE_NAME_PROPERTY_ID = PropertyHelper
       .getPropertyId("RootServiceHostComponents", "service_name");
   public static final String HOST_NAME_PROPERTY_ID = PropertyHelper
@@ -55,9 +55,9 @@ public class RootServiceHostComponentResourceProvider extends
       .getPropertyId("RootServiceHostComponents", "component_state");
   public static final String PROPERTIES_PROPERTY_ID = PropertyHelper
       .getPropertyId("RootServiceHostComponents", "properties");
-  
-  
-  private Set<String> pkPropertyIds = new HashSet<String>(
+
+
+  private Set<String> pkPropertyIds = new HashSet<>(
       Arrays.asList(new String[] { SERVICE_NAME_PROPERTY_ID, HOST_NAME_PROPERTY_ID, COMPONENT_NAME_PROPERTY_ID }));
 
 
@@ -67,13 +67,13 @@ public class RootServiceHostComponentResourceProvider extends
     super(propertyIds, keyPropertyIds, managementController);
   }
 
-  
+
   @Override
   public Set<Resource> getResources(Request request, Predicate predicate)
       throws SystemException, UnsupportedPropertyException,
       NoSuchResourceException, NoSuchParentResourceException {
 
-    final Set<RootServiceHostComponentRequest> requests = new HashSet<RootServiceHostComponentRequest>();
+    final Set<RootServiceHostComponentRequest> requests = new HashSet<>();
 
     if (predicate == null) {
       requests.add(getRequest(Collections.<String, Object>emptyMap()));
@@ -92,26 +92,26 @@ public class RootServiceHostComponentResourceProvider extends
       }
     });
 
-    Set<Resource> resources = new HashSet<Resource>();
+    Set<Resource> resources = new HashSet<>();
 
     for (RootServiceHostComponentResponse response : responses) {
       Resource resource = new ResourceImpl(Resource.Type.RootServiceHostComponent);
 
       setResourceProperty(resource, SERVICE_NAME_PROPERTY_ID,
           response.getServiceName(), requestedIds);
-      
+
       setResourceProperty(resource, HOST_NAME_PROPERTY_ID,
           response.getHostName(), requestedIds);
-      
+
       setResourceProperty(resource, COMPONENT_NAME_PROPERTY_ID,
           response.getComponentName(), requestedIds);
-      
+
       setResourceProperty(resource, COMPONENT_STATE_PROPERTY_ID,
           response.getComponentState(), requestedIds);
-      
+
       setResourceProperty(resource, COMPONENT_VERSION_PROPERTY_ID,
           response.getComponentVersion(), requestedIds);
-      
+
       setResourceProperty(resource, PROPERTIES_PROPERTY_ID,
           response.getProperties(), requestedIds);
 
@@ -120,7 +120,7 @@ public class RootServiceHostComponentResourceProvider extends
 
     return resources;
   }
-  
+
   private RootServiceHostComponentRequest getRequest(Map<String, Object> properties) {
     return new RootServiceHostComponentRequest((String) properties.get(SERVICE_NAME_PROPERTY_ID),
                                                (String) properties.get(HOST_NAME_PROPERTY_ID),
@@ -135,14 +135,10 @@ public class RootServiceHostComponentResourceProvider extends
   // Get the root service host components for the given set of requests
   protected Set<RootServiceHostComponentResponse> getRootServiceHostComponents(
       Set<RootServiceHostComponentRequest> requests) throws AmbariException {
-    Set<RootServiceHostComponentResponse> response = new HashSet<RootServiceHostComponentResponse>();
+    Set<RootServiceHostComponentResponse> response = new HashSet<>();
     for (RootServiceHostComponentRequest request : requests) {
-      String serviceName = request.getServiceName();
       try {
         Set<RootServiceHostComponentResponse> rootServiceHostComponents = getRootServiceHostComponents(request);
-        for (RootServiceHostComponentResponse rootServiceHostComponentResponse : rootServiceHostComponents ) {
-          rootServiceHostComponentResponse.setServiceName(serviceName);
-        }
 
         response.addAll(rootServiceHostComponents);
       } catch (AmbariException e) {
@@ -163,7 +159,7 @@ public class RootServiceHostComponentResourceProvider extends
     AmbariManagementController controller = getManagementController();
     //Get all hosts of all clusters
     Set<HostResponse> hosts = HostResourceProvider.getHosts(controller,
-        new HostRequest(request.getHostName(), null, null));
+        new HostRequest(request.getHostName(), null));
 
     return controller.getRootServiceResponseFactory().getRootServiceHostComponent(request, hosts);
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ScaleClusterRequest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ScaleClusterRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ScaleClusterRequest.java
index 2a91bfe..f960490 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ScaleClusterRequest.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ScaleClusterRequest.java
@@ -105,7 +105,7 @@ public class ScaleClusterRequest extends BaseClusterRequest {
       throw new InvalidTopologyTemplateException("Blueprint name must be specified for all host groups");
     }
 
-    String hgName = String.valueOf(properties.get(HostResourceProvider.HOSTGROUP_PROPERTY_ID));
+    String hgName = String.valueOf(properties.get(HostResourceProvider.HOST_GROUP_PROPERTY_ID));
     if (hgName == null || hgName.equals("null")) {
       throw new InvalidTopologyTemplateException("A name must be specified for all host groups");
     }
@@ -180,8 +180,6 @@ public class ScaleClusterRequest extends BaseClusterRequest {
     String rackInfo = null;
     if (properties.containsKey(HostResourceProvider.HOST_RACK_INFO_PROPERTY_ID)) {
       rackInfo = (String) properties.get(HostResourceProvider.HOST_RACK_INFO_PROPERTY_ID);
-    } else if (properties.containsKey(HostResourceProvider.HOST_RACK_INFO_NO_CATEGORY_PROPERTY_ID)) {
-      rackInfo = (String) properties.get(HostResourceProvider.HOST_RACK_INFO_NO_CATEGORY_PROPERTY_ID);
     } else {
       LOGGER.debug("No rack info provided");
     }
@@ -220,9 +218,7 @@ public class ScaleClusterRequest extends BaseClusterRequest {
   //todo: this was copied exactly from HostResourceProvider
   private String getHostNameFromProperties(Map<String, Object> properties) {
     String hostName = (String) properties.get(HostResourceProvider.HOST_NAME_PROPERTY_ID);
-    if (hostName == null) {
-      hostName = (String) properties.get(HostResourceProvider.HOST_NAME_NO_CATEGORY_PROPERTY_ID);
-    }
+
     return hostName;
   }
 


[12/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
index b2c6b9f..8c209ae 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -17,7 +17,6 @@
  */
 package org.apache.ambari.server.controller.internal;
 
-import static org.easymock.EasyMock.createNiceMock;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.replay;
 import static org.junit.Assert.assertEquals;
@@ -26,9 +25,12 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.lang.reflect.Field;
 import java.sql.SQLException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -46,8 +48,8 @@ import org.apache.ambari.server.actionmanager.ExecutionCommandWrapper;
 import org.apache.ambari.server.actionmanager.HostRoleCommand;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.actionmanager.Stage;
+import org.apache.ambari.server.agent.ExecutionCommand;
 import org.apache.ambari.server.agent.ExecutionCommand.KeyNames;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.audit.AuditLogger;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.AmbariManagementController;
@@ -65,7 +67,6 @@ import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.OrmTestHelper;
 import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.RequestDAO;
@@ -79,6 +80,7 @@ import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.orm.entities.StageEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.orm.entities.UpgradeGroupEntity;
+import org.apache.ambari.server.orm.entities.UpgradeHistoryEntity;
 import org.apache.ambari.server.orm.entities.UpgradeItemEntity;
 import org.apache.ambari.server.security.TestAuthenticationFactory;
 import org.apache.ambari.server.serveraction.upgrades.AutoSkipFailedSummaryAction;
@@ -87,10 +89,9 @@ import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.ConfigFactory;
 import org.apache.ambari.server.state.ConfigHelper;
-import org.apache.ambari.server.state.DesiredConfig;
 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.RepositoryType;
 import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentHost;
@@ -98,20 +99,24 @@ import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.UpgradeContext;
 import org.apache.ambari.server.state.UpgradeHelper;
 import org.apache.ambari.server.state.UpgradeState;
-import org.apache.ambari.server.state.stack.UpgradePack;
 import org.apache.ambari.server.state.stack.upgrade.Direction;
 import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
 import org.apache.ambari.server.topology.TopologyManager;
 import org.apache.ambari.server.utils.StageUtils;
 import org.apache.ambari.server.view.ViewRegistry;
-import org.easymock.Capture;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.easymock.EasyMock;
+import org.easymock.EasyMockSupport;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.springframework.security.core.context.SecurityContextHolder;
 
+import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
 import com.google.common.collect.Lists;
 import com.google.gson.Gson;
 import com.google.gson.JsonArray;
@@ -126,26 +131,31 @@ import com.google.inject.util.Modules;
 /**
  * UpgradeResourceDefinition tests.
  */
-public class UpgradeResourceProviderTest {
+public class UpgradeResourceProviderTest extends EasyMockSupport {
 
   private UpgradeDAO upgradeDao = null;
   private RequestDAO requestDao = null;
   private RepositoryVersionDAO repoVersionDao = null;
   private Injector injector;
   private Clusters clusters;
-  private OrmTestHelper helper;
   private AmbariManagementController amc;
   private ConfigHelper configHelper;
   private StackDAO stackDAO;
-  private AmbariMetaInfo ambariMetaInfo;
   private TopologyManager topologyManager;
   private ConfigFactory configFactory;
   private HostRoleCommandDAO hrcDAO;
 
   RepositoryVersionEntity repoVersionEntity2110;
   RepositoryVersionEntity repoVersionEntity2111;
+  RepositoryVersionEntity repoVersionEntity2112;
   RepositoryVersionEntity repoVersionEntity2200;
 
+  /**
+   * Creates a single host cluster with ZOOKEEPER_SERVER and ZOOKEEPER_CLIENT on
+   * {@link #repoVersionEntity2110}.
+   *
+   * @throws Exception
+   */
   @Before
   public void before() throws Exception {
     SecurityContextHolder.getContext().setAuthentication(
@@ -161,25 +171,21 @@ public class UpgradeResourceProviderTest {
 
     expect(
         configHelper.getDefaultProperties(EasyMock.anyObject(StackId.class),
-            EasyMock.anyObject(Cluster.class))).andReturn(
+            EasyMock.anyString())).andReturn(
         new HashMap<String, Map<String, String>>()).anyTimes();
 
-
-    EasyMock.replay(configHelper);
+    replay(configHelper);
 
     InMemoryDefaultTestModule module = new InMemoryDefaultTestModule();
 
     // create an injector which will inject the mocks
-    injector = Guice.createInjector(Modules.override(module).with(new MockModule()));
+    injector = Guice.createInjector(
+        Modules.override(module).with(new MockModule()));
 
     H2DatabaseCleaner.resetSequences(injector);
     injector.getInstance(GuiceJpaInitializer.class);
 
-
-    helper = injector.getInstance(OrmTestHelper.class);
-
     amc = injector.getInstance(AmbariManagementController.class);
-    ambariMetaInfo = injector.getInstance(AmbariMetaInfo.class);
     configFactory = injector.getInstance(ConfigFactory.class);
 
     Field field = AmbariServer.class.getDeclaredField("clusterController");
@@ -192,7 +198,7 @@ public class UpgradeResourceProviderTest {
     repoVersionDao = injector.getInstance(RepositoryVersionDAO.class);
     hrcDAO = injector.getInstance(HostRoleCommandDAO.class);
 
-    AmbariEventPublisher publisher = createNiceMock(AmbariEventPublisher.class);
+    AmbariEventPublisher publisher = EasyMock.createNiceMock(AmbariEventPublisher.class);
     replay(publisher);
     ViewRegistry.initInstance(new ViewRegistry(publisher));
 
@@ -201,9 +207,7 @@ public class UpgradeResourceProviderTest {
     // For now, Ignore the tests that fail.
     StackEntity stackEntity211 = stackDAO.find("HDP", "2.1.1");
     StackEntity stackEntity220 = stackDAO.find("HDP", "2.2.0");
-    StackId stack211 = new StackId("HDP-2.1.1");
-    StackId stack220 = new StackId("HDP-2.2.0");
-
+    StackId stack211 = new StackId(stackEntity211);
 
     repoVersionEntity2110 = new RepositoryVersionEntity();
     repoVersionEntity2110.setDisplayName("My New Version 1");
@@ -213,14 +217,23 @@ public class UpgradeResourceProviderTest {
     repoVersionDao.create(repoVersionEntity2110);
 
     repoVersionEntity2111 = new RepositoryVersionEntity();
-    repoVersionEntity2111.setDisplayName("My New Version 2 for patch upgrade");
+    repoVersionEntity2111.setDisplayName("My New Version 2 for minor upgrade");
     repoVersionEntity2111.setOperatingSystems("");
     repoVersionEntity2111.setStack(stackEntity211);
     repoVersionEntity2111.setVersion("2.1.1.1");
     repoVersionDao.create(repoVersionEntity2111);
 
+    repoVersionEntity2112 = new RepositoryVersionEntity();
+    repoVersionEntity2112.setDisplayName("My New Version 3 for patch upgrade");
+    repoVersionEntity2112.setOperatingSystems("");
+    repoVersionEntity2112.setStack(stackEntity211);
+    repoVersionEntity2112.setVersion("2.1.1.2");
+    repoVersionEntity2112.setType(RepositoryType.PATCH);
+    repoVersionEntity2112.setVersionXml("");
+    repoVersionDao.create(repoVersionEntity2112);
+
     repoVersionEntity2200 = new RepositoryVersionEntity();
-    repoVersionEntity2200.setDisplayName("My New Version 3 for major upgrade");
+    repoVersionEntity2200.setDisplayName("My New Version 4 for major upgrade");
     repoVersionEntity2200.setOperatingSystems("");
     repoVersionEntity2200.setStack(stackEntity220);
     repoVersionEntity2200.setVersion("2.2.0.0");
@@ -231,12 +244,6 @@ public class UpgradeResourceProviderTest {
     clusters.addCluster("c1", stack211);
     Cluster cluster = clusters.getCluster("c1");
 
-    helper.getOrCreateRepositoryVersion(stack211, stack211.getStackVersion());
-    helper.getOrCreateRepositoryVersion(stack220, stack220.getStackVersion());
-
-    cluster.createClusterVersion(stack211, stack211.getStackVersion(), "admin", RepositoryVersionState.INSTALLING);
-    cluster.transitionClusterVersion(stack211, stack211.getStackVersion(), RepositoryVersionState.CURRENT);
-
     clusters.addHost("h1");
     Host host = clusters.getHost("h1");
     Map<String, String> hostAttributes = new HashMap<>();
@@ -247,10 +254,8 @@ public class UpgradeResourceProviderTest {
 
     clusters.mapHostToCluster("h1", "c1");
 
-    // add a single ZK server
-    Service service = cluster.addService("ZOOKEEPER");
-    service.setDesiredStackVersion(cluster.getDesiredStackVersion());
-
+    // add a single ZK server and client on 2.1.1.0
+    Service service = cluster.addService("ZOOKEEPER", repoVersionEntity2110);
     ServiceComponent component = service.addServiceComponent("ZOOKEEPER_SERVER");
     ServiceComponentHost sch = component.addServiceComponentHost("h1");
     sch.setVersion("2.1.1.0");
@@ -294,7 +299,7 @@ public class UpgradeResourceProviderTest {
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_TYPE, UpgradeType.ROLLING.toString());
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_FAILURES, Boolean.TRUE.toString());
@@ -357,7 +362,7 @@ public class UpgradeResourceProviderTest {
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_TYPE, UpgradeType.ROLLING.toString());
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_MANUAL_VERIFICATION, Boolean.TRUE.toString());
@@ -401,7 +406,7 @@ public class UpgradeResourceProviderTest {
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_TYPE, UpgradeType.ROLLING.toString());
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_FAILURES, Boolean.TRUE.toString());
@@ -558,7 +563,7 @@ public class UpgradeResourceProviderTest {
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.1.1.1");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2111.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
     requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name());
@@ -606,11 +611,6 @@ public class UpgradeResourceProviderTest {
     Cluster cluster = clusters.getCluster("c1");
     Service service = cluster.getService("ZOOKEEPER");
 
-    // this should get skipped
-    ServiceComponent component = service.getServiceComponent("ZOOKEEPER_SERVER");
-    ServiceComponentHost sch = component.addServiceComponentHost("h2");
-    sch.setVersion(repoVersionEntity2200.getVersion());
-
     // start out with 0 (sanity check)
     List<UpgradeEntity> upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
     assertEquals(0, upgrades.size());
@@ -620,29 +620,48 @@ public class UpgradeResourceProviderTest {
     RequestEntity requestEntity = new RequestEntity();
     requestEntity.setRequestId(2L);
     requestEntity.setClusterId(cluster.getClusterId());
-    requestEntity.setStatus(HostRoleStatus.PENDING);
     requestEntity.setStages(new ArrayList<StageEntity>());
     requestDao.create(requestEntity);
 
     UpgradeEntity upgradeEntity = new UpgradeEntity();
     upgradeEntity.setClusterId(cluster.getClusterId());
     upgradeEntity.setDirection(Direction.UPGRADE);
-    upgradeEntity.setFromRepositoryVersion(repoVersionEntity2111);
-    upgradeEntity.setToRepositoryVersion(repoVersionEntity2200);
+    upgradeEntity.setRepositoryVersion(repoVersionEntity2200);
     upgradeEntity.setUpgradePackage("upgrade_test");
     upgradeEntity.setUpgradeType(UpgradeType.ROLLING);
     upgradeEntity.setRequestEntity(requestEntity);
 
+    UpgradeHistoryEntity history = new UpgradeHistoryEntity();
+    history.setUpgrade(upgradeEntity);
+    history.setFromRepositoryVersion(service.getDesiredRepositoryVersion());
+    history.setTargetRepositoryVersion(repoVersionEntity2200);
+    history.setServiceName(service.getName());
+    history.setComponentName("ZOKKEEPER_SERVER");
+    upgradeEntity.addHistory(history);
+
+    history = new UpgradeHistoryEntity();
+    history.setUpgrade(upgradeEntity);
+    history.setFromRepositoryVersion(service.getDesiredRepositoryVersion());
+    history.setTargetRepositoryVersion(repoVersionEntity2200);
+    history.setServiceName(service.getName());
+    history.setComponentName("ZOKKEEPER_CLIENT");
+    upgradeEntity.addHistory(history);
+
     upgradeDao.create(upgradeEntity);
     upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
     assertEquals(1, upgrades.size());
 
+    // push a ZK server foward to the new repo version, leaving the old one on
+    // the old version
+    ServiceComponent component = service.getServiceComponent("ZOOKEEPER_SERVER");
+    ServiceComponentHost sch = component.addServiceComponentHost("h2");
+    sch.setVersion(repoVersionEntity2200.getVersion());
+
     UpgradeEntity lastUpgrade = upgradeDao.findLastUpgradeForCluster(cluster.getClusterId(), Direction.UPGRADE);
     assertNotNull(lastUpgrade);
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_ID, String.valueOf(repoVersionEntity2111.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
     requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.DOWNGRADE.name());
@@ -663,10 +682,11 @@ public class UpgradeResourceProviderTest {
     List<UpgradeGroupEntity> upgradeGroups = downgrade.getUpgradeGroups();
     assertEquals(3, upgradeGroups.size());
 
+    // the ZK restart group should only have 3 entries since the ZK server on h1
+    // didn't get upgraded
     UpgradeGroupEntity group = upgradeGroups.get(1);
     assertEquals("ZOOKEEPER", group.getName());
-    assertEquals(4, group.getItems().size());
-
+    assertEquals(3, group.getItems().size());
   }
 
 
@@ -676,7 +696,7 @@ public class UpgradeResourceProviderTest {
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.1.1.1");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2111.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
     requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name());
@@ -697,7 +717,7 @@ public class UpgradeResourceProviderTest {
     // create another upgrade which should fail
     requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, "9999");
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
     request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
@@ -710,10 +730,8 @@ public class UpgradeResourceProviderTest {
 
     // fix the properties and try again
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.1.1.0");
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_FROM_VERSION, "2.1.1.1");
     requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.DOWNGRADE.name());
 
     Map<String, String> requestInfoProperties = new HashMap<>();
@@ -726,16 +744,21 @@ public class UpgradeResourceProviderTest {
 
     UpgradeEntity entity = upgradeDao.findUpgrade(Long.parseLong(id));
     assertNotNull(entity);
-    assertEquals("2.1.1.1", entity.getFromRepositoryVersion().getVersion());
-    assertEquals("2.1.1.0", entity.getToRepositoryVersion().getVersion());
     assertEquals(Direction.DOWNGRADE, entity.getDirection());
 
+    // associated version is the FROM on DOWNGRADE
+    assertEquals(repoVersionEntity2111.getVersion(), entity.getRepositoryVersion().getVersion());
+
+    // target is by service
+    assertEquals(repoVersionEntity2110.getVersion(),
+        entity.getHistory().iterator().next().getTargetVersion());
+
     StageDAO dao = injector.getInstance(StageDAO.class);
     List<StageEntity> stages = dao.findByRequestId(entity.getRequestId());
 
     Gson gson = new Gson();
     for (StageEntity se : stages) {
-      Map<String, String> map = gson.fromJson(se.getCommandParamsStage(), Map.class);
+      Map<String, String> map = gson.<Map<String, String>>fromJson(se.getCommandParamsStage(), Map.class);
       assertTrue(map.containsKey("upgrade_direction"));
       assertEquals("downgrade", map.get("upgrade_direction"));
     }
@@ -752,8 +775,7 @@ public class UpgradeResourceProviderTest {
     Cluster cluster = clusters.getCluster("c1");
 
     // add additional service for the test
-    Service service = cluster.addService("HIVE");
-    service.setDesiredStackVersion(cluster.getDesiredStackVersion());
+    Service service = cluster.addService("HIVE", repoVersionEntity2110);
 
     ServiceComponent component = service.addServiceComponent("HIVE_SERVER");
     ServiceComponentHost sch = component.addServiceComponentHost("h1");
@@ -762,7 +784,7 @@ public class UpgradeResourceProviderTest {
     // create upgrade request
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_nonrolling_new_stack");
     requestProps.put(UpgradeResourceProvider.UPGRADE_TYPE, "NON_ROLLING");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
@@ -802,14 +824,11 @@ public class UpgradeResourceProviderTest {
     abortUpgrade(upgrade.getRequestId());
 
     // create downgrade with one upgraded service
-    StackId stackId = new StackId("HDP", "2.2.0");
-    cluster.setDesiredStackVersion(stackId, true);
+    service.setDesiredRepositoryVersion(repoVersionEntity2200);
 
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.1.1.0");
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_nonrolling_new_stack");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_FROM_VERSION, "2.2.0.0");
     requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.DOWNGRADE.name());
 
     Map<String, String> requestInfoProperties = new HashMap<>();
@@ -938,6 +957,7 @@ public class UpgradeResourceProviderTest {
     Cluster cluster = clusters.getCluster("c1");
 
     StackEntity stackEntity = stackDAO.find("HDP", "2.1.1");
+
     RepositoryVersionEntity repoVersionEntity = new RepositoryVersionEntity();
     repoVersionEntity.setDisplayName("My New Version 3");
     repoVersionEntity.setOperatingSystems("");
@@ -947,7 +967,7 @@ public class UpgradeResourceProviderTest {
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.2.3");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_direction");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
     requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name());
@@ -977,10 +997,8 @@ public class UpgradeResourceProviderTest {
     requestProps.clear();
     // Now perform a downgrade
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_direction");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_FROM_VERSION, "2.2.2.3");
     requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.DOWNGRADE.name());
 
     request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
@@ -996,8 +1014,9 @@ public class UpgradeResourceProviderTest {
       }
     }
     assertNotNull(upgrade);
-    assertEquals("Downgrade groups reduced from 3 to 2", 2, upgrade.getUpgradeGroups().size());
-    group = upgrade.getUpgradeGroups().get(1);
+    List<UpgradeGroupEntity> groups = upgrade.getUpgradeGroups();
+    assertEquals("Downgrade groups reduced from 3 to 2", 1, groups.size());
+    group = upgrade.getUpgradeGroups().get(0);
     assertEquals("Execution items increased from 1 to 2", 2, group.getItems().size());
   }
 
@@ -1055,16 +1074,16 @@ public class UpgradeResourceProviderTest {
   @Test
   public void testCreateCrossStackUpgrade() throws Exception {
     Cluster cluster = clusters.getCluster("c1");
-    StackId oldStack = cluster.getDesiredStackVersion();
+    StackId oldStack = repoVersionEntity2110.getStackId();
 
     for (Service s : cluster.getServices().values()) {
-      assertEquals(oldStack, s.getDesiredStackVersion());
+      assertEquals(oldStack, s.getDesiredStackId());
 
       for (ServiceComponent sc : s.getServiceComponents().values()) {
-        assertEquals(oldStack, sc.getDesiredStackVersion());
+        assertEquals(oldStack, sc.getDesiredStackId());
 
         for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
-          assertEquals(oldStack, sch.getDesiredStackVersion());
+          assertEquals(repoVersionEntity2110.getVersion(), sch.getVersion());
         }
       }
     }
@@ -1073,15 +1092,15 @@ public class UpgradeResourceProviderTest {
     Config config = configFactory.createNew(cluster, "zoo.cfg", "abcdefg", Collections.singletonMap("a", "b"), null);
     cluster.addDesiredConfig("admin", Collections.singleton(config));
 
+    // create the upgrade across major versions
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
     requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name());
 
     ResourceProvider upgradeResourceProvider = createProvider(amc);
-
     Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
     upgradeResourceProvider.createResources(request);
 
@@ -1099,144 +1118,16 @@ public class UpgradeResourceProviderTest {
 
     assertTrue(cluster.getDesiredConfigs().containsKey("zoo.cfg"));
 
-    StackId newStack = cluster.getDesiredStackVersion();
-
-    assertFalse(oldStack.equals(newStack));
-
     for (Service s : cluster.getServices().values()) {
-      assertEquals(newStack, s.getDesiredStackVersion());
+      assertEquals(repoVersionEntity2200, s.getDesiredRepositoryVersion());
 
       for (ServiceComponent sc : s.getServiceComponents().values()) {
-        assertEquals(newStack, sc.getDesiredStackVersion());
-
-        for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
-          assertEquals(newStack, sch.getDesiredStackVersion());
-        }
+        assertEquals(repoVersionEntity2200, sc.getDesiredRepositoryVersion());
       }
     }
   }
 
   /**
-   * Tests merging configurations between existing and new stack values on
-   * upgrade.
-   *
-   * @throws Exception
-   */
-  @Test
-  public void testMergeConfigurations() throws Exception {
-    StackId stack211 = new StackId("HDP-2.1.1");
-    StackId stack220 = new StackId("HDP-2.2.0");
-
-    Map<String, Map<String, String>> stack211Configs = new HashMap<>();
-    Map<String, String> stack211FooType = new HashMap<>();
-    Map<String, String> stack211BarType = new HashMap<>();
-    Map<String, String> stack211BazType = new HashMap<>();
-    stack211Configs.put("foo-site", stack211FooType);
-    stack211Configs.put("bar-site", stack211BarType);
-    stack211Configs.put("baz-site", stack211BazType);
-    stack211FooType.put("1", "one");
-    stack211FooType.put("11", "one-one");
-    stack211BarType.put("2", "two");
-    stack211BazType.put("3", "three");
-
-    Map<String, Map<String, String>> stack220Configs = new HashMap<>();
-    Map<String, String> stack220FooType = new HashMap<>();
-    Map<String, String> stack220BazType = new HashMap<>();
-    Map<String, String> stack220FlumeEnvType = new HashMap<>();
-    stack220Configs.put("foo-site", stack220FooType);
-    stack220Configs.put("baz-site", stack220BazType);
-    stack220Configs.put("flume-env", stack220FlumeEnvType);
-    stack220FooType.put("1", "one-new");
-    stack220FooType.put("111", "one-one-one");
-    stack220BazType.put("3", "three-new");
-    stack220FlumeEnvType.put("flume_env_key", "flume-env-value");
-
-    Map<String, String> clusterFooType = new HashMap<>();
-    Map<String, String> clusterBarType = new HashMap<>();
-    Map<String, String> clusterBazType = new HashMap<>();
-
-    Config fooConfig = EasyMock.createNiceMock(Config.class);
-    Config barConfig = EasyMock.createNiceMock(Config.class);
-    Config bazConfig = EasyMock.createNiceMock(Config.class);
-
-    clusterFooType.put("1", "one");
-    clusterFooType.put("11", "one-one");
-    clusterBarType.put("2", "two");
-    clusterBazType.put("3", "three-changed");
-
-    expect(fooConfig.getProperties()).andReturn(clusterFooType);
-    expect(barConfig.getProperties()).andReturn(clusterBarType);
-    expect(bazConfig.getProperties()).andReturn(clusterBazType);
-
-    Map<String, DesiredConfig> desiredConfigurations = new HashMap<>();
-    desiredConfigurations.put("foo-site", null);
-    desiredConfigurations.put("bar-site", null);
-    desiredConfigurations.put("baz-site", null);
-
-    Cluster cluster = EasyMock.createNiceMock(Cluster.class);
-    expect(cluster.getCurrentStackVersion()).andReturn(stack211);
-    expect(cluster.getDesiredStackVersion()).andReturn(stack220);
-    expect(cluster.getDesiredConfigs()).andReturn(desiredConfigurations);
-    expect(cluster.getDesiredConfigByType("foo-site")).andReturn(fooConfig);
-    expect(cluster.getDesiredConfigByType("bar-site")).andReturn(barConfig);
-    expect(cluster.getDesiredConfigByType("baz-site")).andReturn(bazConfig);
-
-    // setup the config helper for placeholder resolution
-    EasyMock.reset(configHelper);
-
-    expect(
-        configHelper.getDefaultProperties(EasyMock.eq(stack211), EasyMock.anyObject(Cluster.class))).andReturn(
-        stack211Configs).anyTimes();
-
-    expect(
-        configHelper.getDefaultProperties(EasyMock.eq(stack220), EasyMock.anyObject(Cluster.class))).andReturn(
-        stack220Configs).anyTimes();
-
-    Capture<Map<String, Map<String, String>>> expectedConfigurationsCapture = new Capture<>();
-
-    configHelper.createConfigTypes(EasyMock.anyObject(Cluster.class),
-        EasyMock.anyObject(AmbariManagementController.class),
-        EasyMock.capture(expectedConfigurationsCapture),
-        EasyMock.anyObject(String.class), EasyMock.anyObject(String.class));
-
-    EasyMock.expectLastCall().once();
-
-    Map<String, UpgradePack> upgradePacks = ambariMetaInfo.getUpgradePacks("HDP", "2.1.1");
-    UpgradePack upgradePack = upgradePacks.get("upgrade_to_new_stack");
-
-    UpgradeContext context = EasyMock.createNiceMock(UpgradeContext.class);
-    expect(context.getCluster()).andReturn(cluster).atLeastOnce();
-    expect(context.getType()).andReturn(UpgradeType.ROLLING).atLeastOnce();
-    expect(context.getDirection()).andReturn(Direction.UPGRADE).atLeastOnce();
-    expect(context.getSourceRepositoryVersion()).andReturn(repoVersionEntity2111).atLeastOnce();
-    expect(context.getTargetRepositoryVersion()).andReturn(repoVersionEntity2200).atLeastOnce();
-    expect(context.getUpgradePack()).andReturn(upgradePack).atLeastOnce();
-
-    EasyMock.replay(configHelper, cluster, fooConfig, barConfig, bazConfig, context);
-
-    UpgradeResourceProvider upgradeResourceProvider = createProvider(amc);
-
-    upgradeResourceProvider.applyStackAndProcessConfigurations(context);
-
-    Map<String, Map<String, String>> expectedConfigurations = expectedConfigurationsCapture.getValue();
-    Map<String, String> expectedFooType = expectedConfigurations.get("foo-site");
-    Map<String, String> expectedBarType = expectedConfigurations.get("bar-site");
-    Map<String, String> expectedBazType = expectedConfigurations.get("baz-site");
-
-    // As the upgrade pack did not have any Flume updates, its configs should not be updated.
-    assertFalse(expectedConfigurations.containsKey("flume-env"));
-
-    // the really important values are one-new and three-changed; one-new
-    // indicates that the new stack value is changed since it was not customized
-    // while three-changed represents that the customized value was preserved
-    // even though the stack value changed
-    assertEquals("one-new", expectedFooType.get("1"));
-    assertEquals("one-one", expectedFooType.get("11"));
-    assertEquals("two", expectedBarType.get("2"));
-    assertEquals("three-changed", expectedBazType.get("3"));
-  }
-
-  /**
    * @param amc
    * @return the provider
    */
@@ -1259,7 +1150,7 @@ public class UpgradeResourceProviderTest {
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.1.1.1");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2111.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
     requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name());
@@ -1364,7 +1255,7 @@ public class UpgradeResourceProviderTest {
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_FAILURES, Boolean.TRUE.toString());
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_SC_FAILURES, Boolean.FALSE.toString());
     requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_ID, "" + entity.getRequestId());
@@ -1389,7 +1280,7 @@ public class UpgradeResourceProviderTest {
 
     requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_FAILURES, Boolean.FALSE.toString());
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_SC_FAILURES, Boolean.TRUE.toString());
     requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_ID, "" + entity.getRequestId());
@@ -1413,7 +1304,7 @@ public class UpgradeResourceProviderTest {
 
     requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_FAILURES, Boolean.FALSE.toString());
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_SC_FAILURES, Boolean.FALSE.toString());
     requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_ID, "" + entity.getRequestId());
@@ -1439,7 +1330,7 @@ public class UpgradeResourceProviderTest {
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_TYPE, UpgradeType.ROLLING.toString());
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_MANUAL_VERIFICATION, Boolean.FALSE.toString());
@@ -1477,7 +1368,7 @@ public class UpgradeResourceProviderTest {
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test_host_ordered");
     requestProps.put(UpgradeResourceProvider.UPGRADE_TYPE, UpgradeType.HOST_ORDERED.toString());
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, Boolean.TRUE.toString());
@@ -1520,15 +1411,15 @@ public class UpgradeResourceProviderTest {
 
   /**
    * Exercises that a component that goes from upgrade->downgrade that switches
-   * {@code versionAdvertised} between will go to UKNOWN.  This exercises
-   * {@link UpgradeHelper#putComponentsToUpgradingState(String, Map, StackId)}
+   * {@code versionAdvertised} between will go to UKNOWN. This exercises
+   * {@link UpgradeHelper#updateDesiredRepositoriesAndConfigs(UpgradeContext)}
+   *
    * @throws Exception
    */
   @Test
   public void testCreateUpgradeDowngradeCycleAdvertisingVersion() throws Exception {
     Cluster cluster = clusters.getCluster("c1");
-    Service service = cluster.addService("STORM");
-    service.setDesiredStackVersion(cluster.getDesiredStackVersion());
+    Service service = cluster.addService("STORM", repoVersionEntity2110);
 
     ServiceComponent component = service.addServiceComponent("DRPC_SERVER");
     ServiceComponentHost sch = component.addServiceComponentHost("h1");
@@ -1538,10 +1429,9 @@ public class UpgradeResourceProviderTest {
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_FROM_VERSION, "2.1.1.0");
     requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name());
 
     Map<String, String> requestInfoProperties = new HashMap<>();
@@ -1566,10 +1456,8 @@ public class UpgradeResourceProviderTest {
 
     requestProps.clear();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.1.1.0");
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_FROM_VERSION, "2.2.0.0");
     requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.DOWNGRADE.name());
 
     request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), requestInfoProperties);
@@ -1577,7 +1465,7 @@ public class UpgradeResourceProviderTest {
 
     component = service.getServiceComponent("DRPC_SERVER");
     assertNotNull(component);
-    assertEquals("UNKNOWN", component.getDesiredVersion());
+    assertEquals(repoVersionEntity2110, component.getDesiredRepositoryVersion());
 
     hostComponent = component.getServiceComponentHost("h1");
     assertEquals(UpgradeState.NONE, hostComponent.getUpgradeState());
@@ -1619,7 +1507,7 @@ public class UpgradeResourceProviderTest {
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "2.2.0.0");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test_host_ordered");
     requestProps.put(UpgradeResourceProvider.UPGRADE_TYPE, UpgradeType.HOST_ORDERED.toString());
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS,Boolean.TRUE.toString());
@@ -1639,6 +1527,144 @@ public class UpgradeResourceProviderTest {
     }
   }
 
+  /**
+   * Tests that from/to repository version history is created correctly on the
+   * upgrade.
+   *
+   * @throws Exception
+   */
+  @Test
+  public void testUpgradeHistory() throws Exception {
+    Cluster cluster = clusters.getCluster("c1");
+
+    Map<String, Object> requestProps = new HashMap<>();
+    requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
+    requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_TYPE, UpgradeType.ROLLING.toString());
+    requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_MANUAL_VERIFICATION, Boolean.TRUE.toString());
+    requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, Boolean.TRUE.toString());
+    requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name());
+
+    ResourceProvider upgradeResourceProvider = createProvider(amc);
+    Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
+    upgradeResourceProvider.createResources(request);
+
+    List<UpgradeEntity> upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
+    assertEquals(1, upgrades.size());
+
+    UpgradeEntity upgrade = cluster.getUpgradeInProgress();
+    List<UpgradeHistoryEntity> histories = upgrade.getHistory();
+    assertEquals(2, histories.size());
+
+    for( UpgradeHistoryEntity history : histories){
+      assertEquals( "ZOOKEEPER", history.getServiceName() );
+      assertEquals(repoVersionEntity2110, history.getFromReposistoryVersion());
+      assertEquals(repoVersionEntity2200, history.getTargetRepositoryVersion());
+    }
+
+    // abort the upgrade and create the downgrade
+    abortUpgrade(upgrade.getRequestId());
+
+    requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_nonrolling_new_stack");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.DOWNGRADE.name());
+
+    Map<String, String> requestInfoProperties = new HashMap<>();
+
+    request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps),
+        requestInfoProperties);
+    RequestStatus status = upgradeResourceProvider.createResources(request);
+    UpgradeEntity downgrade = upgradeDao.findUpgradeByRequestId(getRequestId(status));
+    assertEquals(Direction.DOWNGRADE, downgrade.getDirection());
+
+    // check from/to history
+    histories = downgrade.getHistory();
+    assertEquals(2, histories.size());
+
+    for (UpgradeHistoryEntity history : histories) {
+      assertEquals("ZOOKEEPER", history.getServiceName());
+      assertEquals(repoVersionEntity2200, history.getFromReposistoryVersion());
+      assertEquals(repoVersionEntity2110, history.getTargetRepositoryVersion());
+    }
+  }
+
+
+  /**
+   * Tests that from/to repository version history is created correctly on the
+   * upgrade.
+   *
+   * @throws Exception
+   */
+  @Test
+  public void testCreatePatchRevertUpgrade() throws Exception {
+    Cluster cluster = clusters.getCluster("c1");
+
+    File f = new File("src/test/resources/hbase_version_test.xml");
+    repoVersionEntity2112.setVersionXml(IOUtils.toString(new FileInputStream(f)));
+    repoVersionEntity2112.setVersionXsd("version_definition.xsd");
+    repoVersionDao.merge(repoVersionEntity2112);
+
+    List<UpgradeEntity> upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
+    assertEquals(0, upgrades.size());
+
+    Map<String, Object> requestProps = new HashMap<>();
+    requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2112.getId()));
+    requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name());
+
+    ResourceProvider upgradeResourceProvider = createProvider(amc);
+
+    Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
+    upgradeResourceProvider.createResources(request);
+
+    upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
+    assertEquals(1, upgrades.size());
+    UpgradeEntity upgradeEntity = upgrades.get(0);
+    assertEquals(RepositoryType.PATCH, upgradeEntity.getOrchestration());
+
+    // !!! make it look like the cluster is done
+    cluster.setUpgradeEntity(null);
+
+    requestProps = new HashMap<>();
+    requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REVERT_UPGRADE_ID, upgradeEntity.getId());
+    requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, Boolean.TRUE.toString());
+
+    request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
+    upgradeResourceProvider.createResources(request);
+
+    upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
+    assertEquals(2, upgrades.size());
+
+    boolean found = false;
+
+    Function<UpgradeHistoryEntity, String> function = new Function<UpgradeHistoryEntity, String>() {
+      @Override
+      public String apply(UpgradeHistoryEntity input) {
+        return input.getServiceName() + "/" + input.getComponentName();
+      };
+    };
+
+    for (UpgradeEntity upgrade : upgrades) {
+      if (upgrade.getId() != upgradeEntity.getId()) {
+        found = true;
+        assertEquals(upgradeEntity.getOrchestration(), upgrade.getOrchestration());
+
+        Collection<String> upgradeEntityStrings = Collections2.transform(upgradeEntity.getHistory(), function);
+        Collection<String> upgradeStrings = Collections2.transform(upgrade.getHistory(), function);
+
+        Collection<?> diff = CollectionUtils.disjunction(upgradeEntityStrings, upgradeStrings);
+        assertEquals("Verify the same set of components was orchestrated", 0, diff.size());
+      }
+    }
+
+    assertTrue(found);
+  }
+
   private String parseSingleMessage(String msgStr){
     JsonParser parser = new JsonParser();
     JsonArray msgArray = (JsonArray) parser.parse(msgStr);
@@ -1674,14 +1700,17 @@ public class UpgradeResourceProviderTest {
 
   @Test
   public void testTimeouts() throws Exception {
-    Cluster cluster = clusters.getCluster("c1");
-
-    cluster.createClusterVersion(repoVersionEntity2111.getStackId(),
-        repoVersionEntity2111.getVersion(), "admin", RepositoryVersionState.INSTALLED);
+    StackEntity stackEntity = stackDAO.find("HDP", "2.1.1");
+    RepositoryVersionEntity repoVersionEntity = new RepositoryVersionEntity();
+    repoVersionEntity.setDisplayName("My New Version 3");
+    repoVersionEntity.setOperatingSystems("");
+    repoVersionEntity.setStack(stackEntity);
+    repoVersionEntity.setVersion("2.2.2.3");
+    repoVersionDao.create(repoVersionEntity);
 
     Map<String, Object> requestProps = new HashMap<>();
     requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
-    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, repoVersionEntity2111.getVersion());
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity.getId()));
     requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
     requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
     requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name());
@@ -1722,6 +1751,57 @@ public class UpgradeResourceProviderTest {
   }
 
   /**
+   * Tests that commands created for {@link org.apache.ambari.server.state.stack.upgrade.StageWrapper.Type#UPGRADE_TASKS} set the
+   * service and component on the {@link ExecutionCommand}.
+   * <p/>
+   * Without this, commands of this type would not be able to determine which
+   * service/component repository they should use when the command is scheduled
+   * to run.
+   *
+   * @throws Exception
+   */
+  @Test
+  public void testExecutionCommandServiceAndComponent() throws Exception {
+    Map<String, Object> requestProps = new HashMap<>();
+    requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, String.valueOf(repoVersionEntity2200.getId()));
+    requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_execute_task_test");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
+    requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name());
+
+    ResourceProvider upgradeResourceProvider = createProvider(amc);
+
+    Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
+    RequestStatus status = upgradeResourceProvider.createResources(request);
+
+    Set<Resource> createdResources = status.getAssociatedResources();
+    assertEquals(1, createdResources.size());
+    Resource res = createdResources.iterator().next();
+    Long id = (Long) res.getPropertyValue("Upgrade/request_id");
+    assertNotNull(id);
+    assertEquals(Long.valueOf(1), id);
+
+
+    ActionManager am = injector.getInstance(ActionManager.class);
+    List<HostRoleCommand> commands = am.getRequestTasks(id);
+
+    boolean foundActionExecuteCommand = false;
+    for (HostRoleCommand command : commands) {
+      ExecutionCommand executionCommand = command.getExecutionCommandWrapper().getExecutionCommand();
+      if (StringUtils.equals(UpgradeResourceProvider.EXECUTE_TASK_ROLE,
+          executionCommand.getRole())) {
+        foundActionExecuteCommand = true;
+        assertNotNull(executionCommand.getServiceName());
+        assertNotNull(executionCommand.getComponentName());
+      }
+    }
+
+    assertTrue(
+        "There was no task found with the role of " + UpgradeResourceProvider.EXECUTE_TASK_ROLE,
+        foundActionExecuteCommand);
+  }
+
+  /**
    *
    */
   private class MockModule implements Module {

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListenerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListenerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListenerTest.java
index 978080a..076190a 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListenerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListenerTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -23,7 +23,6 @@ import static org.junit.Assert.assertEquals;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -42,7 +41,6 @@ import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.Cluster;
@@ -71,6 +69,8 @@ public class HostVersionOutOfSyncListenerTest {
   private final String stackId = "HDP-2.2.0";
   private final String yetAnotherStackId = "HDP-2.1.1";
 
+  private final String CURRENT_VERSION = "2.2.0-2086";
+
   private Injector injector;
 
   @Inject
@@ -99,13 +99,14 @@ public class HostVersionOutOfSyncListenerTest {
     injector.injectMembers(this);
 
     StackId stackId = new StackId(this.stackId);
+
+    helper.createStack(stackId);
+
     clusters.addCluster("c1", stackId);
     c1 = clusters.getCluster("c1");
     addHost("h1");
 
     helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
-    c1.createClusterVersion(stackId, stackId.getStackVersion(), "admin", RepositoryVersionState.INSTALLING);
-    c1.transitionClusterVersion(stackId, stackId.getStackVersion(), RepositoryVersionState.CURRENT);
     clusters.mapHostToCluster("h1", "c1");
   }
 
@@ -121,10 +122,8 @@ public class HostVersionOutOfSyncListenerTest {
    * @param stackId Stack Id to use
    * @throws AmbariException
    */
-  private void createClusterAndHosts(String INSTALLED_VERSION, StackId stackId) throws AmbariException {
-    // Configuring 3-node cluster with 2 repo versions
-    String CURRENT_VERSION = "2.2.0-2086";
-
+  private RepositoryVersionEntity createClusterAndHosts(String INSTALLED_VERSION, StackId stackId)
+      throws AmbariException {
     Host h1 = clusters.getHost("h1");
     h1.setState(HostState.HEALTHY);
 
@@ -133,32 +132,32 @@ public class HostVersionOutOfSyncListenerTest {
     addHost("h3");
     clusters.mapHostToCluster("h3", "c1");
 
-    c1.createClusterVersion(stackId, INSTALLED_VERSION, "admin", RepositoryVersionState.INSTALLING);
+    // create the new repo version
+    RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(stackId,
+        INSTALLED_VERSION);
+
     c1.setCurrentStackVersion(stackId);
-    c1.recalculateAllClusterVersionStates();
-    assertRepoVersionState(stackId.getStackId(), INSTALLED_VERSION,
-        RepositoryVersionState.INSTALLING);
+    assertRepoVersionState(INSTALLED_VERSION, RepositoryVersionState.INSTALLING);
 
-    assertRepoVersionState(stackId.getStackId(), CURRENT_VERSION, RepositoryVersionState.CURRENT);
+    assertRepoVersionState(CURRENT_VERSION, RepositoryVersionState.CURRENT);
 
     // Add ZK service with only ZOOKEEPER_SERVER
-    List<String> hostList = new ArrayList<String>();
+    List<String> hostList = new ArrayList<>();
     hostList.add("h1");
     hostList.add("h2");
     hostList.add("h3");
-    Map<String, List<Integer>> zkTopology = new HashMap<String, List<Integer>>();
+    Map<String, List<Integer>> zkTopology = new HashMap<>();
     List<Integer> zkServerHosts = Arrays.asList(0, 1, 2);
-    zkTopology.put("ZOOKEEPER_SERVER", new ArrayList<Integer>(zkServerHosts));
-    addService(c1, hostList, zkTopology, "ZOOKEEPER");
+    zkTopology.put("ZOOKEEPER_SERVER", new ArrayList<>(zkServerHosts));
+    addService(c1, hostList, zkTopology, "ZOOKEEPER", repositoryVersionEntity);
 
-    // Register and install new version
-    RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(stackId,
-        INSTALLED_VERSION);
+    // install new version
     helper.createHostVersion("h1", repositoryVersionEntity, RepositoryVersionState.INSTALLED);
-    c1.recalculateAllClusterVersionStates();
-    assertRepoVersionState(stackId.getStackId(), INSTALLED_VERSION,
-        RepositoryVersionState.INSTALLED);
-    assertRepoVersionState(stackId.getStackId(), CURRENT_VERSION, RepositoryVersionState.CURRENT);
+    helper.createHostVersion("h2", repositoryVersionEntity, RepositoryVersionState.INSTALLED);
+    helper.createHostVersion("h3", repositoryVersionEntity, RepositoryVersionState.INSTALLED);
+
+    assertRepoVersionState(INSTALLED_VERSION, RepositoryVersionState.INSTALLED);
+    assertRepoVersionState(CURRENT_VERSION, RepositoryVersionState.CURRENT);
 
     // Add new host and verify that it has all host versions present
     List<HostVersionEntity> h2Versions = hostVersionDAO.findAll();
@@ -169,6 +168,8 @@ public class HostVersionOutOfSyncListenerTest {
         assertEquals(hostVersionEntity.getState(), RepositoryVersionState.INSTALLED);
       }
     }
+
+    return repositoryVersionEntity;
   }
 
   /***
@@ -178,14 +179,12 @@ public class HostVersionOutOfSyncListenerTest {
    * @param stackId Stack Id to use
    * @throws AmbariException
    */
-  private void addRepoVersion(String INSTALLED_VERSION, StackId stackId) throws AmbariException {
+  private RepositoryVersionEntity addRepoVersion(String INSTALLED_VERSION, StackId stackId) throws AmbariException {
     // Register and install new version
     RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(stackId,
             INSTALLED_VERSION);
     helper.createHostVersion("h1", repositoryVersionEntity, RepositoryVersionState.INSTALLED);
-    c1.recalculateAllClusterVersionStates();
-    assertRepoVersionState(stackId.getStackId(), INSTALLED_VERSION,
-        RepositoryVersionState.INSTALLED);
+    assertRepoVersionState(INSTALLED_VERSION, RepositoryVersionState.INSTALLED);
 
     // Add new host and verify that it has all host versions present
     List<HostVersionEntity> h2Versions = hostVersionDAO.findAll();
@@ -196,6 +195,8 @@ public class HostVersionOutOfSyncListenerTest {
         assertEquals(hostVersionEntity.getState(), RepositoryVersionState.INSTALLED);
       }
     }
+
+    return repositoryVersionEntity;
   }
 
   /**
@@ -210,33 +211,35 @@ public class HostVersionOutOfSyncListenerTest {
     StackId stackId = new StackId(this.stackId);
     StackId yaStackId = new StackId(yetAnotherStackId);
 
-    createClusterAndHosts(INSTALLED_VERSION, stackId);
+    // get new hosts installed with the first repo
+    RepositoryVersionEntity repositoryVersion = createClusterAndHosts(INSTALLED_VERSION, stackId);
+
+    // register the new repo
     addRepoVersion(INSTALLED_VERSION_2, yaStackId);
 
+    assertRepoVersionState(INSTALLED_VERSION, RepositoryVersionState.INSTALLED);
+    assertRepoVersionState(INSTALLED_VERSION_2, RepositoryVersionState.INSTALLED);
+    assertRepoVersionState(CURRENT_VERSION, RepositoryVersionState.CURRENT);
 
     //Add HDFS service
-    List<String> hostList = new ArrayList<String>();
+    List<String> hostList = new ArrayList<>();
     hostList.add("h1");
     hostList.add("h2");
     hostList.add("h3");
-    Map<String, List<Integer>> hdfsTopology = new HashMap<String, List<Integer>>();
+    Map<String, List<Integer>> hdfsTopology = new HashMap<>();
     hdfsTopology.put("NAMENODE", Collections.singletonList(0));
     hdfsTopology.put("SECONDARY_NAMENODE", Collections.singletonList(1));
     List<Integer> datanodeHosts = Arrays.asList(0, 1);
-    hdfsTopology.put("DATANODE", new ArrayList<Integer>(datanodeHosts));
-    addService(c1, hostList, hdfsTopology, "HDFS");
+    hdfsTopology.put("DATANODE", new ArrayList<>(datanodeHosts));
+    addService(c1, hostList, hdfsTopology, "HDFS", repositoryVersion);
 
     // Check result
-    Set<String> changedHosts = new HashSet<String>();
+    Set<String> changedHosts = new HashSet<>();
     changedHosts.add("h1");
     changedHosts.add("h2");
 
     List<HostVersionEntity> hostVersions = hostVersionDAO.findAll();
 
-    assertRepoVersionState(stackId.getStackId(), INSTALLED_VERSION,
-        RepositoryVersionState.INSTALLED);
-    assertRepoVersionState(yaStackId.getStackId(), INSTALLED_VERSION_2,
-        RepositoryVersionState.INSTALLED);
     for (HostVersionEntity hostVersionEntity : hostVersions) {
       if (hostVersionEntity.getRepositoryVersion().getVersion().equals(INSTALLED_VERSION) ||
               hostVersionEntity.getRepositoryVersion().getVersion().equals(INSTALLED_VERSION_2)) {
@@ -247,6 +250,11 @@ public class HostVersionOutOfSyncListenerTest {
         }
       }
     }
+
+
+    assertRepoVersionState(INSTALLED_VERSION, RepositoryVersionState.OUT_OF_SYNC);
+    assertRepoVersionState(INSTALLED_VERSION_2, RepositoryVersionState.OUT_OF_SYNC);
+    assertRepoVersionState(CURRENT_VERSION, RepositoryVersionState.CURRENT);
   }
 
 
@@ -261,29 +269,28 @@ public class HostVersionOutOfSyncListenerTest {
     String INSTALLED_VERSION = "2.2.0-1000";
     StackId stackId = new StackId(this.stackId);
 
-    createClusterAndHosts(INSTALLED_VERSION, stackId);
+    RepositoryVersionEntity repositoryVersion = createClusterAndHosts(INSTALLED_VERSION, stackId);
 
     //Add Ganglia service
-    List<String> hostList = new ArrayList<String>();
+    List<String> hostList = new ArrayList<>();
     hostList.add("h1");
     hostList.add("h2");
     hostList.add("h3");
-    Map<String, List<Integer>> hdfsTopology = new HashMap<String, List<Integer>>();
+    Map<String, List<Integer>> hdfsTopology = new HashMap<>();
     hdfsTopology.put("GANGLIA_SERVER", Collections.singletonList(0));
     List<Integer> monitorHosts = Arrays.asList(0, 1);
-    hdfsTopology.put("GANGLIA_MONITOR", new ArrayList<Integer>(monitorHosts));
-    addService(c1, hostList, hdfsTopology, "GANGLIA");
+    hdfsTopology.put("GANGLIA_MONITOR", new ArrayList<>(monitorHosts));
+    addService(c1, hostList, hdfsTopology, "GANGLIA", repositoryVersion);
 
     // Check result
-    Set<String> changedHosts = new HashSet<String>();
+    Set<String> changedHosts = new HashSet<>();
     changedHosts.add("h1");
     changedHosts.add("h2");
 
     List<HostVersionEntity> hostVersions = hostVersionDAO.findAll();
 
     // Host version should not transition to OUT_OF_SYNC state
-    assertRepoVersionState(stackId.getStackId(), INSTALLED_VERSION,
-        RepositoryVersionState.INSTALLED);
+    assertRepoVersionState(INSTALLED_VERSION, RepositoryVersionState.INSTALLED);
     for (HostVersionEntity hostVersionEntity : hostVersions) {
       if (hostVersionEntity.getRepositoryVersion().getVersion().equals(INSTALLED_VERSION)) {
         assertEquals(hostVersionEntity.getState(), RepositoryVersionState.INSTALLED);
@@ -306,21 +313,24 @@ public class HostVersionOutOfSyncListenerTest {
     createClusterAndHosts(INSTALLED_VERSION, stackId);
     addRepoVersion(INSTALLED_VERSION_2, yaStackId);
 
+    assertRepoVersionState(INSTALLED_VERSION, RepositoryVersionState.INSTALLED);
+    assertRepoVersionState(INSTALLED_VERSION_2, RepositoryVersionState.INSTALLED);
+
     //Add ZOOKEEPER_CLIENT component
-    List<String> hostList = new ArrayList<String>();
+    List<String> hostList = new ArrayList<>();
     hostList.add("h1");
     hostList.add("h2");
     hostList.add("h3");
     addServiceComponent(c1, hostList, "ZOOKEEPER", "ZOOKEEPER_CLIENT");
 
     // Check result
-    Set<String> changedHosts = new HashSet<String>();
+    Set<String> changedHosts = new HashSet<>();
     changedHosts.add("h1");
     changedHosts.add("h2");
     changedHosts.add("h3");
 
-    assertRepoVersionState(stackId.getStackId(), INSTALLED_VERSION,
-        RepositoryVersionState.INSTALLED);
+    assertRepoVersionState(INSTALLED_VERSION, RepositoryVersionState.OUT_OF_SYNC);
+
     List<HostVersionEntity> hostVersions = hostVersionDAO.findAll();
 
     for (HostVersionEntity hostVersionEntity : hostVersions) {
@@ -350,18 +360,17 @@ public class HostVersionOutOfSyncListenerTest {
     StackId stackId = new StackId(this.stackId);
     RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(stackId,"2.2.0-1000");
     RepositoryVersionEntity repositoryVersionEntity2 = helper.getOrCreateRepositoryVersion(stackId,"2.2.0-2000");
-    c1.createClusterVersion(stackId, "2.2.0-1000", "admin", RepositoryVersionState.INSTALLING);
     c1.setCurrentStackVersion(stackId);
-    c1.recalculateAllClusterVersionStates();
-    assertRepoVersionState(stackId.getStackId(), "2.2.0-1000", RepositoryVersionState.INSTALLING);
-    assertRepoVersionState(stackId.getStackId(), "2.2.0-2086", RepositoryVersionState.CURRENT);
+
+    assertRepoVersionState("2.2.0-1000", RepositoryVersionState.INSTALLING);
+    assertRepoVersionState("2.2.0-2086", RepositoryVersionState.CURRENT);
 
     helper.createHostVersion("h1", repositoryVersionEntity, RepositoryVersionState.INSTALLED);
     helper.createHostVersion("h1", repositoryVersionEntity2, RepositoryVersionState.INSTALLED);
-    c1.recalculateAllClusterVersionStates();
-    assertRepoVersionState(stackId.getStackId(), "2.2.0-1000", RepositoryVersionState.INSTALLED);
-    assertRepoVersionState(stackId.getStackId(), "2.2.0-2000", RepositoryVersionState.INSTALLED);
-    assertRepoVersionState(stackId.getStackId(), "2.2.0-2086", RepositoryVersionState.CURRENT);
+
+    assertRepoVersionState("2.2.0-1000", RepositoryVersionState.INSTALLED);
+    assertRepoVersionState("2.2.0-2000", RepositoryVersionState.INSTALLED);
+    assertRepoVersionState("2.2.0-2086", RepositoryVersionState.CURRENT);
 
     // Add new host and verify that it has all host versions present
     addHost("h2");
@@ -393,12 +402,10 @@ public class HostVersionOutOfSyncListenerTest {
     RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(stackId,
         "2.2.9-9999");
 
-    c1.createClusterVersion(stackId, "2.2.9-9999", "admin", RepositoryVersionState.INSTALLING);
     c1.setCurrentStackVersion(stackId);
-    c1.recalculateAllClusterVersionStates();
 
-    assertRepoVersionState(stackId.getStackId(), "2.2.0", RepositoryVersionState.CURRENT);
-    assertRepoVersionState(stackId.getStackId(), "2.2.9-9999", RepositoryVersionState.INSTALLING);
+    assertRepoVersionState("2.2.0", RepositoryVersionState.CURRENT);
+    assertRepoVersionState("2.2.9-9999", RepositoryVersionState.INSTALLING);
 
     HostVersionEntity hv1 = helper.createHostVersion("h1", repositoryVersionEntity,
         RepositoryVersionState.INSTALLED);
@@ -406,9 +413,8 @@ public class HostVersionOutOfSyncListenerTest {
         RepositoryVersionState.INSTALLED);
 
     // do an initial calculate to make sure the new repo is installing
-    c1.recalculateAllClusterVersionStates();
-    assertRepoVersionState(stackId.getStackId(), "2.2.0", RepositoryVersionState.CURRENT);
-    assertRepoVersionState(stackId.getStackId(), "2.2.9-9999", RepositoryVersionState.INSTALLED);
+    assertRepoVersionState("2.2.0", RepositoryVersionState.CURRENT);
+    assertRepoVersionState("2.2.9-9999", RepositoryVersionState.INSTALLED);
 
     // make it seems like we upgraded, but 1 host still hasn't finished
     hv1.setState(RepositoryVersionState.INSTALLED);
@@ -417,9 +423,8 @@ public class HostVersionOutOfSyncListenerTest {
     hostVersionDAO.merge(hv2);
 
     // recalculate and ensure that the cluster is UPGRADING
-    c1.recalculateAllClusterVersionStates();
-    assertRepoVersionState(stackId.getStackId(), "2.2.0", RepositoryVersionState.CURRENT);
-    assertRepoVersionState(stackId.getStackId(), "2.2.9-9999", RepositoryVersionState.INSTALLING);
+    assertRepoVersionState("2.2.0", RepositoryVersionState.CURRENT);
+    assertRepoVersionState("2.2.9-9999", RepositoryVersionState.INSTALLING);
 
     // delete the host that was UPGRADING, and DON'T call recalculate; let the
     // event handle it
@@ -427,8 +432,8 @@ public class HostVersionOutOfSyncListenerTest {
     clusters.deleteHost("h2");
     clusters.publishHostsDeletion(Collections.singleton(c1), Collections.singleton("h2"));
     injector.getInstance(UnitOfWork.class).end();
-    assertRepoVersionState(stackId.getStackId(), "2.2.0", RepositoryVersionState.CURRENT);
-    assertRepoVersionState(stackId.getStackId(), "2.2.9-9999", RepositoryVersionState.INSTALLED);
+    assertRepoVersionState("2.2.0", RepositoryVersionState.CURRENT);
+    assertRepoVersionState("2.2.9-9999", RepositoryVersionState.INSTALLED);
   }
 
   @Test
@@ -450,7 +455,7 @@ public class HostVersionOutOfSyncListenerTest {
 
     // create repo version
     RepositoryVersionEntity repo = helper.getOrCreateRepositoryVersion(stackId, stackId.getStackVersion());
-    c1.createClusterVersion(stackId, stackId.getStackVersion(), "admin", RepositoryVersionState.INSTALLING);
+
     clusters.mapHostToCluster(host1, clusterName);
     clusters.mapHostToCluster(host2, clusterName);
     clusters.mapHostToCluster(host3, clusterName);
@@ -467,13 +472,13 @@ public class HostVersionOutOfSyncListenerTest {
         .put("NAMENODE", Lists.newArrayList(0))
         .put("DATANODE", Lists.newArrayList(1))
         .build();
-    addService(c1, allHosts, topology, "HDFS");
+    addService(c1, allHosts, topology, "HDFS", repo);
 
     topology = new ImmutableMap.Builder<String, List<Integer>>()
         .put("GANGLIA_SERVER", Lists.newArrayList(0))
         .put("GANGLIA_MONITOR", Lists.newArrayList(2))
         .build();
-    addService(c1, allHosts, topology, "GANGLIA");
+    addService(c1, allHosts, topology, "GANGLIA", repo);
 
     List<HostVersionEntity> hostVersions = hostVersionDAO.findAll();
     assertEquals(3, hostVersions.size());
@@ -532,18 +537,17 @@ public class HostVersionOutOfSyncListenerTest {
     host1.setIPv4("ipv4");
     host1.setIPv6("ipv6");
 
-    Map<String, String> hostAttributes = new HashMap<String, String>();
+    Map<String, String> hostAttributes = new HashMap<>();
     hostAttributes.put("os_family", "redhat");
     hostAttributes.put("os_release_version", "6.4");
     host1.setHostAttributes(hostAttributes);
   }
 
-  private void addService(Cluster cl, List<String> hostList,
-                                Map<String, List<Integer>> topology, String serviceName
-                          ) throws AmbariException {
+  private void addService(Cluster cl, List<String> hostList, Map<String, List<Integer>> topology,
+      String serviceName, RepositoryVersionEntity repositoryVersionEntity) throws AmbariException {
     StackId stackIdObj = new StackId(stackId);
     cl.setDesiredStackVersion(stackIdObj);
-    cl.addService(serviceName);
+    cl.addService(serviceName, repositoryVersionEntity);
 
     for (Map.Entry<String, List<Integer>> component : topology.entrySet()) {
 
@@ -585,15 +589,15 @@ public class HostVersionOutOfSyncListenerTest {
     }
   }
 
-  private void assertRepoVersionState(String stack, String version, RepositoryVersionState state) {
-    StackId stackId = new StackId(stack);
-    Collection<ClusterVersionEntity> allClusterVersions = c1.getAllClusterVersions();
-    for (ClusterVersionEntity entity : allClusterVersions) {
-      StackId clusterVersionStackId = new StackId(entity.getRepositoryVersion().getStack());
-      if (clusterVersionStackId.equals(stackId)
-          && entity.getRepositoryVersion().getVersion().equals(version)) {
-        assertEquals(state, entity.getState());
+  private void assertRepoVersionState(String version, RepositoryVersionState state) {
+
+    for (Host host : c1.getHosts()) {
+      for (HostVersionEntity hostVersionEntity : host.getAllHostVersions()) {
+        if (hostVersionEntity.getRepositoryVersion().getVersion().equals("version")) {
+          assertEquals(state, hostVersionEntity.getState());
+        }
       }
     }
+
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
index f20c46c..efe594a 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/StackVersionListenerTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -22,22 +22,21 @@ import static org.easymock.EasyMock.expectLastCall;
 
 import java.lang.reflect.Field;
 
-import org.apache.ambari.annotations.Experimental;
-import org.apache.ambari.annotations.ExperimentalFeature;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.events.HostComponentVersionAdvertisedEvent;
 import org.apache.ambari.server.events.publishers.VersionEventPublisher;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
+import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.ComponentInfo;
 import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentHost;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.UpgradeState;
-import org.easymock.EasyMock;
 import org.easymock.EasyMockRunner;
 import org.easymock.EasyMockSupport;
 import org.easymock.Mock;
@@ -64,6 +63,7 @@ public class StackVersionListenerTest extends EasyMockSupport {
   private static final String UNKNOWN_VERSION = "UNKNOWN";
   private static final String VALID_PREVIOUS_VERSION = "2.2.0.0";
   private static final RepositoryVersionEntity DUMMY_REPOSITORY_VERSION_ENTITY = new RepositoryVersionEntity();
+  private static final HostVersionEntity DUMMY_HOST_VERSION_ENTITY = new HostVersionEntity();
   private static final UpgradeEntity DUMMY_UPGRADE_ENTITY = new UpgradeEntity();
   public static final String STACK_NAME = "HDP-2.4.0.0";
   public static final String STACK_VERSION = "2.4.0.0";
@@ -73,6 +73,7 @@ public class StackVersionListenerTest extends EasyMockSupport {
   private Service service;
   private ServiceComponent serviceComponent;
   private VersionEventPublisher publisher = new VersionEventPublisher();
+  private ComponentInfo componentInfo;
   private StackId stackId;
 
   @TestSubject
@@ -87,12 +88,10 @@ public class StackVersionListenerTest extends EasyMockSupport {
     sch = createNiceMock(ServiceComponentHost.class);
     service = createNiceMock(Service.class);
     serviceComponent = createNiceMock(ServiceComponent.class);
+    componentInfo = createNiceMock(ComponentInfo.class);
     stackId = createNiceMock(StackId.class);
 
-    expect(cluster.getDesiredStackVersion()).andReturn(stackId).anyTimes();
-    expect(stackId.getStackName()).andReturn(STACK_NAME).anyTimes();
-    expect(stackId.getStackVersion()).andReturn(STACK_VERSION).anyTimes();
-    expect(cluster.getClusterId()).andReturn(CLUSTER_ID).atLeastOnce();
+    expect(cluster.getClusterId()).andReturn(CLUSTER_ID);
 
     expect(cluster.getService(SERVICE_NAME)).andReturn(service).atLeastOnce();
     expect(service.getServiceComponent(SERVICE_COMPONENT_NAME)).andReturn(serviceComponent).atLeastOnce();
@@ -102,9 +101,8 @@ public class StackVersionListenerTest extends EasyMockSupport {
 
   @Test
   public void testRecalculateHostVersionStateWhenVersionIsNullAndNewVersionIsNotBlank() throws AmbariException {
-    expect(sch.getVersion()).andReturn(null).atLeastOnce();
-    expect(serviceComponent.getDesiredVersion()).andReturn(INVALID_NEW_VERSION).atLeastOnce();
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE).atLeastOnce();
+    expect(sch.getVersion()).andReturn(null);
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
     sch.setVersion(INVALID_NEW_VERSION);
     expectLastCall().once();
     expect(sch.recalculateHostVersionState()).andReturn(null).once();
@@ -116,8 +114,8 @@ public class StackVersionListenerTest extends EasyMockSupport {
 
   @Test
   public void testRecalculateHostVersionStateWhenVersionIsUnknownAndNewVersionIsNotBlank() throws AmbariException {
-    expect(sch.getVersion()).andReturn(UNKNOWN_VERSION).atLeastOnce();
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE).atLeastOnce();
+    expect(sch.getVersion()).andReturn(UNKNOWN_VERSION);
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
     sch.setVersion(INVALID_NEW_VERSION);
     expectLastCall().once();
     expect(sch.recalculateHostVersionState()).andReturn(null).once();
@@ -129,13 +127,11 @@ public class StackVersionListenerTest extends EasyMockSupport {
 
   @Test
   public void testRecalculateClusterVersionStateWhenVersionIsNullAndNewVersionIsValid() throws AmbariException {
-    expect(sch.getVersion()).andReturn(null).atLeastOnce();
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE).atLeastOnce();
+    expect(sch.getVersion()).andReturn(null);
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
     sch.setVersion(VALID_NEW_VERSION);
     expectLastCall().once();
-    expect(sch.recalculateHostVersionState()).andReturn(DUMMY_REPOSITORY_VERSION_ENTITY).once();
-    cluster.recalculateClusterVersionState(DUMMY_REPOSITORY_VERSION_ENTITY);
-    expectLastCall().once();
+    expect(sch.recalculateHostVersionState()).andReturn(DUMMY_HOST_VERSION_ENTITY).once();
 
     replayAll();
 
@@ -144,13 +140,11 @@ public class StackVersionListenerTest extends EasyMockSupport {
 
   @Test
   public void testRecalculateClusterVersionStateWhenVersionIsUnknownAndNewVersionIsValid() throws AmbariException {
-    expect(sch.getVersion()).andReturn(UNKNOWN_VERSION).atLeastOnce();
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE).atLeastOnce();
+    expect(sch.getVersion()).andReturn(UNKNOWN_VERSION);
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
     sch.setVersion(VALID_NEW_VERSION);
     expectLastCall().once();
-    expect(sch.recalculateHostVersionState()).andReturn(DUMMY_REPOSITORY_VERSION_ENTITY).once();
-    cluster.recalculateClusterVersionState(DUMMY_REPOSITORY_VERSION_ENTITY);
-    expectLastCall().once();
+    expect(sch.recalculateHostVersionState()).andReturn(DUMMY_HOST_VERSION_ENTITY).once();
 
     replayAll();
 
@@ -159,10 +153,8 @@ public class StackVersionListenerTest extends EasyMockSupport {
 
   @Test
   public void testRecalculateHostVersionStateWhenComponentDesiredVersionIsUnknownAndNewVersionIsNotValid() throws AmbariException {
-    expect(serviceComponent.getDesiredVersion()).andReturn(UNKNOWN_VERSION).atLeastOnce();
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE).atLeastOnce();
-    serviceComponent.setDesiredVersion(INVALID_NEW_VERSION);
-    expectLastCall().once();
+    expect(serviceComponent.getDesiredVersion()).andReturn(UNKNOWN_VERSION);
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
     sch.setUpgradeState(UpgradeState.NONE);
     expectLastCall().once();
     sch.setVersion(INVALID_NEW_VERSION);
@@ -176,17 +168,13 @@ public class StackVersionListenerTest extends EasyMockSupport {
 
   @Test
   public void testRecalculateClusterVersionStateWhenComponentDesiredVersionIsUnknownAndNewVersionIsValid() throws AmbariException {
-    expect(serviceComponent.getDesiredVersion()).andReturn(UNKNOWN_VERSION).atLeastOnce();
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE).atLeastOnce();
-    serviceComponent.setDesiredVersion(VALID_NEW_VERSION);
-    expectLastCall().once();
+    expect(serviceComponent.getDesiredVersion()).andReturn(UNKNOWN_VERSION);
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
     sch.setUpgradeState(UpgradeState.NONE);
     expectLastCall().once();
     sch.setVersion(VALID_NEW_VERSION);
     expectLastCall().once();
-    expect(sch.recalculateHostVersionState()).andReturn(DUMMY_REPOSITORY_VERSION_ENTITY).once();
-    cluster.recalculateClusterVersionState(DUMMY_REPOSITORY_VERSION_ENTITY);
-    expectLastCall().once();
+    expect(sch.recalculateHostVersionState()).andReturn(DUMMY_HOST_VERSION_ENTITY).once();
 
     replayAll();
 
@@ -195,7 +183,7 @@ public class StackVersionListenerTest extends EasyMockSupport {
 
   @Test
   public void testRecalculateClusterVersionStateWhenVersionNotAdvertised() throws AmbariException {
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.FALSE).atLeastOnce();
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.FALSE);
     replayAll();
     sendEventAndVerify(VALID_NEW_VERSION);
   }
@@ -212,10 +200,11 @@ public class StackVersionListenerTest extends EasyMockSupport {
 
   @Test
   public void testSetUpgradeStateToCompleteWhenUpgradeIsInProgressAndNewVersionIsEqualToComponentDesiredVersion() {
-    expect(cluster.getUpgradeInProgress()).andReturn(EasyMock.niceMock(UpgradeEntity.class)).atLeastOnce();
-    expect(sch.getVersion()).andReturn(VALID_PREVIOUS_VERSION).atLeastOnce();
-    expect(sch.getUpgradeState()).andReturn(UpgradeState.IN_PROGRESS).atLeastOnce();
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE).atLeastOnce();
+    expect(cluster.getUpgradeInProgress()).andReturn(DUMMY_UPGRADE_ENTITY);
+
+    expect(sch.getVersion()).andReturn(VALID_PREVIOUS_VERSION);
+    expect(sch.getUpgradeState()).andReturn(UpgradeState.IN_PROGRESS);
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
     sch.setUpgradeState(UpgradeState.COMPLETE);
     expectLastCall().once();
 
@@ -226,14 +215,30 @@ public class StackVersionListenerTest extends EasyMockSupport {
   }
 
   @Test
+  public void testSetUpgradeStateToNoneWhenNoUpgradeAndNewVersionIsEqualToComponentDesiredVersion() {
+    expect(sch.getVersion()).andReturn(VALID_PREVIOUS_VERSION);
+    expect(sch.getUpgradeState()).andReturn(UpgradeState.IN_PROGRESS);
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
+    sch.setUpgradeState(UpgradeState.NONE);
+    expectLastCall().once();
+
+    expect(serviceComponent.getDesiredVersion()).andStubReturn(VALID_NEW_VERSION);
+    replayAll();
+
+    sendEventAndVerify(VALID_NEW_VERSION);
+  }
+
+  @Test
   public void testSetUpgradeStateToVersionMismatchWhenUpgradeIsInProgressAndNewVersionIsNotEqualToComponentDesiredVersion() {
-    expect(cluster.getUpgradeInProgress()).andReturn(createNiceMock(UpgradeEntity.class)).atLeastOnce();
-    expect(sch.getVersion()).andReturn(VALID_PREVIOUS_VERSION).atLeastOnce();
-    expect(sch.getUpgradeState()).andReturn(UpgradeState.IN_PROGRESS).atLeastOnce();
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE).atLeastOnce();
+    expect(sch.getVersion()).andReturn(VALID_PREVIOUS_VERSION);
+    expect(sch.getUpgradeState()).andReturn(UpgradeState.IN_PROGRESS);
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
     sch.setUpgradeState(UpgradeState.VERSION_MISMATCH);
     expectLastCall().once();
 
+    sch.setVersion(VALID_NEW_VERSION);
+    expectLastCall().once();
+
     expect(serviceComponent.getDesiredVersion()).andStubReturn(VALID_PREVIOUS_VERSION);
     replayAll();
 
@@ -242,11 +247,11 @@ public class StackVersionListenerTest extends EasyMockSupport {
 
   @Test
   public void testSetUpgradeStateToCompleteWhenHostHasVersionMismatchAndNewVersionIsEqualToComponentDesiredVersionAndClusterUpgradeIsInProgress() {
-    expect(sch.getVersion()).andReturn(VALID_PREVIOUS_VERSION).atLeastOnce();
-    expect(sch.getUpgradeState()).andReturn(UpgradeState.VERSION_MISMATCH).atLeastOnce();
-    expect(cluster.getUpgradeInProgress()).andReturn(DUMMY_UPGRADE_ENTITY).atLeastOnce();
+    expect(sch.getVersion()).andReturn(VALID_PREVIOUS_VERSION);
+    expect(sch.getUpgradeState()).andReturn(UpgradeState.VERSION_MISMATCH);
+    expect(cluster.getUpgradeInProgress()).andReturn(DUMMY_UPGRADE_ENTITY);
     expect(serviceComponent.getDesiredVersion()).andStubReturn(VALID_NEW_VERSION);
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE).atLeastOnce();
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
     sch.setUpgradeState(UpgradeState.COMPLETE);
     expectLastCall().once();
 
@@ -257,10 +262,10 @@ public class StackVersionListenerTest extends EasyMockSupport {
 
   @Test
   public void testSetUpgradeStateToNoneWhenHostHasVersionMismatchAndNewVersionIsEqualToComponentDesiredVersion() {
-    expect(sch.getVersion()).andReturn(VALID_PREVIOUS_VERSION).atLeastOnce();
-    expect(sch.getUpgradeState()).andReturn(UpgradeState.VERSION_MISMATCH).atLeastOnce();
+    expect(sch.getVersion()).andReturn(VALID_PREVIOUS_VERSION);
+    expect(sch.getUpgradeState()).andReturn(UpgradeState.VERSION_MISMATCH);
     expect(serviceComponent.getDesiredVersion()).andStubReturn(VALID_NEW_VERSION);
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE).atLeastOnce();
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
     sch.setUpgradeState(UpgradeState.NONE);
     expectLastCall().once();
 
@@ -270,20 +275,9 @@ public class StackVersionListenerTest extends EasyMockSupport {
   }
 
   @Test
-  @Experimental(
-      feature = ExperimentalFeature.STACK_UPGRADES_BETWEEN_VENDORS,
-      comment = "Version Mismatch happened previously when not in an upgrade")
   public void testSetUpgradeStateToVersionMismatchByDefaultWhenHostAndNewVersionsAreValid() {
-    expect(sch.getVersion()).andReturn(VALID_PREVIOUS_VERSION).atLeastOnce();
-    expect(serviceComponent.getDesiredVersion()).andReturn(VALID_PREVIOUS_VERSION).atLeastOnce();
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE).atLeastOnce();
-
-    // !!! VERSION_MISMATCH might need to be allowed to happen when not in an
-    // upgrade
-    expect(cluster.getUpgradeInProgress()).andReturn(
-        EasyMock.niceMock(UpgradeEntity.class)).atLeastOnce();
-    expect(sch.getUpgradeState()).andReturn(UpgradeState.IN_PROGRESS).atLeastOnce();
-
+    expect(sch.getVersion()).andReturn(VALID_PREVIOUS_VERSION);
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
     sch.setUpgradeState(UpgradeState.VERSION_MISMATCH);
     expectLastCall().once();
 
@@ -294,8 +288,8 @@ public class StackVersionListenerTest extends EasyMockSupport {
 
   @Test
   public void testSetRepositoryVersion() throws Exception {
-    expect(sch.getVersion()).andReturn(UNKNOWN_VERSION).atLeastOnce();
-    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE).atLeastOnce();
+    expect(sch.getVersion()).andReturn(UNKNOWN_VERSION);
+    expect(serviceComponent.isVersionAdvertised()).andReturn(Boolean.TRUE);
 
     RepositoryVersionDAO dao = createNiceMock(RepositoryVersionDAO.class);
     RepositoryVersionEntity entity = createNiceMock(RepositoryVersionEntity.class);
@@ -328,8 +322,7 @@ public class StackVersionListenerTest extends EasyMockSupport {
   @Test
   public void testRepositoryVersionNotSetDuringUpgrade() throws Exception {
     // this call will make it seem like there is an upgrade in progress
-    expect(cluster.getUpgradeInProgress()).andReturn(
-        createNiceMock(UpgradeEntity.class)).atLeastOnce();
+    expect(cluster.getUpgradeInProgress()).andReturn(createNiceMock(UpgradeEntity.class));
 
     // create the DAO - nothing will be called on it, so make it strict
     RepositoryVersionDAO dao = createStrictMock(RepositoryVersionDAO.class);
@@ -359,4 +352,4 @@ public class StackVersionListenerTest extends EasyMockSupport {
 
     verifyAll();
   }
-}
\ No newline at end of file
+}


[03/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog240Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog240Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog240Test.java
deleted file mode 100644
index 8baf496..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog240Test.java
+++ /dev/null
@@ -1,2586 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-
-import com.google.common.collect.Maps;
-import com.google.gson.Gson;
-import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.Provider;
-import junit.framework.Assert;
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.H2DatabaseCleaner;
-import org.apache.ambari.server.actionmanager.ActionManager;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.controller.AmbariManagementControllerImpl;
-import org.apache.ambari.server.controller.KerberosHelper;
-import org.apache.ambari.server.controller.MaintenanceStateHelper;
-import org.apache.ambari.server.hooks.HookContextFactory;
-import org.apache.ambari.server.hooks.HookService;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.GuiceJpaInitializer;
-import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
-import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.PrivilegeDAO;
-import org.apache.ambari.server.orm.dao.RemoteAmbariClusterDAO;
-import org.apache.ambari.server.orm.dao.RequestScheduleDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.dao.UserDAO;
-import org.apache.ambari.server.orm.dao.ViewInstanceDAO;
-import org.apache.ambari.server.orm.dao.WidgetDAO;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.PermissionEntity;
-import org.apache.ambari.server.orm.entities.PrincipalEntity;
-import org.apache.ambari.server.orm.entities.PrivilegeEntity;
-import org.apache.ambari.server.orm.entities.RemoteAmbariClusterEntity;
-import org.apache.ambari.server.orm.entities.RequestScheduleEntity;
-import org.apache.ambari.server.orm.entities.ResourceEntity;
-import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
-import org.apache.ambari.server.orm.entities.UserEntity;
-import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
-import org.apache.ambari.server.orm.entities.WidgetEntity;
-import org.apache.ambari.server.security.authorization.ResourceType;
-import org.apache.ambari.server.security.authorization.User;
-import org.apache.ambari.server.security.authorization.UserName;
-import org.apache.ambari.server.security.authorization.Users;
-import org.apache.ambari.server.stack.StackManagerFactory;
-import org.apache.ambari.server.state.AlertFirmness;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.ConfigHelper;
-import org.apache.ambari.server.state.PropertyInfo;
-import org.apache.ambari.server.state.SecurityType;
-import org.apache.ambari.server.state.Service;
-import org.apache.ambari.server.state.ServiceInfo;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.StackInfo;
-import org.apache.ambari.server.state.alert.SourceType;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosIdentityDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosKeytabDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosPrincipalDescriptor;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.apache.ambari.server.view.DefaultMasker;
-import org.apache.ambari.view.ClusterType;
-import org.apache.ambari.view.MaskException;
-import org.apache.commons.io.FileUtils;
-import org.easymock.Capture;
-import org.easymock.CaptureType;
-import org.easymock.EasyMock;
-import org.easymock.EasyMockSupport;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.springframework.security.crypto.password.PasswordEncoder;
-
-import javax.persistence.EntityManager;
-import java.io.File;
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-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 static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.anyString;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.createMockBuilder;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.newCapture;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-public class UpgradeCatalog240Test {
-  private static final String CAPACITY_SCHEDULER_CONFIG_TYPE = "capacity-scheduler";
-  private static final String WEBHCAT_SITE_CONFIG_TYPE = "webhcat-site";
-  private static final String TEZ_SITE_CONFIG_TYPE = "tez-site";
-  private static final String MAPRED_SITE_CONFIG_TYPE = "mapred-site";
-  private static final String YARN_ENV_CONFIG_TYPE = "yarn-env";
-  private static Injector injector;
-  private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
-  private EntityManager entityManager = createNiceMock(EntityManager.class);
-
-
-  @BeforeClass
-  public static void classSetUp() {
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-  }
-
-  @Before
-  public void init() {
-    reset(entityManagerProvider);
-    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
-    replay(entityManagerProvider);
-
-    injector.getInstance(UpgradeCatalogHelper.class);
-    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
-    injector.getInstance(AmbariMetaInfo.class);
-    // load the stack entity
-    StackDAO stackDAO = injector.getInstance(StackDAO.class);
-    stackDAO.find("HDP", "2.2.0");
-  }
-
-  @AfterClass
-  public static void tearDown() throws AmbariException, SQLException {
-    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
-  }
-
-  @Test
-  public void testExecuteDDLUpdates() throws SQLException, AmbariException, ClassNotFoundException {
-    Capture<DBAccessor.DBColumnInfo> capturedSortOrderColumnInfo = newCapture();
-    Capture<DBAccessor.DBColumnInfo> capturedPermissionIDColumnInfo = newCapture();
-    Capture<DBAccessor.DBColumnInfo> capturedScColumnInfo = newCapture();
-    Capture<DBAccessor.DBColumnInfo> capturedScDesiredVersionColumnInfo = newCapture();
-
-    final DBAccessor dbAccessor = createStrictMock(DBAccessor.class);
-    Configuration configuration = createNiceMock(Configuration.class);
-    Connection connection = createNiceMock(Connection.class);
-    Statement statement = createNiceMock(Statement.class);
-    ResultSet resultSet = createNiceMock(ResultSet.class);
-
-    Capture<List<DBAccessor.DBColumnInfo>> capturedExtensionColumns = EasyMock.newCapture();
-    Capture<List<DBAccessor.DBColumnInfo>> capturedExtensionLinkColumns = EasyMock.newCapture();
-    Capture<List<DBAccessor.DBColumnInfo>> capturedSettingColumns = EasyMock.newCapture();
-
-    dbAccessor.addColumn(eq("adminpermission"), capture(capturedSortOrderColumnInfo));
-    dbAccessor.addColumn(eq("adminpermission"), capture(capturedPermissionIDColumnInfo));
-    dbAccessor.addColumn(eq(UpgradeCatalog240.SERVICE_COMPONENT_DESIRED_STATE_TABLE), capture(capturedScColumnInfo));
-    dbAccessor.addColumn(eq(UpgradeCatalog240.SERVICE_COMPONENT_DESIRED_STATE_TABLE),
-        capture(capturedScDesiredVersionColumnInfo));
-
-    dbAccessor.createTable(eq("extension"), capture(capturedExtensionColumns), eq("extension_id"));
-    dbAccessor.addUniqueConstraint("extension", "UQ_extension", "extension_name", "extension_version");
-
-    expect(dbAccessor.getConnection()).andReturn(connection);
-    dbAccessor.createTable(eq("extensionlink"), capture(capturedExtensionLinkColumns), eq("link_id"));
-    dbAccessor.addUniqueConstraint("extensionlink", "UQ_extension_link", "stack_id", "extension_id");
-    dbAccessor.addFKConstraint("extensionlink", "FK_extensionlink_extension_id", "extension_id", "extension",
-                               "extension_id", false);
-    dbAccessor.addFKConstraint("extensionlink", "FK_extensionlink_stack_id", "stack_id", "stack",
-                               "stack_id", false);
-
-    expect(dbAccessor.getConnection()).andReturn(connection);
-    dbAccessor.createTable(eq("setting"), capture(capturedSettingColumns), eq("id"));
-
-    expect(configuration.getDatabaseUrl()).andReturn(Configuration.JDBC_IN_MEMORY_URL).anyTimes();
-    expect(dbAccessor.getConnection()).andReturn(connection);
-    expect(connection.createStatement()).andReturn(statement);
-    expect(statement.executeQuery(anyObject(String.class))).andReturn(resultSet);
-
-    Capture<DBAccessor.DBColumnInfo> repoVersionRepoTypeColumnCapture = newCapture();
-    Capture<DBAccessor.DBColumnInfo> repoVersionUrlColumnCapture = newCapture();
-    Capture<DBAccessor.DBColumnInfo> repoVersionXmlColumnCapture = newCapture();
-    Capture<DBAccessor.DBColumnInfo> repoVersionXsdColumnCapture = newCapture();
-    Capture<DBAccessor.DBColumnInfo> repoVersionParentIdColumnCapture = newCapture();
-
-    dbAccessor.addColumn(eq("repo_version"), capture(repoVersionRepoTypeColumnCapture));
-    dbAccessor.addColumn(eq("repo_version"), capture(repoVersionUrlColumnCapture));
-    dbAccessor.addColumn(eq("repo_version"), capture(repoVersionXmlColumnCapture));
-    dbAccessor.addColumn(eq("repo_version"), capture(repoVersionXsdColumnCapture));
-    dbAccessor.addColumn(eq("repo_version"), capture(repoVersionParentIdColumnCapture));
-
-    // skip all of the drama of the servicecomponentdesiredstate table for now
-    expect(dbAccessor.tableHasPrimaryKey("servicecomponentdesiredstate", "id")).andReturn(true);
-
-    Capture<List<DBAccessor.DBColumnInfo>> capturedHistoryColumns = EasyMock.newCapture();
-    dbAccessor.createTable(eq("servicecomponent_history"), capture(capturedHistoryColumns),
-            eq((String[]) null));
-
-    dbAccessor.addPKConstraint("servicecomponent_history", "PK_sc_history", "id");
-    dbAccessor.addFKConstraint("servicecomponent_history", "FK_sc_history_component_id",
-        "component_id", "servicecomponentdesiredstate", "id", false);
-
-    dbAccessor.addFKConstraint("servicecomponent_history", "FK_sc_history_upgrade_id", "upgrade_id",
-        "upgrade", "upgrade_id", false);
-
-    dbAccessor.addFKConstraint("servicecomponent_history", "FK_sc_history_from_stack_id",
-            "from_stack_id", "stack", "stack_id", false);
-
-    dbAccessor.addFKConstraint("servicecomponent_history", "FK_sc_history_to_stack_id",
-            "to_stack_id", "stack", "stack_id", false);
-
-
-    expect(dbAccessor.getConnection()).andReturn(connection);
-    expect(connection.createStatement()).andReturn(statement);
-    expect(statement.executeQuery(anyObject(String.class))).andReturn(resultSet);
-
-    Capture<DBAccessor.DBColumnInfo> capturedClusterUpgradeColumnInfo = newCapture();
-    dbAccessor.addColumn(eq(UpgradeCatalog240.CLUSTER_TABLE), capture(capturedClusterUpgradeColumnInfo));
-    dbAccessor.addFKConstraint(UpgradeCatalog240.CLUSTER_TABLE, "FK_clusters_upgrade_id",
-            UpgradeCatalog240.CLUSTER_UPGRADE_ID_COLUMN, UpgradeCatalog240.UPGRADE_TABLE, "upgrade_id", false);
-
-    Capture<DBAccessor.DBColumnInfo> capturedHelpURLColumnInfo = newCapture();
-    Capture<DBAccessor.DBColumnInfo> capturedRepeatToleranceColumnInfo = newCapture();
-    Capture<DBAccessor.DBColumnInfo> capturedRepeatToleranceEnabledColumnInfo = newCapture();
-    Capture<DBAccessor.DBColumnInfo> capturedOccurrencesColumnInfo = newCapture();
-    Capture<DBAccessor.DBColumnInfo> capturedFirmnessColumnInfo = newCapture();
-    Capture<DBAccessor.DBColumnInfo> capturedTargetEnabledColumnInfo = newCapture();
-
-    dbAccessor.addColumn(eq(UpgradeCatalog240.ALERT_DEFINITION_TABLE), capture(capturedHelpURLColumnInfo));
-    dbAccessor.addColumn(eq(UpgradeCatalog240.ALERT_DEFINITION_TABLE), capture(capturedRepeatToleranceColumnInfo));
-    dbAccessor.addColumn(eq(UpgradeCatalog240.ALERT_DEFINITION_TABLE), capture(capturedRepeatToleranceEnabledColumnInfo));
-    dbAccessor.addColumn(eq(UpgradeCatalog240.ALERT_CURRENT_TABLE), capture(capturedOccurrencesColumnInfo));
-    dbAccessor.addColumn(eq(UpgradeCatalog240.ALERT_CURRENT_TABLE), capture(capturedFirmnessColumnInfo));
-    dbAccessor.addColumn(eq(UpgradeCatalog240.ALERT_TARGET_TABLE), capture(capturedTargetEnabledColumnInfo));
-
-    // Test creation of blueprint_setting table
-    Capture<List<DBAccessor.DBColumnInfo>> capturedBlueprintSettingColumns = EasyMock.newCapture();
-    dbAccessor.createTable(eq(UpgradeCatalog240.BLUEPRINT_SETTING_TABLE), capture(capturedBlueprintSettingColumns));
-    dbAccessor.addPKConstraint(UpgradeCatalog240.BLUEPRINT_SETTING_TABLE, "PK_blueprint_setting", UpgradeCatalog240.ID);
-    dbAccessor.addUniqueConstraint(UpgradeCatalog240.BLUEPRINT_SETTING_TABLE, "UQ_blueprint_setting_name",
-            UpgradeCatalog240.BLUEPRINT_NAME_COL, UpgradeCatalog240.SETTING_NAME_COL);
-    dbAccessor.addFKConstraint(UpgradeCatalog240.BLUEPRINT_SETTING_TABLE, "FK_blueprint_setting_name",
-            UpgradeCatalog240.BLUEPRINT_NAME_COL, UpgradeCatalog240.BLUEPRINT_TABLE,
-            UpgradeCatalog240.BLUEPRINT_NAME_COL, false);
-    expect(dbAccessor.getConnection()).andReturn(connection);
-    expect(connection.createStatement()).andReturn(statement);
-    expect(statement.executeQuery(anyObject(String.class))).andReturn(resultSet);
-
-    // Test host_role_command adds a column called original_start_time
-    Capture<DBAccessor.DBColumnInfo> hostRoleCommandOriginalStartTimeColumnInfo = newCapture();
-    dbAccessor.addColumn(eq(UpgradeCatalog240.HOST_ROLE_COMMAND_TABLE), capture(hostRoleCommandOriginalStartTimeColumnInfo));
-
-    Capture<List<DBAccessor.DBColumnInfo>> capturedViewUrlColums = EasyMock.newCapture();
-    dbAccessor.createTable(eq(UpgradeCatalog240.VIEWURL_TABLE), capture(capturedViewUrlColums),eq("url_id"));
-    expect(dbAccessor.getConnection()).andReturn(connection);
-    expect(connection.createStatement()).andReturn(statement);
-
-    Capture<DBAccessor.DBColumnInfo> viewInstanceShortUrlInfo = newCapture();
-    dbAccessor.addColumn(eq(UpgradeCatalog240.VIEWINSTANCE_TABLE), capture(viewInstanceShortUrlInfo));
-
-    dbAccessor.addFKConstraint(UpgradeCatalog240.VIEWINSTANCE_TABLE, "FK_instance_url_id",
-            UpgradeCatalog240.SHORT_URL_COLUMN, UpgradeCatalog240.VIEWURL_TABLE, "url_id", false);
-
-    Capture<DBAccessor.DBColumnInfo> viewInstanceClusterType = newCapture();
-    dbAccessor.addColumn(eq(UpgradeCatalog240.VIEWINSTANCE_TABLE), capture(viewInstanceClusterType));
-
-    // Test remote Cluster Tables
-    Capture<List<DBAccessor.DBColumnInfo>> capturedRemoteAmbariClusterColumns = EasyMock.newCapture();
-    dbAccessor.createTable(eq(UpgradeCatalog240.REMOTE_AMBARI_CLUSTER_TABLE), capture(capturedRemoteAmbariClusterColumns),anyString());
-    dbAccessor.addUniqueConstraint(UpgradeCatalog240.REMOTE_AMBARI_CLUSTER_TABLE , "UQ_remote_ambari_cluster" , UpgradeCatalog240.CLUSTER_NAME);
-    expect(dbAccessor.getConnection()).andReturn(connection);
-    expect(connection.createStatement()).andReturn(statement);
-
-    Capture<List<DBAccessor.DBColumnInfo>> capturedRemoteClusterServiceColumns = EasyMock.newCapture();
-    dbAccessor.createTable(eq(UpgradeCatalog240.REMOTE_AMBARI_CLUSTER_SERVICE_TABLE), capture(capturedRemoteClusterServiceColumns),anyString());
-    dbAccessor.addFKConstraint(UpgradeCatalog240.REMOTE_AMBARI_CLUSTER_SERVICE_TABLE, "FK_remote_ambari_cluster_id",
-      UpgradeCatalog240.CLUSTER_ID, UpgradeCatalog240.REMOTE_AMBARI_CLUSTER_TABLE, UpgradeCatalog240.CLUSTER_ID, false);
-    expect(dbAccessor.getConnection()).andReturn(connection);
-    expect(connection.createStatement()).andReturn(statement);
-
-    // Test viewInstance update
-    expect(dbAccessor.getColumnClass(UpgradeCatalog240.VIEWINSTANCE_TABLE, UpgradeCatalog240.CLUSTER_HANDLE_COLUMN)).andReturn(String.class);
-    dbAccessor.addColumn(eq(UpgradeCatalog240.VIEWINSTANCE_TABLE), anyObject(DBAccessor.DBColumnInfo.class));
-
-    expect(dbAccessor.getConnection()).andReturn(connection);
-    expect(connection.createStatement()).andReturn(statement);
-
-    dbAccessor.dropColumn(UpgradeCatalog240.VIEWINSTANCE_TABLE, UpgradeCatalog240.CLUSTER_HANDLE_COLUMN);
-
-    Capture<DBAccessor.DBColumnInfo> capturedClusterHandleColumn = EasyMock.newCapture();
-    dbAccessor.renameColumn(eq(UpgradeCatalog240.VIEWINSTANCE_TABLE), anyString(), capture(capturedClusterHandleColumn));
-    Capture<DBAccessor.DBColumnInfo> requestScheduleUserIdInfo = newCapture();
-    dbAccessor.addColumn(eq(UpgradeCatalog240.REQUESTSCHEDULE_TABLE), capture(requestScheduleUserIdInfo));
-
-    Capture<DBAccessor.DBColumnInfo> provisionActionColumnInfo = newCapture();
-    dbAccessor.addColumn(eq(UpgradeCatalog240.TOPOLOGY_REQUEST_TABLE), capture(provisionActionColumnInfo));
-
-    replay(dbAccessor, configuration, connection, statement, resultSet);
-
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        binder.bind(EntityManager.class).toInstance(entityManager);
-        binder.bind(PasswordEncoder.class).toInstance(createNiceMock(PasswordEncoder.class));
-        binder.bind(HookContextFactory.class).toInstance(createMock(HookContextFactory.class));
-        binder.bind(HookService.class).toInstance(createMock(HookService.class));
-      }
-      };
-
-    Injector injector = Guice.createInjector(module);
-    UpgradeCatalog240 upgradeCatalog240 = injector.getInstance(UpgradeCatalog240.class);
-    upgradeCatalog240.executeDDLUpdates();
-
-    DBAccessor.DBColumnInfo columnSortOrderInfo = capturedSortOrderColumnInfo.getValue();
-    Assert.assertNotNull(columnSortOrderInfo);
-    Assert.assertEquals(UpgradeCatalog240.SORT_ORDER_COL, columnSortOrderInfo.getName());
-    Assert.assertEquals(null, columnSortOrderInfo.getLength());
-    Assert.assertEquals(Short.class, columnSortOrderInfo.getType());
-    Assert.assertEquals(1, columnSortOrderInfo.getDefaultValue());
-    Assert.assertEquals(false, columnSortOrderInfo.isNullable());
-
-    DBAccessor.DBColumnInfo columnPrincipalIDInfo = capturedPermissionIDColumnInfo.getValue();
-    Assert.assertNotNull(columnPrincipalIDInfo);
-    Assert.assertEquals(UpgradeCatalog240.PRINCIPAL_ID_COL, columnPrincipalIDInfo.getName());
-    Assert.assertEquals(null, columnPrincipalIDInfo.getLength());
-    Assert.assertEquals(Long.class, columnPrincipalIDInfo.getType());
-    Assert.assertEquals(null, columnPrincipalIDInfo.getDefaultValue());
-    Assert.assertEquals(true, columnPrincipalIDInfo.isNullable());
-
-    // Verify if recovery_enabled column was added to servicecomponentdesiredstate table
-    DBAccessor.DBColumnInfo columnScInfo = capturedScColumnInfo.getValue();
-    Assert.assertNotNull(columnScInfo);
-    Assert.assertEquals(UpgradeCatalog240.RECOVERY_ENABLED_COL, columnScInfo.getName());
-    Assert.assertEquals(null, columnScInfo.getLength());
-    Assert.assertEquals(Short.class, columnScInfo.getType());
-    Assert.assertEquals(0, columnScInfo.getDefaultValue());
-    Assert.assertEquals(false, columnScInfo.isNullable());
-
-    DBAccessor.DBColumnInfo columnScDesiredVersionInfo = capturedScDesiredVersionColumnInfo.getValue();
-    Assert.assertNotNull(columnScDesiredVersionInfo);
-    Assert.assertEquals(UpgradeCatalog240.DESIRED_VERSION_COLUMN_NAME, columnScDesiredVersionInfo.getName());
-    Assert.assertEquals(Integer.valueOf(255), columnScDesiredVersionInfo.getLength());
-    Assert.assertEquals(String.class, columnScDesiredVersionInfo.getType());
-    Assert.assertEquals("UNKNOWN", columnScDesiredVersionInfo.getDefaultValue());
-    Assert.assertEquals(false, columnScDesiredVersionInfo.isNullable());
-
-    // Verify if upgrade_id column was added to clusters table
-    DBAccessor.DBColumnInfo clusterUpgradeColumnInfo = capturedClusterUpgradeColumnInfo.getValue();
-    Assert.assertNotNull(clusterUpgradeColumnInfo);
-    Assert.assertEquals(UpgradeCatalog240.CLUSTER_UPGRADE_ID_COLUMN, clusterUpgradeColumnInfo.getName());
-    Assert.assertEquals(null, clusterUpgradeColumnInfo.getLength());
-    Assert.assertEquals(Long.class, clusterUpgradeColumnInfo.getType());
-    Assert.assertEquals(null, clusterUpgradeColumnInfo.getDefaultValue());
-    Assert.assertEquals(true, clusterUpgradeColumnInfo.isNullable());
-
-    Map<String, Class> expectedCaptures = new HashMap<>();
-    expectedCaptures.put("id", Long.class);
-    expectedCaptures.put("name", String.class);
-    expectedCaptures.put("setting_type", String.class);
-    expectedCaptures.put("content", String.class);
-    expectedCaptures.put("updated_by", String.class);
-    expectedCaptures.put("update_timestamp", Long.class);
-
-    Map<String, Class> actualCaptures = new HashMap<>();
-    for(DBAccessor.DBColumnInfo settingColumnInfo : capturedSettingColumns.getValue()) {
-      actualCaptures.put(settingColumnInfo.getName(), settingColumnInfo.getType());
-    }
-
-    assertEquals(expectedCaptures, actualCaptures);
-
-    expectedCaptures = new HashMap<>();
-    expectedCaptures.put("extension_id", Long.class);
-    expectedCaptures.put("extension_name", String.class);
-    expectedCaptures.put("extension_version", String.class);
-
-    actualCaptures = new HashMap<>();
-    for(DBAccessor.DBColumnInfo settingColumnInfo : capturedExtensionColumns.getValue()) {
-      actualCaptures.put(settingColumnInfo.getName(), settingColumnInfo.getType());
-    }
-
-    assertEquals(expectedCaptures, actualCaptures);
-
-
-    expectedCaptures = new HashMap<>();
-    expectedCaptures.put("link_id", Long.class);
-    expectedCaptures.put("stack_id", Long.class);
-    expectedCaptures.put("extension_id", Long.class);
-
-    actualCaptures = new HashMap<>();
-    for(DBAccessor.DBColumnInfo settingColumnInfo : capturedExtensionLinkColumns.getValue()) {
-      actualCaptures.put(settingColumnInfo.getName(), settingColumnInfo.getType());
-    }
-
-    assertEquals(expectedCaptures, actualCaptures);
-
-    expectedCaptures = new HashMap<>();
-    expectedCaptures.put("id", Long.class);
-    expectedCaptures.put("component_id", Long.class);
-    expectedCaptures.put("upgrade_id", Long.class);
-    expectedCaptures.put("from_stack_id", Long.class);
-    expectedCaptures.put("to_stack_id", Long.class);
-
-    actualCaptures = new HashMap<>();
-    for (DBAccessor.DBColumnInfo historyColumnInfo : capturedHistoryColumns.getValue()) {
-      actualCaptures.put(historyColumnInfo.getName(), historyColumnInfo.getType());
-    }
-
-    DBAccessor.DBColumnInfo columnHelpURLInfo = capturedHelpURLColumnInfo.getValue();
-    Assert.assertNotNull(columnHelpURLInfo);
-    Assert.assertEquals(UpgradeCatalog240.HELP_URL_COLUMN, columnHelpURLInfo.getName());
-    Assert.assertEquals(Integer.valueOf(512), columnHelpURLInfo.getLength());
-    Assert.assertEquals(String.class, columnHelpURLInfo.getType());
-    Assert.assertEquals(null, columnHelpURLInfo.getDefaultValue());
-    Assert.assertEquals(true, columnHelpURLInfo.isNullable());
-
-    DBAccessor.DBColumnInfo columnRepeatToleranceInfo = capturedRepeatToleranceColumnInfo.getValue();
-    Assert.assertNotNull(columnRepeatToleranceInfo);
-    Assert.assertEquals(UpgradeCatalog240.REPEAT_TOLERANCE_COLUMN, columnRepeatToleranceInfo.getName());
-    Assert.assertEquals(Integer.class, columnRepeatToleranceInfo.getType());
-    Assert.assertEquals(1, columnRepeatToleranceInfo.getDefaultValue());
-    Assert.assertEquals(false, columnRepeatToleranceInfo.isNullable());
-
-    DBAccessor.DBColumnInfo columnRepeatToleranceEnabledInfo = capturedRepeatToleranceEnabledColumnInfo.getValue();
-    Assert.assertNotNull(columnRepeatToleranceEnabledInfo);
-    Assert.assertEquals(UpgradeCatalog240.REPEAT_TOLERANCE_ENABLED_COLUMN, columnRepeatToleranceEnabledInfo.getName());
-    Assert.assertEquals(Short.class, columnRepeatToleranceEnabledInfo.getType());
-    Assert.assertEquals(0, columnRepeatToleranceEnabledInfo.getDefaultValue());
-    Assert.assertEquals(false, columnRepeatToleranceEnabledInfo.isNullable());
-
-    DBAccessor.DBColumnInfo columnOccurrencesInfo = capturedOccurrencesColumnInfo.getValue();
-    Assert.assertNotNull(columnOccurrencesInfo);
-    Assert.assertEquals(UpgradeCatalog240.ALERT_CURRENT_OCCURRENCES_COLUMN, columnOccurrencesInfo.getName());
-    Assert.assertEquals(Long.class, columnOccurrencesInfo.getType());
-    Assert.assertEquals(1, columnOccurrencesInfo.getDefaultValue());
-    Assert.assertEquals(false, columnOccurrencesInfo.isNullable());
-
-    DBAccessor.DBColumnInfo columnFirmnessInfo = capturedFirmnessColumnInfo.getValue();
-    Assert.assertNotNull(columnFirmnessInfo);
-    Assert.assertEquals(UpgradeCatalog240.ALERT_CURRENT_FIRMNESS_COLUMN, columnFirmnessInfo.getName());
-    Assert.assertEquals(String.class, columnFirmnessInfo.getType());
-    Assert.assertEquals(AlertFirmness.HARD.name(), columnFirmnessInfo.getDefaultValue());
-    Assert.assertEquals(false, columnFirmnessInfo.isNullable());
-
-    DBAccessor.DBColumnInfo targetEnabledColumnInfo = capturedTargetEnabledColumnInfo.getValue();
-    Assert.assertNotNull(targetEnabledColumnInfo);
-    Assert.assertEquals(UpgradeCatalog240.ALERT_TARGET_ENABLED_COLUMN, targetEnabledColumnInfo.getName());
-    Assert.assertEquals(Short.class, targetEnabledColumnInfo.getType());
-    Assert.assertEquals(1, targetEnabledColumnInfo.getDefaultValue());
-    Assert.assertEquals(false, targetEnabledColumnInfo.isNullable());
-
-    assertEquals(expectedCaptures, actualCaptures);
-
-    // Verify blueprint_setting columns
-    expectedCaptures = new HashMap<>();
-    expectedCaptures.put(UpgradeCatalog240.ID, Long.class);
-    expectedCaptures.put(UpgradeCatalog240.BLUEPRINT_NAME_COL, String.class);
-    expectedCaptures.put(UpgradeCatalog240.SETTING_NAME_COL, String.class);
-    expectedCaptures.put(UpgradeCatalog240.SETTING_DATA_COL, char[].class);
-
-    actualCaptures = new HashMap<>();
-    for(DBAccessor.DBColumnInfo blueprintSettingsColumnInfo : capturedBlueprintSettingColumns.getValue()) {
-      actualCaptures.put(blueprintSettingsColumnInfo.getName(), blueprintSettingsColumnInfo.getType());
-    }
-
-    assertEquals(expectedCaptures, actualCaptures);
-
-    // Verify host_role_command column
-    DBAccessor.DBColumnInfo originalStartTimeInfo = hostRoleCommandOriginalStartTimeColumnInfo.getValue();
-    Assert.assertNotNull(originalStartTimeInfo);
-    Assert.assertEquals("original_start_time", originalStartTimeInfo.getName());
-    Assert.assertEquals(Long.class, originalStartTimeInfo.getType());
-    Assert.assertEquals(-1L, originalStartTimeInfo.getDefaultValue());
-
-    DBAccessor.DBColumnInfo viewInstanceEntityUrlColInfoValue = viewInstanceShortUrlInfo.getValue();
-    Assert.assertNotNull(viewInstanceEntityUrlColInfoValue);
-    Assert.assertEquals("short_url", viewInstanceEntityUrlColInfoValue.getName());
-    Assert.assertEquals(Long.class, viewInstanceEntityUrlColInfoValue.getType());
-
-    List<DBAccessor.DBColumnInfo> capturedViewUrlColumsValue = capturedViewUrlColums.getValue();
-    Assert.assertNotNull(capturedViewUrlColumsValue);
-    Assert.assertEquals(3, capturedViewUrlColumsValue.size());
-
-    // Verify cluster_type column
-    DBAccessor.DBColumnInfo viewInstanceEntityClusterTypeValue = viewInstanceClusterType.getValue();
-    Assert.assertNotNull(viewInstanceClusterType);
-    Assert.assertEquals("cluster_type", viewInstanceEntityClusterTypeValue.getName());
-    Assert.assertEquals(String.class, viewInstanceEntityClusterTypeValue.getType());
-
-    List<DBAccessor.DBColumnInfo> capturedRemoteAmbariClusterColumnsValue = capturedRemoteAmbariClusterColumns.getValue();
-    Assert.assertNotNull(capturedRemoteAmbariClusterColumnsValue);
-    Assert.assertEquals(5, capturedRemoteAmbariClusterColumnsValue.size());
-
-    List<DBAccessor.DBColumnInfo> capturedRemoteClusterServiceColumnsValue = capturedRemoteClusterServiceColumns.getValue();
-    Assert.assertNotNull(capturedRemoteClusterServiceColumnsValue);
-    Assert.assertEquals(3, capturedRemoteClusterServiceColumnsValue.size());
-
-    DBAccessor.DBColumnInfo clusterHandleColumn = capturedClusterHandleColumn.getValue();
-    Assert.assertEquals(UpgradeCatalog240.CLUSTER_HANDLE_COLUMN, clusterHandleColumn.getName());
-    Assert.assertEquals(Long.class, clusterHandleColumn.getType());
-
-    // Verify authenticated_user_id column
-    DBAccessor.DBColumnInfo requestScheduleUserIdInfoValue = requestScheduleUserIdInfo.getValue();
-    Assert.assertNotNull(requestScheduleUserIdInfoValue);
-    Assert.assertEquals("authenticated_user_id", requestScheduleUserIdInfoValue.getName());
-    Assert.assertEquals(Integer.class, requestScheduleUserIdInfoValue.getType());
-    Assert.assertEquals(null, requestScheduleUserIdInfoValue.getDefaultValue());
-
-    DBAccessor.DBColumnInfo provisionActionColumnInfoValue = provisionActionColumnInfo.getValue();
-    Assert.assertNotNull(provisionActionColumnInfoValue);
-    Assert.assertEquals(UpgradeCatalog240.PROVISION_ACTION_COL, provisionActionColumnInfoValue.getName());
-    Assert.assertEquals(String.class, provisionActionColumnInfoValue.getType());
-    Assert.assertEquals(true, provisionActionColumnInfoValue.isNullable());
-
-    verify(dbAccessor);
-  }
-
-  @Test
-  public void testExecuteDMLUpdates() throws Exception {
-    Method addNewConfigurationsFromXml = AbstractUpgradeCatalog.class.getDeclaredMethod("addNewConfigurationsFromXml");
-    Method updateAlerts = UpgradeCatalog240.class.getDeclaredMethod("updateAlerts");
-    Method addManageUserPersistedDataPermission = UpgradeCatalog240.class.getDeclaredMethod("addManageUserPersistedDataPermission");
-    Method allowClusterOperatorToManageCredentials = UpgradeCatalog240.class.getDeclaredMethod("allowClusterOperatorToManageCredentials");
-    Method addSettingPermission = UpgradeCatalog240.class.getDeclaredMethod("addSettingPermission");
-    Method addViewOperationalLogsPermission = UpgradeCatalog240.class.getDeclaredMethod("addViewOperationalLogsPermission");
-    Method updateHDFSConfigs = UpgradeCatalog240.class.getDeclaredMethod("updateHDFSConfigs");
-    Method updateHIVEConfigs = UpgradeCatalog240.class.getDeclaredMethod("updateHIVEConfigs");
-    Method updateAmsConfigs = UpgradeCatalog240.class.getDeclaredMethod("updateAMSConfigs");
-    Method updateClusterEnv = UpgradeCatalog240.class.getDeclaredMethod("updateClusterEnv");
-    Method updateSequenceForView = UpgradeCatalog240.class.getDeclaredMethod("updateSequenceForView");
-    Method adjustHiveJobTimestamps = UpgradeCatalog240.class.getDeclaredMethod("adjustHiveJobTimestamps");
-    Method updateHostRoleCommandTableDML = UpgradeCatalog240.class.getDeclaredMethod("updateHostRoleCommandTableDML");
-    Method updateKerberosEnv = UpgradeCatalog240.class.getDeclaredMethod("updateKerberosConfigs");
-    Method updateYarnEnv = UpgradeCatalog240.class.getDeclaredMethod("updateYarnEnv");
-    Method removeHiveOozieDBConnectionConfigs = UpgradeCatalog240.class.getDeclaredMethod("removeHiveOozieDBConnectionConfigs");
-    Method updateClustersAndHostsVersionStateTableDML = UpgradeCatalog240.class.getDeclaredMethod("updateClustersAndHostsVersionStateTableDML");
-    Method removeStandardDeviationAlerts = UpgradeCatalog240.class.getDeclaredMethod("removeStandardDeviationAlerts");
-    Method consolidateUserRoles = UpgradeCatalog240.class.getDeclaredMethod("consolidateUserRoles");
-    Method updateClusterInheritedPermissionsConfig = UpgradeCatalog240.class.getDeclaredMethod("updateClusterInheritedPermissionsConfig");
-    Method createRolePrincipals = UpgradeCatalog240.class.getDeclaredMethod("createRolePrincipals");
-    Method updateHDFSWidget = UpgradeCatalog240.class.getDeclaredMethod("updateHDFSWidgetDefinition");
-    Method upgradeCapSchedulerView = UpgradeCatalog240.class.getDeclaredMethod("upgradeCapSchedulerView");
-    Method updatePhoenixConfigs = UpgradeCatalog240.class.getDeclaredMethod("updatePhoenixConfigs");
-    Method updateSparkConfigs = UpgradeCatalog240.class.getDeclaredMethod("updateSparkConfigs");
-    Method updateHBaseConfigs = UpgradeCatalog240.class.getDeclaredMethod("updateHBaseConfigs");
-    Method updateKerberosDescriptorArtifacts = AbstractUpgradeCatalog.class.getDeclaredMethod("updateKerberosDescriptorArtifacts");
-    Method updateFalconConfigs = UpgradeCatalog240.class.getDeclaredMethod("updateFalconConfigs");
-    Method fixAuthorizationDescriptions = UpgradeCatalog240.class.getDeclaredMethod("fixAuthorizationDescriptions");
-    Method removeAuthorizations = UpgradeCatalog240.class.getDeclaredMethod("removeAuthorizations");
-    Method addConnectionTimeoutParamForWebAndMetricAlerts = AbstractUpgradeCatalog.class.getDeclaredMethod("addConnectionTimeoutParamForWebAndMetricAlerts");
-    Method addSliderClientConfig = UpgradeCatalog240.class.getDeclaredMethod("addSliderClientConfig");
-    Method updateRequestScheduleEntityUserIds = UpgradeCatalog240.class.getDeclaredMethod("updateRequestScheduleEntityUserIds");
-    Method updateRecoveryConfigurationDML = UpgradeCatalog240.class.getDeclaredMethod("updateRecoveryConfigurationDML");
-    Method removeAtlasMetaserverAlert = UpgradeCatalog240.class.getDeclaredMethod("removeAtlasMetaserverAlert");
-    Method updateRangerHbasePluginProperties = UpgradeCatalog240.class.getDeclaredMethod("updateRangerHbasePluginProperties");
-
-    Capture<String> capturedStatements = newCapture(CaptureType.ALL);
-
-    DBAccessor dbAccessor = createStrictMock(DBAccessor.class);
-    expect(dbAccessor.executeUpdate(capture(capturedStatements))).andReturn(1).times(7);
-
-    Capture<String> capturedTezViewUpdate = newCapture();
-    expect(dbAccessor.executeUpdate(capture(capturedTezViewUpdate))).andReturn(1).once();
-
-    Capture<String> capturedPigSmokeTestEntityUpdate = newCapture();
-    expect(dbAccessor.executeUpdate(capture(capturedPigSmokeTestEntityUpdate))).andReturn(1).once();
-
-    UpgradeCatalog240 upgradeCatalog240 = createMockBuilder(UpgradeCatalog240.class)
-            .addMockedMethod(addNewConfigurationsFromXml)
-            .addMockedMethod(updateAlerts)
-            .addMockedMethod(addSettingPermission)
-            .addMockedMethod(addViewOperationalLogsPermission)
-            .addMockedMethod(addManageUserPersistedDataPermission)
-            .addMockedMethod(allowClusterOperatorToManageCredentials)
-            .addMockedMethod(updateHDFSConfigs)
-            .addMockedMethod(updateHIVEConfigs)
-            .addMockedMethod(updateAmsConfigs)
-            .addMockedMethod(updateClusterEnv)
-            .addMockedMethod(updateSequenceForView)
-            .addMockedMethod(adjustHiveJobTimestamps)
-            .addMockedMethod(updateHostRoleCommandTableDML)
-            .addMockedMethod(updateKerberosEnv)
-            .addMockedMethod(updateYarnEnv)
-            .addMockedMethod(removeHiveOozieDBConnectionConfigs)
-            .addMockedMethod(updateClustersAndHostsVersionStateTableDML)
-            .addMockedMethod(removeStandardDeviationAlerts)
-            .addMockedMethod(consolidateUserRoles)
-            .addMockedMethod(updateClusterInheritedPermissionsConfig)
-            .addMockedMethod(createRolePrincipals)
-            .addMockedMethod(updateHDFSWidget)
-            .addMockedMethod(upgradeCapSchedulerView)
-            .addMockedMethod(updatePhoenixConfigs)
-            .addMockedMethod(updateSparkConfigs)
-            .addMockedMethod(updateKerberosDescriptorArtifacts)
-            .addMockedMethod(updateFalconConfigs)
-            .addMockedMethod(fixAuthorizationDescriptions)
-            .addMockedMethod(removeAuthorizations)
-            .addMockedMethod(addConnectionTimeoutParamForWebAndMetricAlerts)
-            .addMockedMethod(updateHBaseConfigs)
-            .addMockedMethod(addSliderClientConfig)
-            .addMockedMethod(updateRequestScheduleEntityUserIds)
-            .addMockedMethod(updateRecoveryConfigurationDML)
-            .addMockedMethod(removeAtlasMetaserverAlert)
-            .addMockedMethod(updateRangerHbasePluginProperties)
-            .createMock();
-
-    Field field = AbstractUpgradeCatalog.class.getDeclaredField("dbAccessor");
-    field.set(upgradeCatalog240, dbAccessor);
-
-    upgradeCatalog240.addNewConfigurationsFromXml();
-    upgradeCatalog240.updateAlerts();
-    upgradeCatalog240.addSettingPermission();
-    upgradeCatalog240.addViewOperationalLogsPermission();
-    upgradeCatalog240.addManageUserPersistedDataPermission();
-    upgradeCatalog240.allowClusterOperatorToManageCredentials();
-    upgradeCatalog240.updateHDFSConfigs();
-    upgradeCatalog240.updateHIVEConfigs();
-    upgradeCatalog240.updateAMSConfigs();
-    upgradeCatalog240.updateClusterEnv();
-    upgradeCatalog240.updateSequenceForView();
-    upgradeCatalog240.updateHostRoleCommandTableDML();
-    upgradeCatalog240.updateKerberosConfigs();
-    upgradeCatalog240.updateYarnEnv();
-    upgradeCatalog240.removeHiveOozieDBConnectionConfigs();
-    upgradeCatalog240.updateClustersAndHostsVersionStateTableDML();
-    upgradeCatalog240.removeStandardDeviationAlerts();
-    upgradeCatalog240.consolidateUserRoles();
-    upgradeCatalog240.createRolePrincipals();
-    upgradeCatalog240.updateClusterInheritedPermissionsConfig();
-    upgradeCatalog240.updateHDFSWidgetDefinition();
-    upgradeCatalog240.upgradeCapSchedulerView();
-    upgradeCatalog240.updatePhoenixConfigs();
-    upgradeCatalog240.updateSparkConfigs();
-    upgradeCatalog240.updateKerberosDescriptorArtifacts();
-    upgradeCatalog240.updateFalconConfigs();
-    upgradeCatalog240.fixAuthorizationDescriptions();
-    upgradeCatalog240.removeAuthorizations();
-    upgradeCatalog240.addConnectionTimeoutParamForWebAndMetricAlerts();
-    upgradeCatalog240.updateHBaseConfigs();
-    upgradeCatalog240.addSliderClientConfig();
-    upgradeCatalog240.updateRequestScheduleEntityUserIds();
-    upgradeCatalog240.updateRecoveryConfigurationDML();
-    upgradeCatalog240.removeAtlasMetaserverAlert();
-    upgradeCatalog240.updateRangerHbasePluginProperties();
-    upgradeCatalog240.adjustHiveJobTimestamps();
-
-    replay(upgradeCatalog240, dbAccessor);
-
-    upgradeCatalog240.executeDMLUpdates();
-
-    verify(upgradeCatalog240, dbAccessor);
-
-    List<String> statements = capturedStatements.getValues();
-    Assert.assertNotNull(statements);
-    Assert.assertEquals(7, statements.size());
-    Assert.assertTrue(statements.contains("UPDATE adminpermission SET sort_order=1 WHERE permission_name='AMBARI.ADMINISTRATOR'"));
-    Assert.assertTrue(statements.contains("UPDATE adminpermission SET sort_order=2 WHERE permission_name='CLUSTER.ADMINISTRATOR'"));
-    Assert.assertTrue(statements.contains("UPDATE adminpermission SET sort_order=3 WHERE permission_name='CLUSTER.OPERATOR'"));
-    Assert.assertTrue(statements.contains("UPDATE adminpermission SET sort_order=4 WHERE permission_name='SERVICE.ADMINISTRATOR'"));
-    Assert.assertTrue(statements.contains("UPDATE adminpermission SET sort_order=5 WHERE permission_name='SERVICE.OPERATOR'"));
-    Assert.assertTrue(statements.contains("UPDATE adminpermission SET sort_order=6 WHERE permission_name='CLUSTER.USER'"));
-    Assert.assertTrue(statements.contains("UPDATE adminpermission SET sort_order=7 WHERE permission_name='VIEW.USER'"));
-
-    Assert.assertNotNull(capturedTezViewUpdate.getValue());
-    Assert.assertEquals("UPDATE viewinstanceproperty SET name = 'yarn.ats.url' where name = 'yarn.timeline-server.url'",
-      capturedTezViewUpdate.getValue());
-
-    Assert.assertNotNull(capturedPigSmokeTestEntityUpdate.getValue());
-    Assert.assertEquals("UPDATE viewentity " +
-        "SET class_name = 'org.apache.ambari.view.pig.persistence.SmokeTestEntity' " +
-        "WHERE class_name = 'org.apache.ambari.view.pig.persistence.DataStoreStorage$SmokeTestEntity'",
-      capturedPigSmokeTestEntityUpdate.getValue());
-  }
-
-  @Test
-  public void testRemoveHiveOozieDBConnectionConfigs() throws Exception{
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(
-            AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Config mockOozieEnv = easyMockSupport.createNiceMock(Config.class);
-    final Config mockHiveEnv = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> propertiesExpectedOozieEnv = new HashMap<String, String>();
-    propertiesExpectedOozieEnv.put("oozie_derby_database", "Derby");
-    propertiesExpectedOozieEnv.put("property", "value");
-    // Imitate missing property
-    // propertiesExpectedOozieEnv.put("oozie_hostname", "hostname");
-    final Map<String, String> propertiesExpectedHiveEnv = new HashMap<String, String>();
-    propertiesExpectedHiveEnv.put("hive_hostname", "hostname");
-
-    final Injector mockInjector = Guice.createInjector(new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        binder.bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        binder.bind(Clusters.class).toInstance(mockClusters);
-        binder.bind(EntityManager.class).toInstance(entityManager);
-        binder.bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        binder.bind(PasswordEncoder.class).toInstance(createNiceMock(PasswordEncoder.class));
-        binder.bind(HookContextFactory.class).toInstance(createMock(HookContextFactory.class));
-        binder.bind(HookService.class).toInstance(createMock(HookService.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    expect(mockClusterExpected.getDesiredConfigByType("oozie-env")).andReturn(mockOozieEnv).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnv).atLeastOnce();
-    expect(mockOozieEnv.getProperties()).andReturn(propertiesExpectedOozieEnv).anyTimes();
-    expect(mockHiveEnv.getProperties()).andReturn(propertiesExpectedHiveEnv).anyTimes();
-
-    Capture<Map<String, String>> oozieCapture =  newCapture();
-    Capture<Map<String, String>> hiveCapture =  newCapture();
-    expect(mockAmbariManagementController.createConfig(eq(mockClusterExpected), eq("oozie-env"),
-        capture(oozieCapture), anyString(), (Map<String, Map<String, String>>)anyObject())).andReturn(null).once();
-    expect(mockAmbariManagementController.createConfig(eq(mockClusterExpected), eq("hive-env"),
-            capture(hiveCapture), anyString(), (Map<String, Map<String, String>>)anyObject())).andReturn(null).once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog240.class).removeHiveOozieDBConnectionConfigs();
-    easyMockSupport.verifyAll();
-
-    assertEquals("value", oozieCapture.getValue().get("property"));
-    assertNull(oozieCapture.getValue().get("oozie_derby_database"));
-    assertNull(oozieCapture.getValue().get("oozie_hostname"));
-    assertNull(hiveCapture.getValue().get("hive_hostname"));
-  }
-
-  @Test
-  public void testUpdateFalconConfigs() throws Exception{
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(
-            AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Config mockFalconEnv = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> propertiesExpectedFalconEnv = new HashMap<String, String>();
-    propertiesExpectedFalconEnv.put("falcon_store_uri", "file:///hadoop/falcon/store");
-    propertiesExpectedFalconEnv.put("content", "# content");
-    propertiesExpectedFalconEnv.put("property", "value");
-
-    final String applicationServicesOldPropertyValue =
-        "org.apache.falcon.security.AuthenticationInitializationService,\\\n" +
-        "      org.apache.falcon.workflow.WorkflowJobEndNotificationService, \\\n" +
-        "      org.apache.falcon.service.ProcessSubscriberService,\\\n" +
-        "      org.apache.falcon.entity.store.ConfigurationStore,\\\n" +
-        "      org.apache.falcon.rerun.service.RetryService,\\\n" +
-        "      org.apache.falcon.rerun.service.LateRunService,\\\n" +
-        "      org.apache.falcon.service.LogCleanupService,\\\n" +
-        "      org.apache.falcon.metadata.MetadataMappingService";
-
-    final String applicationServicesExpectedPropertyValue =
-        "org.apache.falcon.security.AuthenticationInitializationService,\\\n" +
-        "      org.apache.falcon.workflow.WorkflowJobEndNotificationService, \\\n" +
-        "      org.apache.falcon.service.ProcessSubscriberService,\\\n" +
-        "      org.apache.falcon.entity.store.ConfigurationStore,\\\n" +
-        "      org.apache.falcon.rerun.service.RetryService,\\\n" +
-        "      org.apache.falcon.rerun.service.LateRunService,\\\n" +
-        "      org.apache.falcon.service.LogCleanupService,\\\n" +
-        "      org.apache.falcon.metadata.MetadataMappingService{{atlas_application_class_addition}}";
-
-    final Config falconStartupConfig = easyMockSupport.createNiceMock(Config.class);
-
-    final Map<String, String> falconStartupConfigProperties= new HashMap<String, String>();
-    falconStartupConfigProperties.put("*.application.services", applicationServicesOldPropertyValue);
-    falconStartupConfigProperties.put("property", "value");
-    final Injector mockInjector = Guice.createInjector(new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        binder.bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        binder.bind(Clusters.class).toInstance(mockClusters);
-        binder.bind(EntityManager.class).toInstance(entityManager);
-        binder.bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        binder.bind(PasswordEncoder.class).toInstance(createNiceMock(PasswordEncoder.class));
-        binder.bind(HookContextFactory.class).toInstance(createMock(HookContextFactory.class));
-        binder.bind(HookService.class).toInstance(createMock(HookService.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).once();
-
-    expect(mockClusterExpected.getDesiredConfigByType("falcon-env")).andReturn(mockFalconEnv).atLeastOnce();
-    expect(mockFalconEnv.getProperties()).andReturn(propertiesExpectedFalconEnv).anyTimes();
-
-    expect(mockClusterExpected.getDesiredConfigByType("falcon-startup.properties")).andReturn(falconStartupConfig).atLeastOnce();
-    expect(falconStartupConfig.getProperties()).andReturn(falconStartupConfigProperties).anyTimes();
-
-    Capture<Map<String, String>> falconCapture =  newCapture();
-    expect(mockAmbariManagementController.createConfig(eq(mockClusterExpected), eq("falcon-env"),
-        capture(falconCapture), anyString(), (Map<String, Map<String, String>>) anyObject())).andReturn(null).once();
-
-    Capture<Map<String, String>> falconCapture2 =  newCapture();
-    expect(mockAmbariManagementController.createConfig(eq(mockClusterExpected), eq("falcon-env"),
-        capture(falconCapture2), anyString(), (Map<String, Map<String, String>>) anyObject())).andReturn(null).once();
-
-    Capture<Map<String, String>> falconStartupCapture =  newCapture();
-    expect(mockAmbariManagementController.createConfig(eq(mockClusterExpected), eq("falcon-startup.properties"),
-        capture(falconStartupCapture), anyString(), (Map<String, Map<String, String>>)anyObject())).andReturn(null).once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog240.class).updateFalconConfigs();
-    easyMockSupport.verifyAll();
-
-    final String expectredEnvContent = "# content\n" +
-                                       "\n" +
-                                       "{% if falcon_atlas_support %}\n" +
-                                       "# Add the Atlas Falcon hook to the Falcon classpath\n" +
-                                       "export FALCON_EXTRA_CLASS_PATH={{atlas_hook_cp}}${FALCON_EXTRA_CLASS_PATH}\n" +
-                                       "{% endif %}";
-
-    assertEquals("value", falconCapture.getValue().get("property"));
-    assertEquals("# content", falconCapture.getValue().get("content"));
-    assertNull(falconCapture.getValue().get("falcon_store_uri"));
-
-    assertEquals(expectredEnvContent, falconCapture2.getValue().get("content"));
-
-    assertEquals("value", falconStartupCapture.getValue().get("property"));
-    assertEquals(applicationServicesExpectedPropertyValue, falconStartupCapture.getValue().get("*.application.services"));
-  }
-
-  @Test
-  public void testUpdateHbaseConfigs() throws Exception{
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(
-        AmbariManagementController.class);
-    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
-
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockCluster = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Injector mockInjector = Guice.createInjector(new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        binder.bind(ConfigHelper.class).toInstance(mockConfigHelper);
-        binder.bind(Clusters.class).toInstance(mockClusters);
-        binder.bind(EntityManager.class).toInstance(entityManager);
-        binder.bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        binder.bind(PasswordEncoder.class).toInstance(createNiceMock(PasswordEncoder.class));
-        binder.bind(HookContextFactory.class).toInstance(createMock(HookContextFactory.class));
-        binder.bind(HookService.class).toInstance(createMock(HookService.class));
-
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).anyTimes();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockCluster);
-    }}).anyTimes();
-
-    expect(mockCluster.getServices()).andReturn(new HashMap<String, Service>(){{put("HBASE",null);}}).anyTimes();
-    expect(mockCluster.getSecurityType()).andReturn(SecurityType.KERBEROS).anyTimes();
-
-    final Config mockHbaseSiteConfigs = easyMockSupport.createNiceMock(Config.class);
-    expect(mockCluster.getDesiredConfigByType("hbase-site")).andReturn(mockHbaseSiteConfigs).atLeastOnce();
-    expect(mockHbaseSiteConfigs.getProperties()).andReturn(new HashMap<String, String>(){{
-      put("hbase.coprocessor.regionserver.classes","{{hbase_coprocessor_regionserver_classes}}");
-      put("hbase.coprocessor.region.classes","{{hbase_coprocessor_region_classes}}");
-    }}).anyTimes();
-
-    final Config mockRangerHbaseConfigs = easyMockSupport.createNiceMock(Config.class);
-    expect(mockCluster.getDesiredConfigByType("ranger-hbase-plugin-properties")).andReturn(mockRangerHbaseConfigs).atLeastOnce();
-    expect(mockRangerHbaseConfigs.getProperties()).andReturn(new HashMap<String, String>(){{
-      put("ranger-hbase-plugin-enabled", "yes");
-    }}).anyTimes();
-
-    final Config mockRangerEnvConfigs = easyMockSupport.createNiceMock(Config.class);
-    expect(mockCluster.getDesiredConfigByType("ranger-env")).andReturn(mockRangerEnvConfigs).atLeastOnce();
-    expect(mockRangerEnvConfigs.getProperties()).andReturn(new HashMap<String, String>(){{
-      put("xml_configurations_supported", "true");
-    }}).anyTimes();
-
-
-
-
-    Capture<Map<String, String>> hbaseCapture =  newCapture();
-    expect(mockAmbariManagementController.createConfig(eq(mockCluster), eq("hbase-site"),
-        capture(hbaseCapture), anyString(), (Map<String, Map<String, String>>)anyObject())).andReturn(null).once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog240.class).updateHBaseConfigs();
-    easyMockSupport.verifyAll();
-
-    assertEquals("org.apache.ranger.authorization.hbase.RangerAuthorizationCoprocessor", hbaseCapture.getValue().get("hbase.coprocessor.regionserver.classes"));
-    assertEquals("org.apache.hadoop.hbase.security.token.TokenProvider,org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint,org.apache.ranger.authorization.hbase.RangerAuthorizationCoprocessor", hbaseCapture.getValue().get("hbase.coprocessor.region.classes"));
-  }
-
-  @Test
-  public void test_addParam_ParamsNotAvailable() {
-
-    UpgradeCatalog240 upgradeCatalog240 = new UpgradeCatalog240(injector);
-    String inputSource = "{ \"path\" : \"test_path\", \"type\" : \"SCRIPT\"}";
-    List<String> params = Arrays.asList("connection.timeout", "checkpoint.time.warning.threshold", "checkpoint.time.critical.threshold");
-    String expectedSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"name\":\"connection.timeout\",\"display_name\":\"Connection Timeout\",\"value\":5.0,\"type\":\"NUMERIC\",\"description\":\"The maximum time before this alert is considered to be CRITICAL\",\"units\":\"seconds\",\"threshold\":\"CRITICAL\"},{\"name\":\"checkpoint.time.warning.threshold\",\"display_name\":\"Checkpoint Warning\",\"value\":2.0,\"type\":\"PERCENT\",\"description\":\"The percentage of the last checkpoint time greater than the interval in order to trigger a warning alert.\",\"units\":\"%\",\"threshold\":\"WARNING\"},{\"name\":\"checkpoint.time.critical.threshold\",\"display_name\":\"Checkpoint Critical\",\"value\":4.0,\"type\":\"PERCENT\",\"description\":\"The percentage of the last checkpoint time greater than the interval in order to trigger a critical alert.\",\"units\":\"%\",\"threshold\":\"CRITICAL\"}]}";
-
-    String result = upgradeCatalog240.addParam(inputSource, params);
-    Assert.assertEquals(result, expectedSource);
-  }
-
-  @Test
-  public void test_addParam_ParamsAvailableWithOneOFNeededItem() {
-
-    UpgradeCatalog240 upgradeCatalog240 = new UpgradeCatalog240(injector);
-    String inputSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"name\":\"connection.timeout\",\"display_name\":\"Connection Timeout\",\"value\":5.0,\"type\":\"NUMERIC\",\"description\":\"The maximum time before this alert is considered to be CRITICAL\",\"units\":\"seconds\",\"threshold\":\"CRITICAL\"}]}";
-    List<String> params = new ArrayList<>(Arrays.asList("connection.timeout", "checkpoint.time.warning.threshold", "checkpoint.time.critical.threshold"));
-    String expectedSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"name\":\"connection.timeout\",\"display_name\":\"Connection Timeout\",\"value\":5.0,\"type\":\"NUMERIC\",\"description\":\"The maximum time before this alert is considered to be CRITICAL\",\"units\":\"seconds\",\"threshold\":\"CRITICAL\"},{\"name\":\"checkpoint.time.warning.threshold\",\"display_name\":\"Checkpoint Warning\",\"value\":2.0,\"type\":\"PERCENT\",\"description\":\"The percentage of the last checkpoint time greater than the interval in order to trigger a warning alert.\",\"units\":\"%\",\"threshold\":\"WARNING\"},{\"name\":\"checkpoint.time.critical.threshold\",\"display_name\":\"Checkpoint Critical\",\"value\":4.0,\"type\":\"PERCENT\",\"description\":\"The percentage of the last checkpoint time greater than the interval in order to trigger a critical alert.\",\"units\":\"%\",\"threshold\":\"CRITICAL\"}]}";
-
-    String result = upgradeCatalog240.addParam(inputSource, params);
-    Assert.assertEquals(result, expectedSource);
-  }
-
-  /**
-   * Test that dfs.internal.nameservices is not affected
-   * Also, dfs.client.retry.policy.enabled is reset
-   * @throws Exception
-   */
-  @Test
-  public void testHdfsSiteUpdateConfigs2() throws Exception{
-    Map<String, String> oldPropertiesHdfsSite = new HashMap<String, String>() {
-      {
-        put("dfs.client.retry.policy.enabled", "true");
-      }
-    };
-    Map<String, String> newPropertiesHdfsSite = new HashMap<String, String>() {
-      {
-        put("dfs.client.retry.policy.enabled", "false");
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    final Service service = createStrictMock(Service.class);
-    final Map<String, Service> services = Collections.singletonMap("HDFS", service);
-    Config mockHdfsSite = easyMockSupport.createNiceMock(Config.class);
-
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).anyTimes();
-    expect(cluster.getDesiredConfigByType("hdfs-site")).andReturn(mockHdfsSite).atLeastOnce();
-    expect(mockHdfsSite.getProperties()).andReturn(oldPropertiesHdfsSite).anyTimes();
-    expect(cluster.getServices()).andReturn(services).once();
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockHdfsSite, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-        .addMockedMethod("createConfiguration")
-        .addMockedMethod("getClusters", new Class[] { })
-        .addMockedMethod("createConfig")
-        .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-        .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-                                   anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog240(injector2).updateHDFSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesHdfsSite, updatedProperties).areEqual());
-  }
-
-  /**
-   * Test that dfs.nameservices is copied over to dfs.internal.nameservices
-   * @throws Exception
-   */
-  @Test
-  public void testHdfsSiteUpdateConfigs() throws Exception{
-    Map<String, String> oldPropertiesHdfsSite = new HashMap<String, String>() {
-      {
-        put("dfs.nameservices", "nnha");
-      }
-    };
-    Map<String, String> newPropertiesHdfsSite = new HashMap<String, String>() {
-      {
-        put("dfs.nameservices", "nnha");
-        put("dfs.internal.nameservices", "nnha");
-      }
-    };
-    Map<String, String> oldPropertiesHadoopEnv = new HashMap<String, String>() {
-      {
-        put("keyserver_port", " ");
-      }
-    };
-    Map<String, String> newPropertiesHadoopEnv = new HashMap<String, String>() {
-      {
-        put("keyserver_port", "");
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    final Service service = createStrictMock(Service.class);
-    final Map<String, Service> services = Collections.singletonMap("HDFS", service);
-    Config mockHdfsSite = easyMockSupport.createNiceMock(Config.class);
-    Config mockHadoopEnv = easyMockSupport.createNiceMock(Config.class);
-
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).anyTimes();
-    expect(cluster.getDesiredConfigByType("hdfs-site")).andReturn(mockHdfsSite).atLeastOnce();
-    expect(cluster.getDesiredConfigByType("hadoop-env")).andReturn(mockHadoopEnv).atLeastOnce();
-    expect(mockHdfsSite.getProperties()).andReturn(oldPropertiesHdfsSite).anyTimes();
-    expect(mockHadoopEnv.getProperties()).andReturn(oldPropertiesHadoopEnv).anyTimes();
-    expect(cluster.getServices()).andReturn(services).once();
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockHdfsSite, mockHadoopEnv, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-        .addMockedMethod("createConfiguration")
-        .addMockedMethod("getClusters", new Class[] { })
-        .addMockedMethod("createConfig")
-        .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-        .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesCaptureHdfsSite = EasyMock.newCapture();
-    Capture<Map> propertiesCaptureHadoopEnv = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), eq("hdfs-site"), capture(propertiesCaptureHdfsSite), anyString(),
-                                   anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-    expect(controller.createConfig(anyObject(Cluster.class), eq("hadoop-env"), capture(propertiesCaptureHadoopEnv), anyString(),
-        anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog240(injector2).updateHDFSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedPropertiesHdfsSite = propertiesCaptureHdfsSite.getValue();
-    assertTrue(Maps.difference(newPropertiesHdfsSite, updatedPropertiesHdfsSite).areEqual());
-
-    Map<String, String> updatedPropertiesHadoopEnv = propertiesCaptureHadoopEnv.getValue();
-    assertTrue(Maps.difference(newPropertiesHadoopEnv, updatedPropertiesHadoopEnv).areEqual());
-  }
-
-  @Test
-  public void testYarnEnvUpdateConfigs() throws Exception{
-
-    Map<String, String> oldPropertiesYarnEnv = new HashMap<String, String>() {
-      {
-        put("content", "export YARN_HISTORYSERVER_HEAPSIZE={{apptimelineserver_heapsize}}");
-      }
-    };
-    Map<String, String> newPropertiesYarnEnv = new HashMap<String, String>() {
-      {
-        put("content", "# export YARN_HISTORYSERVER_HEAPSIZE={{apptimelineserver_heapsize}}" +
-                "\n\n      # Specify the max Heapsize for the timeline server using a numerical value\n" +
-                "      # in the scale of MB. For example, to specify an jvm option of -Xmx1000m, set\n" +
-                "      # the value to 1024.\n" +
-                "      # This value will be overridden by an Xmx setting specified in either YARN_OPTS\n" +
-                "      # and/or YARN_TIMELINESERVER_OPTS.\n" +
-                "      # If not specified, the default value will be picked from either YARN_HEAPMAX\n" +
-                "      # or JAVA_HEAP_MAX with YARN_HEAPMAX as the preferred option of the two.\n" +
-                "      export YARN_TIMELINESERVER_HEAPSIZE={{apptimelineserver_heapsize}}");
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    Config mockYarnEnv = easyMockSupport.createNiceMock(Config.class);
-
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("yarn-env")).andReturn(mockYarnEnv).atLeastOnce();
-    expect(mockYarnEnv.getProperties()).andReturn(oldPropertiesYarnEnv).anyTimes();
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockYarnEnv, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-            .addMockedMethod("createConfiguration")
-            .addMockedMethod("getClusters", new Class[] { })
-            .addMockedMethod("createConfig")
-            .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-            .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-            anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog240(injector2).updateYarnEnv();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesYarnEnv, updatedProperties).areEqual());
-  }
-
-  @Test
-  public void testSparkConfigUpdate() throws Exception{
-
-    Map<String, String> oldPropertiesSparkDefaults = new HashMap<String, String>() {
-      {
-        put("spark.driver.extraJavaOptions", "-Dhdp.version={{hdp_full_version}}");
-        put("spark.yarn.am.extraJavaOptions", "-Dhdp.version={{hdp_full_version}}");
-      }
-    };
-    Map<String, String> newPropertiesSparkDefaults = new HashMap<String, String>() {
-      {
-        put("spark.driver.extraJavaOptions", "-Dhdp.version={{full_stack_version}}");
-        put("spark.yarn.am.extraJavaOptions", "-Dhdp.version={{full_stack_version}}");
-      }
-    };
-
-    Map<String, String> oldPropertiesSparkJavaOpts = new HashMap<String, String>() {
-      {
-        put("content", "-Dhdp.version={{hdp_full_version}}");
-      }
-    };
-    Map<String, String> newPropertiesSparkJavaOpts = new HashMap<String, String>() {
-      {
-        put("content", "-Dhdp.version={{full_stack_version}}");
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    Config mockSparkDefaults = easyMockSupport.createNiceMock(Config.class);
-    Config mockSparkJavaOpts = easyMockSupport.createNiceMock(Config.class);
-
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("spark-defaults")).andReturn(mockSparkDefaults).atLeastOnce();
-    expect(mockSparkDefaults.getProperties()).andReturn(oldPropertiesSparkDefaults).anyTimes();
-    expect(cluster.getDesiredConfigByType("spark-javaopts-properties")).andReturn(mockSparkJavaOpts).atLeastOnce();
-    expect(mockSparkJavaOpts.getProperties()).andReturn(oldPropertiesSparkJavaOpts).anyTimes();
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockSparkDefaults, mockSparkJavaOpts, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-        .addMockedMethod("createConfiguration")
-        .addMockedMethod("getClusters", new Class[] { })
-        .addMockedMethod("createConfig")
-        .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-        .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesSparkDefaultsCapture = EasyMock.newCapture();
-    Capture<Map> propertiesSparkJavaOptsCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), eq("spark-defaults"), capture(propertiesSparkDefaultsCapture), anyString(),
-        anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-    expect(controller.createConfig(anyObject(Cluster.class), eq("spark-javaopts-properties"), capture(propertiesSparkJavaOptsCapture), anyString(),
-        anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog240(injector2).updateSparkConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedSparkDefaultsProperties = propertiesSparkDefaultsCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesSparkDefaults, updatedSparkDefaultsProperties).areEqual());
-
-    Map<String, String> updatedSparkJavaOptsProperties = propertiesSparkJavaOptsCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesSparkJavaOpts, updatedSparkJavaOptsProperties).areEqual());
-  }
-
-  @Test
-  public void testAmsHbaseEnvUpdateConfigs() throws Exception{
-
-    Map<String, String> oldPropertiesAmsHbaseEnv = new HashMap<String, String>() {
-      {
-        put("content", "some_content");
-      }
-    };
-    Map<String, String> newPropertiesAmsHbaseEnv = new HashMap<String, String>() {
-      {
-        put("content", "some_content"+ "\n # Explicitly Setting HBASE_HOME for AMS HBase so that there is no conflict\n" +
-          "export HBASE_HOME={{ams_hbase_home_dir}}\n");
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    Config mockAmsHbaseEnv = easyMockSupport.createNiceMock(Config.class);
-
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("ams-hbase-env")).andReturn(mockAmsHbaseEnv).atLeastOnce();
-    expect(mockAmsHbaseEnv.getProperties()).andReturn(oldPropertiesAmsHbaseEnv).anyTimes();
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockAmsHbaseEnv, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-      .addMockedMethod("createConfiguration")
-      .addMockedMethod("getClusters", new Class[] { })
-      .addMockedMethod("createConfig")
-      .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-      .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-      anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog240(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesAmsHbaseEnv, updatedProperties).areEqual());
-  }
-
-  @Test
-  public void testAmsSiteUpdateConfigs() throws Exception{
-
-    Map<String, String> oldPropertiesAmsSite = new HashMap<String, String>() {
-      {
-        put("timeline.metrics.sink.collection.period", "60");
-        put("timeline.metrics.host.aggregator.ttl", "86400");
-        put("timeline.metrics.cluster.aggregator.second.ttl", "604800");
-      }
-    };
-    Map<String, String> newPropertiesAmsSite = new HashMap<String, String>() {
-      {
-        put("timeline.metrics.sink.collection.period", "10");
-        put("timeline.metrics.host.aggregator.ttl", "86400");
-        put("timeline.metrics.cluster.aggregator.second.ttl", "259200");
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    Config mockAmsSite = easyMockSupport.createNiceMock(Config.class);
-
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("ams-site")).andReturn(mockAmsSite).atLeastOnce();
-    expect(mockAmsSite.getProperties()).andReturn(oldPropertiesAmsSite).anyTimes();
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockAmsSite, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-      .addMockedMethod("createConfiguration")
-      .addMockedMethod("getClusters", new Class[] { })
-      .addMockedMethod("createConfig")
-      .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-      .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-      anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog240(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesAmsSite, updatedProperties).areEqual());
-  }
-
-  @Test
-  public void testAmsHbaseSiteUpdateConfigs() throws Exception{
-
-    Map<String, String> oldPropertiesAmsHbaseSite = new HashMap<String, String>() {
-      {
-        put("hbase.rpc.timeout", "30000");
-        put("hbase.normalizer.enabled", String.valueOf(true));
-      }
-    };
-    Map<String, String> newPropertiesAmsHbaseSite = new HashMap<String, String>() {
-      {
-        put("hbase.rpc.timeout", "300000");
-        put("hbase.normalizer.enabled", String.valueOf(false));
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    Config mockAmsHbaseSite = easyMockSupport.createNiceMock(Config.class);
-
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("ams-hbase-site")).andReturn(mockAmsHbaseSite).atLeastOnce();
-    expect(mockAmsHbaseSite.getProperties()).andReturn(oldPropertiesAmsHbaseSite).anyTimes();
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockAmsHbaseSite, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-      .addMockedMethod("createConfiguration")
-      .addMockedMethod("getClusters", new Class[] { })
-      .addMockedMethod("createConfig")
-      .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-      .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-      anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog240(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesAmsHbaseSite, updatedProperties).areEqual());
-  }
-
-  @Test
-  public void testUpdateKerberosConfiguration() throws Exception {
-    final AmbariManagementController controller = createMock(AmbariManagementController.class);
-    final AmbariMetaInfo metaInfo = createMock(AmbariMetaInfo.class);
-    final StackInfo stackInfo = createMock(StackInfo.class);
-    final ServiceInfo serviceInfo = createMock(ServiceInfo.class);
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    final OsFamily osFamily = createNiceMock(OsFamily.class);
-
-    final Map<String, String> propertiesKerberosEnv = new HashMap<String, String>() {
-      {
-        put("realm", "EXAMPLE.COM");
-        put("encryption_types", "aes des3-cbc-sha1 rc4 des-cbc-md5");
-        put("kdc_host", "c6407.ambari.apache.org");
-        put("admin_server_host", "c6407.ambari.apache.org");
-        put("kdc_type", "mit-kdc");
-      }
-    };
-
-    final Map<String, String> propertiesKrb5Conf = new HashMap<String, String>() {
-      {
-        put("content", "\n" +
-            "[libdefaults]\n" +
-            "  renew_lifetime = 7d\n" +
-            "  forwardable = true\n" +
-            "  default_realm = {{realm}}\n" +
-            "  ticket_lifetime = 24h\n" +
-            "  dns_lookup_realm = false\n" +
-            "  dns_lookup_kdc = false\n" +
-            "  #default_tgs_enctypes = {{encryption_types}}\n" +
-            "  #default_tkt_enctypes = {{encryption_types}}\n" +
-            "\n" +
-            "{% if domains %}\n" +
-            "[domain_realm]\n" +
-            "{% for domain in domains.split(',') %}\n" +
-            "  {{domain}} = {{realm}}\n" +
-            "{% endfor %}\n" +
-            "{% endif %}\n" +
-            "\n" +
-            "[logging]\n" +
-            "  default = FILE:/var/log/krb5kdc.log\n" +
-            "  admin_server = FILE:/var/log/kadmind.log\n" +
-            "  kdc = FILE:/var/log/krb5kdc.log\n" +
-            "\n" +
-            "[realms]\n" +
-            "  {{realm}} = {\n" +
-            "    admin_server = {{admin_server_host|default(kdc_host, True)}}\n" +
-            "    kdc = {{kdc_host}}\n" +
-            "  }\n" +
-            "\n" +
-            "{# Append additional realm declarations below #}");
-      }
-    };
-
-    final Config configKerberosEnv = createNiceMock(Config.class);
-    expect(configKerberosEnv.getProperties()).andReturn(propertiesKerberosEnv).anyTimes();
-    expect(configKerberosEnv.getTag()).andReturn("tag1").anyTimes();
-
-    final Config configKrb5Conf = createNiceMock(Config.class);
-    expect(configKrb5Conf.getProperties()).andReturn(propertiesKrb5Conf).anyTimes();
-    expect(configKrb5Conf.getTag()).andReturn("tag1").anyTimes();
-
-    final StackId currentStackVersion = new StackId("HDP", "2.4.2");
-
-    final Cluster cluster = createNiceMock(Cluster.class);
-    expect(cluster.getClusterName()).andReturn("c1").anyTimes();
-    expect(cluster.getDesiredConfigByType("kerberos-env")).andReturn(configKerberosEnv).anyTimes();
-    expect(cluster.getDesiredConfigByType("krb5-conf")).andReturn(configKrb5Conf).anyTimes();
-    expect(cluster.getCurrentStackVersion()).andReturn(currentStackVersion).once();
-
-    expect(cluster.getConfigsByType("kerberos-env"))
-        .andReturn(Collections.singletonMap("tag1", configKerberosEnv))
-        .once();
-    expect(cluster.getConfigsByType("krb5-conf"))
-        .andReturn(Collections.singletonMap("tag1", configKerberosEnv))
-        .once();
-
-    final Clusters clusters = createNiceMock(Clusters.class);
-    expect(clusters.getClusters()).andReturn(Collections.singletonMap("c1", cluster));
-
-    expect(controller.getClusters()).andReturn(clusters).once();
-
-    Capture<Cluster> clusterCapture = newCapture(CaptureType.ALL);
-    Capture<String> typeCapture = newCapture(CaptureType.ALL);
-    Capture<Map> propertiesCapture = newCapture(CaptureType.ALL);
-    Capture<String> tagCapture = newCapture(CaptureType.ALL);
-    Capture<Map> attributesCapture = newCapture(CaptureType.ALL);
-
-    expect(controller.createConfig(capture(clusterCapture), capture(typeCapture),
-        capture(propertiesCapture), capture(tagCapture), capture(attributesCapture) ))
-        .andReturn(createNiceMock(Config.class))
-        .anyTimes();
-    expect(controller.getAmbariMetaInfo()).andReturn(metaInfo).once();
-
-    expect(metaInfo.getStack(currentStackVersion.getStackName(), currentStackVersion.getStackVersion()))
-        .andReturn(stackInfo)
-        .once();
-
-    expect(stackInfo.getService("KERBEROS")).andReturn(serviceInfo).once();
-
-    final PropertyInfo propertyInfo = new PropertyInfo();
-    propertyInfo.setFilename("krb5-conf.xml");
-    propertyInfo.setName("content");
-    propertyInfo.setValue("new content template");
-
-    expect(serviceInfo.getProperties()).andReturn(Collections.singletonList(propertyInfo)).once();
-
-    replay(controller, metaInfo, stackInfo, serviceInfo, dbAccessor, osFamily, cluster, configKerberosEnv, configKrb5Conf, clusters);
-
-    final Injector injector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(controller);
-        bind(DBAccessor.class).toInstance(dbAccessor);
-        bind(OsFamily.class).toInstance(osFamily);
-        bind(EntityManager.class).toInstance(entityManager);
-        bind(PasswordEncoder.class).toInstance(createNiceMock(PasswordEncoder.class));
-        bind(HookContextFactory.class).toInstance(createMock(HookContextFactory.class));
-        bind(HookService.class).toInstance(createMock(HookService.class));
-      }
-    });
-
-    injector.getInstance(UpgradeCatalog240.class).updateKerberosConfigs();
-
-    verify(controller, metaInfo, stackInfo, serviceInfo, dbAccessor, osFamily, cluster, configKerberosEnv, configKrb5Conf, clusters);
-
-    List<String> typeCaptureValues = typeCapture.getValues();
-    Assert.assertEquals(2, typeCaptureValues.size());
-    Assert.assertEquals("kerberos-env", typeCaptureValues.get(0));
-    Assert.assertEquals("krb5-conf", typeCaptureValues.get(1));
-
-    List<Map> propertiesCaptureValues = propertiesCapture.getValues();
-    Assert.assertEquals(2, propertiesCaptureValues.size());
-
-    Map<String, String> capturedCRProperties;
-
-    capturedCRProperties = propertiesCaptureValues.get(0);
-    Assert.assertNotNull(capturedCRProperties);
-    Assert.assertFalse(capturedCRProperties.containsKey("kdc_host"));
-    Assert.assertTrue(capturedCRProperties.containsKey("kdc_hosts"));
-    Assert.assertTrue(capturedCRProperties.containsKey("create_ambari_principal"));
-
-    for (String property : propertiesKerberosEnv.keySet()) {
-    

<TRUNCATED>

[14/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProviderTest.java
index 7018245..32a5358 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProviderTest.java
@@ -1,4 +1,4 @@
- /**
+/*
  * 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
@@ -31,7 +31,6 @@ import static org.easymock.EasyMock.verify;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.lang.reflect.Field;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -54,7 +53,6 @@ import org.apache.ambari.server.actionmanager.ExecutionCommandWrapper;
 import org.apache.ambari.server.actionmanager.HostRoleCommand;
 import org.apache.ambari.server.actionmanager.Stage;
 import org.apache.ambari.server.actionmanager.StageFactory;
-import org.apache.ambari.server.agent.CommandReport;
 import org.apache.ambari.server.agent.ExecutionCommand;
 import org.apache.ambari.server.agent.ExecutionCommand.KeyNames;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
@@ -70,24 +68,16 @@ import org.apache.ambari.server.controller.spi.ResourceProvider;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
-import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
-import org.apache.ambari.server.orm.entities.ResourceEntity;
-import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.security.TestAuthenticationFactory;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
-import org.apache.ambari.server.security.authorization.ResourceType;
-import org.apache.ambari.server.serveraction.upgrades.FinalizeUpgradeAction;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ConfigHelper;
@@ -102,17 +92,18 @@ import org.apache.ambari.server.state.ServiceInfo;
 import org.apache.ambari.server.state.ServiceOsSpecific;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.cluster.ClusterImpl;
+import org.apache.ambari.server.state.repository.VersionDefinitionXml;
 import org.apache.ambari.server.state.stack.upgrade.Direction;
 import org.apache.ambari.server.topology.TopologyManager;
 import org.apache.ambari.server.utils.StageUtils;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
 import org.easymock.Capture;
 import org.easymock.EasyMock;
 import org.easymock.IAnswer;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContextHolder;
@@ -135,9 +126,6 @@ public class ClusterStackVersionResourceProviderTest {
   private Injector injector;
   private AmbariMetaInfo ambariMetaInfo;
   private RepositoryVersionDAO repositoryVersionDAOMock;
-  private ResourceTypeDAO resourceTypeDAO;
-  private StackDAO stackDAO;
-  private ClusterVersionDAO clusterVersionDAO;
   private ConfigHelper configHelper;
   private Configuration configuration;
   private StageFactory stageFactory;
@@ -177,14 +165,11 @@ public class ClusterStackVersionResourceProviderTest {
             String.valueOf(MAX_TASKS_PER_STAGE));
     configuration = new Configuration(properties);
     stageFactory = createNiceMock(StageFactory.class);
-    clusterVersionDAO = createNiceMock(ClusterVersionDAO.class);
 
     // Initialize injector
     injector = Guice.createInjector(Modules.override(inMemoryModule).with(new MockModule()));
     injector.getInstance(GuiceJpaInitializer.class);
     ambariMetaInfo = injector.getInstance(AmbariMetaInfo.class);
-    resourceTypeDAO = injector.getInstance(ResourceTypeDAO.class);
-    stackDAO = injector.getInstance(StackDAO.class);
   }
 
   @After
@@ -218,10 +203,18 @@ public class ClusterStackVersionResourceProviderTest {
     Map<String, String> hostLevelParams = new HashMap<>();
     StackId stackId = new StackId("HDP", "2.0.1");
 
+    StackEntity stackEntity = new StackEntity();
+    stackEntity.setStackName("HDP");
+    stackEntity.setStackVersion("2.1.1");
+
     RepositoryVersionEntity repoVersion = new RepositoryVersionEntity();
     repoVersion.setId(1l);
     repoVersion.setOperatingSystems(OS_JSON);
+    repoVersion.setStack(stackEntity);
 
+    final String hostWithoutVersionableComponents = "host2";
+
+    List<Host> hostsNeedingInstallCommands = new ArrayList<>();
     Map<String, Host> hostsForCluster = new HashMap<>();
     int hostCount = 10;
     for (int i = 0; i < hostCount; i++) {
@@ -236,6 +229,10 @@ public class ClusterStackVersionResourceProviderTest {
 
       replay(host);
       hostsForCluster.put(hostname, host);
+
+      if (!StringUtils.equals(hostWithoutVersionableComponents, hostname)) {
+        hostsNeedingInstallCommands.add(host);
+      }
     }
 
     final ServiceComponentHost schDatanode = createMock(ServiceComponentHost.class);
@@ -280,11 +277,11 @@ public class ClusterStackVersionResourceProviderTest {
     expect(managementController.getActionManager()).andReturn(actionManager).anyTimes();
     expect(managementController.getJdkResourceUrl()).andReturn("/JdkResourceUrl").anyTimes();
     expect(managementController.getPackagesForServiceHost(anyObject(ServiceInfo.class),
-            (Map<String, String>) anyObject(List.class), anyObject(String.class))).
+            EasyMock.<Map<String, String>>anyObject(), anyObject(String.class))).
             andReturn(packages).times((hostCount - 1) * 2); // 1 host has no versionable components, other hosts have 2 services
 //            // that's why we don't send commands to it
 
-    expect(resourceProviderFactory.getHostResourceProvider(anyObject(Set.class), anyObject(Map.class),
+    expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(), EasyMock.<Map<Resource.Type, String>>anyObject(),
             eq(managementController))).andReturn(csvResourceProvider).anyTimes();
 
     expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster);
@@ -300,7 +297,7 @@ public class ClusterStackVersionResourceProviderTest {
       @Override
       public List<ServiceComponentHost> answer() throws Throwable {
         String hostname = (String) EasyMock.getCurrentArguments()[0];
-        if (hostname.equals("host2")) {
+            if (hostname.equals(hostWithoutVersionableComponents)) {
           return schsH2;
         } else {
           return schsH1;
@@ -308,6 +305,10 @@ public class ClusterStackVersionResourceProviderTest {
       }
     }).anyTimes();
 
+    expect(cluster.transitionHostsToInstalling(
+        anyObject(RepositoryVersionEntity.class), anyObject(VersionDefinitionXml.class),
+        eq(false))).andReturn(hostsNeedingInstallCommands).atLeastOnce();
+
     ExecutionCommand executionCommand = createNiceMock(ExecutionCommand.class);
     ExecutionCommandWrapper executionCommandWrapper = createNiceMock(ExecutionCommandWrapper.class);
 
@@ -339,20 +340,14 @@ public class ClusterStackVersionResourceProviderTest {
     ClusterEntity clusterEntity = new ClusterEntity();
     clusterEntity.setClusterId(1l);
     clusterEntity.setClusterName(clusterName);
-    ClusterVersionEntity cve = new ClusterVersionEntity(clusterEntity,
-            repoVersion, RepositoryVersionState.INSTALL_FAILED, 0, "");
-    expect(clusterVersionDAO.findByClusterAndStackAndVersion(anyObject(String.class),
-            anyObject(StackId.class), anyObject(String.class))).andReturn(cve);
 
     StageUtils.setTopologyManager(injector.getInstance(TopologyManager.class));
     StageUtils.setConfiguration(injector.getInstance(Configuration.class));
 
-    expect(clusterVersionDAO.findByCluster(anyObject(String.class))).andReturn(Collections.<ClusterVersionEntity>emptyList()).once();
-
     // replay
     replay(managementController, response, clusters, resourceProviderFactory, csvResourceProvider,
             cluster, repositoryVersionDAOMock, configHelper, schDatanode, schNamenode, schAMS, actionManager,
-            executionCommand, executionCommandWrapper,stage, stageFactory, clusterVersionDAO);
+            executionCommand, executionCommandWrapper,stage, stageFactory);
 
     ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(
         type,
@@ -392,7 +387,6 @@ public class ClusterStackVersionResourceProviderTest {
   }
 
   @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES)
-  @Ignore
   public void testCreateResourcesForPatch() throws Exception {
     Resource.Type type = Resource.Type.ClusterStackVersion;
 
@@ -433,23 +427,14 @@ public class ClusterStackVersionResourceProviderTest {
     Service hbaseService = createNiceMock(Service.class);
     expect(hdfsService.getName()).andReturn("HDFS").anyTimes();
     expect(hbaseService.getName()).andReturn("HBASE").anyTimes();
-//    Service metricsService = createNiceMock(Service.class);
-
-    ServiceComponent scNameNode = createNiceMock(ServiceComponent.class);
-    ServiceComponent scDataNode = createNiceMock(ServiceComponent.class);
-    ServiceComponent scHBaseMaster = createNiceMock(ServiceComponent.class);
-    ServiceComponent scMetricCollector = createNiceMock(ServiceComponent.class);
 
     expect(hdfsService.getServiceComponents()).andReturn(new HashMap<String, ServiceComponent>());
     expect(hbaseService.getServiceComponents()).andReturn(new HashMap<String, ServiceComponent>());
-//    expect(metricsService.getServiceComponents()).andReturn(new HashMap<String, ServiceComponent>());
-
 
     Map<String, Service> serviceMap = new HashMap<>();
     serviceMap.put("HDFS", hdfsService);
     serviceMap.put("HBASE", hbaseService);
 
-
     final ServiceComponentHost schDatanode = createMock(ServiceComponentHost.class);
     expect(schDatanode.getServiceName()).andReturn("HDFS").anyTimes();
     expect(schDatanode.getServiceComponentName()).andReturn("DATANODE").anyTimes();
@@ -478,9 +463,6 @@ public class ClusterStackVersionResourceProviderTest {
     ServiceOsSpecific.Package hdfsPackage = new ServiceOsSpecific.Package();
     hdfsPackage.setName("hdfs");
 
-//    ServiceOsSpecific.Package hbasePackage = new ServiceOsSpecific.Package();
-//    hbasePackage.setName("hbase");
-
     List<ServiceOsSpecific.Package> packages = Collections.singletonList(hdfsPackage);
 
     ActionManager actionManager = createNiceMock(ActionManager.class);
@@ -500,10 +482,10 @@ public class ClusterStackVersionResourceProviderTest {
     expect(managementController.getActionManager()).andReturn(actionManager).anyTimes();
     expect(managementController.getJdkResourceUrl()).andReturn("/JdkResourceUrl").anyTimes();
     expect(managementController.getPackagesForServiceHost(anyObject(ServiceInfo.class),
-            (Map<String, String>) anyObject(List.class), anyObject(String.class))).
+            EasyMock.<Map<String, String>>anyObject(), anyObject(String.class))).
             andReturn(packages).times(1); // only one host has the versionable component
 
-    expect(resourceProviderFactory.getHostResourceProvider(anyObject(Set.class), anyObject(Map.class),
+    expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(), EasyMock.<Map<Resource.Type, String>>anyObject(),
             eq(managementController))).andReturn(csvResourceProvider).anyTimes();
 
     expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster);
@@ -563,10 +545,6 @@ public class ClusterStackVersionResourceProviderTest {
     ClusterEntity clusterEntity = new ClusterEntity();
     clusterEntity.setClusterId(1l);
     clusterEntity.setClusterName(clusterName);
-    ClusterVersionEntity cve = new ClusterVersionEntity(clusterEntity,
-            repoVersion, RepositoryVersionState.INSTALL_FAILED, 0, "");
-    expect(clusterVersionDAO.findByClusterAndStackAndVersion(anyObject(String.class),
-            anyObject(StackId.class), anyObject(String.class))).andReturn(cve);
 
     TopologyManager topologyManager = injector.getInstance(TopologyManager.class);
     StageUtils.setTopologyManager(topologyManager);
@@ -574,7 +552,7 @@ public class ClusterStackVersionResourceProviderTest {
     // replay
     replay(managementController, response, clusters, hdfsService, hbaseService, resourceProviderFactory, csvResourceProvider,
             cluster, repositoryVersionDAOMock, configHelper, schDatanode, schNamenode, schAMS, schHBM, actionManager,
-            executionCommand, executionCommandWrapper,stage, stageFactory, clusterVersionDAO);
+            executionCommand, executionCommandWrapper,stage, stageFactory);
 
     ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(
         type,
@@ -636,16 +614,22 @@ public class ClusterStackVersionResourceProviderTest {
 
     File f = new File("src/test/resources/hbase_version_test.xml");
 
+    StackEntity stackEntity = new StackEntity();
+    stackEntity.setStackName("HDP");
+    stackEntity.setStackVersion("2.1.1");
+
     RepositoryVersionEntity repoVersion = new RepositoryVersionEntity();
     repoVersion.setId(1l);
     repoVersion.setOperatingSystems(OS_JSON);
     repoVersion.setVersionXml(IOUtils.toString(new FileInputStream(f)));
     repoVersion.setVersionXsd("version_definition.xsd");
     repoVersion.setType(RepositoryType.STANDARD);
+    repoVersion.setStack(stackEntity);
 
     ambariMetaInfo.getComponent("HDP", "2.1.1", "HBASE", "HBASE_MASTER").setVersionAdvertised(true);
 
-
+    final String hostWithoutVersionableComponents = "host3";
+    List<Host> hostsNeedingInstallCommands = new ArrayList<>();
     Map<String, Host> hostsForCluster = new HashMap<>();
     int hostCount = 10;
     for (int i = 0; i < hostCount; i++) {
@@ -660,6 +644,11 @@ public class ClusterStackVersionResourceProviderTest {
 
       replay(host);
       hostsForCluster.put(hostname, host);
+
+
+      if (!StringUtils.equals(hostWithoutVersionableComponents, hostname)) {
+        hostsNeedingInstallCommands.add(host);
+      }
     }
 
     Service hdfsService = createNiceMock(Service.class);
@@ -717,10 +706,10 @@ public class ClusterStackVersionResourceProviderTest {
     expect(managementController.getActionManager()).andReturn(actionManager).anyTimes();
     expect(managementController.getJdkResourceUrl()).andReturn("/JdkResourceUrl").anyTimes();
     expect(managementController.getPackagesForServiceHost(anyObject(ServiceInfo.class),
-            (Map<String, String>) anyObject(List.class), anyObject(String.class))).
+            EasyMock.<Map<String, String>>anyObject(), anyObject(String.class))).
             andReturn(packages).anyTimes(); // only one host has the versionable component
 
-    expect(resourceProviderFactory.getHostResourceProvider(anyObject(Set.class), anyObject(Map.class),
+    expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(), EasyMock.<Map<Resource.Type, String>>anyObject(),
             eq(managementController))).andReturn(csvResourceProvider).anyTimes();
 
     expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster);
@@ -736,7 +725,7 @@ public class ClusterStackVersionResourceProviderTest {
       @Override
       public List<ServiceComponentHost> answer() throws Throwable {
         String hostname = (String) EasyMock.getCurrentArguments()[0];
-        if (hostname.equals("host2")) {
+            if (hostname.equals("host2")) {
           return schsH2;
         } else if (hostname.equals("host3")) {
           return schsH3;
@@ -746,6 +735,9 @@ public class ClusterStackVersionResourceProviderTest {
       }
     }).anyTimes();
 
+    expect(cluster.transitionHostsToInstalling(anyObject(RepositoryVersionEntity.class),
+        anyObject(VersionDefinitionXml.class), eq(false))).andReturn(hostsNeedingInstallCommands).atLeastOnce();
+
 //    ExecutionCommand executionCommand = createNiceMock(ExecutionCommand.class);
     ExecutionCommand executionCommand = new ExecutionCommand();
     ExecutionCommandWrapper executionCommandWrapper = createNiceMock(ExecutionCommandWrapper.class);
@@ -772,8 +764,6 @@ public class ClusterStackVersionResourceProviderTest {
                     anyObject(StackId.class),
                     anyObject(String.class))).andReturn(repoVersion);
 
-    expect(clusterVersionDAO.findByCluster(anyObject(String.class))).andReturn(Collections.<ClusterVersionEntity>emptyList()).once();
-
     Capture<org.apache.ambari.server.actionmanager.Request> c = Capture.newInstance();
     Capture<ExecuteActionRequest> ear = Capture.newInstance();
 
@@ -784,10 +774,6 @@ public class ClusterStackVersionResourceProviderTest {
     ClusterEntity clusterEntity = new ClusterEntity();
     clusterEntity.setClusterId(1l);
     clusterEntity.setClusterName(clusterName);
-    ClusterVersionEntity cve = new ClusterVersionEntity(clusterEntity,
-            repoVersion, RepositoryVersionState.INSTALL_FAILED, 0, "");
-    expect(clusterVersionDAO.findByClusterAndStackAndVersion(anyObject(String.class),
-            anyObject(StackId.class), anyObject(String.class))).andReturn(cve);
 
     StageUtils.setTopologyManager(injector.getInstance(TopologyManager.class));
     StageUtils.setConfiguration(injector.getInstance(Configuration.class));
@@ -795,7 +781,7 @@ public class ClusterStackVersionResourceProviderTest {
     // replay
     replay(managementController, response, clusters, hdfsService, hbaseService, resourceProviderFactory, csvResourceProvider,
             cluster, repositoryVersionDAOMock, configHelper, schDatanode, schNamenode, schHBM, actionManager,
-            executionCommandWrapper,stage, stageFactory, clusterVersionDAO);
+            executionCommandWrapper,stage, stageFactory);
 
     ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(
         type,
@@ -868,15 +854,23 @@ public class ClusterStackVersionResourceProviderTest {
 
     File f = new File("src/test/resources/hbase_version_test.xml");
 
+    StackEntity stackEntity = new StackEntity();
+    stackEntity.setStackName("HDP");
+    stackEntity.setStackVersion("2.1.1");
+
     RepositoryVersionEntity repoVersion = new RepositoryVersionEntity();
     repoVersion.setId(1l);
     repoVersion.setOperatingSystems(os_json);
     repoVersion.setVersionXml(IOUtils.toString(new FileInputStream(f)));
     repoVersion.setVersionXsd("version_definition.xsd");
     repoVersion.setType(RepositoryType.STANDARD);
+    repoVersion.setStack(stackEntity);
 
     ambariMetaInfo.getComponent("HDP", "2.1.1", "HBASE", "HBASE_MASTER").setVersionAdvertised(true);
 
+    final String hostWithoutVersionableComponents = "host3";
+    List<Host> hostsNeedingInstallCommands = new ArrayList<>();
+
     Map<String, Host> hostsForCluster = new HashMap<>();
     int hostCount = 10;
     for (int i = 0; i < hostCount; i++) {
@@ -891,6 +885,10 @@ public class ClusterStackVersionResourceProviderTest {
 
       replay(host);
       hostsForCluster.put(hostname, host);
+
+      if (!StringUtils.equals(hostWithoutVersionableComponents, hostname)) {
+        hostsNeedingInstallCommands.add(host);
+      }
     }
 
     Service hdfsService = createNiceMock(Service.class);
@@ -948,10 +946,10 @@ public class ClusterStackVersionResourceProviderTest {
     expect(managementController.getActionManager()).andReturn(actionManager).anyTimes();
     expect(managementController.getJdkResourceUrl()).andReturn("/JdkResourceUrl").anyTimes();
     expect(managementController.getPackagesForServiceHost(anyObject(ServiceInfo.class),
-            (Map<String, String>) anyObject(List.class), anyObject(String.class))).
+            EasyMock.<Map<String, String>>anyObject(), anyObject(String.class))).
             andReturn(packages).anyTimes(); // only one host has the versionable component
 
-    expect(resourceProviderFactory.getHostResourceProvider(anyObject(Set.class), anyObject(Map.class),
+    expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(), EasyMock.<Map<Resource.Type, String>>anyObject(),
             eq(managementController))).andReturn(csvResourceProvider).anyTimes();
 
     expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster);
@@ -977,6 +975,9 @@ public class ClusterStackVersionResourceProviderTest {
       }
     }).anyTimes();
 
+    expect(cluster.transitionHostsToInstalling(anyObject(RepositoryVersionEntity.class),
+        anyObject(VersionDefinitionXml.class), eq(false))).andReturn(hostsNeedingInstallCommands).atLeastOnce();
+
 //    ExecutionCommand executionCommand = createNiceMock(ExecutionCommand.class);
     ExecutionCommand executionCommand = new ExecutionCommand();
     ExecutionCommandWrapper executionCommandWrapper = createNiceMock(ExecutionCommandWrapper.class);
@@ -998,10 +999,8 @@ public class ClusterStackVersionResourceProviderTest {
             anyObject(String.class))).andReturn(stage).
             times((int) Math.ceil(hostCount / MAX_TASKS_PER_STAGE));
 
-    expect(
-            repositoryVersionDAOMock.findByStackAndVersion(
-                    anyObject(StackId.class),
-                    anyObject(String.class))).andReturn(repoVersion);
+    expect(repositoryVersionDAOMock.findByStackAndVersion(anyObject(StackId.class),
+        anyObject(String.class))).andReturn(repoVersion);
 
     Capture<org.apache.ambari.server.actionmanager.Request> c = Capture.newInstance();
     Capture<ExecuteActionRequest> ear = Capture.newInstance();
@@ -1013,21 +1012,14 @@ public class ClusterStackVersionResourceProviderTest {
     ClusterEntity clusterEntity = new ClusterEntity();
     clusterEntity.setClusterId(1l);
     clusterEntity.setClusterName(clusterName);
-    ClusterVersionEntity cve = new ClusterVersionEntity(clusterEntity,
-            repoVersion, RepositoryVersionState.INSTALL_FAILED, 0, "");
-    expect(clusterVersionDAO.findByClusterAndStackAndVersion(anyObject(String.class),
-            anyObject(StackId.class), anyObject(String.class))).andReturn(cve);
 
-    TopologyManager topologyManager = injector.getInstance(TopologyManager.class);
-    StageUtils.setTopologyManager(topologyManager);
+    StageUtils.setTopologyManager(injector.getInstance(TopologyManager.class));
     StageUtils.setConfiguration(injector.getInstance(Configuration.class));
 
-    expect(clusterVersionDAO.findByCluster(anyObject(String.class))).andReturn(Collections.<ClusterVersionEntity>emptyList()).once();
-
     // replay
     replay(managementController, response, clusters, hdfsService, hbaseService, resourceProviderFactory, csvResourceProvider,
             cluster, repositoryVersionDAOMock, configHelper, schDatanode, schNamenode, schHBM, actionManager,
-            executionCommandWrapper,stage, stageFactory, clusterVersionDAO);
+            executionCommandWrapper,stage, stageFactory);
 
     ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(
         type,
@@ -1066,321 +1058,8 @@ public class ClusterStackVersionResourceProviderTest {
     Assert.assertEquals(Float.valueOf(0.85f), successFactor);
 
     Assert.assertTrue(executionCommand.getRoleParams().containsKey(KeyNames.PACKAGE_VERSION));
-    Assert.assertTrue(executionCommand.getRoleParams().containsKey("base_urls"));
-    Assert.assertEquals("[]", executionCommand.getRoleParams().get("base_urls"));
-  }
-
-   @Test
-   public void testUpdateResourcesAsAdministrator() throws Exception {
-     testUpdateResources(TestAuthenticationFactory.createAdministrator());
-   }
-
-   @Test
-   public void testUpdateResourcesAsClusterAdministrator() throws Exception {
-     testUpdateResources(TestAuthenticationFactory.createClusterAdministrator());
-   }
-
-   @Test(expected = AuthorizationException.class)
-   public void testUpdateResourcesAsClusterOperator() throws Exception {
-     testUpdateResources(TestAuthenticationFactory.createClusterOperator());
-   }
-
-   private void testUpdateResources(Authentication authentication) throws Exception {
-    Resource.Type type = Resource.Type.ClusterStackVersion;
-    String clusterName = "Cluster100";
-
-    AmbariManagementController managementController = createMock(AmbariManagementController.class);
-
-    StackId stackId = new StackId("HDP", "2.0.1");
-    StackEntity stackEntity = stackDAO.find(stackId.getStackName(), stackId.getStackVersion());
-    Assert.assertNotNull(stackEntity);
-
-    ResourceTypeEntity resourceTypeEntity = resourceTypeDAO.findById(ResourceType.CLUSTER.getId());
-    if (resourceTypeEntity == null) {
-      resourceTypeEntity = new ResourceTypeEntity();
-      resourceTypeEntity.setId(ResourceType.CLUSTER.getId());
-      resourceTypeEntity.setName(ResourceType.CLUSTER.name());
-      resourceTypeEntity = resourceTypeDAO.merge(resourceTypeEntity);
-    }
-    ResourceEntity resourceEntity = new ResourceEntity();
-    resourceEntity.setResourceType(resourceTypeEntity);
-
-    final Host host1 = createNiceMock("host1", Host.class);
-    final Host host2 = createNiceMock("host2", Host.class);
-
-    expect(host1.getHostName()).andReturn("host1").anyTimes();
-    expect(host2.getHostName()).andReturn("host2").anyTimes();
-    replay(host1, host2);
-
-    ServiceComponentHost sch = createMock(ServiceComponentHost.class);
-    List<ServiceComponentHost> schs = Collections.singletonList(sch);
-
-    Cluster cluster = createNiceMock(Cluster.class);
-    cluster.setClusterName(clusterName);
-
-    ArrayList<Host> hosts = new ArrayList<Host>() {{
-      add(host1);
-      add(host2);
-    }};
-
-    Clusters clusters = createNiceMock(Clusters.class);
-    expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster);
-
-    RepositoryVersionEntity repoVersion = new RepositoryVersionEntity();
-    repoVersion.setOperatingSystems(OS_JSON);
-    StackEntity newDesiredStack = stackDAO.find("HDP", "2.0.1");
-    repoVersion.setStack(newDesiredStack);
-
-    final ServiceOsSpecific.Package hivePackage = new ServiceOsSpecific.Package();
-    hivePackage.setName("hive");
-    final ServiceOsSpecific.Package mysqlPackage = new ServiceOsSpecific.Package();
-    mysqlPackage.setName("mysql");
-    mysqlPackage.setSkipUpgrade(Boolean.TRUE);
-    List<ServiceOsSpecific.Package> packages = new ArrayList<ServiceOsSpecific.Package>() {{
-      add(hivePackage);
-      add(mysqlPackage);
-    }};
-
-    ActionManager actionManager = createNiceMock(ActionManager.class);
-
-    RequestStatusResponse response = createNiceMock(RequestStatusResponse.class);
-    ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class);
-    ResourceProvider csvResourceProvider = createNiceMock(ClusterStackVersionResourceProvider.class);
-
-    CommandReport report = createNiceMock(CommandReport.class);
-    FinalizeUpgradeAction finalizeUpgradeAction = createNiceMock(FinalizeUpgradeAction.class);
-
-    AbstractControllerResourceProvider.init(resourceProviderFactory);
-
-    Map<String, Map<String, String>> hostConfigTags = new HashMap<>();
-    expect(configHelper.getEffectiveDesiredTags(anyObject(ClusterImpl.class), anyObject(String.class))).andReturn(hostConfigTags);
-
-    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
-    expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
-    expect(managementController.getAuthName()).andReturn("admin").anyTimes();
-    expect(managementController.getActionManager()).andReturn(actionManager).anyTimes();
-    expect(managementController.getJdkResourceUrl()).andReturn("/JdkResourceUrl").anyTimes();
-    expect(managementController.getPackagesForServiceHost(anyObject(ServiceInfo.class),
-            (Map<String, String>) anyObject(List.class), anyObject(String.class))).andReturn(packages).anyTimes();
-
-    expect(resourceProviderFactory.getHostResourceProvider(anyObject(Set.class), anyObject(Map.class),
-            eq(managementController))).andReturn(csvResourceProvider).anyTimes();
-
-    expect(cluster.getCurrentStackVersion()).andReturn(stackId);
-    expect(cluster.getServiceComponentHosts(anyObject(String.class))).andReturn(schs).anyTimes();
-
-    Capture<StackId> capturedStackId = new Capture<>();
-    cluster.setDesiredStackVersion(capture(capturedStackId));
-      expectLastCall().once();
-    expect(cluster.getHosts()).andReturn(hosts).anyTimes();
-
-
-    expect(sch.getServiceName()).andReturn("HIVE").anyTimes();
-
-    expect(repositoryVersionDAOMock.findByDisplayName(anyObject(String.class))).andReturn(repoVersion);
-
-    expect(actionManager.getRequestTasks(anyLong())).andReturn(Collections.<HostRoleCommand>emptyList()).anyTimes();
-
-    expect(finalizeUpgradeAction.execute(null)).andReturn(report);
-
-    expect(report.getStdOut()).andReturn("Dummy stdout");
-    expect(report.getStdErr()).andReturn("Dummy stderr");
-    expect(report.getStatus()).andReturn("COMPLETED");
-
-    // replay
-    replay(managementController, response, clusters, resourceProviderFactory, csvResourceProvider,
-            cluster, repositoryVersionDAOMock, configHelper, sch, actionManager, finalizeUpgradeAction, report,
-            stageFactory);
-
-    ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(
-            type,
-            PropertyHelper.getPropertyIds(type),
-            PropertyHelper.getKeyPropertyIds(type),
-            managementController);
-
-    injector.injectMembers(provider);
-
-    // Have to inject instance manually because injection via DI fails
-    Field field = ClusterStackVersionResourceProvider.class.getDeclaredField("finalizeUpgradeAction");
-    field.setAccessible(true);
-    field.set(provider, finalizeUpgradeAction);
-
-    // add the property map to a set for the request.  add more maps for multiple creates
-    Map<String, Object> properties = new LinkedHashMap<>();
-
-    // add properties to the request map
-    properties.put(ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID, clusterName);
-    properties.put(ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_STATE_PROPERTY_ID, "CURRENT");
-    properties.put(ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID, "HDP-2.2.2.0-2561");
-
-    // create the request
-    Request request = PropertyHelper.getUpdateRequest(properties, null);
-
-    SecurityContextHolder.getContext().setAuthentication(authentication);
-
-    provider.updateResources(request, null);
-
-    // verify
-    verify(managementController, response);
-    Assert.assertEquals(capturedStackId.getValue(),
-            new StackId(newDesiredStack.getStackName(), newDesiredStack.getStackVersion()));
-  }
-
-   @Test
-   public void testUpdateResourcesWithForceAsAdministrator() throws Exception {
-     testUpdateResourcesWithForce(TestAuthenticationFactory.createAdministrator());
-   }
-
-   @Test
-   public void testUpdateResourcesWithForceAsClusterAdministrator() throws Exception {
-     testUpdateResourcesWithForce(TestAuthenticationFactory.createClusterAdministrator());
-   }
-
-   @Test(expected = AuthorizationException.class)
-   public void testUpdateResourcesWithForceAsClusterOperator() throws Exception {
-     testUpdateResourcesWithForce(TestAuthenticationFactory.createClusterOperator());
-   }
-
-   private void testUpdateResourcesWithForce(Authentication authentication) throws Exception {
-    Resource.Type type = Resource.Type.ClusterStackVersion;
-    String clusterName = "Cluster100";
-
-    AmbariManagementController managementController = createMock(AmbariManagementController.class);
-
-    StackId stackId = new StackId("HDP", "2.0.1");
-    StackEntity stackEntity = stackDAO.find(stackId.getStackName(), stackId.getStackVersion());
-    Assert.assertNotNull(stackEntity);
-
-    ResourceTypeEntity resourceTypeEntity = resourceTypeDAO.findById(ResourceType.CLUSTER.getId());
-    if (resourceTypeEntity == null) {
-      resourceTypeEntity = new ResourceTypeEntity();
-      resourceTypeEntity.setId(ResourceType.CLUSTER.getId());
-      resourceTypeEntity.setName(ResourceType.CLUSTER.name());
-      resourceTypeEntity = resourceTypeDAO.merge(resourceTypeEntity);
-    }
-    ResourceEntity resourceEntity = new ResourceEntity();
-    resourceEntity.setResourceType(resourceTypeEntity);
-
-    final Host host1 = createNiceMock("host1", Host.class);
-    final Host host2 = createNiceMock("host2", Host.class);
-
-    expect(host1.getHostName()).andReturn("host1").anyTimes();
-    expect(host2.getHostName()).andReturn("host2").anyTimes();
-    replay(host1, host2);
-
-    ServiceComponentHost sch = createMock(ServiceComponentHost.class);
-    List<ServiceComponentHost> schs = Collections.singletonList(sch);
-
-    Cluster cluster = createNiceMock(Cluster.class);
-    cluster.setClusterName(clusterName);
-
-    ArrayList<Host> hosts = new ArrayList<Host>() {{
-      add(host1);
-      add(host2);
-    }};
-
-    Clusters clusters = createNiceMock(Clusters.class);
-    expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster);
-
-    RepositoryVersionEntity repoVersion = new RepositoryVersionEntity();
-    repoVersion.setOperatingSystems(OS_JSON);
-    StackEntity newDesiredStack = stackDAO.find("HDP", "2.0.1");
-    repoVersion.setStack(newDesiredStack);
-
-    final ServiceOsSpecific.Package hivePackage = new ServiceOsSpecific.Package();
-    hivePackage.setName("hive");
-    final ServiceOsSpecific.Package mysqlPackage = new ServiceOsSpecific.Package();
-    mysqlPackage.setName("mysql");
-    mysqlPackage.setSkipUpgrade(Boolean.TRUE);
-    List<ServiceOsSpecific.Package> packages = new ArrayList<ServiceOsSpecific.Package>() {{
-      add(hivePackage);
-      add(mysqlPackage);
-    }};
-
-    ActionManager actionManager = createNiceMock(ActionManager.class);
-
-    RequestStatusResponse response = createNiceMock(RequestStatusResponse.class);
-    ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class);
-    ResourceProvider csvResourceProvider = createNiceMock(ClusterStackVersionResourceProvider.class);
-
-    AbstractControllerResourceProvider.init(resourceProviderFactory);
-
-    Map<String, Map<String, String>> hostConfigTags = new HashMap<>();
-    expect(configHelper.getEffectiveDesiredTags(anyObject(ClusterImpl.class), anyObject(String.class))).andReturn(hostConfigTags);
-
-    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
-    expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
-    expect(managementController.getAuthName()).andReturn("admin").anyTimes();
-    expect(managementController.getActionManager()).andReturn(actionManager).anyTimes();
-    expect(managementController.getJdkResourceUrl()).andReturn("/JdkResourceUrl").anyTimes();
-    expect(managementController.getPackagesForServiceHost(anyObject(ServiceInfo.class),
-            (Map<String, String>) anyObject(List.class), anyObject(String.class))).andReturn(packages).anyTimes();
-
-    expect(resourceProviderFactory.getHostResourceProvider(anyObject(Set.class), anyObject(Map.class),
-            eq(managementController))).andReturn(csvResourceProvider).anyTimes();
-
-    expect(cluster.getCurrentStackVersion()).andReturn(stackId);
-    expect(cluster.getServiceComponentHosts(anyObject(String.class))).andReturn(schs).anyTimes();
-
-    RepositoryVersionEntity currentRepo = new RepositoryVersionEntity();
-    currentRepo.setVersion("2.2.2.0-2122");
-    ClusterVersionEntity current = new ClusterVersionEntity();
-    current.setRepositoryVersion(currentRepo);
-
-    Capture<StackId> capturedStackId = new Capture<>();
-    cluster.setDesiredStackVersion(capture(capturedStackId));
-      expectLastCall().once();
-    expect(cluster.getHosts()).andReturn(hosts).anyTimes();
-    expect(cluster.getCurrentClusterVersion()).andReturn(current).anyTimes();
-
-    expect(sch.getServiceName()).andReturn("HIVE").anyTimes();
-
-    expect(repositoryVersionDAOMock.findByDisplayName(anyObject(String.class))).andReturn(repoVersion);
-
-    clusterVersionDAO.updateVersions((Long) anyObject(),
-        (RepositoryVersionEntity) anyObject(), (RepositoryVersionEntity) anyObject());
-    expectLastCall().once();
-
-    hostVersionDAO.updateVersions((RepositoryVersionEntity) anyObject(), (RepositoryVersionEntity) anyObject());
-    expectLastCall().once();
-
-    hostComponentStateDAO.updateVersions((String) anyObject());
-    expectLastCall().once();
-
-    // replay
-    replay(managementController, response, clusters, resourceProviderFactory, csvResourceProvider,
-            cluster, repositoryVersionDAOMock, configHelper, sch, actionManager, clusterVersionDAO,
-            hostVersionDAO, hostComponentStateDAO);
-
-    ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(
-            type,
-            PropertyHelper.getPropertyIds(type),
-            PropertyHelper.getKeyPropertyIds(type),
-            managementController);
-
-    injector.injectMembers(provider);
-
-
-    // add the property map to a set for the request.  add more maps for multiple creates
-    Map<String, Object> properties = new LinkedHashMap<>();
-
-    // add properties to the request map
-    properties.put(ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID, clusterName);
-    properties.put(ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_STATE_PROPERTY_ID, "CURRENT");
-    properties.put(ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID, "HDP-2.2.2.0-2561");
-    properties.put(ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_FORCE, "true");
-
-    // create the request
-    Request request = PropertyHelper.getUpdateRequest(properties, null);
-
-    SecurityContextHolder.getContext().setAuthentication(authentication);
-
-    provider.updateResources(request, null);
-
-    // verify
-    verify(managementController, response, clusterVersionDAO, hostVersionDAO, hostComponentStateDAO);
-    Assert.assertEquals(capturedStackId.getValue(),
-            new StackId(newDesiredStack.getStackName(), newDesiredStack.getStackVersion()));
+    Assert.assertNotNull(executionCommand.getRepositoryFile());
+    Assert.assertEquals(0, executionCommand.getRepositoryFile().getRepositories().size());
   }
 
    @Test
@@ -1428,15 +1107,32 @@ public class ClusterStackVersionResourceProviderTest {
     int hostCount = 10;
     for (int i = 0; i < hostCount; i++) {
       String hostname = "host" + i;
+
+      List<HostVersionEntity> hostVersions = new ArrayList<>();
+
+      HostVersionEntity hostVersion = createNiceMock(HostVersionEntity.class);
+      expect(hostVersion.getRepositoryVersion()).andReturn(repoVersion);
+
+      hostVersions.add(hostVersion);
+      if (i == 2) {
+        // !!! make it look like there is already a versioned installed that is less than the one being installed
+        RepositoryVersionEntity badRve = new RepositoryVersionEntity();
+        badRve.setStack(stack);
+        badRve.setVersion("2.2.1.0-1000");
+
+        HostVersionEntity badHostVersion = createNiceMock(HostVersionEntity.class);
+        expect(badHostVersion.getRepositoryVersion()).andReturn(badRve);
+        hostVersions.add(badHostVersion);
+        replay(badHostVersion);
+      }
       Host host = createNiceMock(hostname, Host.class);
       expect(host.getHostName()).andReturn(hostname).anyTimes();
       expect(host.getOsFamily()).andReturn("redhat6").anyTimes();
       expect(host.getMaintenanceState(EasyMock.anyLong())).andReturn(
           MaintenanceState.OFF).anyTimes();
-      expect(host.getAllHostVersions()).andReturn(
-          Collections.<HostVersionEntity>emptyList()).anyTimes();
+      expect(host.getAllHostVersions()).andReturn(hostVersions).anyTimes();
 
-      replay(host);
+      replay(host, hostVersion);
       hostsForCluster.put(hostname, host);
     }
 
@@ -1482,11 +1178,11 @@ public class ClusterStackVersionResourceProviderTest {
     expect(managementController.getActionManager()).andReturn(actionManager).anyTimes();
     expect(managementController.getJdkResourceUrl()).andReturn("/JdkResourceUrl").anyTimes();
     expect(managementController.getPackagesForServiceHost(anyObject(ServiceInfo.class),
-            (Map<String, String>) anyObject(List.class), anyObject(String.class))).
+            EasyMock.<Map<String, String>>anyObject(), anyObject(String.class))).
             andReturn(packages).times((hostCount - 1) * 2); // 1 host has no versionable components, other hosts have 2 services
 //            // that's why we don't send commands to it
 
-    expect(resourceProviderFactory.getHostResourceProvider(anyObject(Set.class), anyObject(Map.class),
+    expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(), EasyMock.<Map<Resource.Type, String>>anyObject(),
             eq(managementController))).andReturn(csvResourceProvider).anyTimes();
 
     expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster);
@@ -1510,6 +1206,12 @@ public class ClusterStackVersionResourceProviderTest {
       }
     }).anyTimes();
 
+    // now the important expectations - that the cluster transition methods were
+    // called correctly
+    expect(cluster.transitionHostsToInstalling(anyObject(RepositoryVersionEntity.class),
+        anyObject(VersionDefinitionXml.class), eq(false))).andReturn(Collections.<Host>emptyList()).anyTimes();
+
+
     ExecutionCommand executionCommand = createNiceMock(ExecutionCommand.class);
     ExecutionCommandWrapper executionCommandWrapper = createNiceMock(ExecutionCommandWrapper.class);
 
@@ -1539,27 +1241,17 @@ public class ClusterStackVersionResourceProviderTest {
     ClusterEntity clusterEntity = new ClusterEntity();
     clusterEntity.setClusterId(1l);
     clusterEntity.setClusterName(clusterName);
-    ClusterVersionEntity cve = new ClusterVersionEntity(clusterEntity,
-            repoVersion, RepositoryVersionState.INSTALL_FAILED, 0, "");
-    expect(clusterVersionDAO.findByClusterAndStackAndVersion(anyObject(String.class),
-            anyObject(StackId.class), anyObject(String.class))).andReturn(cve);
 
-    StageUtils.setTopologyManager(injector.getInstance(TopologyManager.class));
+    TopologyManager topologyManager = injector.getInstance(TopologyManager.class);
+    StageUtils.setTopologyManager(topologyManager);
     StageUtils.setConfiguration(injector.getInstance(Configuration.class));
 
-    // !!! make it look like there is already a versioned installed that is less than the one being installed
-    ClusterVersionEntity bad = new ClusterVersionEntity();
-    RepositoryVersionEntity badRve = new RepositoryVersionEntity();
-    badRve.setStack(stack);
-    badRve.setVersion("2.2.1.0-1000");
-    bad.setRepositoryVersion(badRve);
 
-    expect(clusterVersionDAO.findByCluster(anyObject(String.class))).andReturn(Collections.<ClusterVersionEntity>singletonList(bad)).once();
 
     // replay
     replay(managementController, response, clusters, resourceProviderFactory, csvResourceProvider,
             cluster, repositoryVersionDAOMock, configHelper, schDatanode, schNamenode, schAMS, actionManager,
-            executionCommand, executionCommandWrapper,stage, stageFactory, clusterVersionDAO);
+            executionCommand, executionCommandWrapper,stage, stageFactory);
 
     ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(
         type,
@@ -1629,13 +1321,20 @@ public class ClusterStackVersionResourceProviderTest {
 
     File f = new File("src/test/resources/hbase_version_test.xml");
 
+    StackEntity stackEntity = new StackEntity();
+    stackEntity.setStackName(stackId.getStackName());
+    stackEntity.setStackVersion(stackId.getStackVersion());
+
     RepositoryVersionEntity repoVersionEntity = new RepositoryVersionEntity();
     repoVersionEntity.setId(1l);
     repoVersionEntity.setOperatingSystems(OS_JSON);
     repoVersionEntity.setVersionXml(IOUtils.toString(new FileInputStream(f)));
     repoVersionEntity.setVersionXsd("version_definition.xsd");
     repoVersionEntity.setType(RepositoryType.STANDARD);
+    repoVersionEntity.setVersion(repoVersion);
+    repoVersionEntity.setStack(stackEntity);
 
+    List<Host> hostsNeedingInstallCommands = new ArrayList<>();
     Map<String, Host> hostsForCluster = new HashMap<>();
     List<HostVersionEntity> hostVersionEntitiesMergedWithNotRequired = new ArrayList<>();
     int hostCount = 10;
@@ -1651,6 +1350,8 @@ public class ClusterStackVersionResourceProviderTest {
       // transition correct into the not required state
       if (i < hostCount - 2) {
         expect(host.hasComponentsAdvertisingVersions(eq(stackId))).andReturn(true).atLeastOnce();
+        hostsNeedingInstallCommands.add(host);
+        expect(host.getAllHostVersions()).andReturn(Collections.<HostVersionEntity>emptyList()).anyTimes();
       } else {
         expect(host.hasComponentsAdvertisingVersions(eq(stackId))).andReturn(false).atLeastOnce();
 
@@ -1699,11 +1400,11 @@ public class ClusterStackVersionResourceProviderTest {
     expect(managementController.getAuthName()).andReturn("admin").anyTimes();
     expect(managementController.getJdkResourceUrl()).andReturn("/JdkResourceUrl").anyTimes();
     expect(managementController.getPackagesForServiceHost(anyObject(ServiceInfo.class),
-        (Map<String, String>) anyObject(List.class), anyObject(String.class))).andReturn(
+        EasyMock.<Map<String, String>>anyObject(), anyObject(String.class))).andReturn(
             packages).anyTimes(); // only one host has the versionable component
 
-    expect(resourceProviderFactory.getHostResourceProvider(anyObject(Set.class),
-        anyObject(Map.class), eq(managementController))).andReturn(csvResourceProvider).anyTimes();
+    expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(),
+        EasyMock.<Map<Resource.Type, String>>anyObject(), eq(managementController))).andReturn(csvResourceProvider).anyTimes();
 
     expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster);
     expect(clusters.getHostsForCluster(anyObject(String.class))).andReturn(
@@ -1711,43 +1412,29 @@ public class ClusterStackVersionResourceProviderTest {
 
     String clusterName = "Cluster100";
     expect(cluster.getClusterId()).andReturn(1L).anyTimes();
-    expect(cluster.getClusterName()).andReturn(clusterName).atLeastOnce();
     expect(cluster.getHosts()).andReturn(hostsForCluster.values()).atLeastOnce();
     expect(cluster.getServices()).andReturn(serviceMap).anyTimes();
-    expect(cluster.getServiceComponentHosts(anyObject(String.class))).andReturn(serviceComponentHosts).anyTimes();
-    expect(cluster.getCurrentStackVersion()).andReturn(stackId).atLeastOnce();
+    expect(cluster.getServiceComponentHosts(anyObject(String.class))).andReturn(
+        serviceComponentHosts).anyTimes();
 
     expect(repositoryVersionDAOMock.findByStackAndVersion(anyObject(StackId.class),
         anyObject(String.class))).andReturn(repoVersionEntity);
 
-    expect(clusterVersionDAO.findByCluster(anyObject(String.class))).andReturn(
-        Collections.<ClusterVersionEntity> emptyList()).once();
-
     ClusterEntity clusterEntity = new ClusterEntity();
     clusterEntity.setClusterId(1l);
     clusterEntity.setClusterName(clusterName);
 
-    ClusterVersionEntity cve = new ClusterVersionEntity(clusterEntity, repoVersionEntity,
-        RepositoryVersionState.INSTALL_FAILED, 0, "");
-
-    // first expect back a null to make the code think it needs to create one,
-    // then return the real one it's going to use
-    expect(clusterVersionDAO.findByClusterAndStackAndVersion(anyObject(String.class),
-        anyObject(StackId.class), anyObject(String.class))).andReturn(null).once();
-    expect(clusterVersionDAO.findByClusterAndStackAndVersion(anyObject(String.class),
-        anyObject(StackId.class), anyObject(String.class))).andReturn(cve).once();
 
     // now the important expectations - that the cluster transition methods were
     // called correctly
-    cluster.transitionHosts(cve, RepositoryVersionState.INSTALLED);
-    for (HostVersionEntity hostVersionEntity : hostVersionEntitiesMergedWithNotRequired) {
-      expect(hostVersionDAO.merge(hostVersionEntity)).andReturn(hostVersionEntity).once();
-    }
+    expect(cluster.transitionHostsToInstalling(repoVersionEntity,
+        repoVersionEntity.getRepositoryXml(), true)).andReturn(
+            hostsNeedingInstallCommands).once();
 
     // replay
     replay(managementController, response, clusters, hdfsService, resourceProviderFactory,
         csvResourceProvider, cluster, repositoryVersionDAOMock, configHelper, schDatanode,
-        stageFactory, clusterVersionDAO, hostVersionDAO);
+        stageFactory, hostVersionDAO);
 
     ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(type,
         PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type),
@@ -1861,7 +1548,6 @@ public class ClusterStackVersionResourceProviderTest {
       bind(ConfigHelper.class).toInstance(configHelper);
       bind(Configuration.class).toInstance(configuration);
       bind(StageFactory.class).toInstance(stageFactory);
-      bind(ClusterVersionDAO.class).toInstance(clusterVersionDAO);
       bind(HostVersionDAO.class).toInstance(hostVersionDAO);
       bind(HostComponentStateDAO.class).toInstance(hostComponentStateDAO);
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostResourceProviderTest.java
index b9b8d2a..a4cc6fc 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostResourceProviderTest.java
@@ -37,6 +37,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.UUID;
 
 import javax.persistence.EntityManager;
 
@@ -73,9 +74,11 @@ import org.apache.ambari.server.state.Host;
 import org.apache.ambari.server.state.HostConfig;
 import org.apache.ambari.server.state.HostHealthStatus;
 import org.apache.ambari.server.state.HostHealthStatus.HealthStatus;
+import org.apache.ambari.server.state.HostState;
 import org.apache.ambari.server.state.MaintenanceState;
 import org.apache.ambari.server.state.ServiceComponentHost;
 import org.apache.ambari.server.state.stack.OsFamily;
+import org.apache.ambari.server.topology.LogicalRequest;
 import org.apache.ambari.server.topology.TopologyManager;
 import org.easymock.EasyMock;
 import org.easymock.EasyMockSupport;
@@ -130,8 +133,6 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     expect(cluster.getClusterId()).andReturn(2L).anyTimes();
     expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes();
-    cluster.recalculateAllClusterVersionStates();
-    EasyMock.expectLastCall().once();
 
     expect(clusters.getCluster("Cluster100")).andReturn(cluster).atLeastOnce();
     expect(clusters.getHost("Host100")).andReturn(null).atLeastOnce();
@@ -155,13 +156,13 @@ public class HostResourceProviderTest extends EasyMockSupport {
         managementController);
 
     // add the property map to a set for the request.  add more maps for multiple creates
-    Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String, Object>>();
+    Set<Map<String, Object>> propertySet = new LinkedHashSet<>();
 
-    Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    Map<String, Object> properties = new LinkedHashMap<>();
 
     // add properties to the request map
     properties.put(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID, "Cluster100");
-    properties.put(HostResourceProvider.HOST_NAME_PROPERTY_ID, "Host100");
+    properties.put(HostResourceProvider.HOST_HOST_NAME_PROPERTY_ID, "Host100");
 
     propertySet.add(properties);
 
@@ -206,12 +207,12 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     AbstractControllerResourceProvider.init(resourceProviderFactory);
 
-    List<Host> hosts = new LinkedList<Host>();
+    List<Host> hosts = new LinkedList<>();
     hosts.add(host100);
     hosts.add(host101);
     hosts.add(host102);
 
-    Set<Cluster> clusterSet = new HashSet<Cluster>();
+    Set<Cluster> clusterSet = new HashSet<>();
     clusterSet.add(cluster);
 
     // set expectations
@@ -240,10 +241,10 @@ public class HostResourceProviderTest extends EasyMockSupport {
     expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.HEALTHY).anyTimes();
     expect(healthStatus.getHealthReport()).andReturn("HEALTHY").anyTimes();
 
-    Set<String> propertyIds = new HashSet<String>();
+    Set<String> propertyIds = new HashSet<>();
 
     propertyIds.add(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID);
-    propertyIds.add(HostResourceProvider.HOST_NAME_PROPERTY_ID);
+    propertyIds.add(HostResourceProvider.HOST_HOST_NAME_PROPERTY_ID);
     propertyIds.add(HostResourceProvider.HOST_MAINTENANCE_STATE_PROPERTY_ID);
 
     Predicate predicate = buildPredicate("Cluster100", null);
@@ -296,17 +297,17 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     AbstractControllerResourceProvider.init(resourceProviderFactory);
 
-    Set<Cluster> clusterSet = new HashSet<Cluster>();
+    Set<Cluster> clusterSet = new HashSet<>();
     clusterSet.add(cluster);
 
     ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component100", "Component 100",
-        "Host100", "Host100", "STARTED", "", null, null, null);
+        "Host100", "Host100", "STARTED", "", null, null, null, null);
     ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component102", "Component 102",
-        "Host100", "Host100", "STARTED", "", null, null, null);
+        "Host100", "Host100", "STARTED", "", null, null, null, null);
     ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component103", "Component 103",
-        "Host100", "Host100", "STARTED", "", null, null, null);
+        "Host100", "Host100", "STARTED", "", null, null, null, null);
 
-    Set<ServiceComponentHostResponse> responses = new HashSet<ServiceComponentHostResponse>();
+    Set<ServiceComponentHostResponse> responses = new HashSet<>();
     responses.add(shr1);
     responses.add(shr2);
     responses.add(shr3);
@@ -328,11 +329,8 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     expect(hostResponse1.getClusterName()).andReturn("").anyTimes();
     expect(hostResponse1.getHostname()).andReturn("Host100").anyTimes();
-    expect(hostResponse1.getHealthStatus()).andReturn(healthStatus).anyTimes();
     expect(hostResponse1.getStatus()).andReturn(HealthStatus.HEALTHY.name()).anyTimes();
-
-    expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.HEALTHY).anyTimes();
-    expect(healthStatus.getHealthReport()).andReturn("HEALTHY").anyTimes();
+    expect(hostResponse1.getHealthReport()).andReturn("HEALTHY").anyTimes();
 
 
     expect(ambariMetaInfo.getComponent((String) anyObject(), (String) anyObject(),
@@ -344,10 +342,10 @@ public class HostResourceProviderTest extends EasyMockSupport {
         EasyMock.<Map<Resource.Type, String>>anyObject(),
         eq(managementController))).andReturn(hostResourceProvider).anyTimes();
 
-    Set<String> propertyIds = new HashSet<String>();
+    Set<String> propertyIds = new HashSet<>();
 
     propertyIds.add(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID);
-    propertyIds.add(HostResourceProvider.HOST_NAME_PROPERTY_ID);
+    propertyIds.add(HostResourceProvider.HOST_HOST_NAME_PROPERTY_ID);
     propertyIds.add(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID);
 
     Predicate predicate = buildPredicate("Cluster100", null);
@@ -397,17 +395,17 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     AbstractControllerResourceProvider.init(resourceProviderFactory);
 
-    Set<Cluster> clusterSet = new HashSet<Cluster>();
+    Set<Cluster> clusterSet = new HashSet<>();
     clusterSet.add(cluster);
 
     ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component100", "Component 100",
-        "Host100", "Host100", "STARTED", "", null, null, null);
+        "Host100", "Host100", "STARTED", "", null, null, null, null);
     ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component102", "Component 102",
-        "Host100", "Host100", "STARTED", "", null, null, null);
+        "Host100", "Host100", "STARTED", "", null, null, null, null);
     ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component103", "Component 103",
-        "Host100", "Host100", "STARTED", "", null, null, null);
+        "Host100", "Host100", "STARTED", "", null, null, null, null);
 
-    Set<ServiceComponentHostResponse> responses = new HashSet<ServiceComponentHostResponse>();
+    Set<ServiceComponentHostResponse> responses = new HashSet<>();
     responses.add(shr1);
     responses.add(shr2);
     responses.add(shr3);
@@ -439,10 +437,10 @@ public class HostResourceProviderTest extends EasyMockSupport {
         EasyMock.<Map<Resource.Type, String>>anyObject(),
         eq(managementController))).andReturn(hostResourceProvider).anyTimes();
 
-    Set<String> propertyIds = new HashSet<String>();
+    Set<String> propertyIds = new HashSet<>();
 
     propertyIds.add(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID);
-    propertyIds.add(HostResourceProvider.HOST_NAME_PROPERTY_ID);
+    propertyIds.add(HostResourceProvider.HOST_HOST_NAME_PROPERTY_ID);
     propertyIds.add(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID);
 
     Predicate predicate = buildPredicate("Cluster100", null);
@@ -482,7 +480,6 @@ public class HostResourceProviderTest extends EasyMockSupport {
     AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class);
     Clusters clusters = injector.getInstance(Clusters.class);
     Cluster cluster = createMock(Cluster.class);
-    HostHealthStatus healthStatus = createNiceMock(HostHealthStatus.class);
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     ComponentInfo componentInfo = createNiceMock(ComponentInfo.class);
     HostResponse hostResponse1 = createNiceMock(HostResponse.class);
@@ -493,17 +490,17 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     AbstractControllerResourceProvider.init(resourceProviderFactory);
 
-    Set<Cluster> clusterSet = new HashSet<Cluster>();
+    Set<Cluster> clusterSet = new HashSet<>();
     clusterSet.add(cluster);
 
     ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component100", "Component 100",
-        "Host100", "Host100", "STARTED", "", null, null, null);
+        "Host100", "Host100", "STARTED", "", null, null, null, null);
     ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component102", "Component 102",
-        "Host100", "Host100", "INSTALLED", "", null, null, null);
+        "Host100", "Host100", "INSTALLED", "", null, null, null, null);
     ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component103", "Component 103",
-        "Host100", "Host100", "STARTED", "", null, null, null);
+        "Host100", "Host100", "STARTED", "", null, null, null, null);
 
-    Set<ServiceComponentHostResponse> responses = new HashSet<ServiceComponentHostResponse>();
+    Set<ServiceComponentHostResponse> responses = new HashSet<>();
     responses.add(shr1);
     responses.add(shr2);
     responses.add(shr3);
@@ -525,11 +522,8 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     expect(hostResponse1.getClusterName()).andReturn("Cluster100").anyTimes();
     expect(hostResponse1.getHostname()).andReturn("Host100").anyTimes();
-    expect(hostResponse1.getHealthStatus()).andReturn(healthStatus).anyTimes();
     expect(hostResponse1.getStatus()).andReturn(HealthStatus.UNHEALTHY.name()).anyTimes();
-
-    expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.HEALTHY).anyTimes();
-    expect(healthStatus.getHealthReport()).andReturn("HEALTHY").anyTimes();
+    expect(hostResponse1.getHealthReport()).andReturn("HEALTHY").anyTimes();
 
     expect(ambariMetaInfo.getComponent((String) anyObject(), (String) anyObject(),
         (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes();
@@ -540,10 +534,10 @@ public class HostResourceProviderTest extends EasyMockSupport {
         EasyMock.<Map<Resource.Type, String>>anyObject(),
         eq(managementController))).andReturn(hostResourceProvider).anyTimes();
 
-    Set<String> propertyIds = new HashSet<String>();
+    Set<String> propertyIds = new HashSet<>();
 
     propertyIds.add(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID);
-    propertyIds.add(HostResourceProvider.HOST_NAME_PROPERTY_ID);
+    propertyIds.add(HostResourceProvider.HOST_HOST_NAME_PROPERTY_ID);
     propertyIds.add(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID);
 
     Predicate predicate = buildPredicate("Cluster100", null);
@@ -582,7 +576,6 @@ public class HostResourceProviderTest extends EasyMockSupport {
     AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class);
     Clusters clusters = injector.getInstance(Clusters.class);
     Cluster cluster = createMock(Cluster.class);
-    HostHealthStatus healthStatus = createNiceMock(HostHealthStatus.class);
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     HostResponse hostResponse1 = createNiceMock(HostResponse.class);
     ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class);
@@ -609,19 +602,16 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     expect(hostResponse1.getClusterName()).andReturn("Cluster100").anyTimes();
     expect(hostResponse1.getHostname()).andReturn("Host100").anyTimes();
-    expect(hostResponse1.getHealthStatus()).andReturn(healthStatus).anyTimes();
     expect(hostResponse1.getStatus()).andReturn(HealthStatus.UNKNOWN.name()).anyTimes();
-
-    expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.UNKNOWN).anyTimes();
-    expect(healthStatus.getHealthReport()).andReturn("UNKNOWN").anyTimes();
+    expect(hostResponse1.getHealthReport()).andReturn("UNKNOWN").anyTimes();
     expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(),
         EasyMock.<Map<Resource.Type, String>>anyObject(),
         eq(managementController))).andReturn(hostResourceProvider).anyTimes();
 
-    Set<String> propertyIds = new HashSet<String>();
+    Set<String> propertyIds = new HashSet<>();
 
     propertyIds.add(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID);
-    propertyIds.add(HostResourceProvider.HOST_NAME_PROPERTY_ID);
+    propertyIds.add(HostResourceProvider.HOST_HOST_NAME_PROPERTY_ID);
     propertyIds.add(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID);
 
     Predicate predicate = buildPredicate("Cluster100", null);
@@ -681,7 +671,7 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     RecoveryReport rr = new RecoveryReport();
     rr.setSummary("RECOVERABLE");
-    List<ComponentRecoveryReport> compRecReports = new ArrayList<ComponentRecoveryReport>();
+    List<ComponentRecoveryReport> compRecReports = new ArrayList<>();
     ComponentRecoveryReport compRecReport = new ComponentRecoveryReport();
     compRecReport.setLimitReached(Boolean.FALSE);
     compRecReport.setName("DATANODE");
@@ -693,13 +683,13 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     AbstractControllerResourceProvider.init(resourceProviderFactory);
 
-    Set<Cluster> clusterSet = new HashSet<Cluster>();
+    Set<Cluster> clusterSet = new HashSet<>();
     clusterSet.add(cluster);
 
     ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component100", "Component 100",
-        "Host100", "Host100", "STARTED", "", null, null, null);
+        "Host100", "Host100", "STARTED", "", null, null, null, null);
 
-    Set<ServiceComponentHostResponse> responses = new HashSet<ServiceComponentHostResponse>();
+    Set<ServiceComponentHostResponse> responses = new HashSet<>();
     responses.add(shr1);
 
     // set expectations
@@ -723,10 +713,10 @@ public class HostResourceProviderTest extends EasyMockSupport {
         eq(managementController))).andReturn(hostResourceProvider).anyTimes();
 
 
-    Set<String> propertyIds = new HashSet<String>();
+    Set<String> propertyIds = new HashSet<>();
 
     propertyIds.add(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID);
-    propertyIds.add(HostResourceProvider.HOST_NAME_PROPERTY_ID);
+    propertyIds.add(HostResourceProvider.HOST_HOST_NAME_PROPERTY_ID);
     propertyIds.add(HostResourceProvider.HOST_RECOVERY_REPORT_PROPERTY_ID);
     propertyIds.add(HostResourceProvider.HOST_RECOVERY_SUMMARY_PROPERTY_ID);
 
@@ -780,17 +770,17 @@ public class HostResourceProviderTest extends EasyMockSupport {
     Host host100 = createMockHost("Host100", "Cluster100", null, "ALERT", "RECOVERABLE", null);
 
     AbstractControllerResourceProvider.init(resourceProviderFactory);
-    Set<Cluster> clusterSet = new HashSet<Cluster>();
+    Set<Cluster> clusterSet = new HashSet<>();
     clusterSet.add(cluster);
 
     ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component100", "Component 100",
-        "Host100", "Host100", "STARTED", "", null, null, null);
+        "Host100", "Host100", "STARTED", "", null, null, null, null);
     ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component102", "Component 102",
-        "Host100", "Host100", "INSTALLED", "", null, null, null);
+        "Host100", "Host100", "INSTALLED", "", null, null, null, null);
     ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component103", "Component 103",
-        "Host100", "Host100", "STARTED", "", null, null, null);
+        "Host100", "Host100", "STARTED", "", null, null, null, null);
 
-    Set<ServiceComponentHostResponse> responses = new HashSet<ServiceComponentHostResponse>();
+    Set<ServiceComponentHostResponse> responses = new HashSet<>();
     responses.add(shr1);
     responses.add(shr2);
     responses.add(shr3);
@@ -809,10 +799,8 @@ public class HostResourceProviderTest extends EasyMockSupport {
     expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes();
     expect(hostResponse1.getClusterName()).andReturn("Cluster100").anyTimes();
     expect(hostResponse1.getHostname()).andReturn("Host100").anyTimes();
-    expect(hostResponse1.getHealthStatus()).andReturn(healthStatus).anyTimes();
     expect(hostResponse1.getStatus()).andReturn(HealthStatus.ALERT.name()).anyTimes();
-    expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.HEALTHY).anyTimes();
-    expect(healthStatus.getHealthReport()).andReturn("HEALTHY").anyTimes();
+    expect(hostResponse1.getHealthReport()).andReturn("HEALTHY").anyTimes();
     expect(ambariMetaInfo.getComponent((String) anyObject(), (String) anyObject(),
         (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes();
     expect(componentInfo.getCategory()).andReturn("SLAVE").anyTimes();
@@ -821,10 +809,10 @@ public class HostResourceProviderTest extends EasyMockSupport {
         eq(managementController))).andReturn(hostResourceProvider).anyTimes();
 
 
-    Set<String> propertyIds = new HashSet<String>();
+    Set<String> propertyIds = new HashSet<>();
 
     propertyIds.add(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID);
-    propertyIds.add(HostResourceProvider.HOST_NAME_PROPERTY_ID);
+    propertyIds.add(HostResourceProvider.HOST_HOST_NAME_PROPERTY_ID);
     propertyIds.add(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID);
 
     Predicate predicate = buildPredicate("Cluster100", null);
@@ -882,7 +870,6 @@ public class HostResourceProviderTest extends EasyMockSupport {
     AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class);
     Clusters clusters = injector.getInstance(Clusters.class);
     Cluster cluster = createMock(Cluster.class);
-    HostHealthStatus healthStatus = createNiceMock(HostHealthStatus.class);
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     HostResponse hostResponse1 = createNiceMock(HostResponse.class);
     ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class);
@@ -907,16 +894,12 @@ public class HostResourceProviderTest extends EasyMockSupport {
     expect(clusters.getHost("Host100")).andReturn(host100).anyTimes();
     clusters.mapAndPublishHostsToCluster(Collections.singleton("Host100"), "Cluster100");
     expectLastCall().anyTimes();
-    cluster.recalculateAllClusterVersionStates();
-    expectLastCall().anyTimes();
     expect(cluster.getClusterId()).andReturn(2L).anyTimes();
     expect(cluster.getResourceId()).andReturn(4L).anyTimes();
     expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes();
     expect(hostResponse1.getClusterName()).andReturn("Cluster100").anyTimes();
     expect(hostResponse1.getHostname()).andReturn("Host100").anyTimes();
-    expect(hostResponse1.getHealthStatus()).andReturn(healthStatus).anyTimes();
-    expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.HEALTHY).anyTimes();
-    expect(healthStatus.getHealthReport()).andReturn("HEALTHY").anyTimes();
+    expect(hostResponse1.getHealthReport()).andReturn("HEALTHY").anyTimes();
     expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(),
         EasyMock.<Map<Resource.Type, String>>anyObject(),
         eq(managementController))).andReturn(hostResourceProvider).anyTimes();
@@ -926,10 +909,10 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     SecurityContextHolder.getContext().setAuthentication(authentication);
 
-    Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    Map<String, Object> properties = new LinkedHashMap<>();
 
     properties.put(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID, "Cluster100");
-    properties.put(HostResourceProvider.HOST_NAME_PROPERTY_ID, "Host100");
+    properties.put(HostResourceProvider.HOST_HOST_NAME_PROPERTY_ID, "Host100");
     properties.put(PropertyHelper.getPropertyId("Hosts.desired_config", "type"), "global");
     properties.put(PropertyHelper.getPropertyId("Hosts.desired_config", "tag"), "version1");
     properties.put(PropertyHelper.getPropertyId("Hosts.desired_config.properties", "a"), "b");
@@ -974,7 +957,6 @@ public class HostResourceProviderTest extends EasyMockSupport {
     AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class);
     Clusters clusters = injector.getInstance(Clusters.class);
     Cluster cluster = createMock(Cluster.class);
-    HostHealthStatus healthStatus = createNiceMock(HostHealthStatus.class);
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     HostResponse hostResponse1 = createNiceMock(HostResponse.class);
     ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class);
@@ -982,7 +964,7 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     AbstractControllerResourceProvider.init(resourceProviderFactory);
 
-    Set<Cluster> clusterSet = new HashSet<Cluster>();
+    Set<Cluster> clusterSet = new HashSet<>();
     clusterSet.add(cluster);
 
     Host host100 = createMockHost("Host100", "Cluster100", null, "HEALTHY", "RECOVERABLE", null);
@@ -1000,16 +982,12 @@ public class HostResourceProviderTest extends EasyMockSupport {
     expect(clusters.getHostsForCluster("Cluster100")).andReturn(Collections.singletonMap("Host100", host100)).anyTimes();
     clusters.mapAndPublishHostsToCluster(Collections.singleton("Host100"), "Cluster100");
     expectLastCall().anyTimes();
-    cluster.recalculateAllClusterVersionStates();
-    expectLastCall().anyTimes();
     expect(cluster.getClusterId()).andReturn(2L).anyTimes();
     expect(cluster.getResourceId()).andReturn(4L).anyTimes();
     expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes();
     expect(hostResponse1.getClusterName()).andReturn("Cluster100").anyTimes();
     expect(hostResponse1.getHostname()).andReturn("Host100").anyTimes();
-    expect(hostResponse1.getHealthStatus()).andReturn(healthStatus).anyTimes();
-    expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.HEALTHY).anyTimes();
-    expect(healthStatus.getHealthReport()).andReturn("HEALTHY").anyTimes();
+    expect(hostResponse1.getHealthReport()).andReturn("HEALTHY").anyTimes();
     expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(),
         EasyMock.<Map<Resource.Type, String>>anyObject(),
         eq(managementController))).andReturn(hostResourceProvider).anyTimes();
@@ -1026,7 +1004,7 @@ public class HostResourceProviderTest extends EasyMockSupport {
         managementController);
 
     // add the property map to a set for the request.
-    Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    Map<String, Object> properties = new LinkedHashMap<>();
 
     properties.put(HostResourceProvider.HOST_RACK_INFO_PROPERTY_ID, "rack info");
 
@@ -1075,16 +1053,15 @@ public class HostResourceProviderTest extends EasyMockSupport {
     expect(clusters.getHost("Host100")).andReturn(host1).anyTimes();
     expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes();
     expect(clusters.getClustersForHost("Host100")).andReturn(clusterSet).anyTimes();
-    expect(cluster.getServiceComponentHosts("Host100")).andReturn(Collections.EMPTY_LIST);
+    expect(cluster.getServiceComponentHosts("Host100")).andReturn(Collections.<ServiceComponentHost>emptyList());
     expect(cluster.getClusterId()).andReturn(100L).anyTimes();
     expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes();
     clusters.deleteHost("Host100");
     clusters.publishHostsDeletion(Collections.EMPTY_SET, Collections.singleton("Host100"));
-    cluster.recalculateAllClusterVersionStates();
     expect(host1.getHostName()).andReturn("Host100").anyTimes();
     expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.HEALTHY).anyTimes();
     expect(healthStatus.getHealthReport()).andReturn("HEALTHY").anyTimes();
-    expect(topologyManager.getRequests(Collections.EMPTY_LIST)).andReturn(Collections.EMPTY_LIST).anyTimes();
+    expect(topologyManager.getRequests(Collections.<Long>emptyList())).andReturn(Collections.<LogicalRequest>emptyList()).anyTimes();
 
     // replay
     replayAll();
@@ -1144,9 +1121,11 @@ public class HostResourceProviderTest extends EasyMockSupport {
     HostResponse response = createNiceMock(HostResponse.class);
 
     Set<Cluster> setCluster = Collections.singleton(cluster);
+    Map<String, DesiredConfig> desiredConfigs = new HashMap<>();
+    Map<String, HostConfig> desiredHostConfigs = new HashMap<>();
 
     // requests
-    HostRequest request1 = new HostRequest("host1", "cluster1", Collections.<String, String>emptyMap());
+    HostRequest request1 = new HostRequest("host1", "cluster1");
 
     Set<HostRequest> setRequests = new HashSet<>();
     setRequests.add(request1);
@@ -1159,7 +1138,8 @@ public class HostResourceProviderTest extends EasyMockSupport {
     expect(clusters.getClustersForHost("host1")).andReturn(setCluster);
     expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes();
     expect(cluster.getClusterId()).andReturn(2L).anyTimes();
-    expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes();
+    expect(cluster.getDesiredConfigs()).andReturn(desiredConfigs);
+    expect(host.getDesiredHostConfigs(cluster, desiredConfigs)).andReturn(desiredHostConfigs);
     expect(host.getHostName()).andReturn("host1").anyTimes();
     expect(host.convertToResponse()).andReturn(response);
     response.setClusterName("cluster1");
@@ -1192,7 +1172,7 @@ public class HostResourceProviderTest extends EasyMockSupport {
     Cluster cluster = createMock(Cluster.class);
 
     // requests
-    HostRequest request1 = new HostRequest("host1", "cluster1", Collections.<String, String>emptyMap());
+    HostRequest request1 = new HostRequest("host1", "cluster1");
     Set<HostRequest> setRequests = Collections.singleton(request1);
 
     // expectations
@@ -1223,7 +1203,7 @@ public class HostResourceProviderTest extends EasyMockSupport {
     Host host = createNiceMock(Host.class);
 
     // requests
-    HostRequest request1 = new HostRequest("host1", "cluster1", Collections.<String, String>emptyMap());
+    HostRequest request1 = new HostRequest("host1", "cluster1");
     Set<HostRequest> setRequests = Collections.singleton(request1);
 
     // expectations
@@ -1263,12 +1243,12 @@ public class HostResourceProviderTest extends EasyMockSupport {
     HostResponse response2 = createNiceMock(HostResponse.class);
 
     // requests
-    HostRequest request1 = new HostRequest("host1", "cluster1", Collections.<String, String>emptyMap());
-    HostRequest request2 = new HostRequest("host2", "cluster1", Collections.<String, String>emptyMap());
-    HostRequest request3 = new HostRequest("host3", "cluster1", Collections.<String, String>emptyMap());
-    HostRequest request4 = new HostRequest("host4", "cluster1", Collections.<String, String>emptyMap());
+    HostRequest request1 = new HostRequest("host1", "cluster1");
+    HostRequest request2 = new HostRequest("host2", "cluster1");
+    HostRequest request3 = new HostRequest("host3", "cluster1");
+    HostRequest request4 = new HostRequest("host4", "cluster1");
 
-    Set<HostRequest> setRequests = new HashSet<HostRequest>();
+    Set<HostRequest> setRequests = new HashSet<>();
     setRequests.add(request1);
     setRequests.add(request2);
     setRequests.add(request3);
@@ -1316,14 +1296,18 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
   public static void createHosts(AmbariManagementController controller, Set<HostRequest> requests) throws AmbariException {
     HostResourceProvider provider = getHostProvider(controller);
-    Set<Map<String, Object>> properties = new HashSet<Map<String, Object>>();
+    Set<Map<String, Object>> properties = new HashSet<>();
 
     for (HostRequest request : requests) {
-      Map<String, Object> requestProperties = new HashMap<String, Object>();
-      requestProperties.put(HostResourceProvider.HOST_NAME_PROPERTY_ID, request.getHostname());
+      Map<String, Object> requestProperties = new HashMap<>();
+      requestProperties.put(HostResourceProvider.HOST_HOST_NAME_PROPERTY_ID, request.getHostname());
       requestProperties.put(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID, request.getClusterName());
+      if (null != request.getRackInfo()) {
+        requestProperties.put(HostResourceProvider.HOST_RACK_INFO_PROPERTY_ID, UUID.randomUUID().toString());
+      }
       properties.add(requestProperties);
     }
+
     provider.createHosts(PropertyHelper.getCreateRequest(properties, Collections.<String, String>emptyMap()));
   }
 
@@ -1336,7 +1320,7 @@ public class HostResourceProviderTest extends EasyMockSupport {
   public static void deleteHosts(AmbariManagementController controller, Set<HostRequest> requests)
       throws AmbariException {
     TopologyManager topologyManager = EasyMock.createNiceMock(TopologyManager.class);
-    expect(topologyManager.getRequests(Collections.EMPTY_LIST)).andReturn(Collections.EMPTY_LIST).anyTimes();
+    expect(topologyManager.getRequests(Collections.<Long>emptyList())).andReturn(Collections.<LogicalRequest>emptyList()).anyTimes();
 
     replay(topologyManager);
 
@@ -1349,7 +1333,7 @@ public class HostResourceProviderTest extends EasyMockSupport {
                                                  Set<HostRequest> requests, boolean dryRun, boolean forceDelete)
       throws AmbariException {
     TopologyManager topologyManager = EasyMock.createNiceMock(TopologyManager.class);
-    expect(topologyManager.getRequests(Collections.EMPTY_LIST)).andReturn(Collections.EMPTY_LIST).anyTimes();
+    expect(topologyManager.getRequests(Collections.<Long>emptyList())).andReturn(Collections.<LogicalRequest>emptyList()).anyTimes();
 
     replay(topologyManager);
 
@@ -1408,8 +1392,8 @@ public class HostResourceProviderTest extends EasyMockSupport {
                               String status, String recoverySummary, RecoveryReport recoveryReport) {
     Host host = createMock(Host.class);
     HostHealthStatus hostHealthStatus = new HostHealthStatus(HealthStatus.HEALTHY, "");
-    HostResponse hostResponse = new HostResponse(hostName, clusterName, null, null, 1, 1, null,
-        "centos6", null, 1024, 1024, null, 1, 1, null, null, null, hostHealthStatus, "HEALTHY", status);
+    HostResponse hostResponse = new HostResponse(hostName, clusterName, null, 1, 1, null,
+        "centos6", 1024, null, 1, 1, null, null, null, hostHealthStatus, HostState.HEALTHY, status);
 
     hostResponse.setRecoverySummary(recoverySummary);
     hostResponse.setRecoveryReport(recoveryReport);
@@ -1417,7 +1401,7 @@ public class HostResourceProviderTest extends EasyMockSupport {
 
     try {
       expect(host.getDesiredHostConfigs(EasyMock.<Cluster> anyObject(),
-          EasyMock.<Map> anyObject())).andReturn(desiredConfigs).anyTimes();
+          EasyMock.<Map<String, DesiredConfig>> anyObject())).andReturn(desiredConfigs).anyTimes();
 
     } catch (AmbariException e) {
       Assert.fail(e.getMessage());
@@ -1433,11 +1417,11 @@ public class HostResourceProviderTest extends EasyMockSupport {
     PredicateBuilder builder = new PredicateBuilder();
     if (clusterName != null && hostName != null) {
       return builder.property(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID).equals(clusterName)
-      .and().property(HostResourceProvider.HOST_NAME_PROPERTY_ID).equals(hostName).toPredicate();
+      .and().property(HostResourceProvider.HOST_HOST_NAME_PROPERTY_ID).equals(hostName).toPredicate();
     }
 
     return clusterName != null ?
             builder.property(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID).equals(clusterName).toPredicate() :
-            builder.property(HostResourceProvider.HOST_NAME_PROPERTY_ID).equals(hostName).toPredicate();
+            builder.property(HostResourceProvider.HOST_HOST_NAME_PROPERTY_ID).equals(hostName).toPredicate();
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProviderTest.java
index 42b9632..26b89d3 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HostStackVersionResourceProviderTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -79,6 +79,7 @@ import org.apache.ambari.server.state.cluster.ClusterImpl;
 import org.apache.ambari.server.topology.TopologyManager;
 import org.apache.ambari.server.utils.StageUtils;
 import org.easymock.Capture;
+import org.easymock.EasyMock;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -153,9 +154,10 @@ public class HostStackVersionResourceProviderTest {
 
     StackEntity stack = new StackEntity();
     stack.setStackName("HDP");
-    stack.setStackVersion("2.2");
+    stack.setStackVersion("2.0.1");
     repoVersion.setStack(stack);
     repoVersion.setVersion("2.2");
+    repoVersion.setId(1L);
   }
 
   @After
@@ -232,9 +234,9 @@ public class HostStackVersionResourceProviderTest {
     expect(managementController.getActionManager()).andReturn(actionManager).anyTimes();
     expect(managementController.getJdkResourceUrl()).andReturn("/JdkResourceUrl").anyTimes();
     expect(managementController.getPackagesForServiceHost(anyObject(ServiceInfo.class),
-            anyObject(Map.class), anyObject(String.class))).andReturn(packages).anyTimes();
+            EasyMock.<Map<String, String>>anyObject(), anyObject(String.class))).andReturn(packages).anyTimes();
 
-    expect(resourceProviderFactory.getHostResourceProvider(anyObject(Set.class), anyObject(Map.class),
+    expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(), EasyMock.<Map<Resource.Type, String>>anyObject(),
         eq(managementController))).andReturn(csvResourceProvider).anyTimes();
 
     expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster);
@@ -426,9 +428,9 @@ public class HostStackVersionResourceProviderTest {
     expect(managementController.getActionManager()).andReturn(actionManager).anyTimes();
     expect(managementController.getJdkResourceUrl()).andReturn("/JdkResourceUrl").anyTimes();
     expect(managementController.getPackagesForServiceHost(anyObject(ServiceInfo.class),
-            anyObject(Map.class), anyObject(String.class))).andReturn(packages).anyTimes();
+            EasyMock.<Map<String, String>>anyObject(), anyObject(String.class))).andReturn(packages).anyTimes();
 
-    expect(resourceProviderFactory.getHostResourceProvider(anyObject(Set.class), anyObject(Map.class),
+    expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(), EasyMock.<Map<Resource.Type, String>>anyObject(),
             eq(managementController))).andReturn(csvResourceProvider).anyTimes();
 
     expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster);


[13/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProviderTest.java
index a41fb0f..2a48fa6 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PreUpgradeCheckResourceProviderTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -101,10 +101,9 @@ public class PreUpgradeCheckResourceProviderTest {
     ServiceFactory serviceFactory = createNiceMock(ServiceFactory.class);
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
 
-    String stackName = "Stack100";
-    Map<String, Service> allServiceMap = new HashMap<String, Service>();
+    Map<String, Service> allServiceMap = new HashMap<>();
     allServiceMap.put("Service100", service);
-    Map<String, ServiceInfo> allServiceInfoMap = new HashMap<String, ServiceInfo>();
+    Map<String, ServiceInfo> allServiceInfoMap = new HashMap<>();
     allServiceInfoMap.put("Service100", serviceInfo);
 
     expect(configuration.isUpgradePrecheckBypass()).andReturn(false).anyTimes();
@@ -117,23 +116,23 @@ public class PreUpgradeCheckResourceProviderTest {
     expect(cluster.getService("Service100")).andReturn(service).anyTimes();
     expect(cluster.getCurrentStackVersion()).andReturn(currentStackId).anyTimes();
 
-    expect(currentStackId.getStackName()).andReturn(stackName).anyTimes();
+    expect(currentStackId.getStackName()).andReturn("Stack100").anyTimes();
     expect(currentStackId.getStackVersion()).andReturn("1.0").anyTimes();
-    expect(targetStackId.getStackName()).andReturn(stackName).anyTimes();
+    expect(targetStackId.getStackName()).andReturn("Stack100").anyTimes();
     expect(targetStackId.getStackVersion()).andReturn("1.1").anyTimes();
 
-    expect(repoDao.findByStackNameAndVersion(stackName, "Repo100")).andReturn(repo).anyTimes();
+    expect(repoDao.findByStackNameAndVersion("Stack100", "Repo100")).andReturn(repo).anyTimes();
     expect(repo.getStackId()).andReturn(targetStackId).atLeastOnce();
-    expect(upgradeHelper.suggestUpgradePack("Cluster100", "1.0", "Repo100", Direction.UPGRADE, UpgradeType.NON_ROLLING, stackName, "upgrade_pack11")).andReturn(upgradePack);
+    expect(upgradeHelper.suggestUpgradePack("Cluster100", "1.0", "Repo100", Direction.UPGRADE, UpgradeType.NON_ROLLING, "upgrade_pack11")).andReturn(upgradePack);
 
-    List<AbstractCheckDescriptor> upgradeChecksToRun = new LinkedList<AbstractCheckDescriptor>();
-    List<String> prerequisiteChecks = new LinkedList<String>();
+    List<AbstractCheckDescriptor> upgradeChecksToRun = new LinkedList<>();
+    List<String> prerequisiteChecks = new LinkedList<>();
     prerequisiteChecks.add("org.apache.ambari.server.sample.checks.SampleServiceCheck");
     expect(upgradePack.getPrerequisiteCheckConfig()).andReturn(config);
     expect(upgradePack.getPrerequisiteChecks()).andReturn(prerequisiteChecks).anyTimes();
     expect(upgradePack.getTarget()).andReturn("1.1.*.*").anyTimes();
 
-    expect(ambariMetaInfo.getServices(stackName, "1.0")).andReturn(allServiceInfoMap).anyTimes();
+    expect(ambariMetaInfo.getServices("Stack100", "1.0")).andReturn(allServiceInfoMap).anyTimes();
     String checks = ClassLoader.getSystemClassLoader().getResource("checks").getPath();
     expect(serviceInfo.getChecksFolder()).andReturn(new File(checks));
 
@@ -148,7 +147,6 @@ public class PreUpgradeCheckResourceProviderTest {
     Predicate predicate = builder.property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID).equals("Cluster100").and()
         .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_PACK_PROPERTY_ID).equals("upgrade_pack11").and()
         .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID).equals(UpgradeType.NON_ROLLING).and()
-        .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_TARGET_STACK_ID).equals(stackName + "-100").and()
         .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID).equals("Repo100").toPredicate();
 
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
index 0230a97..6bc8b95 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RepositoryVersionResourceProviderTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,7 +19,6 @@
 package org.apache.ambari.server.controller.internal;
 
 import java.sql.SQLException;
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
@@ -40,11 +39,8 @@ import org.apache.ambari.server.controller.utilities.PredicateBuilder;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
 import org.apache.ambari.server.orm.entities.RepositoryEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
@@ -54,13 +50,13 @@ import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.OperatingSystemInfo;
 import org.apache.ambari.server.state.RepositoryInfo;
-import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.ServiceInfo;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.StackInfo;
 import org.apache.ambari.server.state.stack.UpgradePack;
 import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
 import org.junit.After;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
@@ -73,57 +69,28 @@ import com.google.common.collect.Sets;
 import com.google.gson.Gson;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-
-import junit.framework.Assert;
-
 /**
  * RepositoryVersionResourceProvider tests.
  */
 public class RepositoryVersionResourceProviderTest {
 
-  private ClusterVersionDAO clusterVersionDAO;
-
   private static Injector injector;
 
   private static String jsonStringRedhat6 = "[{\"OperatingSystems\":{\"os_type\":\"redhat6\"},\"repositories\":[]}]";
   private static String jsonStringRedhat7 = "[{\"OperatingSystems\":{\"os_type\":\"redhat7\"},\"repositories\":[]}]";
 
-  private List<ClusterVersionEntity> getNoClusterVersions() {
-    final List<ClusterVersionEntity> emptyList = new ArrayList<ClusterVersionEntity>();
-    return emptyList;
-  }
-
-  private List<ClusterVersionEntity> getInstallFailedClusterVersions() {
-    ClusterEntity cluster = new ClusterEntity();
-    cluster.setClusterName("c1");
-    cluster.setClusterId(1L);
-
-    final List<ClusterVersionEntity> clusterVersions = new ArrayList<ClusterVersionEntity>();
-    final RepositoryVersionEntity repositoryVersion = new RepositoryVersionEntity();
-    repositoryVersion.setId(1L);
-    final ClusterVersionEntity installFailedVersion = new ClusterVersionEntity();
-    installFailedVersion.setState(RepositoryVersionState.INSTALL_FAILED);
-    installFailedVersion.setRepositoryVersion(repositoryVersion);
-    installFailedVersion.setClusterEntity(cluster);
-    clusterVersions.add(installFailedVersion);
-    cluster.setClusterVersionEntities(clusterVersions);
-    return clusterVersions;
-  }
-
   @Before
   public void before() throws Exception {
     final Set<String> validVersions = Sets.newHashSet("1.1", "1.1-17", "1.1.1.1", "1.1.343432.2", "1.1.343432.2-234234324");
-    final Set<StackInfo> stacks = new HashSet<StackInfo>();
+    final Set<StackInfo> stacks = new HashSet<>();
 
     final AmbariMetaInfo ambariMetaInfo = Mockito.mock(AmbariMetaInfo.class);
-    clusterVersionDAO = Mockito.mock(ClusterVersionDAO.class);
 
     final InMemoryDefaultTestModule injectorModule = new InMemoryDefaultTestModule() {
       @Override
       protected void configure() {
         super.configure();
         bind(AmbariMetaInfo.class).toInstance(ambariMetaInfo);
-        bind(ClusterVersionDAO.class).toInstance(clusterVersionDAO);
       };
     };
     injector = Guice.createInjector(injectorModule);
@@ -131,7 +98,7 @@ public class RepositoryVersionResourceProviderTest {
     final StackInfo stackInfo = new StackInfo() {
       @Override
       public Map<String, UpgradePack> getUpgradePacks() {
-        final Map<String, UpgradePack> map = new HashMap<String, UpgradePack>();
+        final Map<String, UpgradePack> map = new HashMap<>();
         final UpgradePack pack1 = new UpgradePack() {
           @Override
           public String getName() {
@@ -194,7 +161,7 @@ public class RepositoryVersionResourceProviderTest {
 
     });
 
-    final HashSet<OperatingSystemInfo> osInfos = new HashSet<OperatingSystemInfo>();
+    final HashSet<OperatingSystemInfo> osInfos = new HashSet<>();
     osInfos.add(new OperatingSystemInfo("redhat6"));
     osInfos.add(new OperatingSystemInfo("redhat7"));
     Mockito.when(ambariMetaInfo.getOperatingSystems(Mockito.anyString(), Mockito.anyString())).thenAnswer(new Answer<Set<OperatingSystemInfo>>() {
@@ -207,22 +174,7 @@ public class RepositoryVersionResourceProviderTest {
         if (stack.equals("HDP") && validVersions.contains(version)) {
           return osInfos;
         } else {
-          return new HashSet<OperatingSystemInfo>();
-        }
-      }
-    });
-
-    Mockito.when(clusterVersionDAO.findByStackAndVersion(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenAnswer(
-        new Answer<List<ClusterVersionEntity>>() {
-      @Override
-      public List<ClusterVersionEntity> answer(InvocationOnMock invocation) throws Throwable {
-        final String stack = invocation.getArguments()[0].toString();
-        final String version = invocation.getArguments()[1].toString();
-
-        if (stack.equals("HDP-1.1") && version.equals("1.1.1.1")) {
-          return getNoClusterVersions();
-        } else {
-          return getInstallFailedClusterVersions();
+          return new HashSet<>();
         }
       }
     });
@@ -257,8 +209,8 @@ public class RepositoryVersionResourceProviderTest {
 
     final ResourceProvider provider = injector.getInstance(ResourceProviderFactory.class).getRepositoryVersionResourceProvider();
 
-    final Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String, Object>>();
-    final Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    final Set<Map<String, Object>> propertySet = new LinkedHashSet<>();
+    final Map<String, Object> properties = new LinkedHashMap<>();
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID, "name");
     properties.put(RepositoryVersionResourceProvider.SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID, new Gson().fromJson("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"1\",\"Repositories/unique\":\"true\"}]}]", Object.class));
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID, "HDP");
@@ -421,8 +373,8 @@ public class RepositoryVersionResourceProviderTest {
 
     final ResourceProvider provider = injector.getInstance(ResourceProviderFactory.class).getRepositoryVersionResourceProvider();
 
-    final Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String, Object>>();
-    final Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    final Set<Map<String, Object>> propertySet = new LinkedHashSet<>();
+    final Map<String, Object> properties = new LinkedHashMap<>();
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID, "name");
     properties.put(RepositoryVersionResourceProvider.SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID, new Gson().fromJson("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"1\",\"Repositories/unique\":\"true\"}]}]", Object.class));
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID, "HDP");
@@ -461,16 +413,8 @@ public class RepositoryVersionResourceProviderTest {
 
     final ResourceProvider provider = injector.getInstance(ResourceProviderFactory.class).getRepositoryVersionResourceProvider();
 
-    Mockito.when(clusterVersionDAO.findByStackAndVersion(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenAnswer(
-        new Answer<List<ClusterVersionEntity>>() {
-          @Override
-          public List<ClusterVersionEntity> answer(InvocationOnMock invocation) throws Throwable {
-            return getNoClusterVersions();
-          }
-        });
-
-    final Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String, Object>>();
-    final Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    final Set<Map<String, Object>> propertySet = new LinkedHashSet<>();
+    final Map<String, Object> properties = new LinkedHashMap<>();
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID, "name");
     properties.put(RepositoryVersionResourceProvider.SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID, new Gson().fromJson("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"http://example.com/repo1\",\"Repositories/unique\":\"true\"}]}]", Object.class));
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID, "HDP");
@@ -521,14 +465,6 @@ public class RepositoryVersionResourceProviderTest {
 
     properties.put(RepositoryVersionResourceProvider.SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID, new Gson().fromJson("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"2\",\"Repositories/repo_name\":\"2\",\"Repositories/base_url\":\"2\",\"Repositories/unique\":\"true\"}]}]", Object.class));
     provider.updateResources(updateRequest, new AndPredicate(predicateStackName, predicateStackVersion));
-    // Now, insert a cluster version whose state is INSTALL_FAILED, so the operation will not be permitted.
-    Mockito.when(clusterVersionDAO.findByStackAndVersion(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenAnswer(
-      new Answer<List<ClusterVersionEntity>>() {
-        @Override
-        public List<ClusterVersionEntity> answer(InvocationOnMock invocation) throws Throwable {
-          return getInstallFailedClusterVersions();
-        }
-      });
 
     try {
       provider.updateResources(updateRequest, new AndPredicate(predicateStackName, predicateStackVersion));
@@ -545,16 +481,8 @@ public class RepositoryVersionResourceProviderTest {
 
     final ResourceProvider provider = injector.getInstance(ResourceProviderFactory.class).getRepositoryVersionResourceProvider();
 
-    Mockito.when(clusterVersionDAO.findByStackAndVersion(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenAnswer(
-        new Answer<List<ClusterVersionEntity>>() {
-          @Override
-          public List<ClusterVersionEntity> answer(InvocationOnMock invocation) throws Throwable {
-            return getNoClusterVersions();
-          }
-        });
-
-    final Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String, Object>>();
-    final Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    final Set<Map<String, Object>> propertySet = new LinkedHashSet<>();
+    final Map<String, Object> properties = new LinkedHashMap<>();
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_DISPLAY_NAME_PROPERTY_ID, "name");
     properties.put(RepositoryVersionResourceProvider.SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID, new Gson().fromJson("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"http://example.com/repo1\",\"Repositories/unique\":\"true\"}]}]", Object.class));
     properties.put(RepositoryVersionResourceProvider.REPOSITORY_VERSION_STACK_NAME_PROPERTY_ID, "HDP");

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
index 716be62..a0c58ea 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ServiceResourceProviderTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -62,6 +62,8 @@ import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.utilities.PredicateBuilder;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.metadata.RoleCommandOrder;
+import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.security.TestAuthenticationFactory;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.serveraction.kerberos.KerberosAdminAuthenticationException;
@@ -112,7 +114,7 @@ public class ServiceResourceProviderTest {
     Clusters clusters = createNiceMock(Clusters.class);
     Cluster cluster = createNiceMock(Cluster.class);
     Service service = createNiceMock(Service.class);
-    StackId stackId = createNiceMock(StackId.class);
+    StackId stackId = new StackId("HDP-2.5");
     ServiceFactory serviceFactory = createNiceMock(ServiceFactory.class);
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
@@ -120,7 +122,8 @@ public class ServiceResourceProviderTest {
     expect(managementController.getClusters()).andReturn(clusters).anyTimes();
     expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
 
-    expect(cluster.addService("Service100")).andReturn(service);
+    expect(cluster.addService(eq("Service100"),
+        EasyMock.anyObject(RepositoryVersionEntity.class))).andReturn(service);
 
     expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes();
 
@@ -128,29 +131,29 @@ public class ServiceResourceProviderTest {
     expect(cluster.getDesiredStackVersion()).andReturn(stackId).anyTimes();
     expect(cluster.getClusterId()).andReturn(2L).anyTimes();
 
-    expect(stackId.getStackName()).andReturn("HDP").anyTimes();
-    expect(stackId.getStackVersion()).andReturn("2.5").anyTimes();
-
     expect(ambariMetaInfo.isValidService( (String) anyObject(), (String) anyObject(), (String) anyObject())).andReturn(true);
     expect(ambariMetaInfo.getService((String)anyObject(), (String)anyObject(), (String)anyObject())).andReturn(serviceInfo).anyTimes();
 
     // replay
-    replay(managementController, clusters, cluster, service, ambariMetaInfo, stackId, serviceFactory, serviceInfo);
+    replay(managementController, clusters, cluster, service,
+        ambariMetaInfo, serviceFactory, serviceInfo);
 
     SecurityContextHolder.getContext().setAuthentication(authentication);
 
-    ResourceProvider provider = getServiceProvider(managementController);
+    ResourceProvider provider = getServiceProvider(managementController, true);
 
     // add the property map to a set for the request.  add more maps for multiple creates
-    Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String, Object>>();
+    Set<Map<String, Object>> propertySet = new LinkedHashSet<>();
 
     // Service 1: create a map of properties for the request
-    Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    Map<String, Object> properties = new LinkedHashMap<>();
 
     // add properties to the request map
     properties.put(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID, "Cluster100");
     properties.put(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID, "Service100");
     properties.put(ServiceResourceProvider.SERVICE_SERVICE_STATE_PROPERTY_ID, "INIT");
+    properties.put(ServiceResourceProvider.SERVICE_DESIRED_STACK_PROPERTY_ID, "HDP-1.1");
+    properties.put(ServiceResourceProvider.SERVICE_DESIRED_REPO_VERSION_ID_PROPERTY_ID, "1");
 
     propertySet.add(properties);
 
@@ -160,7 +163,8 @@ public class ServiceResourceProviderTest {
     provider.createResources(request);
 
     // verify
-    verify(managementController, clusters, cluster, service, ambariMetaInfo, stackId, serviceFactory, serviceInfo);
+    verify(managementController, clusters, cluster, service,
+        ambariMetaInfo, serviceFactory, serviceInfo);
   }
 
   @Test
@@ -197,7 +201,7 @@ public class ServiceResourceProviderTest {
     ServiceFactory serviceFactory = createNiceMock(ServiceFactory.class);
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
 
-    Map<String, Service> allResponseMap = new HashMap<String, Service>();
+    Map<String, Service> allResponseMap = new HashMap<>();
     allResponseMap.put("Service100", service0);
     allResponseMap.put("Service101", service1);
     allResponseMap.put("Service102", service2);
@@ -254,7 +258,7 @@ public class ServiceResourceProviderTest {
 
     ResourceProvider provider = getServiceProvider(managementController);
 
-    Set<String> propertyIds = new HashSet<String>();
+    Set<String> propertyIds = new HashSet<>();
 
     propertyIds.add(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID);
     propertyIds.add(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID);
@@ -265,7 +269,7 @@ public class ServiceResourceProviderTest {
     Set<Resource> resources = provider.getResources(request, predicate);
 
     Assert.assertEquals(5, resources.size());
-    Set<String> names = new HashSet<String>();
+    Set<String> names = new HashSet<>();
     for (Resource resource : resources) {
       String clusterName = (String) resource.getPropertyValue(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID);
       Assert.assertEquals("Cluster100", clusterName);
@@ -292,7 +296,7 @@ public class ServiceResourceProviderTest {
     resources = provider.getResources(request, predicate);
 
     Assert.assertEquals(3, resources.size());
-    names = new HashSet<String>();
+    names = new HashSet<>();
     for (Resource resource : resources) {
       String clusterName = (String) resource.getPropertyValue(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID);
       Assert.assertEquals("Cluster100", clusterName);
@@ -319,7 +323,7 @@ public class ServiceResourceProviderTest {
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     KerberosHelper kerberosHeper = createStrictMock(KerberosHelper.class);
 
-    Map<String, Service> allResponseMap = new HashMap<String, Service>();
+    Map<String, Service> allResponseMap = new HashMap<>();
     allResponseMap.put("KERBEROS", service0);
 
     // set expectations
@@ -387,7 +391,7 @@ public class ServiceResourceProviderTest {
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     KerberosHelper kerberosHelper = createStrictMock(KerberosHelper.class);
 
-    Map<String, Service> allResponseMap = new HashMap<String, Service>();
+    Map<String, Service> allResponseMap = new HashMap<>();
     allResponseMap.put("KERBEROS", service0);
 
     // set expectations
@@ -454,7 +458,7 @@ public class ServiceResourceProviderTest {
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     KerberosHelper kerberosHeper = createStrictMock(KerberosHelper.class);
 
-    Map<String, Service> allResponseMap = new HashMap<String, Service>();
+    Map<String, Service> allResponseMap = new HashMap<>();
     allResponseMap.put("KERBEROS", service0);
 
     // set expectations
@@ -523,13 +527,13 @@ public class ServiceResourceProviderTest {
     AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
     KerberosHelper kerberosHeper = createStrictMock(KerberosHelper.class);
 
-    Map<String, Service> allResponseMap = new HashMap<String, Service>();
+    Map<String, Service> allResponseMap = new HashMap<>();
     allResponseMap.put("KERBEROS", service0);
 
     // set expectations
     expect(managementController.getClusters()).andReturn(clusters).anyTimes();
     expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
-    expect(managementController.getHostComponents((Set<ServiceComponentHostRequest>) anyObject())).
+    expect(managementController.getHostComponents(EasyMock.<Set<ServiceComponentHostRequest>>anyObject())).
         andReturn(Collections.<ServiceComponentHostResponse>emptySet()).anyTimes();
 
     expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes();
@@ -596,6 +600,7 @@ public class ServiceResourceProviderTest {
 
   private void testUpdateResources(Authentication authentication) throws Exception{
     MaintenanceStateHelper maintenanceStateHelper = createNiceMock(MaintenanceStateHelper.class);
+    RepositoryVersionDAO repositoryVersionDAO = createNiceMock(RepositoryVersionDAO.class);
     AmbariManagementController managementController = createMock(AmbariManagementController.class);
     Clusters clusters = createNiceMock(Clusters.class);
     Cluster cluster = createNiceMock(Cluster.class);
@@ -608,7 +613,7 @@ public class ServiceResourceProviderTest {
     StackId stackId = createNiceMock(StackId.class);
     ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
 
-    Map<String, String> mapRequestProps = new HashMap<String, String>();
+    Map<String, String> mapRequestProps = new HashMap<>();
     mapRequestProps.put("context", "Called from a test");
 
     // set expectations
@@ -626,7 +631,7 @@ public class ServiceResourceProviderTest {
     expect(stackId.getStackId()).andReturn("HDP-2.5").anyTimes();
     expect(stackId.getStackName()).andReturn("HDP").anyTimes();
     expect(stackId.getStackVersion()).andReturn("2.5").anyTimes();
-    expect(service0.getDesiredStackVersion()).andReturn(stackId).anyTimes();
+    expect(service0.getDesiredStackId()).andReturn(stackId).anyTimes();
     expect(service0.getName()).andReturn("Service102").anyTimes();
     expect(serviceInfo.isCredentialStoreSupported()).andReturn(true).anyTimes();
     expect(serviceInfo.isCredentialStoreEnabled()).andReturn(false).anyTimes();
@@ -656,14 +661,16 @@ public class ServiceResourceProviderTest {
 
     // replay
     replay(managementController, clusters, cluster, rco, maintenanceStateHelper,
-        service0, serviceFactory, ambariMetaInfo, requestStages, requestStatusResponse, stackId, serviceInfo);
+        repositoryVersionDAO, service0, serviceFactory, ambariMetaInfo, requestStages,
+        requestStatusResponse, stackId, serviceInfo);
 
     SecurityContextHolder.getContext().setAuthentication(authentication);
 
-    ServiceResourceProvider provider = getServiceProvider(managementController, maintenanceStateHelper);
+    ServiceResourceProvider provider = getServiceProvider(managementController,
+        maintenanceStateHelper, repositoryVersionDAO);
 
     // add the property map to a set for the request.
-    Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    Map<String, Object> properties = new LinkedHashMap<>();
 
     properties.put(ServiceResourceProvider.SERVICE_SERVICE_STATE_PROPERTY_ID, "STARTED");
 
@@ -697,6 +704,7 @@ public class ServiceResourceProviderTest {
 
   private void testReconfigureClientsFlag(Authentication authentication) throws Exception {
     MaintenanceStateHelper maintenanceStateHelper = createNiceMock(MaintenanceStateHelper.class);
+    RepositoryVersionDAO repositoryVersionDAO = createNiceMock(RepositoryVersionDAO.class);
     AmbariManagementController managementController1 = createMock(AmbariManagementController.class);
     AmbariManagementController managementController2 = createMock
         (AmbariManagementController.class);
@@ -716,7 +724,7 @@ public class ServiceResourceProviderTest {
     StackId stackId = createNiceMock(StackId.class);
     ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
 
-    Map<String, String> mapRequestProps = new HashMap<String, String>();
+    Map<String, String> mapRequestProps = new HashMap<>();
     mapRequestProps.put("context", "Called from a test");
 
     // set expectations
@@ -739,7 +747,7 @@ public class ServiceResourceProviderTest {
     expect(stackId.getStackId()).andReturn("HDP-2.5").anyTimes();
     expect(stackId.getStackName()).andReturn("HDP").anyTimes();
     expect(stackId.getStackVersion()).andReturn("2.5").anyTimes();
-    expect(service0.getDesiredStackVersion()).andReturn(stackId).anyTimes();
+    expect(service0.getDesiredStackId()).andReturn(stackId).anyTimes();
     expect(service0.getName()).andReturn("Service102").anyTimes();
     expect(serviceInfo.isCredentialStoreSupported()).andReturn(true).anyTimes();
     expect(serviceInfo.isCredentialStoreEnabled()).andReturn(false).anyTimes();
@@ -787,17 +795,20 @@ public class ServiceResourceProviderTest {
     andReturn(Collections.<Service>emptySet()).anyTimes();
 
     // replay
-    replay(managementController1, response1, managementController2, requestStages1, requestStages2, response2,
-        clusters, cluster, service0, serviceResponse0, ambariMetaInfo, rco, maintenanceStateHelper, stackId, serviceInfo);
+    replay(managementController1, response1, managementController2, requestStages1, requestStages2,
+        response2, clusters, cluster, service0, serviceResponse0, ambariMetaInfo, rco,
+        maintenanceStateHelper, repositoryVersionDAO, stackId, serviceInfo);
 
     SecurityContextHolder.getContext().setAuthentication(authentication);
 
-    ServiceResourceProvider provider1 = getServiceProvider(managementController1, maintenanceStateHelper);
+    ServiceResourceProvider provider1 = getServiceProvider(managementController1,
+        maintenanceStateHelper, repositoryVersionDAO);
 
-    ServiceResourceProvider provider2 = getServiceProvider(managementController2, maintenanceStateHelper);
+    ServiceResourceProvider provider2 = getServiceProvider(managementController2,
+        maintenanceStateHelper, repositoryVersionDAO);
 
     // add the property map to a set for the request.
-    Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    Map<String, Object> properties = new LinkedHashMap<>();
 
     properties.put(ServiceResourceProvider.SERVICE_SERVICE_STATE_PROPERTY_ID,
       "STARTED");
@@ -943,7 +954,7 @@ public class ServiceResourceProviderTest {
     Cluster cluster = createNiceMock(Cluster.class);
     Service service = createNiceMock(Service.class);
     ServiceComponent sc = createNiceMock(ServiceComponent.class);
-    Map<String, ServiceComponent> scMap = new HashMap<String, ServiceComponent>();
+    Map<String, ServiceComponent> scMap = new HashMap<>();
     scMap.put("Component100", sc);
     State componentState = State.STARTED;
     ServiceComponentHost sch = createNiceMock(ServiceComponentHost.class);
@@ -1022,7 +1033,7 @@ public class ServiceResourceProviderTest {
     TestComponent component2 = new TestComponent("Component101", createNiceMock(ServiceComponent.class), State.STARTED);
     TestComponent component3 = new TestComponent("Component102", createNiceMock(ServiceComponent.class), State.STARTED);
 
-    Map<String, ServiceComponent> scMap = new HashMap<String, ServiceComponent>();
+    Map<String, ServiceComponent> scMap = new HashMap<>();
     scMap.put(component1.Name, component1.Component);
     scMap.put(component2.Name, component2.Component);
     scMap.put(component3.Name, component3.Component);
@@ -1108,46 +1119,53 @@ public class ServiceResourceProviderTest {
 
   @Test
   public void testCheckPropertyIds() throws Exception {
-    Set<String> propertyIds = new HashSet<String>();
-    propertyIds.add("foo");
-    propertyIds.add("cat1/foo");
-    propertyIds.add("cat2/bar");
-    propertyIds.add("cat2/baz");
-    propertyIds.add("cat3/sub1/bam");
-    propertyIds.add("cat4/sub2/sub3/bat");
-    propertyIds.add("cat5/subcat5/map");
-
-    Map<Resource.Type, String> keyPropertyIds = new HashMap<Resource.Type, String>();
-
     AmbariManagementController managementController = createMock(AmbariManagementController.class);
 
     MaintenanceStateHelper maintenanceStateHelperMock = createNiceMock(MaintenanceStateHelper.class);
-    AbstractResourceProvider provider = new ServiceResourceProvider(propertyIds,
-        keyPropertyIds,
-        managementController, maintenanceStateHelperMock);
+    RepositoryVersionDAO repositoryVersionDAO = createNiceMock(RepositoryVersionDAO.class);
+    replay(maintenanceStateHelperMock, repositoryVersionDAO);
+
+    AbstractResourceProvider provider = new ServiceResourceProvider(managementController,
+        maintenanceStateHelperMock, repositoryVersionDAO);
+
+    Set<String> unsupported = provider.checkPropertyIds(
+        Collections.singleton(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID));
 
-    Set<String> unsupported = provider.checkPropertyIds(Collections.singleton("foo"));
     Assert.assertTrue(unsupported.isEmpty());
 
     // note that key is not in the set of known property ids.  We allow it if its parent is a known property.
     // this allows for Map type properties where we want to treat the entries as individual properties
-    Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat5/subcat5/map/key")).isEmpty());
+    String subKey = PropertyHelper.getPropertyId(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID, "key");
+    unsupported = provider.checkPropertyIds(Collections.singleton(subKey));
+    Assert.assertTrue(unsupported.isEmpty());
 
     unsupported = provider.checkPropertyIds(Collections.singleton("bar"));
     Assert.assertEquals(1, unsupported.size());
     Assert.assertTrue(unsupported.contains("bar"));
 
-    unsupported = provider.checkPropertyIds(Collections.singleton("cat1/foo"));
-    Assert.assertTrue(unsupported.isEmpty());
+    for (String propertyId : provider.getPKPropertyIds()) {
+      unsupported = provider.checkPropertyIds(Collections.singleton(propertyId));
+      Assert.assertTrue(unsupported.isEmpty());
+    }
+  }
 
-    unsupported = provider.checkPropertyIds(Collections.singleton("cat1"));
-    Assert.assertTrue(unsupported.isEmpty());
+  private static ServiceResourceProvider getServiceProvider(AmbariManagementController managementController, boolean mockFindByStack) throws  AmbariException {
+    MaintenanceStateHelper maintenanceStateHelperMock = createNiceMock(MaintenanceStateHelper.class);
+    RepositoryVersionDAO repositoryVersionDAO = createNiceMock(RepositoryVersionDAO.class);
+    expect(maintenanceStateHelperMock.isOperationAllowed(anyObject(Resource.Type.class), anyObject(Service.class))).andReturn(true).anyTimes();
+    expect(maintenanceStateHelperMock.isOperationAllowed(anyObject(Resource.Type.class), anyObject(ServiceComponentHost.class))).andReturn(true).anyTimes();
 
-    unsupported = provider.checkPropertyIds(Collections.singleton("config"));
-    Assert.assertTrue(unsupported.isEmpty());
+    if (mockFindByStack) {
+      RepositoryVersionEntity repositoryVersion = createNiceMock(RepositoryVersionEntity.class);
 
-    unsupported = provider.checkPropertyIds(Collections.singleton("config/unknown_property"));
-    Assert.assertTrue(unsupported.isEmpty());
+      expect(repositoryVersionDAO.findByPK(EasyMock.anyLong())).andReturn(repositoryVersion).atLeastOnce();
+
+      expect(repositoryVersion.getStackId()).andReturn(new StackId("HDP-2.2")).anyTimes();
+      replay(repositoryVersion);
+    }
+
+    replay(maintenanceStateHelperMock, repositoryVersionDAO);
+    return getServiceProvider(managementController, maintenanceStateHelperMock, repositoryVersionDAO);
   }
 
   /**
@@ -1155,27 +1173,24 @@ public class ServiceResourceProviderTest {
    * It's useful in most cases (when we don't care about Maintenance State)
    */
   public static ServiceResourceProvider getServiceProvider(AmbariManagementController managementController) throws  AmbariException {
-    MaintenanceStateHelper maintenanceStateHelperMock = createNiceMock(MaintenanceStateHelper.class);
-    expect(maintenanceStateHelperMock.isOperationAllowed(anyObject(Resource.Type.class), anyObject(Service.class))).andReturn(true).anyTimes();
-    expect(maintenanceStateHelperMock.isOperationAllowed(anyObject(Resource.Type.class), anyObject(ServiceComponentHost.class))).andReturn(true).anyTimes();
-    replay(maintenanceStateHelperMock);
-    return getServiceProvider(managementController, maintenanceStateHelperMock);
+    return getServiceProvider(managementController, false);
   }
 
   /**
    * This factory method allows to define custom MaintenanceStateHelper mock.
    */
-  public static ServiceResourceProvider getServiceProvider(AmbariManagementController managementController,
-                                                           MaintenanceStateHelper maintenanceStateHelper) {
+  public static ServiceResourceProvider getServiceProvider(
+      AmbariManagementController managementController,
+      MaintenanceStateHelper maintenanceStateHelper, RepositoryVersionDAO repositoryVersionDAO) {
     Resource.Type type = Resource.Type.Service;
-    return new ServiceResourceProvider(PropertyHelper.getPropertyIds(type),
-            PropertyHelper.getKeyPropertyIds(type),
-            managementController, maintenanceStateHelper);
+    return new ServiceResourceProvider(managementController, maintenanceStateHelper, repositoryVersionDAO);
   }
 
-  public static void createServices(AmbariManagementController controller, Set<ServiceRequest> requests)
+  public static void createServices(AmbariManagementController controller,
+      RepositoryVersionDAO repositoryVersionDAO, Set<ServiceRequest> requests)
       throws AmbariException, AuthorizationException {
-    ServiceResourceProvider provider = getServiceProvider(controller);
+    MaintenanceStateHelper maintenanceStateHelperMock = createNiceMock(MaintenanceStateHelper.class);
+    ServiceResourceProvider provider = getServiceProvider(controller, maintenanceStateHelperMock, repositoryVersionDAO);
     provider.createServices(requests);
   }
 
@@ -1205,7 +1220,7 @@ public class ServiceResourceProviderTest {
       throws AmbariException, AuthorizationException {
     ServiceResourceProvider provider;
     if (maintenanceStateHelper != null) {
-      provider = getServiceProvider(controller, maintenanceStateHelper);
+      provider = getServiceProvider(controller, maintenanceStateHelper, null);
     } else {
       provider = getServiceProvider(controller);
     }


[22/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java
deleted file mode 100644
index 5c2d2a6..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java
+++ /dev/null
@@ -1,1761 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.UUID;
-import java.util.regex.Matcher;
-
-import javax.persistence.EntityManager;
-import javax.persistence.Query;
-import javax.persistence.criteria.CriteriaBuilder;
-import javax.persistence.criteria.CriteriaDelete;
-import javax.persistence.criteria.Root;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
-import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
-import org.apache.ambari.server.orm.dao.ArtifactDAO;
-import org.apache.ambari.server.orm.dao.DaoUtils;
-import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.ArtifactEntity;
-import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
-import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.StackEntity;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.Service;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptorFactory;
-import org.apache.ambari.server.state.kerberos.KerberosIdentityDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosServiceDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosServiceDescriptorFactory;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.apache.ambari.server.utils.VersionUtils;
-import org.apache.commons.lang.StringUtils;
-import org.eclipse.persistence.internal.databaseaccess.FieldTypeDefinition;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.gson.Gson;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-
-
-/**
- * Upgrade catalog for version 2.1.0.
- */
-public class UpgradeCatalog210 extends AbstractUpgradeCatalog {
-  private static final String CLUSTERS_TABLE = "clusters";
-  private static final String CLUSTER_HOST_MAPPING_TABLE = "ClusterHostMapping";
-  private static final String HOSTS_TABLE = "hosts";
-  private static final String HOST_COMPONENT_DESIRED_STATE_TABLE = "hostcomponentdesiredstate";
-  private static final String HOST_COMPONENT_STATE_TABLE = "hostcomponentstate";
-  private static final String HOST_STATE_TABLE = "hoststate";
-  private static final String HOST_VERSION_TABLE = "host_version";
-  private static final String HOST_ROLE_COMMAND_TABLE = "host_role_command";
-  private static final String HOST_CONFIG_MAPPING_TABLE = "hostconfigmapping";
-  private static final String CONFIG_GROUP_HOST_MAPPING_TABLE = "configgrouphostmapping";
-  private static final String CONFIG_GROUP_TABLE = "configgroup";
-  private static final String KERBEROS_PRINCIPAL_HOST_TABLE = "kerberos_principal_host";
-  private static final String KERBEROS_PRINCIPAL_TABLE = "kerberos_principal";
-  private static final String REQUEST_OPERATION_LEVEL_TABLE = "requestoperationlevel";
-  private static final String SERVICE_COMPONENT_DESIRED_STATE_TABLE = "servicecomponentdesiredstate";
-  private static final String SERVICE_CONFIG_TABLE = "serviceconfig";
-  private static final String SERVICE_CONFIG_HOSTS_TABLE = "serviceconfighosts";
-  private static final String WIDGET_TABLE = "widget";
-  private static final String WIDGET_LAYOUT_TABLE = "widget_layout";
-  private static final String WIDGET_LAYOUT_USER_WIDGET_TABLE = "widget_layout_user_widget";
-  private static final String VIEW_TABLE = "viewmain";
-  private static final String VIEW_INSTANCE_TABLE = "viewinstance";
-  private static final String VIEW_PARAMETER_TABLE = "viewparameter";
-  private static final String STACK_TABLE = "stack";
-  private static final String REPO_VERSION_TABLE = "repo_version";
-  private static final String ALERT_HISTORY_TABLE = "alert_history";
-  private static final String HOST_ID_COL = "host_id";
-  private static final String HOST_NAME_COL = "host_name";
-  private static final String PUBLIC_HOST_NAME_COL = "public_host_name";
-  private static final String TOPOLOGY_REQUEST_TABLE = "topology_request";
-  private static final String TOPOLOGY_HOST_GROUP_TABLE = "topology_hostgroup";
-  private static final String TOPOLOGY_HOST_INFO_TABLE = "topology_host_info";
-  private static final String TOPOLOGY_LOGICAL_REQUEST_TABLE = "topology_logical_request";
-  private static final String TOPOLOGY_HOST_REQUEST_TABLE = "topology_host_request";
-  private static final String TOPOLOGY_HOST_TASK_TABLE = "topology_host_task";
-  private static final String TOPOLOGY_LOGICAL_TASK_TABLE = "topology_logical_task";
-  private static final String HDFS_SITE_CONFIG = "hdfs-site";
-  private static final String RANGER_SITE_CONFIG = "ranger-site";
-
-  // constants for stack table changes
-  private static final String STACK_ID_COLUMN_NAME = "stack_id";
-  private static final String DESIRED_STACK_ID_COLUMN_NAME = "desired_stack_id";
-  private static final String CURRENT_STACK_ID_COLUMN_NAME = "current_stack_id";
-  private static final String DESIRED_STACK_VERSION_COLUMN_NAME = "desired_stack_version";
-  private static final String CURRENT_STACK_VERSION_COLUMN_NAME = "current_stack_version";
-  private static final DBColumnInfo DESIRED_STACK_ID_COLUMN = new DBColumnInfo(DESIRED_STACK_ID_COLUMN_NAME, Long.class, null, null, true);
-  private static final DBColumnInfo CURRENT_STACK_ID_COLUMN = new DBColumnInfo(CURRENT_STACK_ID_COLUMN_NAME, Long.class, null, null, true);
-  private static final DBColumnInfo STACK_ID_COLUMN = new DBColumnInfo(STACK_ID_COLUMN_NAME, Long.class, null, null, true);
-
-  @Inject
-  DaoUtils daoUtils;
-
-  @Inject
-  private OsFamily osFamily;
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getSourceVersion() {
-    return "2.0.0";
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.1.0";
-  }
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger
-      (UpgradeCatalog210.class);
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog210(Injector injector) {
-    super(injector);
-    this.injector = injector;
-
-    daoUtils = injector.getInstance(DaoUtils.class);
-    osFamily = injector.getInstance(OsFamily.class);
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-    executeAlertDDLUpdates();
-    executeHostsDDLUpdates();
-    executeWidgetDDLUpdates();
-    executeStackDDLUpdates();
-    executeTopologyDDLUpdates();
-    executeViewDDLUpdates();
-  }
-
-  private void executeTopologyDDLUpdates() throws AmbariException, SQLException {
-    List<DBColumnInfo> columns = new ArrayList<DBColumnInfo>();
-
-    columns.add(new DBColumnInfo("id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("action", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("cluster_name", String.class, 100, null, false));
-    columns.add(new DBColumnInfo("bp_name", String.class, 100, null, false));
-    columns.add(new DBColumnInfo("cluster_properties", char[].class, null, null, true));
-    columns.add(new DBColumnInfo("cluster_attributes", char[].class, null, null, true));
-    columns.add(new DBColumnInfo("description", String.class, 1024, null, true));
-
-    dbAccessor.createTable(TOPOLOGY_REQUEST_TABLE, columns, "id");
-
-    columns.clear();
-    columns.add(new DBColumnInfo("id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("name", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("group_properties", char[].class, null, null, true));
-    columns.add(new DBColumnInfo("group_attributes", char[].class, null, null, true));
-    columns.add(new DBColumnInfo("request_id", Long.class, null, null, false));
-
-    dbAccessor.createTable(TOPOLOGY_HOST_GROUP_TABLE, columns, "id");
-    dbAccessor.addFKConstraint(TOPOLOGY_HOST_GROUP_TABLE, "FK_hostgroup_req_id", "request_id", TOPOLOGY_REQUEST_TABLE, "id", false, false);
-
-    columns.clear();
-    columns.add(new DBColumnInfo("id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("group_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("fqdn", String.class, 255, null, true));
-    columns.add(new DBColumnInfo("host_count", Integer.class, null, null, true));
-    columns.add(new DBColumnInfo("predicate", String.class, 2048, null, true));
-
-    dbAccessor.createTable(TOPOLOGY_HOST_INFO_TABLE, columns, "id");
-    dbAccessor.addFKConstraint(TOPOLOGY_HOST_INFO_TABLE, "FK_hostinfo_group_id", "group_id", TOPOLOGY_HOST_GROUP_TABLE, "id", false, false);
-
-    columns.clear();
-    columns.add(new DBColumnInfo("id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("request_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("description", String.class, 1024, null, true));
-
-    dbAccessor.createTable(TOPOLOGY_LOGICAL_REQUEST_TABLE, columns, "id");
-    dbAccessor.addFKConstraint(TOPOLOGY_LOGICAL_REQUEST_TABLE, "FK_logicalreq_req_id", "request_id", TOPOLOGY_REQUEST_TABLE, "id", false, false);
-
-    columns.clear();
-    columns.add(new DBColumnInfo("id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("logical_request_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("group_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("stage_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("host_name", String.class, 255, null, true));
-
-    dbAccessor.createTable(TOPOLOGY_HOST_REQUEST_TABLE, columns, "id");
-    dbAccessor.addFKConstraint(TOPOLOGY_HOST_REQUEST_TABLE, "FK_hostreq_logicalreq_id", "logical_request_id", TOPOLOGY_LOGICAL_REQUEST_TABLE, "id", false, false);
-    dbAccessor.addFKConstraint(TOPOLOGY_HOST_REQUEST_TABLE, "FK_hostreq_group_id", "group_id", TOPOLOGY_HOST_GROUP_TABLE, "id", false, false);
-
-    columns.clear();
-    columns.add(new DBColumnInfo("id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("host_request_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("type", String.class, 255, null, false));
-    dbAccessor.createTable(TOPOLOGY_HOST_TASK_TABLE, columns, "id");
-    dbAccessor.addFKConstraint(TOPOLOGY_HOST_TASK_TABLE, "FK_hosttask_req_id", "host_request_id", TOPOLOGY_HOST_REQUEST_TABLE, "id", false, false);
-
-    columns.clear();
-    columns.add(new DBColumnInfo("id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("host_task_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("physical_task_id", Long.class, null, null, true));
-    columns.add(new DBColumnInfo("component", String.class, 255, null, false));
-    dbAccessor.createTable(TOPOLOGY_LOGICAL_TASK_TABLE, columns, "id");
-    dbAccessor.addFKConstraint(TOPOLOGY_LOGICAL_TASK_TABLE, "FK_ltask_hosttask_id", "host_task_id", TOPOLOGY_HOST_TASK_TABLE, "id", false, false);
-    dbAccessor.addFKConstraint(TOPOLOGY_LOGICAL_TASK_TABLE, "FK_ltask_hrc_id", "physical_task_id", "host_role_command", "task_id", false, false);
-
-    // Sequence updates
-    addSequences(Arrays.asList(
-      "topology_host_info_id_seq",
-      "topology_host_request_id_seq",
-      "topology_host_task_id_seq",
-      "topology_logical_request_id_seq",
-      "topology_logical_task_id_seq",
-      "topology_request_id_seq",
-      "topology_host_group_id_seq"
-    ), 0L, false);
-  }
-
-
-  private void executeAlertDDLUpdates() throws AmbariException, SQLException {
-    //Fix latest_text column type to match for all DBMS
-    Configuration.DatabaseType databaseType = configuration.getDatabaseType();
-
-    // MySQL columns are already TEXT, but we need to be sure in that, since LONGTEXT will really slowdown database when querying the alerts too often
-    if (Configuration.DatabaseType.MYSQL == databaseType) {
-      dbAccessor.alterColumn("alert_current", new DBColumnInfo("latest_text", new FieldTypeDefinition("TEXT"), null));
-      dbAccessor.alterColumn("alert_history", new DBColumnInfo("alert_text", new FieldTypeDefinition("TEXT"), null));
-    } else {
-      dbAccessor.changeColumnType("alert_current", "latest_text", String.class, char[].class);
-      dbAccessor.changeColumnType("alert_history", "alert_text", String.class, char[].class);
-    }
-
-  }
-
-  private void executeViewDDLUpdates() throws AmbariException, SQLException {
-    // cluster association
-    dbAccessor.addColumn(VIEW_INSTANCE_TABLE, new DBColumnInfo("cluster_handle", String.class, 255, null, true));
-    // determine whether to alter the names of the dynamic entities / attributes to
-    // avoid db reserved word conflicts.  should be false for existing instances
-    // for backward compatibility.
-    dbAccessor.addColumn(VIEW_INSTANCE_TABLE, new DBColumnInfo("alter_names", Integer.class, 0, 0, false));
-    // cluster configuration
-    dbAccessor.addColumn(VIEW_PARAMETER_TABLE, new DBColumnInfo("cluster_config", String.class, 255, null, true));
-    // view build number
-    dbAccessor.addColumn(VIEW_TABLE, new DBColumnInfo("build", String.class, 128, null, true));
-  }
-
-  /**
-   * Execute all of the hosts DDL updates.
-   *
-   * @throws org.apache.ambari.server.AmbariException
-   * @throws java.sql.SQLException
-   */
-  private void executeHostsDDLUpdates() throws AmbariException, SQLException {
-    Configuration.DatabaseType databaseType = configuration.getDatabaseType();
-
-    String randomHostName = null;
-    if (dbAccessor.tableHasData(HOST_ROLE_COMMAND_TABLE)) {
-      randomHostName = getRandomHostName();
-      if (StringUtils.isBlank(randomHostName)) {
-        throw new AmbariException("UpgradeCatalog210 could not retrieve a random host_name from the hosts table while running executeHostsDDLUpdates.");
-      }
-    }
-
-    dbAccessor.addColumn(HOSTS_TABLE, new DBColumnInfo(HOST_ID_COL, Long.class, null, null, true));
-
-    // Sequence value for the hosts table primary key. First record will be 1, so ambari_sequence value must be 0.
-    Long hostId = 0L;
-    Statement statement = null;
-    ResultSet rs = null;
-    try {
-      statement = dbAccessor.getConnection().createStatement();
-      if (statement != null) {
-        rs = statement.executeQuery("SELECT host_name, host_id FROM hosts ORDER BY host_id ASC, host_name ASC");
-        if (rs != null) {
-          hostId = populateHostsId(rs);
-        }
-      }
-    } finally {
-      if (rs != null) {
-        rs.close();
-      }
-      if (statement != null) {
-        statement.close();
-      }
-    }
-    // Insert host id number into ambari_sequences
-    addSequence("host_id_seq", hostId, false);
-
-    // Make the hosts id non-null after all the values are populated
-    if (databaseType == Configuration.DatabaseType.DERBY) {
-      // This is a workaround for UpgradeTest.java unit test
-      dbAccessor.executeQuery("ALTER TABLE " + HOSTS_TABLE + " ALTER column " + HOST_ID_COL + " NOT NULL");
-    } else {
-      dbAccessor.alterColumn(HOSTS_TABLE, new DBColumnInfo(HOST_ID_COL, Long.class, null, null, false));
-    }
-
-
-    // Drop the 8 FK constraints in the host-related tables. They will be recreated later after the PK is changed.
-    // The only host-related table not being included is alert_history.
-    if (databaseType == Configuration.DatabaseType.DERBY) {
-      dbAccessor.dropFKConstraint(HOST_COMPONENT_STATE_TABLE, "hostcomponentstate_host_name");
-      dbAccessor.dropFKConstraint(HOST_COMPONENT_DESIRED_STATE_TABLE, "hstcmponentdesiredstatehstname");
-      dbAccessor.dropFKConstraint(HOST_ROLE_COMMAND_TABLE, "FK_host_role_command_host_name");
-      dbAccessor.dropFKConstraint(HOST_STATE_TABLE, "FK_hoststate_host_name");
-      dbAccessor.dropFKConstraint(HOST_VERSION_TABLE, "FK_host_version_host_name");
-      dbAccessor.dropFKConstraint(CONFIG_GROUP_HOST_MAPPING_TABLE, "FK_cghm_hname");
-      // FK_krb_pr_host_hostname used to have a CASCADE DELETE, which is not needed.
-      dbAccessor.dropFKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "FK_krb_pr_host_hostname");
-      // FK_krb_pr_host_principalname used to have a CASCADE DELETE, which is not needed, so it will be recreated without it.
-      dbAccessor.dropFKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "FK_krb_pr_host_principalname");
-
-      // This FK name is actually different on Derby.
-      dbAccessor.dropFKConstraint(HOST_CONFIG_MAPPING_TABLE, "FK_hostconfigmapping_host_name");
-    } else {
-      dbAccessor.dropFKConstraint(HOST_COMPONENT_STATE_TABLE, "hostcomponentstate_host_name");
-      dbAccessor.dropFKConstraint(HOST_COMPONENT_STATE_TABLE, "fk_hostcomponentstate_host_name");
-
-      dbAccessor.dropFKConstraint(HOST_COMPONENT_DESIRED_STATE_TABLE, "hstcmponentdesiredstatehstname");
-      dbAccessor.dropFKConstraint(HOST_COMPONENT_DESIRED_STATE_TABLE, "fk_hostcomponentdesiredstate_host_name");
-
-      dbAccessor.dropFKConstraint(HOST_ROLE_COMMAND_TABLE, "FK_host_role_command_host_name");
-      dbAccessor.dropFKConstraint(HOST_STATE_TABLE, "FK_hoststate_host_name");
-      dbAccessor.dropFKConstraint(HOST_VERSION_TABLE, "FK_host_version_host_name");
-
-      dbAccessor.dropFKConstraint(CONFIG_GROUP_HOST_MAPPING_TABLE, "FK_cghm_hname");
-      dbAccessor.dropFKConstraint(CONFIG_GROUP_HOST_MAPPING_TABLE, "fk_configgrouphostmapping_host_name");
-
-      // FK_krb_pr_host_hostname used to have a CASCADE DELETE, which is not needed.
-      dbAccessor.dropFKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "FK_krb_pr_host_hostname");
-      dbAccessor.dropFKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "fk_kerberos_principal_host_host_name");
-
-      // FK_krb_pr_host_principalname used to have a CASCADE DELETE, which is not needed, so it will be recreated without it.
-      dbAccessor.dropFKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "FK_krb_pr_host_principalname");
-
-      dbAccessor.dropFKConstraint(HOST_CONFIG_MAPPING_TABLE, "FK_hostconfmapping_host_name");
-    }
-
-    // In Ambari 2.0.0, there were discrepancies with the FK in the ClusterHostMapping table in the Postgres databases.
-    // They were either swapped, or pointing to the wrong table. Ignore failures for both of these.
-    try {
-      dbAccessor.dropFKConstraint(CLUSTER_HOST_MAPPING_TABLE, "ClusterHostMapping_host_name", true);
-      dbAccessor.dropFKConstraint(CLUSTER_HOST_MAPPING_TABLE, "fk_clusterhostmapping_host_name", true);
-    } catch (Exception e) {
-      LOG.warn("Performed best attempt at deleting FK ClusterHostMapping_host_name. " +
-          "It is possible it did not exist or the deletion failed. " +  e.getMessage());
-    }
-    try {
-      dbAccessor.dropFKConstraint(CLUSTER_HOST_MAPPING_TABLE, "ClusterHostMapping_cluster_id", true);
-    } catch (Exception e) {
-      LOG.warn("Performed best attempt at deleting FK ClusterHostMapping_cluster_id. " +
-          "It is possible it did not exist or the deletion failed. " +  e.getMessage());
-    }
-
-    // Re-add the FK to the cluster_id; will add the host_id at the end.
-    dbAccessor.addFKConstraint(CLUSTER_HOST_MAPPING_TABLE, "FK_clhostmapping_cluster_id",
-        "cluster_id", CLUSTERS_TABLE, "cluster_id", false);
-
-    // Drop the PK, and recreate it on the host_id instead
-    if (databaseType == Configuration.DatabaseType.DERBY) {
-      String constraintName = getDerbyTableConstraintName("p", HOSTS_TABLE);
-      if (null != constraintName) {
-        // Derby doesn't support CASCADE DELETE.
-        dbAccessor.executeQuery("ALTER TABLE " + HOSTS_TABLE + " DROP CONSTRAINT " + constraintName);
-      }
-    } else {
-      dbAccessor.dropPKConstraint(HOSTS_TABLE, "hosts_pkey", "host_name", true);
-    }
-
-    dbAccessor.addPKConstraint(HOSTS_TABLE, "PK_hosts_id", "host_id");
-    dbAccessor.addUniqueConstraint(HOSTS_TABLE, "UQ_hosts_host_name", "host_name");
-
-
-    // Add host_id to the host-related tables, and populate the host_id, one table at a time.
-    String[] tablesToAddHostID = new String[] {
-        CONFIG_GROUP_HOST_MAPPING_TABLE,
-        CLUSTER_HOST_MAPPING_TABLE,
-        HOST_CONFIG_MAPPING_TABLE,
-        HOST_COMPONENT_STATE_TABLE,
-        HOST_COMPONENT_DESIRED_STATE_TABLE,
-        HOST_ROLE_COMMAND_TABLE,
-        HOST_STATE_TABLE,
-        HOST_VERSION_TABLE,
-        KERBEROS_PRINCIPAL_HOST_TABLE,
-        REQUEST_OPERATION_LEVEL_TABLE,
-        SERVICE_CONFIG_HOSTS_TABLE
-    };
-
-    for (String tableName : tablesToAddHostID) {
-      dbAccessor.addColumn(tableName, new DBColumnInfo(HOST_ID_COL, Long.class, null, null, true));
-
-      // The column name is different for one table
-      String hostNameColumnName = tableName.equals(SERVICE_CONFIG_HOSTS_TABLE) ? "hostname" : "host_name";
-
-      if (dbAccessor.tableHasData(tableName) && dbAccessor.tableHasColumn(tableName, hostNameColumnName)) {
-        dbAccessor.executeQuery("UPDATE " + tableName + " t SET host_id = (SELECT host_id FROM hosts h WHERE h.host_name = t." + hostNameColumnName + ") WHERE t.host_id IS NULL AND t." + hostNameColumnName + " IS NOT NULL");
-
-        // For legacy reasons, the hostrolecommand table will contain "none" for some records where the host_name was not important.
-        // These records were populated during Finalize in Rolling Upgrade, so they must be updated to use a valid host_name.
-        if (tableName.equals(HOST_ROLE_COMMAND_TABLE) && StringUtils.isNotBlank(randomHostName)) {
-          dbAccessor.executeQuery("UPDATE " + tableName + " t SET host_id = (SELECT host_id FROM hosts h WHERE h.host_name = '" + randomHostName + "') WHERE t.host_id IS NULL AND t.host_name = 'none'");
-        }
-      }
-
-      // The one exception for setting NOT NULL is the requestoperationlevel table
-      if (!tableName.equals(REQUEST_OPERATION_LEVEL_TABLE)) {
-        dbAccessor.setColumnNullable(tableName, HOST_ID_COL, false);
-      }
-    }
-
-
-    // For any tables where the host_name was part of the PK, need to drop the PK, and recreate it with the host_id
-    String[] tablesWithHostNameInPK =  new String[] {
-        CONFIG_GROUP_HOST_MAPPING_TABLE,
-        CLUSTER_HOST_MAPPING_TABLE,
-        HOST_CONFIG_MAPPING_TABLE,
-        HOST_COMPONENT_STATE_TABLE,
-        HOST_COMPONENT_DESIRED_STATE_TABLE,
-        HOST_STATE_TABLE,
-        KERBEROS_PRINCIPAL_HOST_TABLE,
-        SERVICE_CONFIG_HOSTS_TABLE
-    };
-
-    // We can't drop PK, if a one of PK columns is a part of foreign key. We should drop FK and re-create him after dropping PK
-    dbAccessor.dropFKConstraint(CONFIG_GROUP_HOST_MAPPING_TABLE, "FK_cghm_cgid");
-    dbAccessor.dropFKConstraint(CLUSTER_HOST_MAPPING_TABLE, "FK_clhostmapping_cluster_id");
-
-    dbAccessor.dropFKConstraint(HOST_CONFIG_MAPPING_TABLE, "FK_hostconfmapping_cluster_id");
-    dbAccessor.dropFKConstraint(HOST_COMPONENT_STATE_TABLE, "hstcomponentstatecomponentname");
-    dbAccessor.dropFKConstraint(HOST_COMPONENT_DESIRED_STATE_TABLE, "hstcmpnntdesiredstatecmpnntnme");
-    dbAccessor.dropFKConstraint(SERVICE_CONFIG_HOSTS_TABLE, "FK_scvhosts_scv");
-
-    //These FK's hasn't been deleted previously due to MySQL case sensitivity
-    if (databaseType == Configuration.DatabaseType.MYSQL) {
-      dbAccessor.dropFKConstraint(CONFIG_GROUP_HOST_MAPPING_TABLE, "FK_configgrouphostmapping_config_group_id");
-      dbAccessor.dropFKConstraint(CLUSTER_HOST_MAPPING_TABLE, "FK_ClusterHostMapping_cluster_id");
-      dbAccessor.dropFKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "FK_kerberos_principal_host_principal_name");
-      dbAccessor.dropFKConstraint(SERVICE_CONFIG_HOSTS_TABLE, "FK_serviceconfighosts_service_config_id");
-    }
-
-    if (databaseType == Configuration.DatabaseType.DERBY) {
-      for (String tableName : tablesWithHostNameInPK) {
-        String constraintName = getDerbyTableConstraintName("p", tableName);
-        if (null != constraintName) {
-          dbAccessor.executeQuery("ALTER TABLE " + tableName + " DROP CONSTRAINT " + constraintName);
-        }
-      }
-    } else {
-      // drop constrain only if existed constraint contains required column
-      dbAccessor.dropPKConstraint(CONFIG_GROUP_HOST_MAPPING_TABLE, "configgrouphostmapping_pkey", HOST_NAME_COL, true);
-      dbAccessor.dropPKConstraint(CLUSTER_HOST_MAPPING_TABLE, "clusterhostmapping_pkey",HOST_NAME_COL, true);
-      dbAccessor.dropPKConstraint(HOST_CONFIG_MAPPING_TABLE, "hostconfigmapping_pkey", HOST_NAME_COL, true);
-      dbAccessor.dropPKConstraint(HOST_COMPONENT_STATE_TABLE, "hostcomponentstate_pkey", HOST_NAME_COL, true);
-      dbAccessor.dropPKConstraint(HOST_COMPONENT_DESIRED_STATE_TABLE, "hostcomponentdesiredstate_pkey", HOST_NAME_COL, true);
-      dbAccessor.dropPKConstraint(HOST_STATE_TABLE, "hoststate_pkey", HOST_NAME_COL, true);
-      dbAccessor.dropPKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "kerberos_principal_host_pkey", HOST_NAME_COL, true);
-      dbAccessor.dropPKConstraint(SERVICE_CONFIG_HOSTS_TABLE, "serviceconfighosts_pkey", "hostname", true);
-    }
-
-    // Finish by deleting the unnecessary host_name columns.
-    dbAccessor.dropColumn(CONFIG_GROUP_HOST_MAPPING_TABLE, HOST_NAME_COL);
-    dbAccessor.dropColumn(CLUSTER_HOST_MAPPING_TABLE, HOST_NAME_COL);
-    dbAccessor.dropColumn(HOST_CONFIG_MAPPING_TABLE, HOST_NAME_COL);
-    dbAccessor.dropColumn(HOST_COMPONENT_STATE_TABLE, HOST_NAME_COL);
-    dbAccessor.dropColumn(HOST_COMPONENT_DESIRED_STATE_TABLE, HOST_NAME_COL);
-    dbAccessor.dropColumn(HOST_ROLE_COMMAND_TABLE, HOST_NAME_COL);
-    dbAccessor.dropColumn(HOST_STATE_TABLE, HOST_NAME_COL);
-    dbAccessor.dropColumn(HOST_VERSION_TABLE, HOST_NAME_COL);
-    dbAccessor.dropColumn(KERBEROS_PRINCIPAL_HOST_TABLE, HOST_NAME_COL);
-    dbAccessor.dropColumn(REQUEST_OPERATION_LEVEL_TABLE, HOST_NAME_COL);
-
-    // Notice that the column name doesn't have an underscore here.
-    dbAccessor.dropColumn(SERVICE_CONFIG_HOSTS_TABLE, "hostname");
-
-    dbAccessor.addPKConstraint(CONFIG_GROUP_HOST_MAPPING_TABLE, "configgrouphostmapping_pkey", "config_group_id", "host_id");
-    dbAccessor.addPKConstraint(CLUSTER_HOST_MAPPING_TABLE, "clusterhostmapping_pkey", "cluster_id", "host_id");
-    dbAccessor.addPKConstraint(HOST_CONFIG_MAPPING_TABLE, "hostconfigmapping_pkey", "create_timestamp", "host_id", "cluster_id", "type_name");
-    dbAccessor.addPKConstraint(HOST_COMPONENT_STATE_TABLE, "hostcomponentstate_pkey", "cluster_id", "component_name", "host_id", "service_name");
-    dbAccessor.addPKConstraint(HOST_COMPONENT_DESIRED_STATE_TABLE, "hostcomponentdesiredstate_pkey", "cluster_id", "component_name", "host_id", "service_name");
-    dbAccessor.addPKConstraint(HOST_STATE_TABLE, "hoststate_pkey", "host_id");
-    dbAccessor.addPKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "kerberos_principal_host_pkey", "principal_name", "host_id");
-    dbAccessor.addPKConstraint(SERVICE_CONFIG_HOSTS_TABLE, "serviceconfighosts_pkey", "service_config_id", "host_id");
-
-    // re-create FK constraints
-    dbAccessor.addFKConstraint(CONFIG_GROUP_HOST_MAPPING_TABLE, "FK_cghm_host_id", "host_id", HOSTS_TABLE, "host_id", false);
-    dbAccessor.addFKConstraint(CLUSTER_HOST_MAPPING_TABLE, "FK_clusterhostmapping_host_id", "host_id", HOSTS_TABLE, "host_id", false);
-    dbAccessor.addFKConstraint(HOST_CONFIG_MAPPING_TABLE, "FK_hostconfmapping_host_id", "host_id", HOSTS_TABLE, "host_id", false);
-    dbAccessor.addFKConstraint(HOST_COMPONENT_STATE_TABLE, "FK_hostcomponentstate_host_id", "host_id", HOSTS_TABLE, "host_id", false);
-    dbAccessor.addFKConstraint(HOST_COMPONENT_DESIRED_STATE_TABLE, "FK_hcdesiredstate_host_id", "host_id", HOSTS_TABLE, "host_id", false);
-    dbAccessor.addFKConstraint(HOST_ROLE_COMMAND_TABLE, "FK_host_role_command_host_id", "host_id", HOSTS_TABLE, "host_id", false);
-    dbAccessor.addFKConstraint(HOST_STATE_TABLE, "FK_hoststate_host_id", "host_id", HOSTS_TABLE, "host_id", false);
-    dbAccessor.addFKConstraint(HOST_VERSION_TABLE, "FK_host_version_host_id", "host_id", HOSTS_TABLE, "host_id", false);
-    dbAccessor.addFKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "FK_krb_pr_host_id", "host_id", HOSTS_TABLE, "host_id", false);
-    dbAccessor.addFKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "FK_krb_pr_host_principalname", "principal_name", KERBEROS_PRINCIPAL_TABLE, "principal_name", false);
-    dbAccessor.addFKConstraint(SERVICE_CONFIG_HOSTS_TABLE, "FK_scvhosts_host_id", "host_id", HOSTS_TABLE, "host_id", false);
-    dbAccessor.addFKConstraint(CONFIG_GROUP_HOST_MAPPING_TABLE, "FK_cghm_cgid", "config_group_id", CONFIG_GROUP_TABLE, "group_id", false);
-    dbAccessor.addFKConstraint(CLUSTER_HOST_MAPPING_TABLE, "FK_clhostmapping_cluster_id", "cluster_id", CLUSTERS_TABLE, "cluster_id", false);
-    dbAccessor.addFKConstraint(HOST_CONFIG_MAPPING_TABLE, "FK_hostconfmapping_cluster_id", "cluster_id", CLUSTERS_TABLE, "cluster_id", false);
-    dbAccessor.addFKConstraint(HOST_COMPONENT_STATE_TABLE, "hstcomponentstatecomponentname",
-                                  new String[]{"component_name", "cluster_id", "service_name"}, SERVICE_COMPONENT_DESIRED_STATE_TABLE,
-                                  new String[]{"component_name", "cluster_id", "service_name"}, false);
-    dbAccessor.addFKConstraint(HOST_COMPONENT_DESIRED_STATE_TABLE, "hstcmpnntdesiredstatecmpnntnme",
-                                  new String[]{"component_name", "cluster_id", "service_name"}, SERVICE_COMPONENT_DESIRED_STATE_TABLE,
-                                  new String[]{"component_name", "cluster_id", "service_name"}, false);
-    dbAccessor.addFKConstraint(SERVICE_CONFIG_HOSTS_TABLE, "FK_scvhosts_scv", "service_config_id", SERVICE_CONFIG_TABLE, "service_config_id", false);
-
-    // Update host names to be case insensitive
-    String UPDATE_TEMPLATE = "UPDATE {0} SET {1} = lower({1})";
-    // First remove duplicate hosts
-    removeDuplicateHosts();
-    // Lowercase host name in hosts
-    String updateHostName = MessageFormat.format(UPDATE_TEMPLATE, HOSTS_TABLE, HOST_NAME_COL);
-    dbAccessor.executeQuery(updateHostName);
-    // Lowercase public host name in hosts
-    String updatePublicHostName = MessageFormat.format(UPDATE_TEMPLATE, HOSTS_TABLE, PUBLIC_HOST_NAME_COL);
-    dbAccessor.executeQuery(updatePublicHostName);
-    // Lowercase host name in alert_history
-    String updateAlertHostName = MessageFormat.format(UPDATE_TEMPLATE, ALERT_HISTORY_TABLE, HOST_NAME_COL);
-    dbAccessor.executeQuery(updateAlertHostName);
-  }
-
-  private void executeWidgetDDLUpdates() throws AmbariException, SQLException {
-    List<DBColumnInfo> columns = new ArrayList<DBColumnInfo>();
-
-    columns.add(new DBColumnInfo("id", Long.class,    null,  null, false));
-    columns.add(new DBColumnInfo("widget_name", String.class,  255,   null, false));
-    columns.add(new DBColumnInfo("widget_type", String.class,  255,   null, false));
-    columns.add(new DBColumnInfo("metrics", char[].class, null, null, true));
-    columns.add(new DBColumnInfo("time_created", Long.class,  null,   null, false));
-    columns.add(new DBColumnInfo("author", String.class, 255, null, true));
-    columns.add(new DBColumnInfo("description", String.class, 255, null, true));
-    columns.add(new DBColumnInfo("default_section_name", String.class, 255, null, true));
-    columns.add(new DBColumnInfo("scope", String.class,  255,   null, true));
-    columns.add(new DBColumnInfo("widget_values", char[].class, null, null, true));
-    columns.add(new DBColumnInfo("properties", char[].class, null, null, true));
-    columns.add(new DBColumnInfo("cluster_id", Long.class, null, null, false));
-    dbAccessor.createTable(WIDGET_TABLE, columns, "id");
-
-    columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBColumnInfo("id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("layout_name", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("section_name", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("cluster_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("scope", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("user_name", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("display_name", String.class, 255, null, true));
-
-    dbAccessor.createTable(WIDGET_LAYOUT_TABLE, columns, "id");
-
-    columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBColumnInfo("widget_layout_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("widget_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("widget_order", Short.class, null, null, true));
-    dbAccessor.createTable(WIDGET_LAYOUT_USER_WIDGET_TABLE, columns, "widget_layout_id", "widget_id");
-    dbAccessor.addFKConstraint(WIDGET_LAYOUT_USER_WIDGET_TABLE, "FK_widget_layout_id", "widget_layout_id", "widget_layout", "id", false, false);
-    dbAccessor.addFKConstraint(WIDGET_LAYOUT_USER_WIDGET_TABLE, "FK_widget_id", "widget_id", "widget", "id", false, false);
-
-    //Alter users to store active widget layouts
-    dbAccessor.addColumn("users", new DBColumnInfo("active_widget_layouts", String.class, 1024, null, true));
-
-    // Sequence updates
-      addSequences(Arrays.asList("widget_id_seq", "widget_layout_id_seq"), 0L, false);
-  }
-
-  /**
-   * Adds the stack table, FKs, and constraints.
-   */
-  private void executeStackDDLUpdates() throws AmbariException, SQLException {
-    // stack table creation
-    ArrayList<DBColumnInfo> columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBColumnInfo("stack_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("stack_name", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("stack_version", String.class, 255, null,
-        false));
-
-    dbAccessor.createTable(STACK_TABLE, columns, "stack_id");
-    dbAccessor.addUniqueConstraint(STACK_TABLE, "unq_stack", "stack_name", "stack_version");
-
-    addSequence("stack_id_seq", 0L, false);
-
-    // create the new stack ID columns NULLABLE for now since we need to insert
-    // data into them later on (we'll change them to NOT NULL after that)
-    dbAccessor.addColumn(CLUSTERS_TABLE, DESIRED_STACK_ID_COLUMN);
-    dbAccessor.addColumn("hostcomponentdesiredstate", DESIRED_STACK_ID_COLUMN);
-    dbAccessor.addColumn(SERVICE_COMPONENT_DESIRED_STATE_TABLE, DESIRED_STACK_ID_COLUMN);
-    dbAccessor.addColumn("servicedesiredstate", DESIRED_STACK_ID_COLUMN);
-
-    dbAccessor.addFKConstraint(CLUSTERS_TABLE, "fk_clusters_desired_stack_id", DESIRED_STACK_ID_COLUMN_NAME, STACK_TABLE, STACK_ID_COLUMN_NAME, true);
-    dbAccessor.addFKConstraint("hostcomponentdesiredstate", "fk_hcds_desired_stack_id", DESIRED_STACK_ID_COLUMN_NAME, STACK_TABLE, STACK_ID_COLUMN_NAME, true);
-    dbAccessor.addFKConstraint(SERVICE_COMPONENT_DESIRED_STATE_TABLE, "fk_scds_desired_stack_id", DESIRED_STACK_ID_COLUMN_NAME, STACK_TABLE, STACK_ID_COLUMN_NAME, true);
-    dbAccessor.addFKConstraint("servicedesiredstate", "fk_sds_desired_stack_id", DESIRED_STACK_ID_COLUMN_NAME, STACK_TABLE, STACK_ID_COLUMN_NAME, true);
-
-    dbAccessor.addColumn("clusterstate", CURRENT_STACK_ID_COLUMN);
-    dbAccessor.addColumn("hostcomponentstate", CURRENT_STACK_ID_COLUMN);
-
-    dbAccessor.addFKConstraint("clusterstate", "fk_cs_current_stack_id", CURRENT_STACK_ID_COLUMN_NAME, STACK_TABLE, STACK_ID_COLUMN_NAME, true);
-    dbAccessor.addFKConstraint("hostcomponentstate", "fk_hcs_current_stack_id", CURRENT_STACK_ID_COLUMN_NAME, STACK_TABLE, STACK_ID_COLUMN_NAME, true);
-
-    dbAccessor.addColumn("clusterconfig", STACK_ID_COLUMN);
-    dbAccessor.addColumn("serviceconfig", STACK_ID_COLUMN);
-    dbAccessor.addColumn("blueprint", STACK_ID_COLUMN);
-    dbAccessor.addColumn(REPO_VERSION_TABLE, STACK_ID_COLUMN);
-
-    dbAccessor.addFKConstraint("clusterconfig", "fk_clusterconfig_stack_id", STACK_ID_COLUMN_NAME, STACK_TABLE, STACK_ID_COLUMN_NAME, true);
-    dbAccessor.addFKConstraint("serviceconfig", "fk_serviceconfig_stack_id", STACK_ID_COLUMN_NAME, STACK_TABLE, STACK_ID_COLUMN_NAME, true);
-    dbAccessor.addFKConstraint("blueprint", "fk_blueprint_stack_id", STACK_ID_COLUMN_NAME, STACK_TABLE, STACK_ID_COLUMN_NAME, true);
-    dbAccessor.addFKConstraint(REPO_VERSION_TABLE, "fk_repoversion_stack_id", STACK_ID_COLUMN_NAME, STACK_TABLE, STACK_ID_COLUMN_NAME, true);
-
-    // drop the unique constraint for the old column and add the new one
-    dbAccessor.dropUniqueConstraint(REPO_VERSION_TABLE, "uq_repo_version_stack_version");
-    dbAccessor.addUniqueConstraint("repo_version", "uq_repo_version_stack_id", "stack_id", "version");
-  }
-
-  /**
-   * Adds the stack table and constraints.
-   */
-  protected void executeStackPreDMLUpdates() throws AmbariException, SQLException {
-    Gson gson = new Gson();
-
-    injector.getInstance(AmbariMetaInfo.class);
-
-    StackDAO stackDAO = injector.getInstance(StackDAO.class);
-    List<StackEntity> stacks = stackDAO.findAll();
-    Map<Long,String> entityToJsonMap = new HashMap<Long, String>();
-
-    // build a mapping of stack entity to old-school JSON
-    for( StackEntity stack : stacks ){
-      StackId stackId = new StackId(stack.getStackName(),
-          stack.getStackVersion());
-      String stackJson = gson.toJson(stackId);
-      entityToJsonMap.put(stack.getStackId(), stackJson);
-    }
-
-    // use a bulk update on all tables to populate the new FK columns
-    String UPDATE_TEMPLATE = "UPDATE {0} SET {1} = {2} WHERE {3} = ''{4}''";
-    String UPDATE_BLUEPRINT_TEMPLATE = "UPDATE blueprint SET stack_id = {0} WHERE stack_name = ''{1}'' AND stack_version = ''{2}''";
-
-    Set<Long> stackEntityIds = entityToJsonMap.keySet();
-    for (Long stackEntityId : stackEntityIds) {
-      StackEntity stackEntity = stackDAO.findById(stackEntityId);
-      String outdatedJson = entityToJsonMap.get(stackEntityId);
-      String outdatedRepoStack = MessageFormat.format("{0}-{1}",stackEntity.getStackName(),stackEntity.getStackVersion());
-
-      String clustersSQL = MessageFormat.format(UPDATE_TEMPLATE, "clusters",
-          DESIRED_STACK_ID_COLUMN_NAME, stackEntityId,
-          DESIRED_STACK_VERSION_COLUMN_NAME, outdatedJson);
-
-      String hostComponentDesiredStateSQL = MessageFormat.format(
-          UPDATE_TEMPLATE, "hostcomponentdesiredstate",
-          DESIRED_STACK_ID_COLUMN_NAME, stackEntityId,
-          DESIRED_STACK_VERSION_COLUMN_NAME, outdatedJson);
-
-      String serviceComponentDesiredStateSQL = MessageFormat.format(
-          UPDATE_TEMPLATE, SERVICE_COMPONENT_DESIRED_STATE_TABLE,
-          DESIRED_STACK_ID_COLUMN_NAME, stackEntityId,
-          DESIRED_STACK_VERSION_COLUMN_NAME, outdatedJson);
-
-      String serviceDesiredStateSQL = MessageFormat.format(UPDATE_TEMPLATE,
-          "servicedesiredstate",
-          DESIRED_STACK_ID_COLUMN_NAME, stackEntityId,
-          DESIRED_STACK_VERSION_COLUMN_NAME, outdatedJson);
-
-      String clusterStateSQL = MessageFormat.format(UPDATE_TEMPLATE,
-          "clusterstate", CURRENT_STACK_ID_COLUMN_NAME, stackEntityId,
-          CURRENT_STACK_VERSION_COLUMN_NAME, outdatedJson);
-
-      String hostComponentStateSQL = MessageFormat.format(UPDATE_TEMPLATE,
-          "hostcomponentstate", CURRENT_STACK_ID_COLUMN_NAME, stackEntityId,
-          CURRENT_STACK_VERSION_COLUMN_NAME, outdatedJson);
-
-      String blueprintSQL = MessageFormat.format(UPDATE_BLUEPRINT_TEMPLATE,
-          stackEntityId, stackEntity.getStackName(),
-          stackEntity.getStackVersion());
-
-      String repoVersionSQL = MessageFormat.format(UPDATE_TEMPLATE,
-          REPO_VERSION_TABLE, STACK_ID_COLUMN_NAME, stackEntityId, "stack",
-          outdatedRepoStack);
-
-        dbAccessor.executeQuery(clustersSQL, "clusters", DESIRED_STACK_VERSION_COLUMN_NAME);
-        dbAccessor.executeQuery(hostComponentDesiredStateSQL, "hostcomponentdesiredstate", DESIRED_STACK_VERSION_COLUMN_NAME);
-        dbAccessor.executeQuery(serviceComponentDesiredStateSQL, SERVICE_COMPONENT_DESIRED_STATE_TABLE, DESIRED_STACK_VERSION_COLUMN_NAME);
-        dbAccessor.executeQuery(serviceDesiredStateSQL, "servicedesiredstate", DESIRED_STACK_VERSION_COLUMN_NAME);
-        dbAccessor.executeQuery(clusterStateSQL, "clusterstate", CURRENT_STACK_VERSION_COLUMN_NAME);
-        dbAccessor.executeQuery(hostComponentStateSQL, "hostcomponentstate", CURRENT_STACK_VERSION_COLUMN_NAME);
-        dbAccessor.executeQuery(blueprintSQL, "blueprint", "stack_name");
-
-        dbAccessor.executeQuery(repoVersionSQL, REPO_VERSION_TABLE, "stack");
-    }
-
-    // for the tables with no prior stack, set these based on the cluster's
-    // stack for each cluster defined
-    String INSERT_STACK_ID_TEMPLATE = "UPDATE {0} SET {1} = {2} WHERE cluster_id = {3}";
-    // we should do the changes only if they are required
-    if (dbAccessor.tableHasColumn(CLUSTERS_TABLE,DESIRED_STACK_VERSION_COLUMN_NAME)) {
-
-      Statement statement = null;
-      ResultSet rs = null;
-      try {
-        statement = dbAccessor.getConnection().createStatement();
-        if (statement != null) {
-          rs = statement.executeQuery("SELECT * FROM " + CLUSTERS_TABLE);
-          if (rs != null) {
-            while (rs.next()) {
-              long clusterId = rs.getLong("cluster_id");
-              String stackJson = rs.getString(DESIRED_STACK_VERSION_COLUMN_NAME);
-              StackId stackId = gson.fromJson(stackJson, StackId.class);
-
-              StackEntity stackEntity = stackDAO.find(stackId.getStackName(),
-                stackId.getStackVersion());
-
-              String clusterConfigSQL = MessageFormat.format(
-                INSERT_STACK_ID_TEMPLATE, "clusterconfig", STACK_ID_COLUMN_NAME,
-                stackEntity.getStackId(), clusterId);
-
-              String serviceConfigSQL = MessageFormat.format(
-                INSERT_STACK_ID_TEMPLATE, "serviceconfig", STACK_ID_COLUMN_NAME,
-                stackEntity.getStackId(), clusterId);
-
-              dbAccessor.executeQuery(clusterConfigSQL);
-              dbAccessor.executeQuery(serviceConfigSQL);
-            }
-          }
-        }
-        String UPDATE_CURRENT_STACK_ID_IF_NULL_TEMPLATE =
-          "UPDATE hostcomponentstate " +
-          "SET current_stack_id={0} " +
-          "WHERE current_stack_id IS NULL " +
-          "AND cluster_id={1} ";
-        rs = statement.executeQuery("SELECT cluster_id, current_stack_id FROM clusterstate");
-        if (rs != null) {
-          while (rs.next()) {
-            // if hostcomponentstate.current_stack_id is null,
-            // set to cluster's current_stack_id
-            long clusterId = rs.getLong("cluster_id");
-            long currentStackId = rs.getLong("current_stack_id");
-            String hostComponentStateSQL = MessageFormat.format(
-              UPDATE_CURRENT_STACK_ID_IF_NULL_TEMPLATE, currentStackId, clusterId);
-            dbAccessor.executeUpdate(hostComponentStateSQL, false);
-          }
-        }
-      } finally {
-        if (rs != null) {
-          rs.close();
-        }
-        if (statement != null) {
-          statement.close();
-        }
-      }
-    }
-  }
-
-  /**
-   * Copy cluster & service widgets from stack to DB.
-   */
-  protected void initializeClusterAndServiceWidgets() throws AmbariException {
-    AmbariManagementController controller = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = controller.getClusters();
-    if (clusters == null) {
-      return;
-    }
-
-    Map<String, Cluster> clusterMap = clusters.getClusters();
-
-    if (clusterMap != null && !clusterMap.isEmpty()) {
-      for (Cluster cluster : clusterMap.values()) {
-        controller.initializeWidgetsAndLayouts(cluster, null);
-
-        Map<String, Service> serviceMap = cluster.getServices();
-        if (serviceMap != null && !serviceMap.isEmpty()) {
-          for (Service service : serviceMap.values()) {
-            controller.initializeWidgetsAndLayouts(cluster, service);
-          }
-        }
-      }
-    }
-  }
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-
-  /**
-   * Populate the id of the hosts table with an auto-increment int.
-   * @param resultSet Rows from the hosts table, sorted first by host_id
-   * @return Returns an integer with the id for the next host record to be inserted.
-   * @throws SQLException
-   */
-  Long populateHostsId(ResultSet resultSet) throws SQLException {
-    Long hostId = 0L;
-    if (resultSet != null) {
-      try {
-        while (resultSet.next()) {
-          hostId++;
-          final String hostName = resultSet.getString(1);
-
-          if (StringUtils.isNotBlank(hostName)) {
-            dbAccessor.executeQuery("UPDATE " + HOSTS_TABLE + " SET host_id = " + hostId +
-                " WHERE " + HOST_NAME_COL + " = '" + hostName + "'");
-          }
-        }
-      } catch (Exception e) {
-        LOG.error("Unable to populate the id of the hosts. " + e.getMessage());
-      }
-    }
-    return hostId;
-  }
-
-  private String getRandomHostName() throws SQLException {
-    String randomHostName = null;
-
-    Statement statement = null;
-    ResultSet rs = null;
-    try {
-      statement = dbAccessor.getConnection().createStatement();
-      if (statement != null) {
-        rs = statement.executeQuery("SELECT " + HOST_NAME_COL + " FROM " + HOSTS_TABLE + " ORDER BY " + HOST_NAME_COL + " ASC");
-        if (rs != null && rs.next()) {
-          randomHostName = rs.getString(1);
-        }
-      }
-    } catch (Exception e) {
-      LOG.error("Failed to retrieve random host name. Exception: " + e.getMessage());
-    } finally {
-      if (rs != null) {
-        rs.close();
-      }
-      if (statement != null) {
-        statement.close();
-      }
-    }
-    return randomHostName;
-  }
-
-  /**
-   * Remove duplicate hosts before making host name case-insensitive
-   * @throws SQLException
-   */
-  private void removeDuplicateHosts() throws SQLException {
-    // Select hosts not in the cluster
-    String hostsNotInClusterQuery = MessageFormat.format(
-        "SELECT * FROM {0} WHERE {1} NOT IN (SELECT {1} FROM {2})",
-        HOSTS_TABLE, HOST_ID_COL, CLUSTER_HOST_MAPPING_TABLE);
-    ResultSet hostsNotInCluster = null;
-    Statement statement = null;
-    Statement duplicatedHostsStatement = null;
-
-    try {
-      statement = dbAccessor.getConnection().createStatement();
-      duplicatedHostsStatement = dbAccessor.getConnection().createStatement();
-      hostsNotInCluster = statement.executeQuery(hostsNotInClusterQuery);
-      if(hostsNotInCluster != null) {
-        while (hostsNotInCluster.next()) {
-          long hostToDeleteId = hostsNotInCluster.getLong(HOST_ID_COL);
-          String hostToDeleteName = hostsNotInCluster.getString(HOST_NAME_COL);
-          String duplicateHostsQuery = "SELECT count(*) FROM hosts WHERE lower(host_name) = '" + hostToDeleteName + "' AND host_id != " + hostToDeleteId;
-          long count = 0;
-          ResultSet duplicateHosts = null;
-          try {
-            duplicateHosts = duplicatedHostsStatement.executeQuery(duplicateHostsQuery);
-            if (duplicateHosts != null && duplicateHosts.next()) {
-              count = duplicateHosts.getLong(1);
-            }
-          } finally {
-            if (null != duplicateHosts) {
-              duplicateHosts.close();
-            }
-          }
-          if (count > 0) {
-            // Delete hosts and host_state table entries for this duplicate host entry
-            dbAccessor.executeQuery(
-                MessageFormat.format("DELETE from {0} WHERE {1} = {2,number,#}", HOST_STATE_TABLE, HOST_ID_COL, hostToDeleteId));
-            dbAccessor.executeQuery(
-                MessageFormat.format("DELETE from {0} WHERE {1} = {2,number,#}", HOSTS_TABLE, HOST_ID_COL, hostToDeleteId));
-          }
-        }
-      }
-    } finally {
-      if (null != hostsNotInCluster) {
-        hostsNotInCluster.close();
-      }
-      if (statement != null) {
-        statement.close();
-      }
-      if (duplicatedHostsStatement != null) {
-        duplicatedHostsStatement.close();
-      }
-    }
-  }
-
-  /**
-   * Get the constraint name created by Derby if one was not specified for the table.
-   * @param type Constraint-type, either, "p" (Primary), "c" (Check), "f" (Foreign), "u" (Unique)
-   * @param tableName Table Name
-   * @return Return the constraint name, or null if not found.
-   * @throws SQLException
-   */
-  private String getDerbyTableConstraintName(String type, String tableName) throws SQLException {
-    boolean found = false;
-    String constraint = null;
-
-    Statement statement = null;
-    ResultSet rs = null;
-    try {
-      statement = dbAccessor.getConnection().createStatement();
-      if (statement != null) {
-        rs = statement.executeQuery("SELECT c.constraintname, c.type, t.tablename FROM sys.sysconstraints c, sys.systables t WHERE c.tableid = t.tableid");
-        if (rs != null) {
-          while(rs.next()) {
-            constraint = rs.getString(1);
-            String recordType = rs.getString(2);
-            String recordTableName = rs.getString(3);
-
-            if (recordType.equalsIgnoreCase(type) && recordTableName.equalsIgnoreCase(tableName)) {
-              found = true;
-              break;
-            }
-          }
-        }
-      }
-    } finally {
-      if (rs != null) {
-        rs.close();
-      }
-      if (statement != null) {
-        statement.close();
-      }
-    }
-    return found ? constraint : null;
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executePreDMLUpdates() throws AmbariException, SQLException {
-    executeStackPreDMLUpdates();
-    cleanupStackUpdates();
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    addNewConfigurationsFromXml();
-
-    // Initialize all default widgets and widget layouts
-    initializeClusterAndServiceWidgets();
-
-    addMissingConfigs();
-    updateAlertDefinitions();
-    removeStormRestApiServiceComponent();
-    updateKerberosDescriptorArtifacts();
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void updateKerberosDescriptorArtifact(ArtifactDAO artifactDAO, ArtifactEntity artifactEntity) throws AmbariException {
-    if (artifactEntity != null) {
-      Map<String, Object> data = artifactEntity.getArtifactData();
-
-      if (data != null) {
-        final KerberosDescriptor kerberosDescriptor = new KerberosDescriptorFactory().createInstance(data);
-
-        if (kerberosDescriptor != null) {
-          // Get the global "hdfs" identity (if it exists)
-          KerberosIdentityDescriptor hdfsIdentity = kerberosDescriptor.getIdentity("hdfs");
-
-          if (hdfsIdentity != null) {
-            // Move the "hdfs" global identity to under HDFS service by removing it from the
-            // collection of global identities and _merging_ it into the identities for the HDFS
-            // service - creating a sparse HDFS service structure if necessary.
-            KerberosServiceDescriptor hdfsService = kerberosDescriptor.getService("HDFS");
-
-            if (hdfsService == null) {
-              hdfsService = new KerberosServiceDescriptorFactory().createInstance("HDFS", (Map) null);
-              hdfsService.putIdentity(hdfsIdentity);
-              kerberosDescriptor.putService(hdfsService);
-            } else {
-              KerberosIdentityDescriptor hdfsReferenceIdentity = hdfsService.getIdentity("/hdfs");
-
-              if (hdfsReferenceIdentity != null) {
-                // Merge the changes from the reference identity into the global identity...
-                hdfsIdentity.update(hdfsReferenceIdentity);
-                // Make sure the identity's name didn't change.
-                hdfsIdentity.setName("hdfs");
-
-                hdfsService.removeIdentity("/hdfs");
-              }
-
-              hdfsService.putIdentity(hdfsIdentity);
-            }
-
-            kerberosDescriptor.removeIdentity("hdfs");
-          }
-
-          // Find all identities named "/hdfs" and update the name to "/HDFS/hdfs"
-          updateKerberosDescriptorIdentityReferences(kerberosDescriptor, "/hdfs", "/HDFS/hdfs");
-          updateKerberosDescriptorIdentityReferences(kerberosDescriptor.getServices(), "/hdfs", "/HDFS/hdfs");
-
-          artifactEntity.setArtifactData(kerberosDescriptor.toMap());
-          artifactDAO.merge(artifactEntity);
-        }
-      }
-    }
-  }
-
-  /**
-   * Delete STORM_REST_API component if HDP is upgraded past 2.2 and the
-   * Component still exists.
-   */
-  protected void removeStormRestApiServiceComponent() {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-      for (final Cluster cluster : clusterMap.values()) {
-        StackId stackId = cluster.getCurrentStackVersion();
-        if (stackId != null && stackId.getStackName().equals("HDP") &&
-          VersionUtils.compareVersions(stackId.getStackVersion(), "2.2") >= 0) {
-
-          executeInTransaction(new Runnable() {
-            @Override
-            public void run() {
-            ServiceComponentDesiredStateDAO dao = injector.getInstance(ServiceComponentDesiredStateDAO.class);
-              ServiceComponentDesiredStateEntity entity = dao.findByName(cluster.getClusterId(),
-                  "STORM", "STORM_REST_API");
-
-            if (entity != null) {
-              EntityManager em = getEntityManagerProvider().get();
-              CriteriaBuilder cb = em.getCriteriaBuilder();
-
-              try {
-                LOG.info("Deleting STORM_REST_API service component.");
-                CriteriaDelete<HostComponentStateEntity> hcsDelete = cb.createCriteriaDelete(HostComponentStateEntity.class);
-                CriteriaDelete<HostComponentDesiredStateEntity> hcdDelete = cb.createCriteriaDelete(HostComponentDesiredStateEntity.class);
-                CriteriaDelete<ServiceComponentDesiredStateEntity> scdDelete = cb.createCriteriaDelete(ServiceComponentDesiredStateEntity.class);
-
-                Root<HostComponentStateEntity> hcsRoot = hcsDelete.from(HostComponentStateEntity.class);
-                Root<HostComponentDesiredStateEntity> hcdRoot = hcdDelete.from(HostComponentDesiredStateEntity.class);
-                Root<ServiceComponentDesiredStateEntity> scdRoot = scdDelete.from(ServiceComponentDesiredStateEntity.class);
-
-                hcsDelete.where(cb.equal(hcsRoot.get("componentName"), "STORM_REST_API"));
-                hcdDelete.where(cb.equal(hcdRoot.get("componentName"), "STORM_REST_API"));
-                scdDelete.where(cb.equal(scdRoot.get("componentName"), "STORM_REST_API"));
-
-                em.createQuery(hcsDelete).executeUpdate();
-                em.createQuery(hcdDelete).executeUpdate();
-                em.createQuery(scdDelete).executeUpdate();
-              } catch (Exception e) {
-                LOG.warn("Error deleting STORM_REST_API service component. " +
-                  "This could result in issue with ambari server start. " +
-                  "Please make sure the STORM_REST_API component is deleted " +
-                  "from the database by running following commands:\n" +
-                  "delete from hostcomponentdesiredstate where component_name='STORM_REST_API';\n" +
-                  "delete from hostcomponentstate where component_name='STORM_REST_API';\n" +
-                  "delete from servicecomponentdesiredstate where component_name='STORM_REST_API';\n", e);
-              }
-            }
-            }
-          });
-        }
-      }
-    }
-  }
-
-  /**
-   * Modifies the JSON of some of the alert definitions which have changed
-   * between Ambari versions.
-   */
-  protected void updateAlertDefinitions() {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    List<String> metricAlerts = Arrays.asList("namenode_cpu", "namenode_hdfs_blocks_health",
-            "namenode_hdfs_capacity_utilization", "namenode_rpc_latency",
-            "namenode_directory_status", "datanode_health_summary", "datanode_storage");
-
-    List<String> mapredAlerts = Arrays.asList("mapreduce_history_server_cpu", "mapreduce_history_server_rpc_latency");
-    List<String> rmAlerts = Arrays.asList("yarn_resourcemanager_cpu", "yarn_resourcemanager_rpc_latency");
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          // HDFS metric alerts
-          for (String alertName : metricAlerts) {
-            AlertDefinitionEntity alertDefinitionEntity = alertDefinitionDAO.findByName(
-                cluster.getClusterId(), alertName);
-
-            if (alertDefinitionEntity != null) {
-              String source = alertDefinitionEntity.getSource();
-              JsonObject rootJson = new JsonParser().parse(source).getAsJsonObject();
-
-              rootJson.get("uri").getAsJsonObject().addProperty("kerberos_keytab",
-                    "{{hdfs-site/dfs.web.authentication.kerberos.keytab}}");
-
-              rootJson.get("uri").getAsJsonObject().addProperty("kerberos_principal",
-                    "{{hdfs-site/dfs.web.authentication.kerberos.principal}}");
-
-              updateAlertDefinitionEntitySource(alertName, rootJson.toString(), UUID.randomUUID().toString());
-            }
-          }
-
-          // MapR alerts update for kerberos
-          for (String alertName : mapredAlerts) {
-            AlertDefinitionEntity alertDefinitionEntity = alertDefinitionDAO.findByName(
-                cluster.getClusterId(), alertName);
-
-            if (alertDefinitionEntity != null) {
-              String source = alertDefinitionEntity.getSource();
-              JsonObject rootJson = new JsonParser().parse(source).getAsJsonObject();
-              rootJson.get("uri").getAsJsonObject().addProperty("kerberos_keytab",
-                    "{{mapred-site/mapreduce.jobhistory.webapp.spnego-keytab-file}}");
-
-              rootJson.get("uri").getAsJsonObject().addProperty("kerberos_principal",
-                    "{{mapred-site/mapreduce.jobhistory.webapp.spnego-principal}}");
-
-              updateAlertDefinitionEntitySource(alertName, rootJson.toString(), UUID.randomUUID().toString());
-            }
-          }
-
-          // YARN alerts
-          for (String alertName : rmAlerts) {
-            AlertDefinitionEntity alertDefinitionEntity = alertDefinitionDAO.findByName(
-                cluster.getClusterId(), alertName);
-
-            if (alertDefinitionEntity != null) {
-              String source = alertDefinitionEntity.getSource();
-              JsonObject rootJson = new JsonParser().parse(source).getAsJsonObject();
-
-              rootJson.get("uri").getAsJsonObject().addProperty("kerberos_keytab",
-                    "{{yarn-site/yarn.resourcemanager.webapp.spnego-keytab-file}}");
-
-              rootJson.get("uri").getAsJsonObject().addProperty("kerberos_principal",
-                    "{{yarn-site/yarn.resourcemanager.webapp.spnego-principal}}");
-
-              updateAlertDefinitionEntitySource(alertName, rootJson.toString(), UUID.randomUUID().toString());
-            }
-          }
-
-          // zookeeper failover conroller alert update for default port and uri
-          // to 8019 and dfs.ha.zkfc.port
-          AlertDefinitionEntity zkFailoverDefinitionEntity = alertDefinitionDAO.findByName(
-              cluster.getClusterId(), "hdfs_zookeeper_failover_controller_process");
-
-          if (zkFailoverDefinitionEntity != null) {
-            String source = zkFailoverDefinitionEntity.getSource();
-            JsonObject rootJson = new JsonParser().parse(source).getAsJsonObject();
-            rootJson.remove("uri");
-            rootJson.remove("default_port");
-            rootJson.addProperty("uri", "{{hdfs-site/dfs.ha.zkfc.port}}");
-            rootJson.addProperty("default_port", new Integer(8019));
-
-            // save the changes
-            updateAlertDefinitionEntitySource("hdfs_zookeeper_failover_controller_process",
-                rootJson.toString(), UUID.randomUUID().toString());
-          }
-
-          // update ranger admin alerts from type port(2.2) to web(2.3)
-          AlertDefinitionEntity rangerAdminDefinitionEntity = alertDefinitionDAO.findByName(
-            cluster.getClusterId(), "ranger_admin_process");
-
-          if (rangerAdminDefinitionEntity != null) {
-            String source = rangerAdminDefinitionEntity.getSource();
-            JsonObject rootJson = new JsonParser().parse(source).getAsJsonObject();
-            JsonObject uriJson = new JsonObject();
-            JsonObject reporting = rootJson.getAsJsonObject("reporting");
-            JsonObject ok = reporting.getAsJsonObject("ok");
-            JsonObject warning = reporting.getAsJsonObject("warning");
-            JsonObject critical = reporting.getAsJsonObject("critical");
-
-            rootJson.remove("type");
-            rootJson.remove("default_port");
-            rootJson.addProperty("type", "WEB");
-
-            uriJson.addProperty("http", "{{admin-properties/policymgr_external_url}}");
-            uriJson.addProperty("https", "{{admin-properties/policymgr_external_url}}");
-            uriJson.addProperty("https_property", "{{ranger-site/http.enabled}}");
-            uriJson.addProperty("https_property_value", "false");
-            uriJson.addProperty("connection_timeout", 5.0f);
-
-            rootJson.remove("uri");
-            rootJson.add("uri", uriJson);
-
-            ok.remove("text");
-            ok.addProperty("text", "HTTP {0} response in {2:.3f}s");
-
-            warning.remove("text");
-            warning.remove("value");
-            warning.addProperty("text", "HTTP {0} response from {1} in {2:.3f}s ({3})");
-
-            critical.remove("text");
-            critical.remove("value");
-            critical.addProperty("text", "Connection failed to {1} ({3})");
-
-            // save the changes
-            updateAlertDefinitionEntitySource("ranger_admin_process",
-              rootJson.toString(), UUID.randomUUID().toString());
-          }
-
-          // update oozie web ui alert
-          AlertDefinitionEntity oozieWebUIAlertDefinitionEntity = alertDefinitionDAO.findByName(
-              cluster.getClusterId(), "oozie_server_webui");
-
-          if (oozieWebUIAlertDefinitionEntity != null) {
-            String source = oozieWebUIAlertDefinitionEntity.getSource();
-            JsonObject rootJson = new JsonParser().parse(source).getAsJsonObject();
-            rootJson.get("uri").getAsJsonObject().remove("http");
-            rootJson.get("uri").getAsJsonObject().remove("kerberos_keytab");
-            rootJson.get("uri").getAsJsonObject().remove("kerberos_principal");
-            rootJson.get("uri").getAsJsonObject().addProperty("http",
-                    "{{oozie-site/oozie.base.url}}/?user.name={{oozie-env/oozie_user}}");
-            rootJson.get("uri").getAsJsonObject().addProperty("kerberos_keytab",
-                    "{{cluster-env/smokeuser_keytab}}");
-            rootJson.get("uri").getAsJsonObject().addProperty("kerberos_principal",
-                    "{{cluster-env/smokeuser_principal_name}}");
-
-            // save the changes
-            updateAlertDefinitionEntitySource("oozie_server_webui", rootJson.toString(),
-                UUID.randomUUID().toString());
-          }
-
-          // update HDFS metric alerts that had changes to their text
-          List<String> hdfsMetricAlertsFloatDivision = Arrays.asList(
-              "namenode_hdfs_capacity_utilization", "datanode_storage");
-
-          for (String metricAlertName : hdfsMetricAlertsFloatDivision) {
-            AlertDefinitionEntity entity = alertDefinitionDAO.findByName(cluster.getClusterId(),
-                metricAlertName);
-
-            if (null == entity) {
-              continue;
-            }
-
-            String source = entity.getSource();
-            JsonObject rootJson = new JsonParser().parse(source).getAsJsonObject();
-            JsonObject reporting = rootJson.getAsJsonObject("reporting");
-            JsonObject ok = reporting.getAsJsonObject("ok");
-            JsonObject warning = reporting.getAsJsonObject("warning");
-            JsonObject critical = reporting.getAsJsonObject("critical");
-
-            JsonElement okText = ok.remove("text");
-            ok.addProperty("text", okText.getAsString().replace("{2:d}", "{2:.0f}"));
-
-            JsonElement warningText = warning.remove("text");
-            warning.addProperty("text", warningText.getAsString().replace("{2:d}", "{2:.0f}"));
-
-            JsonElement criticalText = critical.remove("text");
-            critical.addProperty("text", criticalText.getAsString().replace("{2:d}", "{2:.0f}"));
-
-            // save the changes
-            updateAlertDefinitionEntitySource(metricAlertName, rootJson.toString(),
-                UUID.randomUUID().toString());
-          }
-        }
-      }
-    }
-  }
-
-  private void updateAlertDefinitionEntitySource(final String alertName, final String source, final String newHash) {
-    executeInTransaction(new Runnable() {
-      @Override
-      public void run() {
-        EntityManager em = getEntityManagerProvider().get();
-        Query nativeQuery = em.createNativeQuery("UPDATE alert_definition SET alert_source=?1, hash=?2 WHERE " +
-          "definition_name=?3");
-        nativeQuery.setParameter(1, source);
-        nativeQuery.setParameter(2, newHash);
-        nativeQuery.setParameter(3, alertName);
-        nativeQuery.executeUpdate();
-      }
-    });
-  }
-
-  protected void addMissingConfigs() throws AmbariException {
-    updateHiveConfigs();
-    updateHdfsConfigs();
-    updateStormConfigs();
-    updateRangerHiveConfigs();
-    updateRangerHBaseConfigs();
-    updateRangerSiteConfigs();
-    updateHBaseConfigs();
-  }
-
-  protected void updateRangerSiteConfigs() throws AmbariException{
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    Map<String, String> rangerPropertyMap = new HashMap<String, String>() {{
-      put("HTTPS_CLIENT_AUTH", "https.attrib.clientAuth");
-      put("HTTPS_KEYSTORE_FILE", "https.attrib.keystoreFile");
-      put("HTTPS_KEYSTORE_PASS", "https.attrib.keystorePass");
-      put("HTTPS_KEY_ALIAS", "https.attrib.keyAlias");
-      put("HTTPS_SERVICE_PORT", "https.service.port");
-      put("HTTP_ENABLED", "http.enabled");
-      put("HTTP_SERVICE_PORT", "http.service.port");
-    }};
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-
-        for (final Cluster cluster : clusterMap.values()) {
-         Config rangerSite = cluster.getDesiredConfigByType(RANGER_SITE_CONFIG);
-         Map<String, String> rangerSiteProperties;
-         Map<String, String> convertedRangerSiteProperties = new HashMap<>();
-         Set<String> oldPropertiesList = new HashSet<>();
-
-          if (rangerSite != null) {
-            rangerSiteProperties = rangerSite.getProperties();
-            for (Map.Entry<String, String> mapEntry: rangerPropertyMap.entrySet()) {
-              String oldKey = mapEntry.getKey();
-              String newKey = mapEntry.getValue();
-              if (rangerSiteProperties.containsKey(oldKey)) {
-                convertedRangerSiteProperties.put(newKey, rangerSiteProperties.get(oldKey));
-                oldPropertiesList.add(oldKey);
-              }
-            }
-          }
-
-          updateConfigurationPropertiesForCluster(cluster, RANGER_SITE_CONFIG, convertedRangerSiteProperties, true, false);
-          removeConfigurationPropertiesFromCluster(cluster, RANGER_SITE_CONFIG, oldPropertiesList);
-        }
-
-      }
-    }
-  }
-
-  protected void updateRangerHiveConfigs() throws AmbariException{
-    AmbariManagementController ambariManagementController = injector.getInstance(
-            AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          Config RangerHiveConfig = cluster.getDesiredConfigByType("ranger-hive-plugin-properties");
-          if (RangerHiveConfig != null
-                  && RangerHiveConfig.getProperties().containsKey("ranger-hive-plugin-enabled")
-                  && cluster.getDesiredConfigByType("hive-env") != null) {
-            Map<String, String> newHiveEnvProperties = new HashMap<String, String>();
-            Map<String, String> newHiveServerProperties = new HashMap<String, String>();
-            Set<String> removeRangerHiveProperties = new HashSet<String>();
-            removeRangerHiveProperties.add("ranger-hive-plugin-enabled");
-
-            if (RangerHiveConfig.getProperties().get("ranger-hive-plugin-enabled") != null
-                    && RangerHiveConfig.getProperties().get("ranger-hive-plugin-enabled").equalsIgnoreCase("yes")) {
-              newHiveEnvProperties.put("hive_security_authorization", "Ranger");
-              newHiveServerProperties.put("hive.security.authorization.enabled", "true");
-            }
-            boolean updateProperty = cluster.getDesiredConfigByType("hive-env").getProperties().containsKey("hive_security_authorization");
-            updateConfigurationPropertiesForCluster(cluster, "hive-env", newHiveEnvProperties, updateProperty, true);
-            updateConfigurationPropertiesForCluster(cluster, "hiveserver2-site", newHiveServerProperties, updateProperty, true);
-            removeConfigurationPropertiesFromCluster(cluster, "ranger-hive-plugin-properties", removeRangerHiveProperties);
-          }
-        }
-      }
-    }
-  }
-
-  protected void updateRangerHBaseConfigs() throws AmbariException{
-    AmbariManagementController ambariManagementController = injector.getInstance(
-                                                                                  AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          Config RangerHBaseConfig = cluster.getDesiredConfigByType("ranger-hbase-plugin-properties");
-          if (RangerHBaseConfig != null
-                && RangerHBaseConfig.getProperties().containsKey("ranger-hbase-plugin-enabled")
-                && cluster.getDesiredConfigByType("hbase-site") != null) {
-            Map<String, String> newHBaseSiteProperties = new HashMap<String, String>();
-
-            if (RangerHBaseConfig.getProperties().get("ranger-hbase-plugin-enabled") != null
-                  && RangerHBaseConfig.getProperties().get("ranger-hbase-plugin-enabled").equalsIgnoreCase("yes")) {
-
-              newHBaseSiteProperties.put("hbase.security.authorization", "true");
-            }
-            boolean updateProperty = cluster.getDesiredConfigByType("hbase-site").getProperties().containsKey("hbase.security.authorization");
-            updateConfigurationPropertiesForCluster(cluster, "hbase-site", newHBaseSiteProperties, updateProperty, true);
-          }
-        }
-      }
-    }
-  }
-
-  protected void updateHdfsConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(
-        AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-      Map<String, String> prop = new HashMap<String, String>();
-      String content = null;
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          /***
-           * Append -Dorg.mortbay.jetty.Request.maxFormContentSize=-1 to HADOOP_NAMENODE_OPTS from hadoop-env.sh
-           */
-          content = null;
-          if (cluster.getDesiredConfigByType("hadoop-env") != null) {
-            content = cluster.getDesiredConfigByType(
-                "hadoop-env").getProperties().get("content");
-          }
-
-          if (content != null) {
-            content += "\nexport HADOOP_NAMENODE_OPTS=\"${HADOOP_NAMENODE_OPTS} -Dorg.mortbay.jetty.Request.maxFormContentSize=-1\"";
-
-            prop.put("content", content);
-            updateConfigurationPropertiesForCluster(cluster, "hadoop-env",
-                prop, true, false);
-          }
-          /***
-           * Update dfs.namenode.rpc-address set hostname instead of localhost
-           */
-          if (cluster.getDesiredConfigByType(HDFS_SITE_CONFIG) != null && !cluster.getHosts("HDFS","NAMENODE").isEmpty()) {
-
-            URI nameNodeRpc = null;
-            String hostName = cluster.getHosts("HDFS","NAMENODE").iterator().next();
-            // Try to generate dfs.namenode.rpc-address
-            if (cluster.getDesiredConfigByType("core-site").getProperties() != null &&
-                      cluster.getDesiredConfigByType("core-site").getProperties().get("fs.defaultFS") != null) {
-              try {
-                if (isNNHAEnabled(cluster)) {
-                  // NN HA enabled
-                  // Remove dfs.namenode.rpc-address property
-                  Set<String> removePropertiesSet = new HashSet<>();
-                  removePropertiesSet.add("dfs.namenode.rpc-address");
-                  removeConfigurationPropertiesFromCluster(cluster, HDFS_SITE_CONFIG, removePropertiesSet);
-                } else {
-                  // NN HA disabled
-                  nameNodeRpc = new URI(cluster.getDesiredConfigByType("core-site").getProperties().get("fs.defaultFS"));
-                  Map<String, String> hdfsProp = new HashMap<String, String>();
-                  hdfsProp.put("dfs.namenode.rpc-address", hostName + ":" + nameNodeRpc.getPort());
-                  updateConfigurationPropertiesForCluster(cluster, HDFS_SITE_CONFIG,
-                          hdfsProp, false, false);
-                }
-              } catch (URISyntaxException e) {
-                e.printStackTrace();
-              }
-            }
-          }
-        }
-      }
-    }
-  }
-
-  protected void updateHiveConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(
-            AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          String content = null;
-          String hive_server2_auth = "";
-          if (cluster.getDesiredConfigByType("hive-site") != null &&
-              cluster.getDesiredConfigByType("hive-site").getProperties().containsKey("hive.server2.authentication")) {
-
-            hive_server2_auth = cluster.getDesiredConfigByType("hive-site").getProperties().get("hive.server2.authentication");
-          }
-
-          if(cluster.getDesiredConfigByType("hive-env") != null) {
-            Map<String, String> hiveEnvProps = new HashMap<String, String>();
-            Set<String> hiveServerSiteRemoveProps = new HashSet<String>();
-            // Update logic for setting HIVE_AUX_JARS_PATH in hive-env.sh
-            content = cluster.getDesiredConfigByType("hive-env").getProperties().get("content");
-            if(content != null) {
-              content = updateHiveEnvContent(content);
-              hiveEnvProps.put("content", content);
-            }
-            //hive metastore and client_heapsize are added for HDP2, we should check if it exists and not add it for HDP1
-            if (!cluster.getDesiredConfigByType("hive-env").getProperties().containsKey("hive.client.heapsize")) {
-              hiveEnvProps.put("hive.client.heapsize", "512");
-            }
-            if (!cluster.getDesiredConfigByType("hive-env").getProperties().containsKey("hive.metastore.heapsize")) {
-              hiveEnvProps.put("hive.metastore.heapsize", "1024");
-            }
-
-            boolean isHiveSecurityAuthPresent = cluster.getDesiredConfigByType("hive-env").getProperties().containsKey("hive_security_authorization");
-            String hiveSecurityAuth="";
-
-            if ("kerberos".equalsIgnoreCase(hive_server2_auth) && cluster.getServices().containsKey("KERBEROS")){
-              hiveSecurityAuth = "SQLStdAuth";
-              isHiveSecurityAuthPresent = true;
-              hiveEnvProps.put("hive_security_authorization", hiveSecurityAuth);
-            } else {
-              if (isHiveSecurityAuthPresent) {
-                hiveSecurityAuth = cluster.getDesiredConfigByType("hive-env").getProperties().get("hive_security_authorization");
-              }
-            }
-
-            if (isHiveSecurityAuthPresent && "none".equalsIgnoreCase(hiveSecurityAuth) &&
-                !isConfigEnabled(cluster,
-                    AbstractUpgradeCatalog.CONFIGURATION_TYPE_RANGER_HIVE_PLUGIN_PROPERTIES,
-                    AbstractUpgradeCatalog.PROPERTY_RANGER_HIVE_PLUGIN_ENABLED)) {
-              hiveServerSiteRemoveProps.add("hive.security.authorization.manager");
-              hiveServerSiteRemoveProps.add("hive.security.authenticator.manager");
-            }
-            updateConfigurationPropertiesForCluster(cluster, "hive-env", hiveEnvProps, true, true);
-            removeConfigurationPropertiesFromCluster(cluster, "hiveserver2-site", hiveServerSiteRemoveProps);
-          }
-
-          if(cluster.getDesiredConfigByType("hive-site") != null) {
-            Set<String> hiveSiteRemoveProps = new HashSet<String>();
-            Map<String, String> hiveSiteAddProps = new HashMap<String, String>();
-
-            if (!"pam".equalsIgnoreCase(hive_server2_auth)) {
-              hiveSiteRemoveProps.add("hive.server2.authentication.pam.services");
-            } else {
-              hiveSiteAddProps.put("hive.server2.authentication.pam.services", "");
-            }
-            if (!"custom".equalsIgnoreCase(hive_server2_auth)) {
-              hiveSiteRemoveProps.add("hive.server2.custom.authentication.class");
-            } else {
-              hiveSiteAddProps.put("hive.server2.custom.authentication.class", "");
-            }
-            if (!"ldap".equalsIgnoreCase(hive_server2_auth)) {
-              hiveSiteRemoveProps.add("hive.server2.authentication.ldap.url");
-            } else {
-              hiveSiteAddProps.put("hive.server2.authentication.ldap.url", "");
-            }
-            if (!"kerberos".equalsIgnoreCase(hive_server2_auth) && !cluster.getServices().containsKey("KERBEROS")) {
-              hiveSiteRemoveProps.add("hive.server2.authentication.kerberos.keytab");
-              hiveSiteRemoveProps.add("hive.server2.authentication.kerberos.principal");
-            } else {
-              hiveSiteAddProps.put("hive.server2.authentication.kerberos.keytab", "");
-              hiveSiteAddProps.put("hive.server2.authentication.kerberos.principal", "");
-            }
-
-
-            updateConfigurationPropertiesForCluster(cluster, "hive-site", hiveSiteAddProps, hiveSiteRemoveProps, false, true);
-          }
-        }
-      }
-    }
-  }
-
-  protected void updateHBaseConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(
-        AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          if (cluster.getDesiredConfigByType("hbase-site") != null && cluster.getDesiredConfigByType("hbase-env") != null) {
-            Map<String, String> hbaseEnvProps = new HashMap<String, String>();
-            Map<String, String> hbaseSiteProps = new HashMap<String, String>();
-            Set<String> hbaseEnvRemoveProps = new HashSet<String>();
-            Set<String> hbaseSiteRemoveProps = new HashSet<String>();
-
-            if (cluster.getDesiredConfigByType("hbase-site").getProperties().containsKey("hbase.region.server.rpc.scheduler.factory.class") &&
-                "org.apache.phoenix.hbase.index.ipc.PhoenixIndexRpcSchedulerFactory".equals(cluster.getDesiredConfigByType("hbase-site").getProperties().get(
-                        "hbase.region.server.rpc.scheduler.factory.class"))) {
-              hbaseEnvProps.put("phoenix_sql_enabled", "true");
-            }
-
-            if (cluster.getDesiredConfigByType("hbase-env").getProperties().containsKey("phoenix_sql_enabled") &&
-            "true".equalsIgnoreCase(cluster.getDesiredConfigByType("hbase-env").getProperties().get("phoenix_sql_enabled"))) {
-              hbaseSiteProps.put("hbase.regionserver.wal.codec", "org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec");
-              hbaseSiteProps.put("phoenix.functions.allowUserDefinedFunctions", "true");
-            }
-            else {
-              hbaseSiteProps.put("hbase.regionserver.wal.codec", "org.apache.hadoop.hbase.regionserver.wal.WALCellCodec");
-              hbaseSiteRemoveProps.add("hbase.rpc.controllerfactory.class");
-              hbaseSiteRemoveProps.add("phoenix.functions.allowUserDefinedFunctions");
-            }
-
-            if (cluster.getDesiredConfigByType("hbase-site").getProperties().containsKey("hbase.security.authorization")) {
-              if("true".equalsIgnoreCase(cluster.getDesiredConfigByType("hbase-site").getProperties().get("hbase.security.authorization"))) {
-                hbaseSiteProps.put("hbase.coprocessor.master.classes", "org.apache.hadoop.hbase.security.access.AccessController");
-                hbaseSiteProps.put("hbase.coprocessor.regionserver.classes", "org.apache.hadoop.hbase.security.access.AccessController");
-              }
-              else {
-                hbaseSiteProps.put("hbase.coprocessor.master.classes", "");
-                hbaseSiteRemoveProps.add("hbase.coprocessor.regionserver.classes");
-              }
-            }
-            else {
-              hbaseSiteRemoveProps.add("hbase.coprocessor.regionserver.classes");
-            }
-
-            updateConfigurationPropertiesForCluster(cluster, "hbase-site", hbaseSiteProps, true, false);
-            updateConfigurationPropertiesForCluster(cluster, "hbase-env", hbaseEnvProps, true, false);
-            updateConfigurationPropertiesForCluster(cluster, "hbase-site", new HashMap<String, String>(), hbaseSiteRemoveProps, false, true);
-            updateConfigurationPropertiesForCluster(cluster, "hbase-env", new HashMap<String, String>(), hbaseEnvRemoveProps, false, true);
-          }
-        }
-      }
-    }
-  }
-
-  protected String updateHiveEnvContent(String hiveEnvContent) {
-    if(hiveEnvContent == null) {
-      return null;
-    }
-
-    String oldAuxJarRegex = "if\\s*\\[\\s*\"\\$\\{HIVE_AUX_JARS_PATH\\}\"\\s*!=\\s*\"\"\\s*];\\s*then\\s*\\n" +
-        "\\s*export\\s+HIVE_AUX_JARS_PATH\\s*=\\s*\\$\\{HIVE_AUX_JARS_PATH\\}\\s*\\n" +
-        "\\s*elif\\s*\\[\\s*-d\\s*\"/usr/hdp/current/hive-webhcat/share/hcatalog\"\\s*\\];\\s*then\\s*\\n" +
-        "\\s*export\\s+HIVE_AUX_JARS_PATH\\s*=\\s*/usr/hdp/current/hive-webhcat/share/hcatalog\\s*\n" +
-        "\\s*fi";
-    String newAuxJarPath = "if [ \"${HIVE_AUX_JARS_PATH}\" != \"\" ]; then\n" +
-        "  if [ -f \"${HIVE_AUX_JARS_PATH}\" ]; then    \n" +
-        "    export HIVE_AUX_JARS_PATH=${HIVE_AUX_JARS_PATH}\n" +
-        "  elif [ -d \"/usr/hdp/current/hive-webhcat/share/hcatalog\" ]; then\n" +
-        "    export HIVE_AUX_JARS_PATH=/usr/hdp/current/hive-webhcat/share/hcatalog/hive-hcatalog-core.jar\n" +
-        "  fi\n" +
-        "elif [ -d \"/usr/hdp/current/hive-webhcat/share/hcatalog\" ]; then\n" +
-        "  export HIVE_AUX_JARS_PATH=/usr/hdp/current/hive-webhcat/share/hcatalog/hive-hcatalog-core.jar\n" +
-        "fi";
-    return hiveEnvContent.replaceAll(oldAuxJarRegex, Matcher.quoteReplacement(newAuxJarPath));
-  }
-
-  protected  void updateStormConfigs() throws  AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(
-            AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          //if cluster is secured we should set additional properties
-          if(cluster.getDesiredConfigByType("cluster-env") != null
-                  && cluster.getDesiredConfigByType("cluster-env").getProperties().get("security_enabled").equals("true")
-                  && cluster.getDesiredConfigByType("storm-site") != null ) {
-            Map<String, String> newStormProps = new HashMap<String, String>();
-            if (!cluster.getDesiredConfigByType("storm-site").getProperties().containsKey("java.security.auth.login.config")) {
-              newStormProps.put("java.security.auth.login.config", "{{conf_dir}}/storm_jaas.conf");
-            }
-            if (!cluster.getDesiredCo

<TRUNCATED>

[21/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog211.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog211.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog211.java
deleted file mode 100644
index eb835ef..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog211.java
+++ /dev/null
@@ -1,295 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.text.MessageFormat;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.atomic.AtomicLong;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.configuration.Configuration.DatabaseType;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
-import org.apache.ambari.server.orm.dao.DaoUtils;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.jdbc.support.JdbcUtils;
-
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-
-
-/**
- * Upgrade catalog for version 2.1.1.
- */
-public class UpgradeCatalog211 extends AbstractUpgradeCatalog {
-  private static final String HOST_COMPONENT_STATE_TABLE = "hostcomponentstate";
-  private static final String HOST_COMPONENT_STATE_ID_COLUMN = "id";
-  private static final String HOST_COMPONENT_STATE_INDEX = "idx_host_component_state";
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog211.class);
-
-  // this "id holder" is a field only for a test that verifies "big" 4 digit+
-  // numbers are formatted correctly
-  private AtomicLong m_hcsId = new AtomicLong(1);
-
-
-  @Inject
-  DaoUtils daoUtils;
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   *
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog211(Injector injector) {
-    super(injector);
-
-    daoUtils = injector.getInstance(DaoUtils.class);
-  }
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.1.1";
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getSourceVersion() {
-    return "2.1.0";
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-    // change out the PK on hostcomponentstate
-    executeHostComponentStateDDLUpdates();
-
-    // make viewinstanceproperty.value & viewinstancedata.value nullable
-    dbAccessor.setColumnNullable("viewinstanceproperty", "value", true);
-    dbAccessor.setColumnNullable("viewinstancedata", "value", true);
-
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executePreDMLUpdates() throws AmbariException, SQLException {
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    addNewConfigurationsFromXml();
-    updateExistingConfigurations();
-  }
-
-  // ----- UpgradeCatalog211 --------------------------------------------
-
-  /**
-   * Iterates over the set of clusters to call service-specific configuration
-   * update routines.
-   *
-   * @throws AmbariException
-   *           if an error occurs while updating the configurations
-   */
-  protected void updateExistingConfigurations() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if ((clusterMap != null) && !clusterMap.isEmpty()) {
-        // Iterate through the clusters and perform any configuration updates
-        for (final Cluster cluster : clusterMap.values()) {
-          updateKerberosConfigurations(cluster);
-
-          /* *********************************************************
-           * Add additional configuration update methods here
-           * ********************************************************* */
-        }
-      }
-    }
-  }
-
-  /**
-   * Updates the Kerberos configurations for the given cluster
-   * <p/>
-   * Performs the following updates:
-   * <ul>
-   * <li>Rename <code>create_attributes_template</code> to
-   * <code>ad_create_attributes_template</code></li>
-   * </ul>
-   *
-   * @param cluster
-   *          the cluster
-   * @throws AmbariException
-   *           if an error occurs while updating the configurations
-   */
-  protected void updateKerberosConfigurations(Cluster cluster) throws AmbariException {
-    Config config = cluster.getDesiredConfigByType("kerberos-env");
-
-    if (config != null) {
-      // Rename create_attributes_template to ad_create_attributes_template
-      String value = config.getProperties().get("create_attributes_template");
-      Map<String, String> updates = Collections.singletonMap("ad_create_attributes_template", value);
-      Set<String> removes = Collections.singleton("create_attributes_template");
-
-      updateConfigurationPropertiesForCluster(cluster, "kerberos-env", updates, removes, true, false);
-    }
-  }
-
-  /**
-   * Perform the DDL updates required to add a new Primary Key ID column to the
-   * {@code hostcomponentstate} table. This will perform the following actions:
-   * <ul>
-   * <li>Add a new column to hostcomponentstate named id</li>
-   * <li>Populated id with an incrementing long, then make it non-NULL</li>
-   * <li>Drop the existing PK on hostcomponentstate</li>
-   * <li>Add a new surrogate PK on hostcomponentstate on the id column</li>
-   * <li>Add an index on hostcomponentstate for host_id, component_name,
-   * service_name, cluster_id</li>
-   * </ul>
-   *
-   * @throws AmbariException
-   * @throws SQLException
-   */
-  private void executeHostComponentStateDDLUpdates() throws AmbariException, SQLException {
-    if (!dbAccessor.tableHasPrimaryKey(HOST_COMPONENT_STATE_TABLE, HOST_COMPONENT_STATE_ID_COLUMN)) {
-      // add the new column, nullable for now until we insert unique IDs
-      dbAccessor.addColumn(HOST_COMPONENT_STATE_TABLE,
-          new DBColumnInfo(HOST_COMPONENT_STATE_ID_COLUMN, Long.class, null, null, true));
-
-      Statement statement = null;
-      ResultSet resultSet = null;
-      try {
-        statement = dbAccessor.getConnection().createStatement();
-        if (statement != null) {
-          String selectSQL = MessageFormat.format(
-              "SELECT id, cluster_id, service_name, component_name, host_id FROM {0} ORDER BY {1} {2}",
-              HOST_COMPONENT_STATE_TABLE, "id", "DESC");
-
-          resultSet = statement.executeQuery(selectSQL);
-          while (resultSet.next()) {
-            final Long clusterId = resultSet.getLong("cluster_id");
-            final String serviceName = resultSet.getString("service_name");
-            final String componentName = resultSet.getString("component_name");
-            final Long hostId = resultSet.getLong("host_id");
-            final Long idKey = resultSet.getLong("id");
-
-            if (idKey != 0 && m_hcsId.get() == 1) {
-              m_hcsId.set(idKey);
-              m_hcsId.getAndIncrement();
-            } else if(idKey == 0) {
-              String updateSQL = MessageFormat.format(
-                  "UPDATE {0} SET {1} = {2,number,#} WHERE cluster_id = {3} AND service_name = ''{4}'' AND component_name = ''{5}'' and host_id = {6,number,#}",
-                  HOST_COMPONENT_STATE_TABLE, HOST_COMPONENT_STATE_ID_COLUMN, m_hcsId.getAndIncrement(),
-                  clusterId, serviceName, componentName, hostId);
-
-              dbAccessor.executeQuery(updateSQL);
-            }
-          }
-        }
-      } finally {
-        JdbcUtils.closeResultSet(resultSet);
-        JdbcUtils.closeStatement(statement);
-      }
-
-      // make the column NON NULL now
-      dbAccessor.alterColumn(HOST_COMPONENT_STATE_TABLE,
-          new DBColumnInfo(HOST_COMPONENT_STATE_ID_COLUMN, Long.class, null, null, false));
-
-      // Add sequence for hostcomponentstate id
-      addSequence("hostcomponentstate_id_seq", m_hcsId.get(), false);
-
-      // drop the current PK
-      String primaryKeyConstraintName = null;
-      Configuration.DatabaseType databaseType = configuration.getDatabaseType();
-      switch (databaseType) {
-        case POSTGRES: {
-          primaryKeyConstraintName = "hostcomponentstate_pkey";
-          break;
-        }
-        case ORACLE:
-        case SQL_SERVER: {
-          // Oracle and SQL Server require us to lookup the PK name
-          primaryKeyConstraintName = dbAccessor.getPrimaryKeyConstraintName(
-              HOST_COMPONENT_STATE_TABLE);
-
-          break;
-        }
-        default:
-          break;
-      }
-
-      if (databaseType == DatabaseType.MYSQL) {
-        String mysqlDropQuery = MessageFormat.format("ALTER TABLE {0} DROP PRIMARY KEY",
-            HOST_COMPONENT_STATE_TABLE);
-
-        dbAccessor.executeQuery(mysqlDropQuery, true);
-      } else {
-        // warn if we can't find it
-        if (null == primaryKeyConstraintName) {
-          LOG.warn("Unable to determine the primary key constraint name for {}",
-              HOST_COMPONENT_STATE_TABLE);
-        } else {
-          dbAccessor.dropPKConstraint(HOST_COMPONENT_STATE_TABLE, primaryKeyConstraintName, true);
-        }
-      }
-
-      // create a new PK, matching the name of the constraint found in the SQL
-      // files
-      dbAccessor.addPKConstraint(HOST_COMPONENT_STATE_TABLE, "pk_hostcomponentstate", "id");
-
-      // create index, ensuring column order matches that of the SQL files
-      dbAccessor.createIndex(HOST_COMPONENT_STATE_INDEX, HOST_COMPONENT_STATE_TABLE, "host_id",
-          "component_name", "service_name", "cluster_id");
-    }
-  }
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog212.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog212.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog212.java
deleted file mode 100644
index 854aa68..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog212.java
+++ /dev/null
@@ -1,404 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
-import org.apache.ambari.server.orm.dao.DaoUtils;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.utils.VersionUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import java.util.regex.Matcher;
-
-
-/**
- * Upgrade catalog for version 2.1.2.
- */
-public class UpgradeCatalog212 extends AbstractUpgradeCatalog {
-  private static final String HIVE_SITE = "hive-site";
-  private static final String HIVE_ENV = "hive-env";
-  private static final String HBASE_ENV = "hbase-env";
-  private static final String HBASE_SITE = "hbase-site";
-  private static final String CLUSTER_ENV = "cluster-env";
-  private static final String OOZIE_ENV = "oozie-env";
-
-  private static final String TOPOLOGY_REQUEST_TABLE = "topology_request";
-  private static final String CLUSTERS_TABLE = "clusters";
-  private static final String CLUSTERS_TABLE_CLUSTER_ID_COLUMN = "cluster_id";
-  private static final String TOPOLOGY_REQUEST_CLUSTER_NAME_COLUMN = "cluster_name";
-  private static final String TOPOLOGY_REQUEST_CLUSTER_ID_COLUMN = "cluster_id";
-  private static final String TOPOLOGY_REQUEST_CLUSTER_ID_FK_CONSTRAINT_NAME = "FK_topology_request_cluster_id";
-
-  private static final String HOST_ROLE_COMMAND_TABLE = "host_role_command";
-  private static final String HOST_ROLE_COMMAND_SKIP_COLUMN = "auto_skip_on_failure";
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog212.class);
-
-  @Inject
-  DaoUtils daoUtils;
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   *
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog212(Injector injector) {
-    super(injector);
-
-    daoUtils = injector.getInstance(DaoUtils.class);
-  }
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.1.2";
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getSourceVersion() {
-    return "2.1.1";
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-    executeTopologyDDLUpdates();
-    executeHostRoleCommandDDLUpdates();
-  }
-
-  private void executeTopologyDDLUpdates() throws AmbariException, SQLException {
-    dbAccessor.addColumn(TOPOLOGY_REQUEST_TABLE, new DBColumnInfo(TOPOLOGY_REQUEST_CLUSTER_ID_COLUMN,
-      Long.class, null, null, true));
-    // TOPOLOGY_REQUEST_CLUSTER_NAME_COLUMN will be deleted in PreDML. We need a cluster name to set cluster id.
-    // dbAccessor.dropColumn(TOPOLOGY_REQUEST_TABLE, TOPOLOGY_REQUEST_CLUSTER_NAME_COLUMN);
-    // dbAccessor.setColumnNullable(TOPOLOGY_REQUEST_TABLE, TOPOLOGY_REQUEST_CLUSTER_ID_COLUMN, false);
-    // dbAccessor.addFKConstraint(TOPOLOGY_REQUEST_TABLE, TOPOLOGY_REQUEST_CLUSTER_ID_FK_CONSTRAINT_NAME,
-    //     TOPOLOGY_REQUEST_CLUSTER_ID_COLUMN, CLUSTERS_TABLE, CLUSTERS_TABLE_CLUSTER_ID_COLUMN, false);
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executePreDMLUpdates() throws AmbariException, SQLException {
-    if (dbAccessor.tableHasColumn(TOPOLOGY_REQUEST_TABLE, TOPOLOGY_REQUEST_CLUSTER_NAME_COLUMN)) {
-      addClusterIdToTopology();
-      finilizeTopologyDDL();
-    } else {
-      LOG.debug("The column: [ {} ] has already been dropped from table: [ {} ]. Skipping preDMLUpdate logic.",
-          TOPOLOGY_REQUEST_CLUSTER_NAME_COLUMN, TOPOLOGY_REQUEST_TABLE);
-    }
-  }
-
-  protected void finilizeTopologyDDL() throws AmbariException, SQLException {
-    dbAccessor.dropColumn(TOPOLOGY_REQUEST_TABLE, TOPOLOGY_REQUEST_CLUSTER_NAME_COLUMN);
-    dbAccessor.setColumnNullable(TOPOLOGY_REQUEST_TABLE, TOPOLOGY_REQUEST_CLUSTER_ID_COLUMN, false);
-    dbAccessor.addFKConstraint(TOPOLOGY_REQUEST_TABLE, TOPOLOGY_REQUEST_CLUSTER_ID_FK_CONSTRAINT_NAME,
-      TOPOLOGY_REQUEST_CLUSTER_ID_COLUMN, CLUSTERS_TABLE, CLUSTERS_TABLE_CLUSTER_ID_COLUMN, false);
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    addNewConfigurationsFromXml();
-    addMissingConfigs();
-  }
-
-  protected void addClusterIdToTopology() throws AmbariException, SQLException {
-    Map<String, Long> clusterNameIdMap = new HashMap<String, Long>();
-    try (Statement statement = dbAccessor.getConnection().createStatement();
-         ResultSet rs = statement.executeQuery("SELECT DISTINCT cluster_name, cluster_id FROM clusters");
-    ) {
-      while (rs.next()) {
-        long clusterId = rs.getLong("cluster_id");
-        String clusterName = rs.getString("cluster_name");
-        clusterNameIdMap.put(clusterName, clusterId);
-      }
-    }
-
-    for (String clusterName : clusterNameIdMap.keySet()) {
-      try (PreparedStatement preparedStatement = dbAccessor.getConnection().prepareStatement("UPDATE topology_request " +
-          "SET cluster_id=? WHERE cluster_name=?");
-      ) {
-        preparedStatement.setLong(1, clusterNameIdMap.get(clusterName));
-        preparedStatement.setString(2, clusterName);
-        preparedStatement.executeUpdate();
-      }
-    }
-
-    // Set cluster id for all null values.
-    // Useful if cluster was renamed and cluster name does not match.
-    if (clusterNameIdMap.entrySet().size() >= 1) {
-      try (PreparedStatement preparedStatement = dbAccessor.getConnection().prepareStatement("UPDATE topology_request " +
-          "SET cluster_id=? WHERE cluster_id IS NULL");
-      ) {
-        preparedStatement.setLong(1, clusterNameIdMap.entrySet().iterator().next().getValue());
-        preparedStatement.executeUpdate();
-      }
-    }
-    if (clusterNameIdMap.entrySet().size() == 0) {
-      LOG.warn("Cluster not found. topology_request.cluster_id is not set");
-    }
-    if (clusterNameIdMap.entrySet().size() > 1) {
-      LOG.warn("Found more than one cluster. topology_request.cluster_id can be incorrect if you have renamed the cluster.");
-    }
-  }
-
-  protected void addMissingConfigs() throws AmbariException {
-    updateHiveConfigs();
-    updateOozieConfigs();
-    updateHbaseAndClusterConfigurations();
-    updateKafkaConfigurations();
-    updateStormConfigs();
-    removeDataDirMountConfig();
-  }
-
-  protected void updateStormConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if ((clusterMap != null) && !clusterMap.isEmpty()) {
-        // Iterate through the clusters and perform any configuration updates
-        for (final Cluster cluster : clusterMap.values()) {
-          Set<String> removes = new HashSet<String>();
-          removes.add("topology.metrics.consumer.register");
-          updateConfigurationPropertiesForCluster(cluster, "storm-site",
-            new HashMap<String, String>(), removes, false, false);
-        }
-      }
-    }
-  }
-
-  protected void updateKafkaConfigurations() throws AmbariException {
-    Map<String, String> properties = new HashMap<>();
-    properties.put("external.kafka.metrics.exclude.prefix",
-      "kafka.network.RequestMetrics,kafka.server.DelayedOperationPurgatory," +
-        "kafka.server.BrokerTopicMetrics.BytesRejectedPerSec");
-    properties.put("external.kafka.metrics.include.prefix",
-      "kafka.network.RequestMetrics.ResponseQueueTimeMs.request.OffsetCommit.98percentile," +
-        "kafka.network.RequestMetrics.ResponseQueueTimeMs.request.Offsets.95percentile," +
-        "kafka.network.RequestMetrics.ResponseSendTimeMs.request.Fetch.95percentile," +
-        "kafka.network.RequestMetrics.RequestsPerSec.request");
-
-    updateConfigurationProperties("kafka-broker", properties, false, false);
-  }
-
-  protected void updateHbaseAndClusterConfigurations() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if ((clusterMap != null) && !clusterMap.isEmpty()) {
-        // Iterate through the clusters and perform any configuration updates
-        for (final Cluster cluster : clusterMap.values()) {
-          Config hbaseEnvProps = cluster.getDesiredConfigByType(HBASE_ENV);
-          Config hbaseSiteProps = cluster.getDesiredConfigByType(HBASE_SITE);
-
-          if (hbaseEnvProps != null) {
-            // Remove override_hbase_uid from hbase-env and add override_uid to cluster-env
-            String value = hbaseEnvProps.getProperties().get("override_hbase_uid");
-            if (value != null) {
-              Map<String, String> updates = new HashMap<String, String>();
-              Set<String> removes = new HashSet<String>();
-              updates.put("override_uid", value);
-              removes.add("override_hbase_uid");
-              updateConfigurationPropertiesForCluster(cluster, HBASE_ENV, new HashMap<String, String>(), removes, false, true);
-              updateConfigurationPropertiesForCluster(cluster, CLUSTER_ENV, updates, true, false);
-            }
-          }
-
-          if (hbaseSiteProps != null) {
-            String value = hbaseSiteProps.getProperties().get("hbase.bucketcache.size");
-            if (value != null) {
-              if (value.endsWith("m")) {
-                value = value.substring(0, value.length() - 1);
-                Map<String, String> updates = new HashMap<String, String>();
-                updates.put("hbase.bucketcache.size", value);
-                updateConfigurationPropertiesForCluster(cluster, HBASE_SITE, updates, true, false);
-              }
-            }
-
-          }
-        }
-      }
-    }
-  }
-
-  protected void updateHiveConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(
-            AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          String content = null;
-          Boolean isHiveSitePresent = cluster.getDesiredConfigByType(HIVE_SITE) != null;
-          StackId stackId = cluster.getCurrentStackVersion();
-          Boolean isStackNotLess22 = (stackId != null && stackId.getStackName().equals("HDP") &&
-                  VersionUtils.compareVersions(stackId.getStackVersion(), "2.2") >= 0);
-
-          if (cluster.getDesiredConfigByType(HIVE_ENV) != null && isStackNotLess22) {
-            Map<String, String> hiveEnvProps = new HashMap<String, String>();
-            content = cluster.getDesiredConfigByType(HIVE_ENV).getProperties().get("content");
-            if(content != null) {
-              content = updateHiveEnvContent(content);
-              hiveEnvProps.put("content", content);
-            }
-            updateConfigurationPropertiesForCluster(cluster, HIVE_ENV, hiveEnvProps, true, true);
-          }
-
-          if (isHiveSitePresent && isStackNotLess22) {
-            Set<String> hiveSiteRemoveProps = new HashSet<String>();
-            hiveSiteRemoveProps.add("hive.heapsize");
-            hiveSiteRemoveProps.add("hive.optimize.mapjoin.mapreduce");
-            hiveSiteRemoveProps.add("hive.server2.enable.impersonation");
-            hiveSiteRemoveProps.add("hive.auto.convert.sortmerge.join.noconditionaltask");
-
-            updateConfigurationPropertiesForCluster(cluster, HIVE_SITE, new HashMap<String, String>(), hiveSiteRemoveProps, false, true);
-          }
-        }
-      }
-    }
-  }
-
-  protected void updateOozieConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          Config oozieEnv = cluster.getDesiredConfigByType(OOZIE_ENV);
-          if (oozieEnv != null) {
-            Map<String, String> oozieEnvProperties = oozieEnv.getProperties();
-
-            String hostname = oozieEnvProperties.get("oozie_hostname");
-            String db_type = oozieEnvProperties.get("oozie_database");
-            String final_db_host = null;
-            // fix for empty hostname after 1.7 -> 2.1.x+ upgrade
-            if (hostname != null && db_type != null && hostname.equals("")) {
-              switch (db_type.toUpperCase()) {
-                case "EXISTING MYSQL DATABASE":
-                  final_db_host = oozieEnvProperties.get("oozie_existing_mysql_host");
-                  break;
-                case "EXISTING POSTGRESQL DATABASE":
-                  final_db_host = oozieEnvProperties.get("oozie_existing_postgresql_host");
-                  break;
-                case "EXISTING ORACLE DATABASE":
-                  final_db_host = oozieEnvProperties.get("oozie_existing_oracle_host");
-                  break;
-                default:
-                  final_db_host = null;
-                  break;
-              }
-              if (final_db_host != null) {
-                Map<String, String> newProperties = new HashMap<>();
-                newProperties.put("oozie_hostname", final_db_host);
-                updateConfigurationPropertiesForCluster(cluster, OOZIE_ENV, newProperties, true, true);
-              }
-            }
-          }
-        }
-      }
-    }
-  }
-
-  protected String updateHiveEnvContent(String hiveEnvContent) {
-    if(hiveEnvContent == null) {
-      return null;
-    }
-    String oldHeapSizeRegex = "export HADOOP_HEAPSIZE=\"\\{\\{hive_heapsize\\}\\}\"\\s*\\n" +
-            "export HADOOP_CLIENT_OPTS=\"-Xmx\\$\\{HADOOP_HEAPSIZE\\}m \\$HADOOP_CLIENT_OPTS\"";
-    String newAuxJarPath = "";
-    return hiveEnvContent.replaceAll(oldHeapSizeRegex, Matcher.quoteReplacement(newAuxJarPath));
-  }
-
-  /**
-   * DDL changes for {@link #HOST_ROLE_COMMAND_TABLE}.
-   *
-   * @throws AmbariException
-   * @throws SQLException
-   */
-  private void executeHostRoleCommandDDLUpdates() throws AmbariException, SQLException {
-    dbAccessor.addColumn(HOST_ROLE_COMMAND_TABLE,
-        new DBColumnInfo(HOST_ROLE_COMMAND_SKIP_COLUMN, Integer.class, 1, 0, false));
-  }
-
-  protected void removeDataDirMountConfig() throws AmbariException {
-    Set<String> properties = new HashSet<>();
-    properties.add("dfs.datanode.data.dir.mount.file");
-
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          removeConfigurationPropertiesFromCluster(cluster, "hadoop-env", properties);
-        }
-      }
-    }
-  }
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog2121.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog2121.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog2121.java
deleted file mode 100644
index c5f2327..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog2121.java
+++ /dev/null
@@ -1,194 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.ambari.server.upgrade;
-
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.dao.DaoUtils;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.DesiredConfig;
-import org.apache.ambari.server.state.StackId;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.sql.SQLException;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * Upgrade catalog for version 2.1.2.1
- */
-public class UpgradeCatalog2121 extends AbstractUpgradeCatalog {
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog2121.class);
-
-  @Inject
-  DaoUtils daoUtils;
-
-  private static final String OOZIE_SITE_CONFIG = "oozie-site";
-  private static final String OOZIE_AUTHENTICATION_KERBEROS_NAME_RULES = "oozie.authentication.kerberos.name.rules";
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   *
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog2121(Injector injector) {
-    super(injector);
-
-    daoUtils = injector.getInstance(DaoUtils.class);
-  }
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.1.2.1";
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getSourceVersion() {
-    return "2.1.2";
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executePreDMLUpdates() throws AmbariException, SQLException {
-
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    updatePHDConfigs();
-    updateOozieConfigs();
-  }
-
-  /**
-   * Update PHD stack configs
-   * @throws AmbariException
-   */
-  protected void updatePHDConfigs() throws AmbariException {
-
-    Map<String, String> replacements = new LinkedHashMap<String, String>();
-    replacements.put("-Dstack.name=\\{\\{\\s*stack_name\\s*\\}\\}\\s*", "");
-    replacements.put("-Dstack.name=\\$\\{stack.name\\}\\s*", "");
-    replacements.put("-Dstack.version=\\{\\{\\s*stack_version_buildnum\\s*\\}\\}", "-Dhdp.version=\\$HDP_VERSION");
-    replacements.put("-Dstack.version=\\$\\{stack.version\\}", "-Dhdp.version=\\$\\{hdp.version\\}");
-    replacements.put("\\{\\{\\s*stack_name\\s*\\}\\}", "phd");
-    replacements.put("\\$\\{stack.name\\}", "phd");
-    replacements.put("\\$\\{stack.version\\}", "\\$\\{hdp.version\\}");
-
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-      if ((clusterMap != null) && !clusterMap.isEmpty()) {
-        // Iterate through the clusters and perform any configuration updates
-        for (final Cluster cluster : clusterMap.values()) {
-          StackId currentStackVersion = cluster.getCurrentStackVersion();
-          String currentStackName = currentStackVersion != null? currentStackVersion.getStackName() : null;
-          if (currentStackName != null && currentStackName.equalsIgnoreCase("PHD")) {
-            // Update configs only if PHD stack is deployed
-            Map<String, DesiredConfig> desiredConfigs = cluster.getDesiredConfigs();
-            if(desiredConfigs != null && !desiredConfigs.isEmpty()) {
-              for (Map.Entry<String, DesiredConfig> dc : desiredConfigs.entrySet()) {
-                String configType = dc.getKey();
-                DesiredConfig desiredConfig = dc.getValue();
-                String configTag = desiredConfig.getTag();
-                Config config = cluster.getConfig(configType, configTag);
-
-                Map<String, String> properties = config.getProperties();
-                if(properties != null && !properties.isEmpty()) {
-                  Map<String, String> updates = new HashMap<String, String>();
-                  for (Map.Entry<String, String> property : properties.entrySet()) {
-                    String propertyKey = property.getKey();
-                    String propertyValue = property.getValue();
-                    String modifiedPropertyValue = propertyValue;
-                    for (String regex : replacements.keySet()) {
-                      modifiedPropertyValue = modifiedPropertyValue.replaceAll(regex, replacements.get(regex));
-                    }
-                    if (!modifiedPropertyValue.equals(propertyValue)) {
-                      updates.put(propertyKey, modifiedPropertyValue);
-                    }
-                  }
-                  if (!updates.isEmpty()) {
-                    updateConfigurationPropertiesForCluster(cluster, configType, updates, true, false);
-                  }
-                }
-              }
-            }
-          }
-        }
-      }
-    }
-  }
-
-  protected void updateOozieConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config oozieSiteProps = cluster.getDesiredConfigByType(OOZIE_SITE_CONFIG);
-      if (oozieSiteProps != null) {
-        // Remove oozie.authentication.kerberos.name.rules if empty
-        String oozieAuthKerbRules = oozieSiteProps.getProperties().get(OOZIE_AUTHENTICATION_KERBEROS_NAME_RULES);
-        if (StringUtils.isBlank(oozieAuthKerbRules)) {
-          Set<String> removeProperties = new HashSet<String>();
-          removeProperties.add(OOZIE_AUTHENTICATION_KERBEROS_NAME_RULES);
-          updateConfigurationPropertiesForCluster(cluster, OOZIE_SITE_CONFIG, new HashMap<String, String>(), removeProperties, true, false);
-        }
-      }
-    }
-
-  }
-}
-


[23/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java
deleted file mode 100644
index 736d8f5..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java
+++ /dev/null
@@ -1,611 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
-import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.ClusterServiceDAO;
-import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
-import org.apache.ambari.server.orm.dao.MetainfoDAO;
-import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.ServiceDesiredStateDAO;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
-import org.apache.ambari.server.orm.entities.ClusterServiceEntityPK;
-import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
-import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.ServiceDesiredStateEntity;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.ConfigHelper;
-import org.apache.ambari.server.state.OperatingSystemInfo;
-import org.apache.ambari.server.state.PropertyInfo;
-import org.apache.ambari.server.state.RepositoryInfo;
-import org.apache.ambari.server.state.SecurityState;
-import org.apache.ambari.server.state.SecurityType;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.UpgradeState;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-
-
-/**
- * Upgrade catalog for version 2.0.0.
- */
-public class UpgradeCatalog200 extends AbstractUpgradeCatalog {
-
-  private static final String ALERT_DEFINITION_TABLE = "alert_definition";
-  private static final String ALERT_TARGET_TABLE = "alert_target";
-  private static final String ALERT_TARGET_STATES_TABLE = "alert_target_states";
-  private static final String ALERT_CURRENT_TABLE = "alert_current";
-  private static final String ARTIFACT_TABLE = "artifact";
-  private static final String KERBEROS_PRINCIPAL_TABLE = "kerberos_principal";
-  private static final String KERBEROS_PRINCIPAL_HOST_TABLE = "kerberos_principal_host";
-  private static final String TEZ_USE_CLUSTER_HADOOP_LIBS_PROPERTY = "tez.use.cluster.hadoop-libs";
-  private static final String FLUME_ENV_CONFIG = "flume-env";
-  private static final String CONTENT_PROPERTY = "content";
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.0.0";
-  }
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger
-      (UpgradeCatalog200.class);
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog200(Injector injector) {
-    super(injector);
-    this.injector = injector;
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-    prepareRollingUpgradesDDL();
-    executeAlertDDLUpdates();
-    createArtifactTable();
-    createKerberosPrincipalTables();
-
-    // add viewparameter columns
-    dbAccessor.addColumn("viewparameter", new DBColumnInfo("label",
-        String.class, 255, null, true));
-
-    dbAccessor.addColumn("viewparameter", new DBColumnInfo("placeholder",
-        String.class, 255, null, true));
-
-    dbAccessor.addColumn("viewparameter", new DBColumnInfo("default_value",
-        String.class, 2000, null, true));
-
-    // add security_type to clusters
-    dbAccessor.addColumn("clusters", new DBColumnInfo(
-        "security_type", String.class, 32, SecurityType.NONE.toString(), false));
-
-    // add security_state to various tables
-    dbAccessor.addColumn("hostcomponentdesiredstate", new DBColumnInfo(
-        "security_state", String.class, 32, SecurityState.UNSECURED.toString(), false));
-    dbAccessor.addColumn("hostcomponentstate", new DBColumnInfo(
-        "security_state", String.class, 32, SecurityState.UNSECURED.toString(), false));
-    dbAccessor.addColumn("servicedesiredstate", new DBColumnInfo(
-        "security_state", String.class, 32, SecurityState.UNSECURED.toString(), false));
-
-    // Alter column : make viewinstanceproperty.value & viewinstancedata.value
-    // nullable
-    dbAccessor.alterColumn("viewinstanceproperty", new DBColumnInfo("value",
-        String.class, 2000, null, true));
-    dbAccessor.alterColumn("viewinstancedata", new DBColumnInfo("value",
-        String.class, 2000, null, true));
-  }
-
-  /**
-   * Execute all of the alert DDL updates.
-   *
-   * @throws AmbariException
-   * @throws SQLException
-   */
-  private void executeAlertDDLUpdates() throws AmbariException, SQLException {
-    // add ignore_host column to alert_definition
-    dbAccessor.addColumn(ALERT_DEFINITION_TABLE, new DBColumnInfo(
-            "ignore_host", Short.class, 1, 0, false));
-
-    dbAccessor.addColumn(ALERT_DEFINITION_TABLE, new DBColumnInfo(
-            "description", char[].class, 32672, null, true));
-
-    // update alert target
-    dbAccessor.addColumn(ALERT_TARGET_TABLE, new DBColumnInfo("is_global",
-        Short.class, 1, 0, false));
-
-    // create alert_target_states table
-    ArrayList<DBColumnInfo> columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBColumnInfo("target_id", Long.class, null, null, false));
-    columns.add(new DBColumnInfo("alert_state", String.class, 255, null, false));
-    dbAccessor.createTable(ALERT_TARGET_STATES_TABLE, columns);
-    dbAccessor.addFKConstraint(ALERT_TARGET_STATES_TABLE,
-        "fk_alert_tgt_states_tgt_id", "target_id", ALERT_TARGET_TABLE,
-        "target_id", false);
-
-    // update alert current maintenance mode
-    dbAccessor.alterColumn(ALERT_CURRENT_TABLE, new DBColumnInfo(
-        "maintenance_state", String.class, 255, null, false));
-  }
-
-  /**
-   * Add any columns, tables, and keys needed for Rolling Upgrades.
-   * @throws SQLException
-   */
-  private void prepareRollingUpgradesDDL() throws SQLException {
-    List<DBAccessor.DBColumnInfo> columns = new ArrayList<DBAccessor.DBColumnInfo>();
-
-    columns.add(new DBColumnInfo("repo_version_id", Long.class,    null,  null, false));
-    columns.add(new DBColumnInfo("stack",           String.class,  255,   null, false));
-    columns.add(new DBColumnInfo("version",         String.class,  255,   null, false));
-    columns.add(new DBColumnInfo("display_name",    String.class,  128,   null, false));
-    columns.add(new DBColumnInfo("upgrade_package", String.class,  255,   null, false));
-    columns.add(new DBColumnInfo("repositories",    char[].class,  null,  null, false));
-    dbAccessor.createTable("repo_version", columns, "repo_version_id");
-    addSequence("repo_version_id_seq", 0L, false);
-
-
-    dbAccessor.addUniqueConstraint("repo_version", "UQ_repo_version_display_name", "display_name");
-    dbAccessor.addUniqueConstraint("repo_version", "UQ_repo_version_stack_version", "stack", "version");
-
-    // New columns
-    dbAccessor.addColumn("hostcomponentstate", new DBAccessor.DBColumnInfo("upgrade_state",
-        String.class, 32, "NONE", false));
-
-    dbAccessor.addColumn("hostcomponentstate", new DBAccessor.DBColumnInfo("version",
-        String.class, 32, "UNKNOWN", false));
-
-    dbAccessor.addColumn("host_role_command", new DBAccessor.DBColumnInfo("retry_allowed",
-        Integer.class, 1, 0, false));
-
-    dbAccessor.addColumn("stage", new DBAccessor.DBColumnInfo("skippable",
-        Integer.class, 1, 0, false));
-
-    // New tables
-    columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBAccessor.DBColumnInfo("id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("repo_version_id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("cluster_id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("state", String.class, 32, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("start_time", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("end_time", Long.class, null, null, true));
-    columns.add(new DBAccessor.DBColumnInfo("user_name", String.class, 32, null, true));
-    dbAccessor.createTable("cluster_version", columns, "id");
-
-    columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBAccessor.DBColumnInfo("id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("repo_version_id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("host_name", String.class, 255, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("state", String.class, 32, null, false));
-    dbAccessor.createTable("host_version", columns, "id");
-
-    // Foreign Key Constraints
-    dbAccessor.addFKConstraint("cluster_version", "FK_cluster_version_cluster_id", "cluster_id", "clusters", "cluster_id", false);
-    dbAccessor.addFKConstraint("cluster_version", "FK_cluster_version_repovers_id", "repo_version_id", "repo_version", "repo_version_id", false);
-    if (dbAccessor.tableHasColumn("host_version", "host_name")) {
-      dbAccessor.addFKConstraint("host_version", "FK_host_version_host_name", "host_name", "hosts", "host_name", false);
-    }
-    dbAccessor.addFKConstraint("host_version", "FK_host_version_repovers_id", "repo_version_id", "repo_version", "repo_version_id", false);
-
-    // New sequences
-    addSequence("cluster_version_id_seq", 0L, false);
-    addSequence("host_version_id_seq", 0L, false);
-
-    // upgrade tables
-    columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBAccessor.DBColumnInfo("upgrade_id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("cluster_id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("request_id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("from_version", String.class, 255, "", false));
-    columns.add(new DBAccessor.DBColumnInfo("to_version", String.class, 255, "", false));
-    columns.add(new DBAccessor.DBColumnInfo("direction", String.class, 255, "UPGRADE", false));
-    dbAccessor.createTable("upgrade", columns, "upgrade_id");
-    dbAccessor.addFKConstraint("upgrade", "fk_upgrade_cluster_id", "cluster_id", "clusters", "cluster_id", false);
-    dbAccessor.addFKConstraint("upgrade", "fk_upgrade_request_id", "request_id", "request", "request_id", false);
-    addSequence("upgrade_id_seq", 0L, false);
-
-    columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBAccessor.DBColumnInfo("upgrade_group_id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("upgrade_id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("group_name", String.class, 255, "", false));
-    columns.add(new DBAccessor.DBColumnInfo("group_title", String.class, 1024, "", false));
-    dbAccessor.createTable("upgrade_group", columns, "upgrade_group_id");
-    dbAccessor.addFKConstraint("upgrade_group", "fk_upgrade_group_upgrade_id", "upgrade_id", "upgrade", "upgrade_id", false);
-    addSequence("upgrade_group_id_seq", 0L, false);
-
-
-    columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBAccessor.DBColumnInfo("upgrade_item_id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("upgrade_group_id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("stage_id", Long.class, null, null, false));
-    columns.add(new DBAccessor.DBColumnInfo("state", String.class, 255, UpgradeState.NONE.name(), false));
-    columns.add(new DBAccessor.DBColumnInfo("hosts", char[].class, 32672, null, true));
-    columns.add(new DBAccessor.DBColumnInfo("tasks", char[].class, 32672, null, true));
-    columns.add(new DBAccessor.DBColumnInfo("item_text", String.class, 1024, null, true));
-    dbAccessor.createTable("upgrade_item", columns, "upgrade_item_id");
-    dbAccessor.addFKConstraint("upgrade_item", "fk_upg_item_upgrade_group_id", "upgrade_group_id", "upgrade_group", "upgrade_group_id", false);
-    addSequence("upgrade_item_id_seq", 0L, false);
-  }
-
-  private void createArtifactTable() throws SQLException {
-    ArrayList<DBColumnInfo> columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBColumnInfo("artifact_name", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("foreign_keys", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("artifact_data", char[].class, null, null, false));
-    dbAccessor.createTable(ARTIFACT_TABLE, columns, "artifact_name", "foreign_keys");
-  }
-
-  private void createKerberosPrincipalTables() throws SQLException {
-    ArrayList<DBColumnInfo> columns;
-
-    columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBColumnInfo("principal_name", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("is_service", Short.class, 1, 1, false));
-    columns.add(new DBColumnInfo("cached_keytab_path", String.class, 255, null, true));
-    dbAccessor.createTable(KERBEROS_PRINCIPAL_TABLE, columns, "principal_name");
-
-    columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBColumnInfo("principal_name", String.class, 255, null, false));
-    columns.add(new DBColumnInfo("host_name", String.class, 255, null, false));
-    dbAccessor.createTable(KERBEROS_PRINCIPAL_HOST_TABLE, columns, "principal_name", "host_name");
-    if (dbAccessor.tableHasColumn(KERBEROS_PRINCIPAL_HOST_TABLE, "host_name")) {
-      dbAccessor.addFKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "FK_krb_pr_host_hostname", "host_name", "hosts", "host_name", true, false);
-    }
-    dbAccessor.addFKConstraint(KERBEROS_PRINCIPAL_HOST_TABLE, "FK_krb_pr_host_principalname", "principal_name", KERBEROS_PRINCIPAL_TABLE, "principal_name", true, false);
-  }
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public void executePreDMLUpdates() {
-    ;
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    // remove NAGIOS to make way for the new embedded alert framework
-    removeNagiosService();
-    addNewConfigurationsFromXml();
-    updateHiveDatabaseType();
-    updateTezConfiguration();
-    updateFlumeEnvConfig();
-    addMissingConfigs();
-    persistHDPRepo();
-    updateClusterEnvConfiguration();
-  }
-
-  protected void persistHDPRepo() throws AmbariException{
-    AmbariManagementController amc = injector.getInstance(
-            AmbariManagementController.class);
-    AmbariMetaInfo ambariMetaInfo = amc.getAmbariMetaInfo();
-    Map<String, Cluster> clusterMap = amc.getClusters().getClusters();
-    for (Cluster cluster : clusterMap.values()) {
-      StackId stackId = cluster.getCurrentStackVersion();
-      String stackName = stackId.getStackName();
-      String stackVersion = stackId.getStackVersion();
-      String stackRepoId = stackName + "-" + stackVersion;
-
-      for (OperatingSystemInfo osi : ambariMetaInfo.getOperatingSystems(stackName, stackVersion)) {
-        MetainfoDAO metaInfoDAO = injector.getInstance(MetainfoDAO.class);
-        String repoMetaKey = AmbariMetaInfo.generateRepoMetaKey(stackName,stackVersion,osi.getOsType(),
-                stackRepoId,AmbariMetaInfo.REPOSITORY_XML_PROPERTY_BASEURL);
-        // Check if default repo is used and not persisted
-        if (metaInfoDAO.findByKey(repoMetaKey) == null) {
-          RepositoryInfo repositoryInfo = ambariMetaInfo.getRepository(stackName, stackVersion, osi.getOsType(), stackRepoId);
-          // We save default base url which has not changed during upgrade as base url
-          String baseUrl = repositoryInfo.getDefaultBaseUrl();
-          ambariMetaInfo.updateRepoBaseURL(stackName, stackVersion, osi.getOsType(),
-                  stackRepoId, baseUrl);
-        }
-      }
-
-      // Repositories that have been autoset may be unexpected for user
-      // (especially if they are taken from online json)
-      // We have to output to stdout here, and not to log
-      // to be sure that user sees this message
-      System.out.printf("Ambari has recorded the following repository base urls for cluster %s. Please verify the " +
-              "values and ensure that these are correct. If necessary, " +
-              "after starting Ambari Server, you can edit them using Ambari UI, " +
-              "Admin -> Stacks and Versions -> Versions Tab and editing the base urls for the current Repo. " +
-              "It is critical that these repo base urls are valid for your environment as they " +
-              "will be used for Add Host/Service operations.",
-        cluster.getClusterName());
-      System.out.println(repositoryTable(ambariMetaInfo.getStack(stackName, stackVersion).getRepositories()));
-    }
-
-  }
-
-  /**
-   * Formats a list repositories for printing to console
-   * @param repositories list of repositories
-   * @return multi-line string
-   */
-  static String repositoryTable(List<RepositoryInfo> repositories) {
-    StringBuilder result = new StringBuilder();
-    for (RepositoryInfo repository : repositories) {
-      result.append(String.format(" %8s |", repository.getOsType()));
-      result.append(String.format(" %18s |", repository.getRepoId()));
-      result.append(String.format(" %48s ", repository.getBaseUrl()));
-      result.append("\n");
-    }
-    return result.toString();
-  }
-
-  protected void updateTezConfiguration() throws AmbariException {
-    updateConfigurationProperties("tez-site", Collections.singletonMap(TEZ_USE_CLUSTER_HADOOP_LIBS_PROPERTY, String.valueOf(false)), false, false);
-  }
-
-  protected void updateFlumeEnvConfig() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config flumeEnvConfig = cluster.getDesiredConfigByType(FLUME_ENV_CONFIG);
-      if (flumeEnvConfig != null) {
-        String content = flumeEnvConfig.getProperties().get(CONTENT_PROPERTY);
-        if (content != null && !content.contains("/usr/lib/flume/lib/ambari-metrics-flume-sink.jar")) {
-          String newPartOfContent = "\n\n" +
-            "# Note that the Flume conf directory is always included in the classpath.\n" +
-            "# Add flume sink to classpath\n" +
-            "if [ -e \"/usr/lib/flume/lib/ambari-metrics-flume-sink.jar\" ]; then\n" +
-            "  export FLUME_CLASSPATH=$FLUME_CLASSPATH:/usr/lib/flume/lib/ambari-metrics-flume-sink.jar\n" +
-            "fi\n";
-          content += newPartOfContent;
-          Map<String, String> updates = Collections.singletonMap(CONTENT_PROPERTY, content);
-          updateConfigurationPropertiesForCluster(cluster, FLUME_ENV_CONFIG, updates, true, false);
-        }
-      }
-    }
-  }
-
-  protected void updateHiveDatabaseType() throws AmbariException {
-    final String PROPERTY_NAME = "hive_database_type";
-    final String PROPERTY_VALUE_OLD = "postgresql";
-    final String PROPERTY_VALUE_NEW = "postgres";
-    final String PROPERTY_CONFIG_NAME = "hive-env";
-
-    AmbariManagementController ambariManagementController = injector.getInstance(
-            AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-      Map<String, String> prop = new HashMap<String, String>();
-      String hive_database_type = null;
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          hive_database_type = null;
-
-          if (cluster.getDesiredConfigByType(PROPERTY_CONFIG_NAME) != null) {
-            hive_database_type = cluster.getDesiredConfigByType(
-                    PROPERTY_CONFIG_NAME).getProperties().get(PROPERTY_NAME);
-          }
-
-          if (hive_database_type != null && !hive_database_type.isEmpty() &&
-                  hive_database_type.equals(PROPERTY_VALUE_OLD)) {
-            prop.put(PROPERTY_NAME, PROPERTY_VALUE_NEW);
-            updateConfigurationPropertiesForCluster(cluster, PROPERTY_CONFIG_NAME, prop, true, false);
-          }
-        }
-      }
-
-    }
-  }
-
-  /**
-   * Removes Nagios and all associated components and states.
-   */
-  protected void removeNagiosService() {
-    executeInTransaction(new RemoveNagiosRunnable());
-  }
-
-  /**
-   * The RemoveNagiosRunnable is used to remove Nagios from the cluster. This
-   * runnable is exepected to run inside of a transation so that if any of the
-   * removals fails, Nagios is returned to a valid service state.
-   */
-  protected final class RemoveNagiosRunnable implements Runnable {
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void run() {
-      ClusterDAO clusterDao = injector.getInstance(ClusterDAO.class);
-      ClusterServiceDAO clusterServiceDao = injector.getInstance(ClusterServiceDAO.class);
-      ServiceComponentDesiredStateDAO componentDesiredStateDao = injector.getInstance(ServiceComponentDesiredStateDAO.class);
-      ServiceDesiredStateDAO desiredStateDao = injector.getInstance(ServiceDesiredStateDAO.class);
-      HostComponentDesiredStateDAO hostComponentDesiredStateDao = injector.getInstance(HostComponentDesiredStateDAO.class);
-      HostComponentStateDAO hostComponentStateDao = injector.getInstance(HostComponentStateDAO.class);
-
-      List<ClusterEntity> clusters = clusterDao.findAll();
-      if (null == clusters) {
-        return;
-      }
-
-      for (ClusterEntity cluster : clusters) {
-        ClusterServiceEntity nagios = clusterServiceDao.findByClusterAndServiceNames(
-            cluster.getClusterName(), "NAGIOS");
-
-        if (null == nagios) {
-          continue;
-        }
-
-        Collection<ServiceComponentDesiredStateEntity> serviceComponentDesiredStates = nagios.getServiceComponentDesiredStateEntities();
-        ServiceDesiredStateEntity serviceDesiredState = nagios.getServiceDesiredStateEntity();
-
-        // remove all component states
-        for (ServiceComponentDesiredStateEntity componentDesiredState : serviceComponentDesiredStates) {
-          Collection<HostComponentStateEntity> hostComponentStateEntities = componentDesiredState.getHostComponentStateEntities();
-          Collection<HostComponentDesiredStateEntity> hostComponentDesiredStateEntities = componentDesiredState.getHostComponentDesiredStateEntities();
-
-          // remove host states
-          for (HostComponentStateEntity hostComponentState : hostComponentStateEntities) {
-            hostComponentStateDao.remove(hostComponentState);
-          }
-
-          // remove host desired states
-          for (HostComponentDesiredStateEntity hostComponentDesiredState : hostComponentDesiredStateEntities) {
-            hostComponentDesiredStateDao.remove(hostComponentDesiredState);
-          }
-
-          // remove component state
-          componentDesiredStateDao.removeByName(nagios.getClusterId(),
-              componentDesiredState.getServiceName(), componentDesiredState.getComponentName());
-        }
-
-        // remove service state
-        desiredStateDao.remove(serviceDesiredState);
-
-        // remove service
-        cluster.getClusterServiceEntities().remove(nagios);
-        ClusterServiceEntityPK primaryKey = new ClusterServiceEntityPK();
-        primaryKey.setClusterId(nagios.getClusterId());
-        primaryKey.setServiceName(nagios.getServiceName());
-        clusterServiceDao.removeByPK(primaryKey);
-      }
-    }
-  }
-  protected void addMissingConfigs() throws AmbariException {
-    updateConfigurationProperties("hive-site", Collections.singletonMap("hive.server2.transport.mode", "binary"), false, false);
-  }
-
-  /**
-   * Update the cluster-env configuration (in all clusters) to add missing properties and remove
-   * obsolete properties.
-   *
-   * @throws org.apache.ambari.server.AmbariException
-   */
-  protected void updateClusterEnvConfiguration() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    ConfigHelper configHelper = injector.getInstance(ConfigHelper.class);
-
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null) {
-        for (final Cluster cluster : clusterMap.values()) {
-          Config configClusterEnv = cluster.getDesiredConfigByType("cluster-env");
-
-          if (configClusterEnv != null) {
-            Map<String, String> properties = configClusterEnv.getProperties();
-
-            if (properties != null) {
-              // -----------------------------------------
-              // Add missing properties
-
-              if (!properties.containsKey("smokeuser_principal_name")) {
-                // Add smokeuser_principal_name, from cluster-env/smokeuser
-                // Ideally a realm should be added, but for now we can assume the default realm and
-                // leave it off
-                String smokeUser = properties.get("smokeuser");
-
-                if ((smokeUser == null) || smokeUser.isEmpty()) {
-                  // If the smokeuser property is not set in the current configuration set, grab
-                  // it from the stack defaults:
-                  Set<PropertyInfo> stackProperties = configHelper.getStackProperties(cluster);
-
-                  if (stackProperties != null) {
-                    for (PropertyInfo propertyInfo : stackProperties) {
-                      String filename = propertyInfo.getFilename();
-
-                      if ((filename != null) && "cluster-env".equals(ConfigHelper.fileNameToConfigType(filename))) {
-                        smokeUser = propertyInfo.getValue();
-                        break;
-                      }
-                    }
-                  }
-
-                  // If a default value for smokeuser was not found, force it to be "ambari-qa"
-                  if ((smokeUser == null) || smokeUser.isEmpty()) {
-                    smokeUser = "ambari-qa";
-                  }
-                }
-
-                properties.put("smokeuser_principal_name", smokeUser);
-              }
-
-              // Add missing properties (end)
-              // -----------------------------------------
-
-              // -----------------------------------------
-              // Remove obsolete properties
-
-              // Remove obsolete properties (end)
-              // -----------------------------------------
-
-              // -----------------------------------------
-              // Set the updated configuration
-
-              configHelper.createConfigType(cluster, ambariManagementController, "cluster-env", properties,
-                  AUTHENTICATED_USER_NAME, "Upgrading to Ambari 2.0");
-
-              // Set configuration (end)
-              // -----------------------------------------
-
-            }
-          }
-        }
-      }
-    }
-  }
-}


[05/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog221Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog221Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog221Test.java
deleted file mode 100644
index 4663c6c..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog221Test.java
+++ /dev/null
@@ -1,614 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-
-import com.google.common.collect.Maps;
-import com.google.gson.Gson;
-import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.Provider;
-import com.google.inject.persist.PersistService;
-import junit.framework.Assert;
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.actionmanager.ActionManager;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.controller.AmbariManagementControllerImpl;
-import org.apache.ambari.server.controller.KerberosHelper;
-import org.apache.ambari.server.controller.MaintenanceStateHelper;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.GuiceJpaInitializer;
-import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.StackEntity;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.easymock.Capture;
-import org.easymock.EasyMock;
-import org.easymock.EasyMockSupport;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-import javax.persistence.EntityManager;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.anyString;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.createMockBuilder;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-public class UpgradeCatalog221Test {
-  private static Injector injector;
-  private static Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
-  private static EntityManager entityManager = createNiceMock(EntityManager.class);
-  private static UpgradeCatalogHelper upgradeCatalogHelper;
-  private static StackEntity desiredStackEntity;
-
-  @BeforeClass
-  public static void init() {
-    reset(entityManagerProvider);
-    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
-    replay(entityManagerProvider);
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-
-    upgradeCatalogHelper = injector.getInstance(UpgradeCatalogHelper.class);
-    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
-    injector.getInstance(AmbariMetaInfo.class);
-    // load the stack entity
-    StackDAO stackDAO = injector.getInstance(StackDAO.class);
-    desiredStackEntity = stackDAO.find("HDP", "2.2.0");
-  }
-
-  @AfterClass
-  public static void tearDown() {
-    injector.getInstance(PersistService.class).stop();
-  }
-
-  @Test
-  public void testExecuteDDLUpdates() throws Exception{
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-
-    dbAccessor.createIndex(eq("idx_stage_request_id"), eq("stage"), eq("request_id"));
-    expectLastCall().once();
-    dbAccessor.createIndex(eq("idx_hrc_request_id"), eq("host_role_command"), eq("request_id"));
-    expectLastCall().once();
-    dbAccessor.createIndex(eq("idx_rsc_request_id"), eq("role_success_criteria"), eq("request_id"));
-    expectLastCall().once();
-
-    Capture<DBAccessor.DBColumnInfo> capturedHostGroupComponentProvisionColumn = EasyMock.newCapture();
-    dbAccessor.addColumn(eq("hostgroup_component"), capture(capturedHostGroupComponentProvisionColumn));
-    expectLastCall().once();
-
-
-    replay(dbAccessor);
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        binder.bind(EntityManager.class).toInstance(entityManager);
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    UpgradeCatalog221 upgradeCatalog221 = injector.getInstance(UpgradeCatalog221.class);
-    upgradeCatalog221.executeDDLUpdates();
-
-    // verify that the column was added for provision_action to the hostgroup_component table
-    assertEquals("Incorrect column name added", "provision_action", capturedHostGroupComponentProvisionColumn.getValue().getName());
-    assertNull("Incorrect default value added", capturedHostGroupComponentProvisionColumn.getValue().getDefaultValue());
-    assertEquals("Incorrect column type added", String.class, capturedHostGroupComponentProvisionColumn.getValue().getType());
-    assertEquals("Incorrect column length added", 255, capturedHostGroupComponentProvisionColumn.getValue().getLength().intValue());
-    assertTrue("Incorrect column nullable state added", capturedHostGroupComponentProvisionColumn.getValue().isNullable());
-
-
-    verify(dbAccessor);
-  }
-
-  @Test
-  public void testExecuteDMLUpdates() throws Exception {
-    Method addNewConfigurationsFromXml = AbstractUpgradeCatalog.class.getDeclaredMethod("addNewConfigurationsFromXml");
-    Method updateAlerts = UpgradeCatalog221.class.getDeclaredMethod("updateAlerts");
-    Method updateOozieConfigs = UpgradeCatalog221.class.getDeclaredMethod("updateOozieConfigs");
-    Method updateTezConfigs = UpgradeCatalog221.class.getDeclaredMethod("updateTezConfigs");
-    Method updateRangerKmsDbksConfigs = UpgradeCatalog221.class.getDeclaredMethod("updateRangerKmsDbksConfigs");
-    Method updateAMSConfigs = UpgradeCatalog221.class.getDeclaredMethod("updateAMSConfigs");
-
-    UpgradeCatalog221 upgradeCatalog221 = createMockBuilder(UpgradeCatalog221.class)
-      .addMockedMethod(addNewConfigurationsFromXml)
-      .addMockedMethod(updateAlerts)
-      .addMockedMethod(updateOozieConfigs)
-      .addMockedMethod(updateTezConfigs)
-      .addMockedMethod(updateRangerKmsDbksConfigs)
-      .addMockedMethod(updateAMSConfigs)
-      .createMock();
-
-    upgradeCatalog221.addNewConfigurationsFromXml();
-    expectLastCall().once();
-    upgradeCatalog221.updateAlerts();
-    expectLastCall().once();
-    upgradeCatalog221.updateOozieConfigs();
-    expectLastCall().once();
-    upgradeCatalog221.updateTezConfigs();
-    expectLastCall().once();
-    upgradeCatalog221.updateRangerKmsDbksConfigs();
-    expectLastCall().once();
-    upgradeCatalog221.updateAMSConfigs();
-    expectLastCall().once();
-
-
-    replay(upgradeCatalog221);
-
-    upgradeCatalog221.executeDMLUpdates();
-
-    verify(upgradeCatalog221);
-  }
-
-  @Test
-  public void test_AddCheckCommandTimeoutParam_ParamsNotAvailable() {
-
-    UpgradeCatalog221 upgradeCatalog221 = new UpgradeCatalog221(injector);
-    String inputSource = "{ \"path\" : \"test_path\", \"type\" : \"SCRIPT\"}";
-    String expectedSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"name\":\"check.command.timeout\",\"display_name\":\"Check command timeout\",\"value\":60.0,\"type\":\"NUMERIC\",\"description\":\"The maximum time before check command will be killed by timeout\",\"units\":\"seconds\"}]}";
-
-    String result = upgradeCatalog221.addCheckCommandTimeoutParam(inputSource);
-    Assert.assertEquals(result, expectedSource);
-  }
-
-  @Test
-  public void test_AddCheckCommandTimeoutParam_ParamsAvailable() {
-
-    UpgradeCatalog221 upgradeCatalog221 = new UpgradeCatalog221(injector);
-    String inputSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"name\":\"test\",\"display_name\":\"Test\",\"value\":10.0,\"type\":\"test\",\"description\":\"test\",\"units\":\"test\"}]}";
-    String expectedSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"name\":\"test\",\"display_name\":\"Test\",\"value\":10.0,\"type\":\"test\",\"description\":\"test\",\"units\":\"test\"},{\"name\":\"check.command.timeout\",\"display_name\":\"Check command timeout\",\"value\":60.0,\"type\":\"NUMERIC\",\"description\":\"The maximum time before check command will be killed by timeout\",\"units\":\"seconds\"}]}";
-
-    String result = upgradeCatalog221.addCheckCommandTimeoutParam(inputSource);
-    Assert.assertEquals(result, expectedSource);
-  }
-
-  @Test
-  public void test_AddCheckCommandTimeoutParam_NeededParamAlreadyAdded() {
-
-    UpgradeCatalog221 upgradeCatalog221 = new UpgradeCatalog221(injector);
-    String inputSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"display_name\":\"Test\",\"value\":10.0,\"type\":\"test\",\"description\":\"test\",\"units\":\"test\"},{\"name\":\"check.command.timeout\",\"display_name\":\"Check command timeout\",\"value\":60.0,\"type\":\"NUMERIC\",\"description\":\"The maximum time before check command will be killed by timeout\",\"units\":\"seconds\"}]}";
-    String expectedSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"display_name\":\"Test\",\"value\":10.0,\"type\":\"test\",\"description\":\"test\",\"units\":\"test\"},{\"name\":\"check.command.timeout\",\"display_name\":\"Check command timeout\",\"value\":60.0,\"type\":\"NUMERIC\",\"description\":\"The maximum time before check command will be killed by timeout\",\"units\":\"seconds\"}]}";
-
-    String result = upgradeCatalog221.addCheckCommandTimeoutParam(inputSource);
-    Assert.assertEquals(result, expectedSource);
-  }
-
-  @Test
-  public void testUpdateOozieConfigs() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Config oozieSiteConf = easyMockSupport.createNiceMock(Config.class);
-    final Map<String, String> propertiesOozieSite = new HashMap<String, String>() {{
-      put("oozie.service.HadoopAccessorService.hadoop.configurations", "*=/etc/hadoop/conf");
-    }};
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("oozie-site")).andReturn(oozieSiteConf).atLeastOnce();
-    expect(oozieSiteConf.getProperties()).andReturn(propertiesOozieSite).once();
-
-    UpgradeCatalog221 upgradeCatalog221 = createMockBuilder(UpgradeCatalog221.class)
-        .withConstructor(Injector.class)
-        .withArgs(mockInjector)
-        .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-            Map.class, boolean.class, boolean.class)
-        .createMock();
-    upgradeCatalog221.updateConfigurationPropertiesForCluster(mockClusterExpected, "oozie-site",
-        Collections.singletonMap("oozie.service.HadoopAccessorService.hadoop.configurations", "*={{hadoop_conf_dir}}"),
-        true, false);
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    replay(upgradeCatalog221);
-    upgradeCatalog221.updateOozieConfigs();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateTezConfigs() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Config tezSiteConf = easyMockSupport.createNiceMock(Config.class);
-    final Map<String, String> propertiesTezSite = new HashMap<String, String>() {{
-      put("tez.counters.max", "2000");
-      put("tez.counters.max.groups", "1000");
-    }};
-
-    StackId stackId = new StackId("HDP","2.3");
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-
-
-    expect(mockClusterExpected.getDesiredConfigByType("tez-site")).andReturn(tezSiteConf).atLeastOnce();
-    expect(tezSiteConf.getProperties()).andReturn(propertiesTezSite).once();
-    expect(mockClusterExpected.getCurrentStackVersion()).andReturn(stackId).once();
-
-    UpgradeCatalog221 upgradeCatalog221 = createMockBuilder(UpgradeCatalog221.class)
-        .withConstructor(Injector.class)
-        .withArgs(mockInjector)
-        .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-            Map.class, boolean.class, boolean.class)
-        .createMock();
-    Map<String, String> updates = new HashMap<String, String>();
-    updates.put("tez.counters.max", "10000");
-    updates.put("tez.counters.max.groups", "3000");
-    upgradeCatalog221.updateConfigurationPropertiesForCluster(mockClusterExpected, "tez-site",
-        updates, true, false);
-    expectLastCall().once();
-
-  }
-
-  @Test
-  public void testUpdateRangerKmsDbksConfigs() throws Exception {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Map<String, String> propertiesRangerKmsDbConfigs = new HashMap<String, String>();
-    propertiesRangerKmsDbConfigs.put("DB_FLAVOR", "MYSQL");
-    propertiesRangerKmsDbConfigs.put("db_host", "localhost");
-    propertiesRangerKmsDbConfigs.put("db_name", "testdb");
-
-    final Config mockrangerKmsDbConfigs = easyMockSupport.createNiceMock(Config.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-
-    expect(mockClusterExpected.getDesiredConfigByType("kms-properties")).andReturn(mockrangerKmsDbConfigs).atLeastOnce();
-    expect(mockrangerKmsDbConfigs.getProperties()).andReturn(propertiesRangerKmsDbConfigs).times(3);
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog221.class).updateRangerKmsDbksConfigs();
-    easyMockSupport.verifyAll();
-
-  }
-
-  @Test
-  public void testUpdateAmsHbaseSiteConfigs() throws Exception {
-
-    Map<String, String> clusterEnvProperties = new HashMap<String, String>();
-    Map<String, String> amsHbaseSecuritySite = new HashMap<String, String>();
-    Map<String, String> newPropertiesAmsHbaseSite = new HashMap<String, String>();
-
-    //Unsecure
-    amsHbaseSecuritySite.put("zookeeper.znode.parent", "/ams-hbase-unsecure");
-    newPropertiesAmsHbaseSite.put("zookeeper.znode.parent", "/ams-hbase-unsecure");
-    testAmsHbaseSiteUpdates(new HashMap<String, String>(),
-      newPropertiesAmsHbaseSite,
-      amsHbaseSecuritySite,
-      clusterEnvProperties);
-
-    //Secure
-    amsHbaseSecuritySite.put("zookeeper.znode.parent", "/ams-hbase-secure");
-    newPropertiesAmsHbaseSite.put("zookeeper.znode.parent", "/ams-hbase-secure");
-    testAmsHbaseSiteUpdates(new HashMap<String, String>(),
-      newPropertiesAmsHbaseSite,
-      amsHbaseSecuritySite,
-      clusterEnvProperties);
-
-    //Unsecure with empty value
-    clusterEnvProperties.put("security_enabled", "false");
-    amsHbaseSecuritySite.put("zookeeper.znode.parent", "");
-    newPropertiesAmsHbaseSite.put("zookeeper.znode.parent", "/ams-hbase-unsecure");
-    testAmsHbaseSiteUpdates(new HashMap<String, String>(),
-      newPropertiesAmsHbaseSite,
-      amsHbaseSecuritySite,
-      clusterEnvProperties);
-
-    //Secure with /hbase value
-    clusterEnvProperties.put("security_enabled", "true");
-    amsHbaseSecuritySite.put("zookeeper.znode.parent", "/hbase");
-    newPropertiesAmsHbaseSite.put("zookeeper.znode.parent", "/ams-hbase-secure");
-    testAmsHbaseSiteUpdates(new HashMap<String, String>(),
-      newPropertiesAmsHbaseSite,
-      amsHbaseSecuritySite,
-      clusterEnvProperties);
-
-    // Test zookeeper client port set to default
-    amsHbaseSecuritySite.put("hbase.zookeeper.property.clientPort", "61181");
-    newPropertiesAmsHbaseSite.put("hbase.zookeeper.property.clientPort", "{{zookeeper_clientPort}}");
-    testAmsHbaseSiteUpdates(Collections.singletonMap("hbase.zookeeper.property.clientPort", "61181"),
-      newPropertiesAmsHbaseSite,
-      amsHbaseSecuritySite,
-      clusterEnvProperties);
-  }
-
-  private void testAmsHbaseSiteUpdates(Map<String, String> oldPropertiesAmsHbaseSite,
-                                       Map<String, String> newPropertiesAmsHbaseSite,
-                                       Map<String, String> amsHbaseSecuritySiteProperties,
-                                       Map<String, String> clusterEnvProperties ) throws AmbariException {
-
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-
-    Config mockAmsHbaseSite = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("ams-hbase-site")).andReturn(mockAmsHbaseSite).atLeastOnce();
-    expect(mockAmsHbaseSite.getProperties()).andReturn(oldPropertiesAmsHbaseSite).times(2);
-
-    Config mockAmsHbaseSecuritySite = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("ams-hbase-security-site")).andReturn(mockAmsHbaseSecuritySite).anyTimes();
-    expect(mockAmsHbaseSecuritySite.getProperties()).andReturn(amsHbaseSecuritySiteProperties).anyTimes();
-
-    Config clusterEnv = easyMockSupport.createNiceMock(Config.class);
-    expect(cluster.getDesiredConfigByType("cluster-env")).andReturn(clusterEnv).anyTimes();
-    expect(clusterEnv.getProperties()).andReturn(clusterEnvProperties).anyTimes();
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockAmsHbaseSite, mockAmsHbaseSecuritySite, clusterEnv, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-      .addMockedMethod("createConfiguration")
-      .addMockedMethod("getClusters", new Class[] { })
-      .addMockedMethod("createConfig")
-      .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-      .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-      anyObject(Map.class))).andReturn(createNiceMock(Config.class)).anyTimes();
-
-    replay(controller, injector2);
-    new UpgradeCatalog221(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    // Test zookeeper tick time setting
-    String tickTime = updatedProperties.remove("hbase.zookeeper.property.tickTime");
-    assertEquals("6000", tickTime);
-    assertTrue(Maps.difference(newPropertiesAmsHbaseSite, updatedProperties).areEqual());
-  }
-
-  @Test
-  public void testUpdateAmsHbaseSecuritySiteConfigs() throws Exception{
-
-    Map<String, String> oldPropertiesAmsHbaseSecuritySite = new HashMap<String, String>() {
-      {
-        put("zookeeper.znode.parent", "/ams-hbase-secure");
-      }
-    };
-
-    Map<String, String> newPropertiesAmsHbaseSecuritySite = new HashMap<String, String>() {
-      {
-      }
-    };
-
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    Config mockAmsHbaseSecuritySite = easyMockSupport.createNiceMock(Config.class);
-
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-
-    expect(cluster.getDesiredConfigByType("ams-hbase-security-site")).andReturn(mockAmsHbaseSecuritySite).atLeastOnce();
-    expect(mockAmsHbaseSecuritySite.getProperties()).andReturn(oldPropertiesAmsHbaseSecuritySite).times(2);
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockAmsHbaseSecuritySite, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-      .addMockedMethod("createConfiguration")
-      .addMockedMethod("getClusters", new Class[] { })
-      .addMockedMethod("createConfig")
-      .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-      .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-      anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog221(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesAmsHbaseSecuritySite, updatedProperties).areEqual());
-
-  }
-
-  @Test
-  public void testUpdateAmsHbaseEnvContent() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
-    Method updateAmsHbaseEnvContent = UpgradeCatalog221.class.getDeclaredMethod("updateAmsHbaseEnvContent", String.class);
-    UpgradeCatalog221 upgradeCatalog221 = new UpgradeCatalog221(injector);
-    String oldContent = "some_content\n" +
-      "{% if security_enabled %}\n" +
-      "export HBASE_OPTS=\"$HBASE_OPTS -Djava.security.auth.login.config={{client_jaas_config_file}} -Dzookeeper.sasl.client.username={{zk_servicename}}\"\n" +
-      "export HBASE_MASTER_OPTS=\"$HBASE_MASTER_OPTS -Djava.security.auth.login.config={{master_jaas_config_file}} -Dzookeeper.sasl.client.username={{zk_servicename}}\"\n" +
-      "export HBASE_REGIONSERVER_OPTS=\"$HBASE_REGIONSERVER_OPTS -Djava.security.auth.login.config={{regionserver_jaas_config_file}} -Dzookeeper.sasl.client.username={{zk_servicename}}\"\n" +
-      "export HBASE_ZOOKEEPER_OPTS=\"$HBASE_ZOOKEEPER_OPTS -Djava.security.auth.login.config={{ams_zookeeper_jaas_config_file}} -Dzookeeper.sasl.client.username={{zk_servicename}}\"\n" +
-      "{% endif %}";
-
-    String expectedContent = "some_content\n" +
-      "{% if security_enabled %}\n" +
-      "export HBASE_OPTS=\"$HBASE_OPTS -Djava.security.auth.login.config={{client_jaas_config_file}}\"\n" +
-      "export HBASE_MASTER_OPTS=\"$HBASE_MASTER_OPTS -Djava.security.auth.login.config={{master_jaas_config_file}}\"\n" +
-      "export HBASE_REGIONSERVER_OPTS=\"$HBASE_REGIONSERVER_OPTS -Djava.security.auth.login.config={{regionserver_jaas_config_file}}\"\n" +
-      "export HBASE_ZOOKEEPER_OPTS=\"$HBASE_ZOOKEEPER_OPTS -Djava.security.auth.login.config={{ams_zookeeper_jaas_config_file}}\"\n" +
-      "{% endif %}";
-
-    String result = (String) updateAmsHbaseEnvContent.invoke(upgradeCatalog221, oldContent);
-    Assert.assertEquals(expectedContent, result);
-  }
-
-  @Test
-  public void testUpdateAmsEnvContent() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
-  {
-    Method updateAmsEnvContent = UpgradeCatalog221.class.getDeclaredMethod("updateAmsEnvContent", String.class);
-    UpgradeCatalog221 upgradeCatalog221 = new UpgradeCatalog221(injector);
-    String oldContent = "some_content\n" +
-      "# AMS Collector options\n" +
-      "export AMS_COLLECTOR_OPTS=\"-Djava.library.path=/usr/lib/ams-hbase/lib/hadoop-native\"\n" +
-      "{% if security_enabled %}\n" +
-      "export AMS_COLLECTOR_OPTS=\"$AMS_COLLECTOR_OPTS -Djava.security.auth.login.config={{ams_collector_jaas_config_file}} " +
-      "-Dzookeeper.sasl.client.username={{zk_servicename}}\"\n" +
-      "{% endif %}";
-
-    String expectedContent = "some_content\n" +
-      "# AMS Collector options\n" +
-      "export AMS_COLLECTOR_OPTS=\"-Djava.library.path=/usr/lib/ams-hbase/lib/hadoop-native\"\n" +
-      "{% if security_enabled %}\n" +
-      "export AMS_COLLECTOR_OPTS=\"$AMS_COLLECTOR_OPTS -Djava.security.auth.login.config={{ams_collector_jaas_config_file}}\"\n" +
-      "{% endif %}";
-
-    String result = (String) updateAmsEnvContent.invoke(upgradeCatalog221, oldContent);
-    Assert.assertEquals(expectedContent, result);
-  }
-
-  @Test
-  public void testUpdateAlertDefinitions() {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    long clusterId = 1;
-
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final AlertDefinitionEntity mockAmsZookeeperProcessAlertDefinitionEntity = easyMockSupport.createNiceMock(AlertDefinitionEntity.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-        bind(AlertDefinitionDAO.class).toInstance(mockAlertDefinitionDAO);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-
-    expect(mockClusterExpected.getClusterId()).andReturn(clusterId).anyTimes();
-
-    expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("ams_metrics_collector_zookeeper_server_process")))
-      .andReturn(mockAmsZookeeperProcessAlertDefinitionEntity).atLeastOnce();
-
-    mockAlertDefinitionDAO.remove(mockAmsZookeeperProcessAlertDefinitionEntity);
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog221.class).updateAlerts();
-    easyMockSupport.verifyAll();
-  }
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
deleted file mode 100644
index d0e1adc..0000000
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
+++ /dev/null
@@ -1,1125 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-
-import javax.persistence.EntityManager;
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.anyString;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.createMockBuilder;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.io.File;
-import java.lang.reflect.Method;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
-
-import com.google.inject.Inject;
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.actionmanager.ActionManager;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.controller.AmbariManagementControllerImpl;
-import org.apache.ambari.server.controller.KerberosHelper;
-import org.apache.ambari.server.controller.MaintenanceStateHelper;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.GuiceJpaInitializer;
-import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.dao.WidgetDAO;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.StackEntity;
-import org.apache.ambari.server.orm.entities.WidgetEntity;
-import org.apache.ambari.server.stack.StackManagerFactory;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.Service;
-import org.apache.ambari.server.state.ServiceComponentHost;
-import org.apache.ambari.server.state.ServiceInfo;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.StackInfo;
-import org.apache.ambari.server.state.stack.OsFamily;
-import org.apache.commons.io.FileUtils;
-import org.easymock.Capture;
-import org.easymock.CaptureType;
-import org.easymock.EasyMock;
-import org.easymock.EasyMockSupport;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-
-import com.google.common.collect.Maps;
-import com.google.gson.Gson;
-import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.Provider;
-import com.google.inject.persist.PersistService;
-
-public class UpgradeCatalog222Test {
-  private Injector injector;
-  private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
-  private EntityManager entityManager = createNiceMock(EntityManager.class);
-
-  @Inject
-  private UpgradeCatalogHelper upgradeCatalogHelper;
-
-  private StackEntity desiredStackEntity;
-
-  @Rule
-  public TemporaryFolder temporaryFolder = new TemporaryFolder();
-
-  public UpgradeCatalog222Test(){
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-
-  }
-
-  @Before
-  public void init() {
-    reset(entityManagerProvider);
-    expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
-    replay(entityManagerProvider);
-
-    injector.getInstance(GuiceJpaInitializer.class);
-
-    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
-    injector.getInstance(AmbariMetaInfo.class);
-
-    injector.injectMembers(this);
-
-    // load the stack entity
-    StackDAO stackDAO = injector.getInstance(StackDAO.class);
-    desiredStackEntity = stackDAO.find("HDP", "2.2.0");
-  }
-
-  @After
-  public void tearDown() {
-    injector.getInstance(PersistService.class).stop();
-  }
-
-  @Test
-  public void testExecuteDMLUpdates() throws Exception {
-    Method addNewConfigurationsFromXml = AbstractUpgradeCatalog.class.getDeclaredMethod("addNewConfigurationsFromXml");
-    Method updateAlerts = UpgradeCatalog222.class.getDeclaredMethod("updateAlerts");
-    Method updateStormConfigs = UpgradeCatalog222.class.getDeclaredMethod("updateStormConfigs");
-    Method updateAMSConfigs = UpgradeCatalog222.class.getDeclaredMethod("updateAMSConfigs");
-    Method updateHiveConfigs = UpgradeCatalog222.class.getDeclaredMethod("updateHiveConfig");
-    Method updateHostRoleCommands = UpgradeCatalog222.class.getDeclaredMethod("updateHostRoleCommands");
-    Method updateHDFSWidget = UpgradeCatalog222.class.getDeclaredMethod("updateHDFSWidgetDefinition");
-    Method updateYARNWidget = UpgradeCatalog222.class.getDeclaredMethod("updateYARNWidgetDefinition");
-    Method updateHBASEWidget = UpgradeCatalog222.class.getDeclaredMethod("updateHBASEWidgetDefinition");
-    Method updateHbaseEnvConfig = UpgradeCatalog222.class.getDeclaredMethod("updateHbaseEnvConfig");
-    Method updateCorruptedReplicaWidget = UpgradeCatalog222.class.getDeclaredMethod("updateCorruptedReplicaWidget");
-    Method createNewSliderConfigVersion = UpgradeCatalog222.class.getDeclaredMethod("createNewSliderConfigVersion");
-    Method updateZookeeperConfigs = UpgradeCatalog222.class.getDeclaredMethod("updateZookeeperConfigs");
-    Method updateHBASEConfigs = UpgradeCatalog222.class.getDeclaredMethod("updateHBASEConfigs");
-    Method initializeStromAnsKafkaWidgets = UpgradeCatalog222.class.getDeclaredMethod("initializeStromAndKafkaWidgets");
-
-    UpgradeCatalog222 upgradeCatalog222 = createMockBuilder(UpgradeCatalog222.class)
-      .addMockedMethod(addNewConfigurationsFromXml)
-      .addMockedMethod(updateAlerts)
-      .addMockedMethod(updateStormConfigs)
-      .addMockedMethod(updateAMSConfigs)
-      .addMockedMethod(updateHiveConfigs)
-      .addMockedMethod(updateHostRoleCommands)
-      .addMockedMethod(updateHDFSWidget)
-      .addMockedMethod(updateYARNWidget)
-      .addMockedMethod(updateHBASEWidget)
-      .addMockedMethod(updateHbaseEnvConfig)
-      .addMockedMethod(updateCorruptedReplicaWidget)
-      .addMockedMethod(createNewSliderConfigVersion)
-      .addMockedMethod(updateZookeeperConfigs)
-      .addMockedMethod(updateHBASEConfigs)
-      .addMockedMethod(initializeStromAnsKafkaWidgets)
-      .createMock();
-
-    upgradeCatalog222.addNewConfigurationsFromXml();
-    upgradeCatalog222.updateAlerts();
-    upgradeCatalog222.updateStormConfigs();
-    upgradeCatalog222.updateAMSConfigs();
-    upgradeCatalog222.updateHostRoleCommands();
-    upgradeCatalog222.updateHiveConfig();
-    upgradeCatalog222.updateHDFSWidgetDefinition();
-    upgradeCatalog222.updateHbaseEnvConfig();
-    upgradeCatalog222.updateYARNWidgetDefinition();
-    upgradeCatalog222.updateHBASEWidgetDefinition();
-    upgradeCatalog222.updateCorruptedReplicaWidget();
-    upgradeCatalog222.updateZookeeperConfigs();
-    upgradeCatalog222.updateHBASEConfigs();
-    upgradeCatalog222.createNewSliderConfigVersion();
-    upgradeCatalog222.initializeStromAndKafkaWidgets();
-
-    replay(upgradeCatalog222);
-
-    upgradeCatalog222.executeDMLUpdates();
-
-    verify(upgradeCatalog222);
-  }
-
-  @Test
-  public void testUpdateAlerts_ATSAlert() {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class);
-    final AlertDefinitionEntity mockATSWebAlert = easyMockSupport.createNiceMock(AlertDefinitionEntity.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-        bind(AlertDefinitionDAO.class).toInstance(mockAlertDefinitionDAO);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    long clusterId = 1;
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getClusterId()).andReturn(clusterId).anyTimes();
-    expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("yarn_app_timeline_server_webui")))
-            .andReturn(mockATSWebAlert).atLeastOnce();
-    expect(mockATSWebAlert.getSource()).andReturn("{\"uri\": {\n" +
-      "            \"http\": \"{{yarn-site/yarn.timeline-service.webapp.address}}/ws/v1/timeline\",\n" +
-      "            \"https\": \"{{yarn-site/yarn.timeline-service.webapp.https.address}}/ws/v1/timeline\" } }");
-
-    mockATSWebAlert.setSource("{\"uri\":{\"http\":\"{{yarn-site/yarn.timeline-service.webapp.address}}/ws/v1/timeline\",\"https\":\"{{yarn-site/yarn.timeline-service.webapp.https.address}}/ws/v1/timeline\"}}");
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog222.class).updateAlerts();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testHiveSiteUpdateConfigs() throws AmbariException {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Config hiveSiteConfigs = easyMockSupport.createNiceMock(Config.class);
-    final Config AtlasSiteConfigs = easyMockSupport.createNiceMock(Config.class);
-
-    final ServiceComponentHost atlasHost = easyMockSupport.createNiceMock(ServiceComponentHost.class);
-    final List<ServiceComponentHost> atlasHosts = new ArrayList<>();
-    atlasHosts.add(atlasHost);
-
-    StackId stackId = new StackId("HDP","2.3");
-
-    final Map<String, String> propertiesAtlasSiteConfigs = new HashMap<String, String>() {{
-      put("atlas.enableTLS", "true");
-      put("atlas.server.https.port", "21443");
-    }};
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-        bind(ServiceComponentHost.class).toInstance(atlasHost);
-
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockClusterExpected.getCurrentStackVersion()).andReturn(stackId).once();
-    expect(mockClusterExpected.getServiceComponentHosts("ATLAS", "ATLAS_SERVER")).andReturn(atlasHosts).once();
-    expect(atlasHost.getHostName()).andReturn("c6401").once();
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hive-site")).andReturn(hiveSiteConfigs).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("application-properties")).andReturn(AtlasSiteConfigs).anyTimes();
-    expect(AtlasSiteConfigs.getProperties()).andReturn(propertiesAtlasSiteConfigs).anyTimes();
-
-    UpgradeCatalog222 upgradeCatalog222 = createMockBuilder(UpgradeCatalog222.class)
-      .withConstructor(Injector.class)
-      .withArgs(mockInjector)
-      .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-        Map.class, boolean.class, boolean.class)
-      .createMock();
-
-    Map<String, String> expectedUpdates = new HashMap<>();
-    expectedUpdates.put("atlas.hook.hive.minThreads", "1");
-    expectedUpdates.put("atlas.hook.hive.maxThreads", "1");
-    expectedUpdates.put("atlas.cluster.name", "primary");
-    expectedUpdates.put("atlas.rest.address", "https://c6401:21443");
-
-    upgradeCatalog222.updateConfigurationPropertiesForCluster(mockClusterExpected, "hive-site", expectedUpdates,
-      false, false);
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    replay(upgradeCatalog222);
-    upgradeCatalog222.updateHiveConfig();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateZookeeperConfigs() throws Exception{
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Config zookeeperEnv = easyMockSupport.createNiceMock(Config.class);
-    expect(zookeeperEnv.getProperties()).andReturn(new HashMap<String, String>(){{
-      put("zk_server_heapsize", "1024");
-    }}
-    ).anyTimes();
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("zookeeper-env")).andReturn(zookeeperEnv).atLeastOnce();
-
-    UpgradeCatalog222 upgradeCatalog222 = createMockBuilder(UpgradeCatalog222.class)
-        .withConstructor(Injector.class)
-        .withArgs(mockInjector)
-        .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-            Map.class, boolean.class, boolean.class)
-        .createMock();
-
-    Map<String, String> expectedUpdates = new HashMap<>();
-    expectedUpdates.put("zk_server_heapsize", "1024m");
-
-    upgradeCatalog222.updateConfigurationPropertiesForCluster(mockClusterExpected, "zookeeper-env", expectedUpdates,
-        true, false);
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    replay(upgradeCatalog222);
-    upgradeCatalog222.updateZookeeperConfigs();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateHBASEConfigs() throws Exception{
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-
-    final Config hbaseSite = easyMockSupport.createNiceMock(Config.class);
-    expect(hbaseSite.getProperties()).andReturn(new HashMap<String, String>(){{
-                                                     put(UpgradeCatalog222.HBASE_SITE_HBASE_COPROCESSOR_MASTER_CLASSES, "test1");
-                                                     put(UpgradeCatalog222.HBASE_SITE_HBASE_COPROCESSOR_REGION_CLASSES, "test2");
-                                                     put(UpgradeCatalog222.HBASE_SITE_HBASE_COPROCESSOR_REGIONSERVER_CLASSES, "test3");
-                                                   }}
-    ).anyTimes();
-
-    final Config rangerHbasePluginProperties = easyMockSupport.createNiceMock(Config.class);
-    expect(rangerHbasePluginProperties.getProperties()).andReturn(new HashMap<String, String>(){{
-                                                  put(AbstractUpgradeCatalog.PROPERTY_RANGER_HBASE_PLUGIN_ENABLED, "yes");
-                                                }}
-    ).anyTimes();
-
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).anyTimes();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-
-    UpgradeCatalog222 upgradeCatalog222 = createMockBuilder(UpgradeCatalog222.class)
-      .withConstructor(Injector.class)
-      .withArgs(mockInjector)
-      .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class,
-        Map.class, boolean.class, boolean.class)
-      .createStrictMock();
-
-    // CASE 1 - Ranger enabled, Cluster version is 2.2
-    expect(mockClusterExpected.getCurrentStackVersion()).andReturn(new StackId("HDP", "2.2")).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hbase-site")).andReturn(hbaseSite).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType(AbstractUpgradeCatalog.CONFIGURATION_TYPE_RANGER_HBASE_PLUGIN_PROPERTIES)).
-      andReturn(rangerHbasePluginProperties).once();
-
-    Map<String, String> expectedUpdates = new HashMap<>();
-    expectedUpdates.put(UpgradeCatalog222.HBASE_SITE_HBASE_COPROCESSOR_MASTER_CLASSES, "com.xasecure.authorization.hbase.XaSecureAuthorizationCoprocessor");
-    expectedUpdates.put(UpgradeCatalog222.HBASE_SITE_HBASE_COPROCESSOR_REGIONSERVER_CLASSES, "com.xasecure.authorization.hbase.XaSecureAuthorizationCoprocessor");
-    expectedUpdates.put(UpgradeCatalog222.HBASE_SITE_HBASE_COPROCESSOR_REGION_CLASSES,
-      "org.apache.hadoop.hbase.security.token.TokenProvider,org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint," +
-        "com.xasecure.authorization.hbase.XaSecureAuthorizationCoprocessor");
-
-    upgradeCatalog222.updateConfigurationPropertiesForCluster(mockClusterExpected, "hbase-site", expectedUpdates,
-      true, false);
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    upgradeCatalog222.updateHBASEConfigs();
-    easyMockSupport.verifyAll();
-
-    // CASE 2 - Ranger enabled, Cluster version is 2.3
-    reset(mockClusterExpected, upgradeCatalog222);
-    expect(mockClusterExpected.getCurrentStackVersion()).andReturn(new StackId("HDP", "2.3")).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hbase-site")).andReturn(hbaseSite).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType(AbstractUpgradeCatalog.CONFIGURATION_TYPE_RANGER_HBASE_PLUGIN_PROPERTIES)).
-      andReturn(rangerHbasePluginProperties).once();
-
-    expectedUpdates = new HashMap<>();
-    expectedUpdates.put(UpgradeCatalog222.HBASE_SITE_HBASE_COPROCESSOR_MASTER_CLASSES, "org.apache.ranger.authorization.hbase.RangerAuthorizationCoprocessor ");
-    expectedUpdates.put(UpgradeCatalog222.HBASE_SITE_HBASE_COPROCESSOR_REGIONSERVER_CLASSES, "org.apache.ranger.authorization.hbase.RangerAuthorizationCoprocessor");
-    expectedUpdates.put(UpgradeCatalog222.HBASE_SITE_HBASE_COPROCESSOR_REGION_CLASSES,
-      "org.apache.hadoop.hbase.security.token.TokenProvider,org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint," +
-        "org.apache.ranger.authorization.hbase.RangerAuthorizationCoprocessor");
-
-    upgradeCatalog222.updateConfigurationPropertiesForCluster(mockClusterExpected, "hbase-site", expectedUpdates,
-      true, false);
-    expectLastCall().once();
-
-    replay(mockClusterExpected, upgradeCatalog222);
-    upgradeCatalog222.updateHBASEConfigs();
-    easyMockSupport.verifyAll();
-
-    // CASE 3 - Ranger enabled, Cluster version is 2.1
-    reset(mockClusterExpected, upgradeCatalog222);
-    expect(mockClusterExpected.getCurrentStackVersion()).andReturn(new StackId("HDP", "2.1")).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType("hbase-site")).andReturn(hbaseSite).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType(AbstractUpgradeCatalog.CONFIGURATION_TYPE_RANGER_HBASE_PLUGIN_PROPERTIES)).
-      andReturn(rangerHbasePluginProperties).once();
-
-    replay(mockClusterExpected, upgradeCatalog222);
-    upgradeCatalog222.updateHBASEConfigs();
-    easyMockSupport.verifyAll();
-
-    // CASE 4 - Ranger disabled
-    reset(mockClusterExpected, upgradeCatalog222);
-    expect(mockClusterExpected.getDesiredConfigByType("hbase-site")).andReturn(hbaseSite).atLeastOnce();
-    expect(mockClusterExpected.getDesiredConfigByType(AbstractUpgradeCatalog.CONFIGURATION_TYPE_RANGER_HBASE_PLUGIN_PROPERTIES)).
-      andReturn(null).once();
-
-    replay(mockClusterExpected, upgradeCatalog222);
-    upgradeCatalog222.updateHBASEConfigs();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testAmsSiteUpdateConfigs() throws Exception{
-
-    Map<String, String> oldPropertiesAmsSite = new HashMap<String, String>() {
-      {
-        put("timeline.metrics.host.aggregator.daily.checkpointCutOffMultiplier", String.valueOf(1));
-        put("timeline.metrics.cluster.aggregator.daily.checkpointCutOffMultiplier", String.valueOf(1));
-        put("timeline.metrics.service.operation.mode", "distributed");
-        put("timeline.metrics.host.aggregator.ttl", String.valueOf(86400));
-        put("timeline.metrics.cluster.aggregator.second.ttl", String.valueOf(21600)); //Less than 1 day
-        put("timeline.metrics.cluster.aggregator.minute.ttl", String.valueOf(7776000));
-        put("timeline.metrics.service.webapp.address", "0.0.0.0:6188");
-        put("timeline.metrics.sink.collection.period", "60");
-      }
-    };
-    Map<String, String> newPropertiesAmsSite = new HashMap<String, String>() {
-      {
-        put("timeline.metrics.host.aggregator.daily.checkpointCutOffMultiplier", String.valueOf(2));
-        put("timeline.metrics.cluster.aggregator.daily.checkpointCutOffMultiplier", String.valueOf(2));
-        put("timeline.metrics.service.watcher.disabled", String.valueOf(false));
-        put("timeline.metrics.host.aggregator.ttl", String.valueOf(3 * 86400));
-        put("timeline.metrics.cluster.aggregator.second.ttl", String.valueOf(21600));
-        put("timeline.metrics.cluster.aggregator.minute.ttl", String.valueOf(30 * 86400));
-        put("timeline.metrics.service.operation.mode", "distributed");
-        put("timeline.metrics.service.webapp.address", "host1:6188");
-        put("timeline.metrics.cluster.aggregator.interpolation.enabled", String.valueOf(true));
-        put("timeline.metrics.sink.collection.period", "10");
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    Config mockAmsSite = easyMockSupport.createNiceMock(Config.class);
-
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("ams-site")).andReturn(mockAmsSite).atLeastOnce();
-    expect(mockAmsSite.getProperties()).andReturn(oldPropertiesAmsSite).anyTimes();
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-    expect(cluster.getHosts("AMBARI_METRICS", "METRICS_COLLECTOR")).andReturn( new HashSet<String>() {{
-      add("host1");
-    }}).atLeastOnce();
-
-    replay(injector, clusters, mockAmsSite, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-      .addMockedMethod("createConfiguration")
-      .addMockedMethod("getClusters", new Class[] { })
-      .addMockedMethod("createConfig")
-      .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-      .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-      anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog222(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesAmsSite, updatedProperties).areEqual());
-  }
-
-  @Test
-  public void testAmsHbaseSiteUpdateConfigs() throws Exception{
-
-    Map<String, String> oldPropertiesAmsHbaseSite = new HashMap<String, String>() {
-      {
-        put("hbase.client.scanner.timeout.period", String.valueOf(900000));
-        put("phoenix.query.timeoutMs", String.valueOf(1200000));
-      }
-    };
-    Map<String, String> newPropertiesAmsHbaseSite = new HashMap<String, String>() {
-      {
-        put("hbase.client.scanner.timeout.period", String.valueOf(300000));
-        put("hbase.rpc.timeout", String.valueOf(300000));
-        put("phoenix.query.timeoutMs", String.valueOf(300000));
-        put("phoenix.query.keepAliveMs", String.valueOf(300000));
-      }
-    };
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-
-    Clusters clusters = easyMockSupport.createNiceMock(Clusters.class);
-    final Cluster cluster = easyMockSupport.createNiceMock(Cluster.class);
-    Config mockAmsHbaseSite = easyMockSupport.createNiceMock(Config.class);
-
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).once();
-    expect(cluster.getDesiredConfigByType("ams-hbase-site")).andReturn(mockAmsHbaseSite).atLeastOnce();
-    expect(mockAmsHbaseSite.getProperties()).andReturn(oldPropertiesAmsHbaseSite).anyTimes();
-
-    Injector injector = easyMockSupport.createNiceMock(Injector.class);
-    expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes();
-    expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)).anyTimes();
-
-    replay(injector, clusters, mockAmsHbaseSite, cluster);
-
-    AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
-      .addMockedMethod("createConfiguration")
-      .addMockedMethod("getClusters", new Class[] { })
-      .addMockedMethod("createConfig")
-      .withConstructor(createNiceMock(ActionManager.class), clusters, injector)
-      .createNiceMock();
-
-    Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
-    Capture<Map> propertiesCapture = EasyMock.newCapture();
-
-    expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(propertiesCapture), anyString(),
-      anyObject(Map.class))).andReturn(createNiceMock(Config.class)).once();
-
-    replay(controller, injector2);
-    new UpgradeCatalog222(injector2).updateAMSConfigs();
-    easyMockSupport.verifyAll();
-
-    Map<String, String> updatedProperties = propertiesCapture.getValue();
-    assertTrue(Maps.difference(newPropertiesAmsHbaseSite, updatedProperties).areEqual());
-  }
-
-  @Test
-  public void testHDFSWidgetUpdateWithOnlyZkService() throws Exception {
-    final Clusters clusters = createNiceMock(Clusters.class);
-    final Cluster cluster = createNiceMock(Cluster.class);
-    final AmbariManagementController controller = createNiceMock(AmbariManagementController.class);
-    final Gson gson = new Gson();
-    final WidgetDAO widgetDAO = createNiceMock(WidgetDAO.class);
-    final AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class);
-    StackInfo stackInfo = createNiceMock(StackInfo.class);
-    StackId stackId = new StackId("HDP", "2.0.0");
-
-    String widgetStr = "{\"layouts\":[{\"layout_name\":\"default_hdfs_dashboard\",\"display_name\":\"Standard HDFS Dashboard\",\"section_name\":\"HDFS_SUMMARY\",\"widgetLayoutInfo\":[{\"widget_name\":\"NameNode RPC\",\"metrics\":[],\"values\":[]}]}]}";
-
-    File dataDirectory = temporaryFolder.newFolder();
-    File file = new File(dataDirectory, "hdfs_widget.json");
-    FileUtils.writeStringToFile(file, widgetStr);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(EntityManager.class).toInstance(createNiceMock(EntityManager.class));
-        bind(AmbariManagementController.class).toInstance(controller);
-        bind(Clusters.class).toInstance(clusters);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        bind(Gson.class).toInstance(gson);
-        bind(WidgetDAO.class).toInstance(widgetDAO);
-        bind(StackManagerFactory.class).toInstance(createNiceMock(StackManagerFactory.class));
-        bind(AmbariMetaInfo.class).toInstance(metaInfo);
-      }
-    });
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).anyTimes();
-    expect(cluster.getClusterId()).andReturn(1L).anyTimes();
-    expect(stackInfo.getService("HDFS")).andReturn(null);
-    expect(cluster.getDesiredStackVersion()).andReturn(stackId);
-    expect(metaInfo.getStack("HDP", "2.0.0")).andReturn(stackInfo);
-
-    replay(clusters, cluster, controller, widgetDAO, metaInfo, stackInfo);
-
-    UpgradeCatalog222 upgradeCatalog222 = createMockBuilder(UpgradeCatalog222.class)
-            .withConstructor(Injector.class)
-            .withArgs(mockInjector)
-            .createMock();
-    upgradeCatalog222.updateHDFSWidgetDefinition();
-
-  }
-
-  @Test
-  public void testHDFSWidgetUpdate() throws Exception {
-    final Clusters clusters = createNiceMock(Clusters.class);
-    final Cluster cluster = createNiceMock(Cluster.class);
-    final AmbariManagementController controller = createNiceMock(AmbariManagementController.class);
-    final Gson gson = new Gson();
-    final WidgetDAO widgetDAO = createNiceMock(WidgetDAO.class);
-    final AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class);
-    WidgetEntity widgetEntity = createNiceMock(WidgetEntity.class);
-    WidgetEntity widgetEntity2 = createNiceMock(WidgetEntity.class);
-    StackId stackId = new StackId("HDP", "2.0.0");
-    StackInfo stackInfo = createNiceMock(StackInfo.class);
-    ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
-
-    String widgetStr = "{\n" +
-      "  \"layouts\": [\n" +
-      "    {\n" +
-      "      \"layout_name\": \"default_hdfs_dashboard\",\n" +
-      "      \"display_name\": \"Standard HDFS Dashboard\",\n" +
-      "      \"section_name\": \"HDFS_SUMMARY\",\n" +
-      "      \"widgetLayoutInfo\": [\n" +
-      "        {\n" +
-      "          \"widget_name\": \"NameNode RPC\",\n" +
-      "          \"metrics\": [],\n" +
-      "          \"values\": []\n" +
-      "        }\n" +
-      "      ]\n" +
-      "    },\n" +
-      "        {\n" +
-      "      \"layout_name\": \"default_hdfs_heatmap\",\n" +
-      "      \"display_name\": \"Standard HDFS HeatMaps\",\n" +
-      "      \"section_name\": \"HDFS_HEATMAPS\",\n" +
-      "      \"widgetLayoutInfo\": [\n" +
-      "        {\n" +
-      "          \"widget_name\": \"HDFS Bytes Read\",\n" +
-      "          \"metrics\": [],\n" +
-      "          \"values\": []\n" +
-      "        }\n" +
-      "      ]\n" +
-      "    }\n" +
-      "  ]\n" +
-      "}";
-
-    File dataDirectory = temporaryFolder.newFolder();
-    File file = new File(dataDirectory, "hdfs_widget.json");
-    FileUtils.writeStringToFile(file, widgetStr);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(EntityManager.class).toInstance(createNiceMock(EntityManager.class));
-        bind(AmbariManagementController.class).toInstance(controller);
-        bind(Clusters.class).toInstance(clusters);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        bind(Gson.class).toInstance(gson);
-        bind(WidgetDAO.class).toInstance(widgetDAO);
-        bind(StackManagerFactory.class).toInstance(createNiceMock(StackManagerFactory.class));
-        bind(AmbariMetaInfo.class).toInstance(metaInfo);
-      }
-    });
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).anyTimes();
-    expect(cluster.getClusterId()).andReturn(1L).anyTimes();
-    expect(stackInfo.getService("HDFS")).andReturn(serviceInfo);
-    expect(cluster.getDesiredStackVersion()).andReturn(stackId).anyTimes();
-    expect(metaInfo.getStack("HDP", "2.0.0")).andReturn(stackInfo).anyTimes();
-    expect(serviceInfo.getWidgetsDescriptorFile()).andReturn(file).anyTimes();
-
-    expect(widgetDAO.findByName(1L, "NameNode RPC", "ambari", "HDFS_SUMMARY"))
-      .andReturn(Collections.singletonList(widgetEntity));
-    expect(widgetDAO.merge(widgetEntity)).andReturn(null);
-    expect(widgetEntity.getWidgetName()).andReturn("Namenode RPC").anyTimes();
-
-    expect(widgetDAO.findByName(1L, "HDFS Bytes Read", "ambari", "HDFS_HEATMAPS"))
-      .andReturn(Collections.singletonList(widgetEntity2));
-    expect(widgetDAO.merge(widgetEntity2)).andReturn(null);
-    expect(widgetEntity2.getWidgetName()).andReturn("HDFS Bytes Read").anyTimes();
-
-    replay(clusters, cluster, controller, widgetDAO, metaInfo, widgetEntity, widgetEntity2, stackInfo, serviceInfo);
-
-    mockInjector.getInstance(UpgradeCatalog222.class).updateHDFSWidgetDefinition();
-
-    verify(clusters, cluster, controller, widgetDAO, widgetEntity, widgetEntity2, stackInfo, serviceInfo);
-  }
-
-  @Test
-  public void testYARNWidgetUpdate() throws Exception {
-    final Clusters clusters = createNiceMock(Clusters.class);
-    final Cluster cluster = createNiceMock(Cluster.class);
-    final AmbariManagementController controller = createNiceMock(AmbariManagementController.class);
-    final Gson gson = new Gson();
-    final WidgetDAO widgetDAO = createNiceMock(WidgetDAO.class);
-    final AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class);
-    WidgetEntity widgetEntity = createNiceMock(WidgetEntity.class);
-    WidgetEntity widgetEntity2 = createNiceMock(WidgetEntity.class);
-    StackId stackId = new StackId("HDP", "2.0.0");
-    StackInfo stackInfo = createNiceMock(StackInfo.class);
-    ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
-
-    String widgetStr = "{\n" +
-      "  \"layouts\": [\n" +
-      "    {\n" +
-      "      \"layout_name\": \"default_yarn_dashboard\",\n" +
-      "      \"display_name\": \"Standard YARN Dashboard\",\n" +
-      "      \"section_name\": \"YARN_SUMMARY\",\n" +
-      "      \"widgetLayoutInfo\": [\n" +
-      "        {\n" +
-      "          \"widget_name\": \"Container Failures\",\n" +
-      "          \"metrics\": [],\n" +
-      "          \"values\": []\n" +
-      "        }\n" +
-      "      ]\n" +
-      "    },\n" +
-      "        {\n" +
-      "      \"layout_name\": \"default_yarn_heatmap\",\n" +
-      "      \"display_name\": \"Standard YARN HeatMaps\",\n" +
-      "      \"section_name\": \"YARN_HEATMAPS\",\n" +
-      "      \"widgetLayoutInfo\": [\n" +
-      "        {\n" +
-      "          \"widget_name\": \"Container Failures\",\n" +
-      "          \"metrics\": [],\n" +
-      "          \"values\": []\n" +
-      "        }\n" +
-      "      ]\n" +
-      "    }\n" +
-      "  ]\n" +
-      "}";
-
-    File dataDirectory = temporaryFolder.newFolder();
-    File file = new File(dataDirectory, "yarn_widget.json");
-    FileUtils.writeStringToFile(file, widgetStr);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(EntityManager.class).toInstance(createNiceMock(EntityManager.class));
-        bind(AmbariManagementController.class).toInstance(controller);
-        bind(Clusters.class).toInstance(clusters);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        bind(Gson.class).toInstance(gson);
-        bind(WidgetDAO.class).toInstance(widgetDAO);
-        bind(StackManagerFactory.class).toInstance(createNiceMock(StackManagerFactory.class));
-        bind(AmbariMetaInfo.class).toInstance(metaInfo);
-      }
-    });
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).anyTimes();
-    expect(cluster.getClusterId()).andReturn(1L).anyTimes();
-    expect(stackInfo.getService("YARN")).andReturn(serviceInfo);
-    expect(cluster.getDesiredStackVersion()).andReturn(stackId).anyTimes();
-    expect(metaInfo.getStack("HDP", "2.0.0")).andReturn(stackInfo).anyTimes();
-    expect(serviceInfo.getWidgetsDescriptorFile()).andReturn(file).anyTimes();
-
-    expect(widgetDAO.findByName(1L, "Container Failures", "ambari", "YARN_SUMMARY"))
-      .andReturn(Collections.singletonList(widgetEntity));
-    expect(widgetDAO.merge(widgetEntity)).andReturn(null);
-    expect(widgetEntity.getWidgetName()).andReturn("Container Failures").anyTimes();
-
-    expect(widgetDAO.findByName(1L, "Container Failures", "ambari", "YARN_HEATMAPS"))
-      .andReturn(Collections.singletonList(widgetEntity2));
-    expect(widgetDAO.merge(widgetEntity2)).andReturn(null);
-    expect(widgetEntity2.getWidgetName()).andReturn("Container Failures").anyTimes();
-
-    replay(clusters, cluster, controller, widgetDAO, metaInfo, widgetEntity, widgetEntity2, stackInfo, serviceInfo);
-
-    mockInjector.getInstance(UpgradeCatalog222.class).updateYARNWidgetDefinition();
-
-    verify(clusters, cluster, controller, widgetDAO, widgetEntity, widgetEntity2, stackInfo, serviceInfo);
-  }
-
-
-  @Test
-  public void testHBASEWidgetUpdate() throws Exception {
-    final Clusters clusters = createNiceMock(Clusters.class);
-    final Cluster cluster = createNiceMock(Cluster.class);
-    final AmbariManagementController controller = createNiceMock(AmbariManagementController.class);
-    final Gson gson = new Gson();
-    final WidgetDAO widgetDAO = createNiceMock(WidgetDAO.class);
-    final AmbariMetaInfo metaInfo = createNiceMock(AmbariMetaInfo.class);
-    WidgetEntity widgetEntity = createNiceMock(WidgetEntity.class);
-    StackId stackId = new StackId("HDP", "2.0.0");
-    StackInfo stackInfo = createNiceMock(StackInfo.class);
-    ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
-
-    String widgetStr = "{\n" +
-      "  \"layouts\": [\n" +
-      "    {\n" +
-      "      \"layout_name\": \"default_hbase_dashboard\",\n" +
-      "      \"display_name\": \"Standard HBASE Dashboard\",\n" +
-      "      \"section_name\": \"HBASE_SUMMARY\",\n" +
-      "      \"widgetLayoutInfo\": [\n" +
-      "        {\n" +
-      "          \"widget_name\": \"Blocked Updates\",\n" +
-      "          \"metrics\": [],\n" +
-      "          \"values\": []\n" +
-      "        }\n" +
-      "      ]\n" +
-      "    } " +
-      "]\n" +
-      "}";
-
-    File dataDirectory = temporaryFolder.newFolder();
-    File file = new File(dataDirectory, "hbase_widget.json");
-    FileUtils.writeStringToFile(file, widgetStr);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(EntityManager.class).toInstance(createNiceMock(EntityManager.class));
-        bind(AmbariManagementController.class).toInstance(controller);
-        bind(Clusters.class).toInstance(clusters);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        bind(Gson.class).toInstance(gson);
-        bind(WidgetDAO.class).toInstance(widgetDAO);
-        bind(StackManagerFactory.class).toInstance(createNiceMock(StackManagerFactory.class));
-        bind(AmbariMetaInfo.class).toInstance(metaInfo);
-      }
-    });
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(clusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", cluster);
-    }}).anyTimes();
-    expect(cluster.getClusterId()).andReturn(1L).anyTimes();
-    expect(stackInfo.getService("HBASE")).andReturn(serviceInfo);
-    expect(cluster.getDesiredStackVersion()).andReturn(stackId).anyTimes();
-    expect(metaInfo.getStack("HDP", "2.0.0")).andReturn(stackInfo).anyTimes();
-    expect(serviceInfo.getWidgetsDescriptorFile()).andReturn(file).anyTimes();
-
-    expect(widgetDAO.findByName(1L, "Blocked Updates", "ambari", "HBASE_SUMMARY"))
-      .andReturn(Collections.singletonList(widgetEntity));
-    expect(widgetDAO.merge(widgetEntity)).andReturn(null);
-    expect(widgetEntity.getWidgetName()).andReturn("Blocked Updates").anyTimes();
-
-    replay(clusters, cluster, controller, widgetDAO, metaInfo, widgetEntity, stackInfo, serviceInfo);
-
-    mockInjector.getInstance(UpgradeCatalog222.class).updateHBASEWidgetDefinition();
-
-    verify(clusters, cluster, controller, widgetDAO, widgetEntity, stackInfo, serviceInfo);
-  }
-
-  @Test
-  public void testGetUpdatedHbaseEnvProperties_BadConfig() {
-    String badContent = "export HBASE_HEAPSIZE=1000;\n\n" +
-            "export HBASE_OPTS=\"-Djava.io.tmpdir={{java_io_tmpdir}}\"\n\n" +
-            "export HBASE_LOG_DIR={{log_dir}}";
-    String expectedContent = "export HBASE_HEAPSIZE=1000;\n\n" +
-            "export HBASE_OPTS=\"${HBASE_OPTS} -Djava.io.tmpdir={{java_io_tmpdir}}\"\n\n" +
-            "export HBASE_LOG_DIR={{log_dir}}";
-    testGetUpdatedHbaseEnvProperties(badContent, expectedContent);
-  }
-
-  @Test
-  public void testGetUpdatedHbaseEnvProperties_GoodConfig() {
-
-    String goodContent = "export HBASE_HEAPSIZE=1000;\n\n" +
-            "export HBASE_OPTS=\"${HBASE_OPTS} -Djava.io.tmpdir={{java_io_tmpdir}}\"\n\n" +
-            "export HBASE_LOG_DIR={{log_dir}}";
-    testGetUpdatedHbaseEnvProperties(goodContent, null);
-  }
-
-  @Test
-  public void testGetUpdatedHbaseEnvProperties_NoConfig() {
-    String content = "export HBASE_HEAPSIZE=1000;\n\n" +
-            "export HBASE_LOG_DIR={{log_dir}}";
-    testGetUpdatedHbaseEnvProperties(content, null);
-  }
-
-  private void testGetUpdatedHbaseEnvProperties(String content, String expectedContent) {
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    UpgradeCatalog222 upgradeCatalog222 = injector.getInstance(UpgradeCatalog222.class);
-    Map<String, String> update = upgradeCatalog222.getUpdatedHbaseEnvProperties(content);
-    assertEquals(expectedContent, update.get("content"));
-  }
-
-  @Test
-  public void testUpdateHostRoleCommands() throws Exception {
-    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
-    dbAccessor.createIndex(eq("idx_hrc_status_role"), eq("host_role_command"), eq("status"), eq("role"));
-    expectLastCall().once();
-
-    replay(dbAccessor);
-
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-    UpgradeCatalog222 upgradeCatalog222 = injector.getInstance(UpgradeCatalog222.class);
-    upgradeCatalog222.updateHostRoleCommands();
-
-
-    verify(dbAccessor);
-  }
-
-  @Test
-  public void testUpdateAlerts_AtlasAlert() {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class);
-    final AlertDefinitionEntity atlasMetadataServerWebUIMock = easyMockSupport.createNiceMock(AlertDefinitionEntity.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-        bind(AlertDefinitionDAO.class).toInstance(mockAlertDefinitionDAO);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    long clusterId = 1;
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getClusterId()).andReturn(clusterId).anyTimes();
-    expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("metadata_server_webui")))
-            .andReturn(atlasMetadataServerWebUIMock).atLeastOnce();
-    expect(atlasMetadataServerWebUIMock.getSource()).andReturn("{\"uri\": {\n" +
-            "            \"http\": \"{{hostname}}:{{application-properties/atlas.server.http.port}}\",\n" +
-            "            \"https\": \"{{hostname}}:{{application-properties/atlas.server.https.port}}\" } }");
-
-    atlasMetadataServerWebUIMock.setSource("{\"uri\":{\"http\":\"{{application-properties/atlas.server.http.port}}\",\"https\":\"{{application-properties/atlas.server.https.port}}\"}}");
-    expectLastCall().once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog222.class).updateAlerts();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testUpdateCorruptedReplicaWidget() throws SQLException{
-    final DBAccessor dbAccessor = createStrictMock(DBAccessor.class);
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(DBAccessor.class).toInstance(dbAccessor);
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-        binder.bind(EntityManager.class).toInstance(entityManager);
-      }
-    };
-
-    Injector injector = Guice.createInjector(module);
-
-    String expectedWidgetUpdate = "UPDATE widget SET widget_name='%s', description='%s', " +
-      "widget_values='[{\"name\": \"%s\", \"value\": \"%s\"}]' WHERE widget_name='%s'";
-    Capture<String> capturedStatements = Capture.newInstance(CaptureType.ALL);
-
-    expect(dbAccessor.executeUpdate(capture(capturedStatements))).andReturn(1);
-
-    UpgradeCatalog222 upgradeCatalog222 = injector.getInstance(UpgradeCatalog222.class);
-    replay(dbAccessor);
-
-    upgradeCatalog222.updateCorruptedReplicaWidget();
-
-    List<String> statements = capturedStatements.getValues();
-
-    assertTrue(statements.contains(String.format(expectedWidgetUpdate,
-      UpgradeCatalog222.WIDGET_CORRUPT_REPLICAS,
-      UpgradeCatalog222.WIDGET_CORRUPT_REPLICAS_DESCRIPTION,
-      UpgradeCatalog222.WIDGET_CORRUPT_REPLICAS,
-      UpgradeCatalog222.WIDGET_VALUES_VALUE,
-      UpgradeCatalog222.WIDGET_CORRUPT_BLOCKS)));
-
-  }
-
-  @Test
-  public void testCreateNewSliderConfigVersion() throws AmbariException {
-    EasyMockSupport easyMockSupport = new EasyMockSupport();
-    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
-    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
-    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
-    final Service mockSliderService = easyMockSupport.createNiceMock(Service.class);
-
-    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
-      @Override
-      protected void configure() {
-        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
-        bind(Clusters.class).toInstance(mockClusters);
-        bind(EntityManager.class).toInstance(entityManager);
-        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    });
-
-    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
-    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
-      put("normal", mockClusterExpected);
-    }}).atLeastOnce();
-    expect(mockClusterExpected.getService("SLIDER")).andReturn(mockSliderService);
-    expect(mockClusterExpected.createServiceConfigVersion("SLIDER", "ambari-upgrade", "Creating new service config version for SLIDER service.", null)).andReturn(null).once();
-
-    easyMockSupport.replayAll();
-    mockInjector.getInstance(UpgradeCatalog222.class).createNewSliderConfigVersion();
-    easyMockSupport.verifyAll();
-  }
-
-  @Test
-  public void testInitializeStromAndKafkaWidgets() throws AmbariException {
-
-    String stormServiceName = "STORM";
-    String kafkaServiceName = "KAFKA";
-    String hbaseServiceName = "HBASE";
-
-    final AmbariManagementController controller = createStrictMock(AmbariManagementController.class);
-    final Clusters clusters = createStrictMock(Clusters.class);
-    final Cluster cluster = createStrictMock(Cluster.class);
-    final Service stormService = createStrictMock(Service.class);
-    final Service kafkaService = createStrictMock(Service.class);
-    final Service hbaseService = createStrictMock(Service.class);
-    final Map<String, Cluster> clusterMap = Collections.singletonMap("c1", cluster);
-    // Use a TreeMap so we can assume a particular order when iterating over the services.
-    final Map<String, Service> services = new TreeMap<>();
-    services.put(stormServiceName, stormService);
-    services.put(kafkaServiceName, kafkaService);
-    services.put(hbaseServiceName, hbaseService);
-
-
-    Module module = new Module() {
-      @Override
-      public void configure(Binder binder) {
-        binder.bind(AmbariManagementController.class).toInstance(controller);
-        binder.bind(Clusters.class).toInstance(clusters);
-        binder.bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
-        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
-      }
-    };
-
-    expect(controller.getClusters()).andReturn(clusters).anyTimes();
-    expect(clusters.getClusters()).andReturn(clusterMap).anyTimes();
-
-    expect(cluster.getServices()).andReturn(services).once();
-    expect(stormService.getName()).andReturn(stormServiceName).atLeastOnce();
-    expect(kafkaService.getName()).andReturn(kafkaServiceName).atLeastOnce();
-    expect(hbaseService.getName()).andReturn(hbaseServiceName).atLeastOnce();
-
-    controller.initializeWidgetsAndLayouts(cluster, kafkaService);
-    expectLastCall().once();
-    controller.initializeWidgetsAndLayouts(cluster, stormService);
-    expectLastCall().once();
-    // but no controller call for HBase
-
-    replay(controller, clusters, cluster, stormService, kafkaService, hbaseService);
-
-    Injector injector = Guice.createInjector(module);
-    injector.getInstance(UpgradeCatalog222.class).initializeStromAndKafkaWidgets();
-
-    verify(controller, clusters, cluster, stormService, kafkaService, hbaseService);
-  }
-
-}


[25/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
index 89d529c..06b6217 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -23,7 +23,6 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
-import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -35,33 +34,30 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ConcurrentSkipListMap;
 import java.util.concurrent.CopyOnWriteArrayList;
-import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReadWriteLock;
 
 import javax.annotation.Nullable;
 import javax.persistence.EntityManager;
 import javax.persistence.RollbackException;
 
+import org.apache.ambari.annotations.Experimental;
+import org.apache.ambari.annotations.ExperimentalFeature;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.ConfigGroupNotFoundException;
-import org.apache.ambari.server.DuplicateResourceException;
 import org.apache.ambari.server.ObjectNotFoundException;
 import org.apache.ambari.server.ParentObjectNotFoundException;
 import org.apache.ambari.server.RoleCommand;
 import org.apache.ambari.server.ServiceComponentHostNotFoundException;
 import org.apache.ambari.server.ServiceComponentNotFoundException;
 import org.apache.ambari.server.ServiceNotFoundException;
-import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.agent.ExecutionCommand.KeyNames;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.AmbariSessionManager;
 import org.apache.ambari.server.controller.ClusterResponse;
 import org.apache.ambari.server.controller.ConfigurationResponse;
 import org.apache.ambari.server.controller.MaintenanceStateHelper;
 import org.apache.ambari.server.controller.RootServiceResponseFactory.Services;
 import org.apache.ambari.server.controller.ServiceConfigVersionResponse;
-import org.apache.ambari.server.controller.internal.UpgradeResourceProvider;
 import org.apache.ambari.server.events.AmbariEvent.AmbariEventType;
 import org.apache.ambari.server.events.ClusterConfigChangedEvent;
 import org.apache.ambari.server.events.ClusterEvent;
@@ -78,26 +74,19 @@ import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
 import org.apache.ambari.server.orm.dao.AlertDispatchDAO;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.ClusterStateDAO;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.HostConfigMappingDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
-import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.ServiceConfigDAO;
 import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.dao.TopologyRequestDAO;
 import org.apache.ambari.server.orm.dao.UpgradeDAO;
 import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
-import org.apache.ambari.server.orm.entities.ClusterConfigMappingEntity;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
 import org.apache.ambari.server.orm.entities.ClusterStateEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupEntity;
-import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
-import org.apache.ambari.server.orm.entities.HostRoleCommandEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.PermissionEntity;
 import org.apache.ambari.server.orm.entities.PrivilegeEntity;
@@ -108,24 +97,19 @@ import org.apache.ambari.server.orm.entities.ServiceConfigEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.orm.entities.TopologyRequestEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
-import org.apache.ambari.server.orm.entities.UpgradeGroupEntity;
-import org.apache.ambari.server.orm.entities.UpgradeItemEntity;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
-import org.apache.ambari.server.security.authorization.AuthorizationHelper;
-import org.apache.ambari.server.serveraction.upgrades.UpdateDesiredStackAction;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.ClusterHealthReport;
 import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.ComponentInfo;
 import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.ConfigFactory;
 import org.apache.ambari.server.state.ConfigHelper;
 import org.apache.ambari.server.state.DesiredConfig;
 import org.apache.ambari.server.state.Host;
 import org.apache.ambari.server.state.HostHealthStatus;
-import org.apache.ambari.server.state.HostState;
 import org.apache.ambari.server.state.MaintenanceState;
 import org.apache.ambari.server.state.PropertyInfo;
+import org.apache.ambari.server.state.RepositoryType;
 import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.SecurityType;
 import org.apache.ambari.server.state.Service;
@@ -143,11 +127,9 @@ import org.apache.ambari.server.state.UpgradeContextFactory;
 import org.apache.ambari.server.state.configgroup.ConfigGroup;
 import org.apache.ambari.server.state.configgroup.ConfigGroupFactory;
 import org.apache.ambari.server.state.fsm.InvalidStateTransitionException;
+import org.apache.ambari.server.state.repository.VersionDefinitionXml;
 import org.apache.ambari.server.state.scheduler.RequestExecution;
 import org.apache.ambari.server.state.scheduler.RequestExecutionFactory;
-import org.apache.ambari.server.state.stack.upgrade.Direction;
-import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
-import org.apache.ambari.server.state.svccomphost.ServiceComponentHostSummary;
 import org.apache.ambari.server.topology.TopologyRequest;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang.StringUtils;
@@ -162,7 +144,6 @@ import com.google.common.collect.Iterables;
 import com.google.common.collect.ListMultimap;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Multimap;
-import com.google.common.collect.Sets;
 import com.google.common.eventbus.Subscribe;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
@@ -178,9 +159,6 @@ public class ClusterImpl implements Cluster {
    * Prefix for cluster session attributes name.
    */
   private static final String CLUSTER_SESSION_ATTRIBUTES_PREFIX = "cluster_session_attributes:";
-  private static final Set<RepositoryVersionState> ALLOWED_REPOSITORY_STATES =
-      EnumSet.of(RepositoryVersionState.INIT, RepositoryVersionState.INSTALLING,
-          RepositoryVersionState.INSTALLED);
 
   @Inject
   private Clusters clusters;
@@ -216,9 +194,6 @@ public class ClusterImpl implements Cluster {
 
   private final ReadWriteLock clusterGlobalLock;
 
-  // This is a lock for operations that do not need to be cluster global
-  private final Lock hostTransitionStateWriteLock;
-
   /**
    * The unique ID of the {@link @ClusterEntity}.
    */
@@ -233,12 +208,6 @@ public class ClusterImpl implements Cluster {
   private ClusterStateDAO clusterStateDAO;
 
   @Inject
-  private ClusterVersionDAO clusterVersionDAO;
-
-  @Inject
-  private HostRoleCommandDAO hostRoleCommandDAO;
-
-  @Inject
   private HostDAO hostDAO;
 
   @Inject
@@ -284,12 +253,6 @@ public class ClusterImpl implements Cluster {
   private UpgradeDAO upgradeDAO;
 
   @Inject
-  private RepositoryVersionDAO repositoryVersionDAO;
-
-  @Inject
-  private Configuration configuration;
-
-  @Inject
   private AmbariSessionManager sessionManager;
 
   @Inject
@@ -337,13 +300,6 @@ public class ClusterImpl implements Cluster {
    */
   private Map<String, String> m_clusterPropertyCache = new ConcurrentHashMap<>();
 
-  /**
-   * A simple cache of the effective cluster version. This is mainly used during
-   * upgrades to prevent calculating the value repeatedly. calculation of this
-   * during an upgrade is not very quick or clean, it's good to cache it.
-   */
-  private final Map<Long, Long> upgradeEffectiveVersionCache = new ConcurrentHashMap<>();
-
   @Inject
   public ClusterImpl(@Assisted ClusterEntity clusterEntity, Injector injector,
       AmbariEventPublisher eventPublisher)
@@ -355,7 +311,6 @@ public class ClusterImpl implements Cluster {
     injector.injectMembers(this);
 
     clusterGlobalLock = lockFactory.newReadWriteLock("clusterGlobalLock");
-    hostTransitionStateWriteLock = lockFactory.newLock("hostTransitionStateLock");
 
     loadStackVersion();
     loadServices();
@@ -434,7 +389,7 @@ public class ClusterImpl implements Cluster {
               new ConcurrentHashMap<String, ServiceComponentHost>());
         }
 
-        /** Get Service Host Components **/
+        // Get Service Host Components
         for (Entry<String, ServiceComponentHost> svchost : comp.getServiceComponentHosts().entrySet()) {
           String hostname = svchost.getKey();
           ServiceComponentHost svcHostComponent = svchost.getValue();
@@ -510,16 +465,13 @@ public class ClusterImpl implements Cluster {
       }
     }
 
-    LOG.debug("Adding a new Config group" + ", clusterName = "
-      + getClusterName() + ", groupName = " + configGroup.getName()
-      + ", tag = " + configGroup.getTag() + " with hosts " + hostList);
+    LOG.debug("Adding a new Config group, clusterName = {}, groupName = {}, tag = {} with hosts {}",
+      getClusterName(), configGroup.getName(), configGroup.getTag(), hostList);
 
     if (clusterConfigGroups.containsKey(configGroup.getId())) {
       // The loadConfigGroups will load all groups to memory
-      LOG.debug("Config group already exists" + ", clusterName = "
-          + getClusterName() + ", groupName = " + configGroup.getName()
-          + ", groupId = " + configGroup.getId() + ", tag = "
-          + configGroup.getTag());
+      LOG.debug("Config group already exists, clusterName = {}, groupName = {}, groupId = {}, tag = {}",
+        getClusterName(), configGroup.getName(), configGroup.getId(), configGroup.getTag());
     } else {
       clusterConfigGroups.put(configGroup.getId(), configGroup);
     }
@@ -554,9 +506,8 @@ public class ClusterImpl implements Cluster {
         + requestExecution.getId() + ", description = " + requestExecution.getDescription());
 
     if (requestExecutions.containsKey(requestExecution.getId())) {
-      LOG.debug(
-          "Request schedule already exists" + ", clusterName = " + getClusterName() + ", id = "
-              + requestExecution.getId() + ", description = " + requestExecution.getDescription());
+      LOG.debug("Request schedule already exists, clusterName = {}, id = {}, description = {}",
+        getClusterName(), requestExecution.getId(), requestExecution.getDescription());
     } else {
       requestExecutions.put(requestExecution.getId(), requestExecution);
     }
@@ -587,9 +538,8 @@ public class ClusterImpl implements Cluster {
       throw new ConfigGroupNotFoundException(getClusterName(), id.toString());
     }
 
-    LOG.debug("Deleting Config group" + ", clusterName = " + getClusterName()
-        + ", groupName = " + configGroup.getName() + ", groupId = "
-        + configGroup.getId() + ", tag = " + configGroup.getTag());
+    LOG.debug("Deleting Config group, clusterName = {}, groupName = {}, groupId = {}, tag = {}",
+      getClusterName(), configGroup.getName(), configGroup.getId(), configGroup.getTag());
 
     configGroup.delete();
     clusterConfigGroups.remove(id);
@@ -711,10 +661,8 @@ public class ClusterImpl implements Cluster {
     }
 
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Adding a new ServiceComponentHost" + ", clusterName="
-          + getClusterName() + ", clusterId=" + getClusterId()
-          + ", serviceName=" + serviceName + ", serviceComponentName"
-          + componentName + ", hostname= " + hostname);
+      LOG.debug("Adding a new ServiceComponentHost, clusterName={}, clusterId={}, serviceName={}, serviceComponentName{}, hostname= {}",
+        getClusterName(), getClusterId(), serviceName, componentName, hostname);
     }
 
     serviceComponentHosts.get(serviceName).get(componentName).put(hostname,
@@ -787,10 +735,8 @@ public class ClusterImpl implements Cluster {
     }
 
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Removing a ServiceComponentHost" + ", clusterName="
-          + getClusterName() + ", clusterId=" + getClusterId()
-          + ", serviceName=" + serviceName + ", serviceComponentName"
-          + componentName + ", hostname= " + hostname);
+      LOG.debug("Removing a ServiceComponentHost, clusterName={}, clusterId={}, serviceName={}, serviceComponentName{}, hostname= {}",
+        getClusterName(), getClusterId(), serviceName, componentName, hostname);
     }
 
     serviceComponentHosts.get(serviceName).get(componentName).remove(hostname);
@@ -875,20 +821,25 @@ public class ClusterImpl implements Cluster {
   @Override
   public void addService(Service service) {
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Adding a new Service" + ", clusterName=" + getClusterName() + ", clusterId="
-          + getClusterId() + ", serviceName=" + service.getName());
+      LOG.debug("Adding a new Service, clusterName={}, clusterId={}, serviceName={}", getClusterName(), getClusterId(), service.getName());
     }
     services.put(service.getName(), service);
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
-  public Service addService(String serviceName) throws AmbariException {
+  public Service addService(String serviceName, RepositoryVersionEntity repositoryVersion) throws AmbariException {
     if (services.containsKey(serviceName)) {
-      throw new AmbariException("Service already exists" + ", clusterName=" + getClusterName()
-          + ", clusterId=" + getClusterId() + ", serviceName=" + serviceName);
+      String message = MessageFormat.format("The {0} service already exists in {1}", serviceName,
+          getClusterName());
+
+      throw new AmbariException(message);
     }
 
-    Service service = serviceFactory.createNew(this, serviceName);
+    @Experimental(feature = ExperimentalFeature.PATCH_UPGRADES)
+    Service service = serviceFactory.createNew(this, serviceName, repositoryVersion);
     addService(service);
 
     return service;
@@ -910,24 +861,31 @@ public class ClusterImpl implements Cluster {
   }
 
   @Override
-  public StackId getDesiredStackVersion() {
-    return desiredStackVersion;
+  public Service getServiceByComponentName(String componentName) throws AmbariException {
+    for (Service service : services.values()) {
+      for (ServiceComponent component : service.getServiceComponents().values()) {
+        if (component.getName().equals(componentName)) {
+          return service;
+        }
+      }
+    }
+
+    throw new ServiceNotFoundException(getClusterName(), "component: " + componentName);
   }
 
+
   @Override
-  public void setDesiredStackVersion(StackId stackId) throws AmbariException {
-    setDesiredStackVersion(stackId, false);
+  public StackId getDesiredStackVersion() {
+    return desiredStackVersion;
   }
 
   @Override
-  public void setDesiredStackVersion(StackId stackId, boolean cascade) throws AmbariException {
+  public void setDesiredStackVersion(StackId stackId) throws AmbariException {
     clusterGlobalLock.writeLock().lock();
     try {
       if (LOG.isDebugEnabled()) {
-        LOG.debug("Changing DesiredStackVersion of Cluster" + ", clusterName="
-            + getClusterName() + ", clusterId=" + getClusterId()
-            + ", currentDesiredStackVersion=" + desiredStackVersion
-            + ", newDesiredStackVersion=" + stackId);
+        LOG.debug("Changing DesiredStackVersion of Cluster, clusterName={}, clusterId={}, currentDesiredStackVersion={}, newDesiredStackVersion={}",
+          getClusterName(), getClusterId(), desiredStackVersion, stackId);
       }
 
       desiredStackVersion = stackId;
@@ -938,19 +896,6 @@ public class ClusterImpl implements Cluster {
       clusterEntity.setDesiredStack(stackEntity);
       clusterEntity = clusterDAO.merge(clusterEntity);
 
-      if (cascade) {
-        for (Service service : getServices().values()) {
-          service.setDesiredStackVersion(stackId);
-
-          for (ServiceComponent sc : service.getServiceComponents().values()) {
-            sc.setDesiredStackVersion(stackId);
-
-            for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
-              sch.setDesiredStackVersion(stackId);
-            }
-          }
-        }
-      }
       loadServiceConfigTypes();
     } finally {
       clusterGlobalLock.writeLock().unlock();
@@ -1011,860 +956,109 @@ public class ClusterImpl implements Cluster {
   }
 
   /**
-   * Get the ClusterVersionEntity object whose state is CURRENT.
-   * @return
-   */
-  @Override
-  public ClusterVersionEntity getCurrentClusterVersion() {
-    Collection<ClusterVersionEntity> clusterVersionEntities = getClusterEntity().getClusterVersionEntities();
-    for (ClusterVersionEntity clusterVersionEntity : clusterVersionEntities) {
-      if (clusterVersionEntity.getState() == RepositoryVersionState.CURRENT) {
-        return clusterVersionEntity;
-      }
-    }
-    return null;
-  }
-
-  /**
    * {@inheritDoc}
    */
   @Override
-  public ClusterVersionEntity getEffectiveClusterVersion() throws AmbariException {
-    UpgradeEntity upgradeEntity = getUpgradeInProgress();
-    if (upgradeEntity == null) {
-      return getCurrentClusterVersion();
-    }
-
-    // see if this is in the cache first, and only walk the upgrade if it's not
-    Long upgradeId = upgradeEntity.getId();
-    Long effectiveClusterVersionId = upgradeEffectiveVersionCache.get(upgradeId);
-    if (null == effectiveClusterVersionId) {
-      boolean updateCache = true;
-      final ClusterVersionEntity effectiveClusterVersion;
-
-      if (upgradeEntity.getUpgradeType() != UpgradeType.NON_ROLLING) {
-        RepositoryVersionEntity repositoryVersion = upgradeEntity.getToRepositoryVersion();
-        effectiveClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(
-            clusterName, repositoryVersion.getStackId(), repositoryVersion.getVersion());
-      } else {
-        if (upgradeEntity.getDirection() == Direction.UPGRADE) {
-          HostRoleStatus stackActionStatus = getExpressUpgradeDesiredStackStatus(upgradeEntity);
-          boolean pastChangingStack = stackActionStatus == HostRoleStatus.COMPLETED;
-          updateCache = stackActionStatus != HostRoleStatus.IN_PROGRESS;
-
-          RepositoryVersionEntity repositoryVersion = pastChangingStack
-              ? upgradeEntity.getToRepositoryVersion() : upgradeEntity.getFromRepositoryVersion();
-
-          effectiveClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(clusterName,
-              repositoryVersion.getStackId(), repositoryVersion.getVersion());
-        } else {
-          // Should be the lower value during a Downgrade.
-          RepositoryVersionEntity repositoryVersion = upgradeEntity.getToRepositoryVersion();
-          effectiveClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(clusterName,
-              repositoryVersion.getStackId(), repositoryVersion.getVersion());
-        }
-      }
-
-      if (null != effectiveClusterVersion) {
-        effectiveClusterVersionId = effectiveClusterVersion.getId();
-      }
-
-      // cache for later use, but only if the action is completed
-      if (null != effectiveClusterVersion && updateCache) {
-        upgradeEffectiveVersionCache.put(upgradeId, effectiveClusterVersionId);
-      }
-    }
-
-    if (effectiveClusterVersionId == null) {
-      throw new AmbariException(
-          "Unable to determine which version to use during Stack Upgrade, effectiveVersion is null.");
-    }
-
-    // return cluster version which is "effective" given the upgrade state
-    return clusterVersionDAO.findByPK(effectiveClusterVersionId);
-  }
-
-  /**
-   * Gets the status of the {@link UpdateDesiredStackAction} for an express
-   * upgrade.
-   *
-   * @param upgrade
-   *          Stack Upgrade
-   * @return the status of the command
-   */
-  private HostRoleStatus getExpressUpgradeDesiredStackStatus(UpgradeEntity upgrade) {
-    for (UpgradeGroupEntity group : upgrade.getUpgradeGroups()) {
-      if (group.getName().equalsIgnoreCase(UpgradeResourceProvider.CONST_UPGRADE_GROUP_NAME)) {
-        for (UpgradeItemEntity item : group.getItems()) {
-          List<Long> taskIds = hostRoleCommandDAO.findTaskIdsByStage(upgrade.getRequestId(), item.getStageId());
-          List<HostRoleCommandEntity> commands = hostRoleCommandDAO.findByPKs(taskIds);
-          for (HostRoleCommandEntity command : commands) {
-            if (command.getCustomCommandName() != null &&
-                command.getCustomCommandName().equalsIgnoreCase(
-                    UpgradeResourceProvider.CONST_CUSTOM_COMMAND_NAME)) {
-              return command.getStatus();
-            }
-          }
-        }
-        return HostRoleStatus.PENDING;
-      }
-    }
-    return HostRoleStatus.PENDING;
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public void invalidateUpgradeEffectiveVersion() {
-    upgradeEffectiveVersionCache.clear();
-  }
-
-  /**
-   * Get all of the ClusterVersionEntity objects for the cluster.
-   * @return
-   */
-  @Override
-  public Collection<ClusterVersionEntity> getAllClusterVersions() {
-    return clusterVersionDAO.findByCluster(getClusterName());
-  }
-
-  /**
-   * During the Finalize Action, want to transition all Host Versions from INSTALLED to CURRENT, and the last CURRENT one to INSTALLED.
-   * @param hostNames Collection of host names
-   * @param currentClusterVersion Entity that contains the cluster's current stack (with its name and version)
-   * @param desiredState Desired state must be {@link RepositoryVersionState#CURRENT}
-   * @throws AmbariException
-   */
-  @Override
-  public void mapHostVersions(Set<String> hostNames, ClusterVersionEntity currentClusterVersion, RepositoryVersionState desiredState) throws AmbariException {
-    if (currentClusterVersion == null) {
-      throw new AmbariException("Could not find current stack version of cluster " + getClusterName());
-    }
-
-    final Set<RepositoryVersionState> validStates = Sets.newHashSet(RepositoryVersionState.CURRENT);
-
-    if (!validStates.contains(desiredState)) {
-      throw new AmbariException("The state must be one of [" + StringUtils.join(validStates, ", ") + "]");
-    }
-
-    clusterGlobalLock.writeLock().lock();
-    try {
-      StackEntity repoVersionStackEntity = currentClusterVersion.getRepositoryVersion().getStack();
-      StackId repoVersionStackId = new StackId(repoVersionStackEntity);
-
-      Map<String, HostVersionEntity> existingHostToHostVersionEntity = new HashMap<>();
-      List<HostVersionEntity> existingHostVersionEntities = hostVersionDAO.findByClusterStackAndVersion(
-        getClusterName(), repoVersionStackId,
-        currentClusterVersion.getRepositoryVersion().getVersion());
-
-      if (existingHostVersionEntities != null) {
-        for (HostVersionEntity entity : existingHostVersionEntities) {
-          existingHostToHostVersionEntity.put(entity.getHostName(), entity);
-        }
-      }
-
-      Sets.SetView<String> intersection = Sets.intersection(
-        existingHostToHostVersionEntity.keySet(), hostNames);
-
-      for (String hostname : hostNames) {
-        List<HostVersionEntity> currentHostVersions = hostVersionDAO.findByClusterHostAndState(
-            getClusterName(), hostname, RepositoryVersionState.CURRENT);
-        HostVersionEntity currentHostVersionEntity = (currentHostVersions != null && currentHostVersions.size() == 1) ? currentHostVersions.get(0)
-            : null;
-
-          // Notice that if any hosts already have the desired stack and version, regardless of the state, we try
-          // to be robust and only insert records for the missing hosts.
-          if (!intersection.contains(hostname)) {
-            // According to the business logic, we don't create objects in a CURRENT state.
-            HostEntity hostEntity = hostDAO.findByName(hostname);
-            HostVersionEntity hostVersionEntity = new HostVersionEntity(hostEntity, currentClusterVersion.getRepositoryVersion(), desiredState);
-            hostVersionDAO.create(hostVersionEntity);
-          } else {
-            HostVersionEntity hostVersionEntity = existingHostToHostVersionEntity.get(hostname);
-            if (hostVersionEntity.getState() != desiredState) {
-              hostVersionEntity.setState(desiredState);
-            hostVersionEntity = hostVersionDAO.merge(hostVersionEntity);
-            }
-
-          // Maintain the invariant that only one HostVersionEntity is allowed
-          // to have a state of CURRENT.
-          if (currentHostVersionEntity != null
-              && !currentHostVersionEntity.getRepositoryVersion().equals(
-                  hostVersionEntity.getRepositoryVersion())
-              && desiredState == RepositoryVersionState.CURRENT
-              && currentHostVersionEntity.getState() == RepositoryVersionState.CURRENT) {
-            currentHostVersionEntity.setState(RepositoryVersionState.INSTALLED);
-            hostVersionDAO.merge(currentHostVersionEntity);
-          }
-        }
-      }
-    } finally {
-      clusterGlobalLock.writeLock().unlock();
-    }
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public void transitionHosts(ClusterVersionEntity sourceClusterVersion,
-      RepositoryVersionState state) throws AmbariException {
-
-    if (sourceClusterVersion == null) {
-      throw new AmbariException("Could not find current stack version of cluster " + getClusterName());
-    }
-
-    if (state != sourceClusterVersion.getState()) {
-      throw new AmbariException("Unable to transition cluster hosts into " + state
-          + ". The only valid state is " + sourceClusterVersion.getState());
-    }
-
-    Map<String, Host> hosts = clusters.getHostsForCluster(getClusterName());
-    Set<String> existingHostsWithClusterStackAndVersion = new HashSet<>();
-    HashMap<String, HostVersionEntity> existingHostStackVersions = new HashMap<>();
-
-    clusterGlobalLock.writeLock().lock();
-    try {
-      StackEntity repoVersionStackEntity = sourceClusterVersion.getRepositoryVersion().getStack();
-      StackId repoVersionStackId = new StackId(repoVersionStackEntity);
-
-      List<HostVersionEntity> existingHostVersionEntities = hostVersionDAO.findByClusterStackAndVersion(
-          getClusterName(), repoVersionStackId,
-          sourceClusterVersion.getRepositoryVersion().getVersion());
-
-      // for each host that already has a stack and version, keep track of them
-      for (HostVersionEntity entity : existingHostVersionEntities) {
-        String hostName = entity.getHostName();
-        existingHostsWithClusterStackAndVersion.add(hostName);
-        existingHostStackVersions.put(hostName, entity);
-      }
-
-      // find any hosts that do not have the stack/repo version already
-      Sets.SetView<String> hostsMissingRepoVersion = Sets.difference(
-        hosts.keySet(), existingHostsWithClusterStackAndVersion);
-
-      createOrUpdateHostVersionToState(sourceClusterVersion, hosts,
-          existingHostStackVersions, hostsMissingRepoVersion, state);
-    } finally {
-      clusterGlobalLock.writeLock().unlock();
-    }
-  }
-
-  /**
-   * Moved out to a separate method due to performance reasons
-   * Iterates over all hosts and creates or transitions existing host versions
-   * to a given state. If host version for desired stack/version does not exist,
-   * host version is created and initialized to a given state. Otherwise, existing
-   * host version state is updated
-   * Hosts in maintenance mode are auto skipped.
-   *
-   * @param sourceClusterVersion cluster version to be queried for a stack
-   *                             name/version info when creating a new host version
-   * @param hosts list of all hosts
-   * @param existingHostStackVersions map of existing host versions to be updated
-   * @param hostsMissingRepoVersion set of hostnames of hosts that have no desired host version
-   * @param newState target host version state for transition
-   */
   @Transactional
-  void createOrUpdateHostVersionToState(ClusterVersionEntity sourceClusterVersion,
-      Map<String, Host> hosts, HashMap<String, HostVersionEntity> existingHostStackVersions,
-      Sets.SetView<String> hostsMissingRepoVersion, RepositoryVersionState newState) {
-
-    for (String hostname : hosts.keySet()) {
-      // start off with the requested new state for each host
-      RepositoryVersionState repositoryVersionState = newState;
-
-      // if the host is in maintenance mode, that's an explicit marker which
-      // indicates that it should not be transitioned to INSTALLING; instead
-      // they will be transitioned to OUT_OF_SYNC
-      Host host = hosts.get(hostname);
-      if (host.getMaintenanceState(getClusterId()) != MaintenanceState.OFF) {
-        repositoryVersionState = RepositoryVersionState.OUT_OF_SYNC;
-      }
-
-      if (hostsMissingRepoVersion.contains(hostname)) {
-        // Create new host stack version
-        HostEntity hostEntity = hostDAO.findByName(hostname);
-        HostVersionEntity hostVersionEntity = new HostVersionEntity(hostEntity,
-            sourceClusterVersion.getRepositoryVersion(), repositoryVersionState);
-
-        LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
-            hostVersionEntity.getHostName(), hostVersionEntity.getState(),
-            hostVersionEntity.getRepositoryVersion().getVersion(), hostVersionEntity.getRepositoryVersion().getId());
-
-        hostVersionDAO.create(hostVersionEntity);
-      } else {
-        // Update existing host stack version
-        HostVersionEntity hostVersionEntity = existingHostStackVersions.get(hostname);
-        hostVersionEntity.setState(repositoryVersionState);
-        hostVersionEntity = hostVersionDAO.merge(hostVersionEntity);
-      }
-    }
-  }
-
-  /**
-   * Calculate the effective Cluster Version State based on the state of its hosts.
-   *
-   * CURRENT: all hosts are CURRENT
-   * INSTALLED: all hosts in INSTALLED
-   * INSTALL_FAILED: at least one host in INSTALL_FAILED
-   * INSTALLING: all hosts in INSTALLING -or- INSTALLING and NOT_REQUIRED. Notice that if one host is CURRENT and another is INSTALLING, then the
-   * effective version will be OUT_OF_SYNC.
-   * OUT_OF_SYNC: otherwise
-   * @param stateToHosts Map from state to the collection of hosts with that state
-   * @return Return the effective Cluster Version State
-   */
-  private RepositoryVersionState getEffectiveState(Map<RepositoryVersionState, Set<String>> stateToHosts) {
-    if (stateToHosts == null || stateToHosts.size() < 1) {
-      return null;
-    }
-
-    int totalHosts = 0;
-    for (Set<String> hosts : stateToHosts.values()) {
-      totalHosts += hosts.size();
-    }
-
-    if (stateToHosts.containsKey(RepositoryVersionState.CURRENT) && stateToHosts.get(RepositoryVersionState.CURRENT).size() == totalHosts) {
-      return RepositoryVersionState.CURRENT;
-    }
-    if (stateToHosts.containsKey(RepositoryVersionState.INSTALLED) && stateToHosts.get(RepositoryVersionState.INSTALLED).size() == totalHosts) {
-      return RepositoryVersionState.INSTALLED;
-    }
-    if (stateToHosts.containsKey(RepositoryVersionState.INSTALL_FAILED) &&
-      !stateToHosts.get(RepositoryVersionState.INSTALL_FAILED).isEmpty()) {
-      // Installation failed on some host(s). But
-      // cluster version state should transition to Install Failed only after
-      // all hosts have finished installation. Otherwise, UI will misbehave
-      // (hide progress dialog before installation is finished)
-      if (! stateToHosts.containsKey(RepositoryVersionState.INSTALLING) ||
-        stateToHosts.get(RepositoryVersionState.INSTALLING).isEmpty()) {
-        return RepositoryVersionState.INSTALL_FAILED;
-      }
-    }
-
-    int totalInstalling = stateToHosts.containsKey(RepositoryVersionState.INSTALLING) ? stateToHosts.get(RepositoryVersionState.INSTALLING).size() : 0;
-    int totalInstalled = stateToHosts.containsKey(RepositoryVersionState.INSTALLED) ? stateToHosts.get(RepositoryVersionState.INSTALLED).size() : 0;
-    int totalNotRequired = stateToHosts.containsKey(RepositoryVersionState.NOT_REQUIRED) ? stateToHosts.get(RepositoryVersionState.NOT_REQUIRED).size() : 0;
-    int totalInstallFailed = stateToHosts.containsKey(RepositoryVersionState.INSTALL_FAILED) ? stateToHosts.get(RepositoryVersionState.INSTALL_FAILED).size() : 0;
-
-    if (totalInstalling + totalInstalled + totalInstallFailed == totalHosts) {
-      return RepositoryVersionState.INSTALLING;
-    }
-
-    if (totalNotRequired > 0) {
-
-      // !!! if all we have is NOT_REQUIRED and something else, the return should be the something else
-      if (2 == stateToHosts.size()) {
-
-        Map<RepositoryVersionState, Set<String>> map = Maps.filterKeys(stateToHosts,
-            new com.google.common.base.Predicate<RepositoryVersionState>() {
-              @Override
-              public boolean apply(RepositoryVersionState repoState) {
-                return repoState != RepositoryVersionState.NOT_REQUIRED;
-              }
-            });
-
-        // !!! better be true
-        if (1 == map.size()) {
-          return map.keySet().iterator().next();
-        } else {
-          LOG.warn("The mix of NON_REQUIRED hosts is unexpected: {}", stateToHosts);
-          return RepositoryVersionState.OUT_OF_SYNC;
-        }
-      }
-
-      // if any hosts are still installing, then cluster is INSTALLING
-      if (totalInstalling > 0) {
-        return RepositoryVersionState.INSTALLING;
-      }
+  public List<Host> transitionHostsToInstalling(RepositoryVersionEntity repoVersionEntity,
+      VersionDefinitionXml versionDefinitionXml, boolean forceInstalled) throws AmbariException {
 
-      // if any hosts are install_failed, then cluster is INSTALL_FAILED
-      if (totalInstallFailed > 0) {
-        return RepositoryVersionState.INSTALL_FAILED;
-      }
 
-      // should be covered by the 2-state check above
-      if (totalInstalled > 0) {
-        return RepositoryVersionState.INSTALLED;
-      }
-
-      // rare
-      if (totalNotRequired == totalHosts) {
-        return RepositoryVersionState.NOT_REQUIRED;
-      }
+    // the hosts to return so that INSTALL commands can be generated for them
+    final List<Host> hostsRequiringInstallation;
 
-    }
-
-    // Also returns when have a mix of CURRENT and INSTALLING|INSTALLED
-    LOG.warn("Have a mix of CURRENT and INSTALLING|INSTALLED host versions, " +
-      "returning OUT_OF_SYNC as cluster version. Host version states: {}", stateToHosts);
-    return RepositoryVersionState.OUT_OF_SYNC;
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public void recalculateClusterVersionState(RepositoryVersionEntity repositoryVersion) throws AmbariException {
-    if (repositoryVersion == null) {
-      return;
-    }
-
-    StackId stackId = repositoryVersion.getStackId();
-    String version = repositoryVersion.getVersion();
-
-    Map<String, Host> hosts = clusters.getHostsForCluster(getClusterName());
     clusterGlobalLock.writeLock().lock();
-
     try {
-      // Part 1, bootstrap cluster version if necessary.
-
-      ClusterVersionEntity clusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(
-          getClusterName(), stackId, version);
-
-      boolean performingInitialBootstrap = false;
-      if (clusterVersion == null) {
-        if (clusterVersionDAO.findByCluster(getClusterName()).isEmpty()) {
-          // During an Ambari Upgrade from 1.7.0 -> 2.0.0, the Cluster Version
-          // will not exist, so bootstrap it.
-          // This can still fail if the Repository Version has not yet been created,
-          // which can happen if the first HostComponentState to trigger this method
-          // cannot advertise a version.
-          performingInitialBootstrap = true;
-          createClusterVersionInternal(
-              stackId,
-              version,
-              AuthorizationHelper.getAuthenticatedName(configuration.getAnonymousAuditName()),
-              RepositoryVersionState.INSTALLING);
-          clusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(
-              getClusterName(), stackId, version);
-
-          if (clusterVersion == null) {
-            LOG.warn(String.format(
-                "Could not create a cluster version for cluster %s and stack %s using repo version %s",
-                getClusterName(), stackId.getStackId(), repositoryVersion));
-            return;
-          }
-        } else {
-          LOG.warn(String.format(
-              "Repository version %s not found for cluster %s",
-              repositoryVersion, getClusterName()));
-          return;
-        }
-      }
 
-      // Ignore if cluster version is CURRENT or UPGRADE_FAILED
-      if (clusterVersion.getState() != RepositoryVersionState.INSTALL_FAILED &&
-              clusterVersion.getState() != RepositoryVersionState.OUT_OF_SYNC &&
-              clusterVersion.getState() != RepositoryVersionState.INSTALLING &&
-              clusterVersion.getState() != RepositoryVersionState.INSTALLED &&
-              clusterVersion.getState() != RepositoryVersionState.INIT) {
-        // anything else is not supported as of now
-        return;
-      }
+      // get this once for easy lookup later
+      Map<String, Host> hosts = clusters.getHostsForCluster(getClusterName());
+      hostsRequiringInstallation = new ArrayList<>(hosts.size());
 
-      // Part 2, check for transitions.
-      Set<String> hostsWithoutHostVersion = new HashSet<>();
-      Map<RepositoryVersionState, Set<String>> stateToHosts = new HashMap<>();
+      // for every host, either create or update the host version to the right
+      // state - starting with STATE
+      Collection<HostEntity> hostEntities = getClusterEntity().getHostEntities();
 
-      //hack until better hostversion integration into in-memory cluster structure
+      for (HostEntity hostEntity : hostEntities) {
 
-      List<HostVersionEntity> hostVersionEntities =
-              hostVersionDAO.findByClusterStackAndVersion(getClusterName(), stackId, version);
-
-      Set<String> hostsWithState = new HashSet<>();
-      Set<String> hostsInMaintenanceState = new HashSet<>();
-      for (HostVersionEntity hostVersionEntity : hostVersionEntities) {
-        String hostname = hostVersionEntity.getHostEntity().getHostName();
-        Host host = hosts.get(hostname);
-        if(host != null && host.getMaintenanceState(getClusterId()) == MaintenanceState.ON) {
-          hostsInMaintenanceState.add(hostname);
-          continue;
+        // start with INSTALLING
+        RepositoryVersionState state = RepositoryVersionState.INSTALLING;
+        if (forceInstalled) {
+          state = RepositoryVersionState.INSTALLED;
         }
-        hostsWithState.add(hostname);
-        RepositoryVersionState hostState = hostVersionEntity.getState();
 
-        if (stateToHosts.containsKey(hostState)) {
-          stateToHosts.get(hostState).add(hostname);
-        } else {
-          Set<String> hostsInState = new HashSet<>();
-          hostsInState.add(hostname);
-          stateToHosts.put(hostState, hostsInState);
+        // is this host version not required b/c of versionable components
+        Host host = hosts.get(hostEntity.getHostName());
+        if (!host.hasComponentsAdvertisingVersions(desiredStackVersion)) {
+          state = RepositoryVersionState.NOT_REQUIRED;
         }
-      }
 
-      hostsWithoutHostVersion.addAll(hosts.keySet());
-      hostsWithoutHostVersion.removeAll(hostsWithState);
-      hostsWithoutHostVersion.removeAll(hostsInMaintenanceState);
-
-      // Ensure that all of the hosts without a Host Version only have
-      // Components that do not advertise a version.
-      // Otherwise, operations are still in progress.
-      for (String hostname : hostsWithoutHostVersion) {
-        HostEntity hostEntity = hostDAO.findByName(hostname);
-
-        // During initial bootstrap, unhealthy hosts are ignored
-        // so we boostrap the CURRENT version anyway
-        if (performingInitialBootstrap &&
-                hostEntity.getHostStateEntity().getCurrentState() != HostState.HEALTHY) {
-          continue;
-        }
+        // if the repository is still required, check against the repo type
+        if (state != RepositoryVersionState.NOT_REQUIRED) {
+          if (repoVersionEntity.getType() != RepositoryType.STANDARD) {
+            // does the host gets a different repo state based on VDF and repo
+            // type
+            boolean hostRequiresRepository = false;
+            Set<String> servicesInRepository = versionDefinitionXml.getAvailableServiceNames();
+
+            List<ServiceComponentHost> schs = getServiceComponentHosts(hostEntity.getHostName());
+            for (ServiceComponentHost serviceComponentHost : schs) {
+              String serviceName = serviceComponentHost.getServiceName();
+              if (servicesInRepository.contains(serviceName)) {
+                hostRequiresRepository = true;
+                break;
+              }
+            }
 
-        final Collection<HostComponentStateEntity> allHostComponents = hostEntity.getHostComponentStateEntities();
-
-        for (HostComponentStateEntity hostComponentStateEntity : allHostComponents) {
-          if (hostComponentStateEntity.getVersion().equalsIgnoreCase(
-              State.UNKNOWN.toString())) {
-            // Some Components cannot advertise a version. E.g., ZKF, AMBARI_METRICS,
-            // Kerberos
-            ComponentInfo compInfo = ambariMetaInfo.getComponent(
-                stackId.getStackName(), stackId.getStackVersion(),
-                hostComponentStateEntity.getServiceName(),
-                hostComponentStateEntity.getComponentName());
-
-            if (compInfo.isVersionAdvertised()) {
-              LOG.debug("Skipping transitioning the cluster version because host "
-                  + hostname + " does not have a version yet.");
-              return;
+            // if not required, then move onto the next host
+            if (!hostRequiresRepository) {
+              state = RepositoryVersionState.NOT_REQUIRED;
             }
           }
         }
-      }
-
-      RepositoryVersionState effectiveClusterVersionState = getEffectiveState(stateToHosts);
-
-      if (effectiveClusterVersionState != null
-          && effectiveClusterVersionState != clusterVersion.getState()) {
-        // Any mismatch will be caught while transitioning, and raise an
-        // exception.
-        try {
-          transitionClusterVersion(stackId, version,
-              effectiveClusterVersionState);
-        } catch (AmbariException e) {
-          ;
-        }
-      }
-    } finally {
-      clusterGlobalLock.writeLock().unlock();
-    }
-  }
-
-  /**
-   * Transition the Host Version across states.
-   * @param host Host object
-   * @param repositoryVersion Repository Version with stack and version information
-   * @param stack Stack information
-   * @throws AmbariException
-   */
-  @Override
-  @Transactional
-  public HostVersionEntity transitionHostVersionState(HostEntity host, final RepositoryVersionEntity repositoryVersion, final StackId stack) throws AmbariException {
-    StackEntity repoVersionStackEntity = repositoryVersion.getStack();
-    StackId repoVersionStackId = new StackId(repoVersionStackEntity);
-
-    HostVersionEntity hostVersionEntity = hostVersionDAO.findByClusterStackVersionAndHost(
-      getClusterId(), repoVersionStackId, repositoryVersion.getVersion(),
-      host.getHostId());
-
-    hostTransitionStateWriteLock.lock();
-    try {
-      // Create one if it doesn't already exist. It will be possible to make further transitions below.
-      boolean performingInitialBootstrap = false;
-      if (hostVersionEntity == null) {
-        if (hostVersionDAO.findByClusterAndHost(getClusterName(), host.getHostName()).isEmpty()) {
-          // That is an initial bootstrap
-          performingInitialBootstrap = true;
-        }
-        hostVersionEntity = new HostVersionEntity(host, repositoryVersion, RepositoryVersionState.INSTALLING);
-
-        LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
-            hostVersionEntity.getHostName(), hostVersionEntity.getState(),
-            hostVersionEntity.getRepositoryVersion().getVersion(), hostVersionEntity.getRepositoryVersion().getId());
-
-        hostVersionDAO.create(hostVersionEntity);
-      }
-
-      HostVersionEntity currentVersionEntity = hostVersionDAO.findByHostAndStateCurrent(getClusterId(), host.getHostId());
-      boolean isCurrentPresent = (currentVersionEntity != null);
-      final ServiceComponentHostSummary hostSummary = new ServiceComponentHostSummary(ambariMetaInfo, host, stack);
-
-      if (!isCurrentPresent) {
-        // Transition from UPGRADING -> CURRENT. This is allowed because Host Version Entity is bootstrapped in an UPGRADING state.
-        // Alternatively, transition to CURRENT during initial bootstrap if at least one host component advertised a version
-        if (hostSummary.isUpgradeFinished() || performingInitialBootstrap) {
-          hostVersionEntity.setState(RepositoryVersionState.CURRENT);
-          hostVersionEntity = hostVersionDAO.merge(hostVersionEntity);
-        }
-      } else {
-        // Handle transitions during a Stack Upgrade
-        if (hostSummary.isUpgradeFinished() && hostVersionEntity.getState().equals(RepositoryVersionState.INSTALLED)) {
-          currentVersionEntity.setState(RepositoryVersionState.INSTALLED);
-          hostVersionEntity.setState(RepositoryVersionState.CURRENT);
-
-          hostVersionDAO.merge(currentVersionEntity);
-          hostVersionEntity = hostVersionDAO.merge(hostVersionEntity);
-        }
-      }
-    } finally {
-      hostTransitionStateWriteLock.unlock();
-    }
-    return hostVersionEntity;
-  }
-
-  @Override
-  public void recalculateAllClusterVersionStates() throws AmbariException {
-    clusterGlobalLock.writeLock().lock();
-    try {
-      List<ClusterVersionEntity> clusterVersionEntities = clusterVersionDAO.findByCluster(getClusterName());
-      StackId currentStackId = getCurrentStackVersion();
-      for (ClusterVersionEntity clusterVersionEntity : clusterVersionEntities) {
-        RepositoryVersionEntity repositoryVersionEntity = clusterVersionEntity.getRepositoryVersion();
-        StackId repoVersionStackId = repositoryVersionEntity.getStackId();
-
-        if (repoVersionStackId.equals(currentStackId)
-            && clusterVersionEntity.getState() != RepositoryVersionState.CURRENT) {
-          recalculateClusterVersionState(clusterVersionEntity.getRepositoryVersion());
-        }
-      }
-    } finally {
-      clusterGlobalLock.writeLock().unlock();
-    }
-  }
-
-  @Override
-  public void createClusterVersion(StackId stackId, String version,
-      String userName, RepositoryVersionState state) throws AmbariException {
-    clusterGlobalLock.writeLock().lock();
-    try {
-      createClusterVersionInternal(stackId, version, userName, state);
-    } finally {
-      clusterGlobalLock.writeLock().unlock();
-    }
-  }
-
-  /**
-   * See {@link #createClusterVersion}
-   *
-   * This method is intended to be called only when cluster lock is already acquired.
-   */
-  private void createClusterVersionInternal(StackId stackId, String version,
-      String userName, RepositoryVersionState state) throws AmbariException {
-    if (!ALLOWED_REPOSITORY_STATES.contains(state)) {
-      throw new AmbariException("The allowed state for a new cluster version must be within " + ALLOWED_REPOSITORY_STATES);
-    }
-
-    ClusterVersionEntity existing = clusterVersionDAO.findByClusterAndStackAndVersion(
-      getClusterName(), stackId, version);
-    if (existing != null) {
-      throw new DuplicateResourceException(
-          "Duplicate item, a cluster version with stack=" + stackId
-              + ", version=" +
-          version + " for cluster " + getClusterName() + " already exists");
-    }
-
-    RepositoryVersionEntity repositoryVersionEntity = repositoryVersionDAO.findByStackAndVersion(
-      stackId, version);
-    if (repositoryVersionEntity == null) {
-      LOG.warn("Could not find repository version for stack=" + stackId
-          + ", version=" + version);
-      return;
-    }
-
-    ClusterEntity clusterEntity = getClusterEntity();
-    ClusterVersionEntity clusterVersionEntity = new ClusterVersionEntity(
-        clusterEntity, repositoryVersionEntity, state,
-      System.currentTimeMillis(), System.currentTimeMillis(), userName);
-    clusterVersionDAO.create(clusterVersionEntity);
-    clusterEntity.getClusterVersionEntities().add(clusterVersionEntity);
-    clusterEntity = clusterDAO.merge(clusterEntity);
-  }
-
-  /**
-   * Transition an existing cluster version from one state to another. The
-   * following are some of the steps that are taken when transitioning between
-   * specific states:
-   * <ul>
-   * <li>INSTALLING/INSTALLED --> CURRENT</lki>: Set the current stack to the
-   * desired stack, ensure all hosts with the desired stack are CURRENT as well.
-   * </ul>
-   * <li>INSTALLING/INSTALLED --> CURRENT</lki>: Set the current stack to the
-   * desired stack. </ul>
-   *
-   * @param stackId
-   *          Stack ID
-   * @param version
-   *          Stack version
-   * @param state
-   *          Desired state
-   * @throws AmbariException
-   */
-  @Override
-  @Transactional
-  public void transitionClusterVersion(StackId stackId, String version,
-      RepositoryVersionState state) throws AmbariException {
-    Set<RepositoryVersionState> allowedStates = new HashSet<>();
-    clusterGlobalLock.writeLock().lock();
-    try {
-      ClusterEntity clusterEntity = getClusterEntity();
-      ClusterVersionEntity existingClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(
-          getClusterName(), stackId, version);
-
-      if (existingClusterVersion == null) {
-        throw new AmbariException("Existing cluster version not found for cluster="
-            + getClusterName() + ", stack=" + stackId + ", version=" + version);
-      }
-
-      // NOOP
-      if (existingClusterVersion.getState() == state) {
-        return;
-      }
 
-      switch (existingClusterVersion.getState()) {
-        case CURRENT:
-          // If CURRENT state is changed here cluster will not have CURRENT
-          // state.
-          // CURRENT state will be changed to INSTALLED when another CURRENT
-          // state is added.
-          // allowedStates.add(RepositoryVersionState.INSTALLED);
-          break;
-        case INSTALLING:
-          allowedStates.add(RepositoryVersionState.INSTALLED);
-          allowedStates.add(RepositoryVersionState.INSTALL_FAILED);
-          allowedStates.add(RepositoryVersionState.OUT_OF_SYNC);
-          if (clusterVersionDAO.findByClusterAndStateCurrent(getClusterName()) == null) {
-            allowedStates.add(RepositoryVersionState.CURRENT);
+        // last check if it's still required - check for MM
+        if (state != RepositoryVersionState.NOT_REQUIRED) {
+          if (host.getMaintenanceState(clusterId) != MaintenanceState.OFF) {
+            state = RepositoryVersionState.OUT_OF_SYNC;
           }
-          break;
-        case INSTALL_FAILED:
-          allowedStates.add(RepositoryVersionState.INSTALLING);
-          break;
-        case INSTALLED:
-          allowedStates.add(RepositoryVersionState.INSTALLING);
-          allowedStates.add(RepositoryVersionState.OUT_OF_SYNC);
-          allowedStates.add(RepositoryVersionState.CURRENT);
-          break;
-        case OUT_OF_SYNC:
-          allowedStates.add(RepositoryVersionState.INSTALLING);
-          break;
-        case INIT:
-          allowedStates.add(RepositoryVersionState.CURRENT);
-          break;
-      }
-
-      if (!allowedStates.contains(state)) {
-        throw new AmbariException("Invalid cluster version transition from "
-            + existingClusterVersion.getState() + " to " + state);
-      }
-
-      // There must be at most one cluster version whose state is CURRENT at
-      // all times.
-      if (state == RepositoryVersionState.CURRENT) {
-        ClusterVersionEntity currentVersion = clusterVersionDAO.findByClusterAndStateCurrent(
-            getClusterName());
-        if (currentVersion != null) {
-          currentVersion.setState(RepositoryVersionState.INSTALLED);
-          currentVersion = clusterVersionDAO.merge(currentVersion);
         }
-      }
 
-      existingClusterVersion.setState(state);
-      existingClusterVersion.setEndTime(System.currentTimeMillis());
-      existingClusterVersion = clusterVersionDAO.merge(existingClusterVersion);
-
-      if (state == RepositoryVersionState.CURRENT) {
-        for (HostEntity hostEntity : clusterEntity.getHostEntities()) {
-          if (hostHasReportables(existingClusterVersion.getRepositoryVersion(), hostEntity)) {
-            continue;
-          }
-
-          Collection<HostVersionEntity> versions = hostVersionDAO.findByHost(
-              hostEntity.getHostName());
-
-          HostVersionEntity target = null;
-          if (null != versions) {
-            // Set anything that was previously marked CURRENT as INSTALLED, and
-            // the matching version as CURRENT
-            for (HostVersionEntity entity : versions) {
-              if (entity.getRepositoryVersion().getId().equals(
-                  existingClusterVersion.getRepositoryVersion().getId())) {
-                target = entity;
-                target.setState(state);
-                target = hostVersionDAO.merge(target);
-              } else if (entity.getState() == RepositoryVersionState.CURRENT) {
-                entity.setState(RepositoryVersionState.INSTALLED);
-                entity = hostVersionDAO.merge(entity);
-              }
-            }
+        // now that the correct state is determdined for the host version,
+        // either update or create it
+        HostVersionEntity hostVersionEntity = null;
+        Collection<HostVersionEntity> hostVersions = hostEntity.getHostVersionEntities();
+        for (HostVersionEntity existingHostVersion : hostVersions) {
+          if (existingHostVersion.getRepositoryVersion().getId() == repoVersionEntity.getId()) {
+            hostVersionEntity = existingHostVersion;
+            break;
           }
+        }
 
-          if (null == target) {
-            // If no matching version was found, create one with the desired
-            // state
-            HostVersionEntity hve = new HostVersionEntity(hostEntity,
-                existingClusterVersion.getRepositoryVersion(), state);
-
-            LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
-                hve.getHostName(), hve.getState(), hve.getRepositoryVersion().getVersion(),
-                hve.getRepositoryVersion().getId());
+        if (null == hostVersionEntity) {
+          hostVersionEntity = new HostVersionEntity(hostEntity, repoVersionEntity, state);
+          hostVersionDAO.create(hostVersionEntity);
 
-            hostVersionDAO.create(hve);
-          }
+          // bi-directional association update
+          hostVersions.add(hostVersionEntity);
+          hostDAO.merge(hostEntity);
+        } else {
+          hostVersionEntity.setState(state);
+          hostVersionEntity = hostVersionDAO.merge(hostVersionEntity);
         }
 
-        // when setting the cluster's state to current, we must also
-        // bring the desired stack and current stack in line with each other
-        StackEntity desiredStackEntity = clusterEntity.getDesiredStack();
-        StackId desiredStackId = new StackId(desiredStackEntity);
-
-        // if the desired stack ID doesn't match the target when setting the
-        // cluster to CURRENT, then there's a problem
-        if (!desiredStackId.equals(stackId)) {
-          String message = MessageFormat.format(
-              "The desired stack ID {0} must match {1} when transitioning the cluster''s state to {2}",
-              desiredStackId, stackId, RepositoryVersionState.CURRENT);
+        LOG.info("Created host version for {}, state={}, repository version={} (repo_id={})",
+            hostVersionEntity.getHostName(), hostVersionEntity.getState(),
+            repoVersionEntity.getVersion(), repoVersionEntity.getId());
 
-          throw new AmbariException(message);
+        if (state == RepositoryVersionState.INSTALLING) {
+          hostsRequiringInstallation.add(host);
         }
-
-        setCurrentStackVersion(stackId);
       }
-    } catch (RollbackException e) {
-      String message = MessageFormat.format(
-        "Unable to transition stack {0} at version {1} for cluster {2} to state {3}",
-        stackId, version, getClusterName(), state);
-
-      LOG.warn(message);
-      throw new AmbariException(message, e);
-
     } finally {
       clusterGlobalLock.writeLock().unlock();
     }
 
-  }
-
-  /**
-   * Checks if the host has any components reporting version information.
-   * @param repoVersion the repo version
-   * @param host        the host entity
-   * @return {@code true} if the host has any component that report version
-   * @throws AmbariException
-   */
-  private boolean hostHasReportables(RepositoryVersionEntity repoVersion, HostEntity host)
-      throws AmbariException {
-
-    for (HostComponentStateEntity hcse : host.getHostComponentStateEntities()) {
-      ComponentInfo ci = ambariMetaInfo.getComponent(
-          repoVersion.getStackName(),
-          repoVersion.getStackVersion(),
-          hcse.getServiceName(),
-          hcse.getComponentName());
-
-      if (ci.isVersionAdvertised()) {
-        return true;
-      }
-    }
-    return false;
+    return hostsRequiringInstallation;
   }
 
   @Override
@@ -2223,40 +1417,39 @@ public class ClusterImpl implements Cluster {
     try {
       Map<String, Set<DesiredConfig>> map = new HashMap<>();
       Collection<String> types = new HashSet<>();
-      Collection<ClusterConfigMappingEntity> entities = getClusterEntity().getConfigMappingEntities();
-
-      for (ClusterConfigMappingEntity e : entities) {
-        if (allVersions || e.isSelected() > 0) {
-          DesiredConfig c = new DesiredConfig();
-          c.setServiceName(null);
-          c.setTag(e.getTag());
-          c.setUser(e.getUser());
-          if(!allConfigs.containsKey(e.getType())) {
-            LOG.error("Config inconsistency exists:" +
-                " unknown configType=" + e.getType());
+      Collection<ClusterConfigEntity> entities = getClusterEntity().getClusterConfigEntities();
+
+      for (ClusterConfigEntity configEntity : entities) {
+        if (allVersions || configEntity.isSelected()) {
+          DesiredConfig desiredConfig = new DesiredConfig();
+          desiredConfig.setServiceName(null);
+          desiredConfig.setTag(configEntity.getTag());
+
+          if (!allConfigs.containsKey(configEntity.getType())) {
+            LOG.error("An inconsistency exists for configuration {}", configEntity.getType());
             continue;
           }
 
-          Map<String, Config> configMap = allConfigs.get(e.getType());
-          if(!configMap.containsKey(e.getTag())) {
-            LOG.debug("Config inconsistency exists for typeName=" +
-                    e.getType() +
-                    ", unknown versionTag=" + e.getTag());
+          Map<String, Config> configMap = allConfigs.get(configEntity.getType());
+          if(!configMap.containsKey(configEntity.getTag())) {
+            LOG.error("An inconsistency exists for the configuration {} with tag {}",
+                configEntity.getType(), configEntity.getTag());
+
             continue;
           }
 
-          Config config = configMap.get(e.getTag());
-          c.setVersion(config.getVersion());
+          Config config = configMap.get(configEntity.getTag());
+          desiredConfig.setVersion(config.getVersion());
 
-          Set<DesiredConfig> configs = map.get(e.getType());
+          Set<DesiredConfig> configs = map.get(configEntity.getType());
           if (configs == null) {
             configs = new HashSet<>();
           }
 
-          configs.add(c);
+          configs.add(desiredConfig);
 
-          map.put(e.getType(), configs);
-          types.add(e.getType());
+          map.put(configEntity.getType(), configs);
+          types.add(configEntity.getType());
         }
       }
 
@@ -2325,12 +1518,20 @@ public class ClusterImpl implements Cluster {
       long nextServiceConfigVersion = serviceConfigDAO.findNextServiceConfigVersion(clusterId,
           serviceName);
 
+      // get the correct stack ID to use when creating the service config
+      StackEntity stackEntity = clusterEntity.getDesiredStack();
+      Service service = services.get(serviceName);
+      if (null != service) {
+        StackId serviceStackId = service.getDesiredStackId();
+        stackEntity = stackDAO.find(serviceStackId);
+      }
+
       serviceConfigEntity.setServiceName(serviceName);
       serviceConfigEntity.setClusterEntity(clusterEntity);
       serviceConfigEntity.setVersion(nextServiceConfigVersion);
       serviceConfigEntity.setUser(user);
       serviceConfigEntity.setNote(note);
-      serviceConfigEntity.setStack(clusterEntity.getDesiredStack());
+      serviceConfigEntity.setStack(stackEntity);
 
       serviceConfigDAO.create(serviceConfigEntity);
       if (configGroup != null) {
@@ -2592,18 +1793,18 @@ public class ClusterImpl implements Cluster {
       throw new ObjectNotFoundException("Service config version with serviceName={} and version={} not found");
     }
 
-    //disable all configs related to service
+    // disable all configs related to service
     if (serviceConfigEntity.getGroupId() == null) {
       Collection<String> configTypes = serviceConfigTypes.get(serviceName);
-      List<ClusterConfigMappingEntity> mappingEntities =
-          clusterDAO.getSelectedConfigMappingByTypes(getClusterId(), new ArrayList<>(configTypes));
-      for (ClusterConfigMappingEntity entity : mappingEntities) {
-        entity.setSelected(0);
-        clusterDAO.mergeConfigMapping(entity);
+      List<ClusterConfigEntity> enabledConfigs = clusterDAO.getEnabledConfigsByTypes(clusterId, configTypes);
+      for (ClusterConfigEntity enabledConfig : enabledConfigs) {
+        enabledConfig.setSelected(false);
+        clusterDAO.merge(enabledConfig);
       }
 
       for (ClusterConfigEntity configEntity : serviceConfigEntity.getClusterConfigEntities()) {
-        selectConfig(configEntity.getType(), configEntity.getTag(), user);
+        configEntity.setSelected(true);
+        clusterDAO.merge(configEntity);
       }
     } else {
       Long configGroupId = serviceConfigEntity.getGroupId();
@@ -2656,32 +1857,6 @@ public class ClusterImpl implements Cluster {
   }
 
   @Transactional
-  void selectConfig(String type, String tag, String user) {
-    Collection<ClusterConfigMappingEntity> entities =
-      clusterDAO.getLatestClusterConfigMappingsEntityByType(getClusterId(), type);
-
-    //disable previous config
-    for (ClusterConfigMappingEntity e : entities) {
-      e.setSelected(0);
-      clusterDAO.mergeConfigMapping(e);
-    }
-
-    ClusterEntity clusterEntity = getClusterEntity();
-    ClusterConfigMappingEntity entity = new ClusterConfigMappingEntity();
-    entity.setClusterEntity(clusterEntity);
-    entity.setClusterId(clusterEntity.getClusterId());
-    entity.setCreateTimestamp(System.currentTimeMillis());
-    entity.setSelected(1);
-    entity.setUser(user);
-    entity.setType(type);
-    entity.setTag(tag);
-    clusterDAO.persistConfigMapping(entity);
-
-    clusterEntity.getConfigMappingEntities().add(entity);
-    clusterDAO.merge(clusterEntity);
-  }
-
-  @Transactional
   ServiceConfigVersionResponse applyConfigs(Set<Config> configs, String user, String serviceConfigVersionNote) {
 
     String serviceName = null;
@@ -2705,10 +1880,25 @@ public class ClusterImpl implements Cluster {
       }
     }
 
+    // update the selected flag for every config type
+    ClusterEntity clusterEntity = getClusterEntity();
+    Collection<ClusterConfigEntity> clusterConfigs = clusterEntity.getClusterConfigEntities();
     for (Config config: configs) {
-      selectConfig(config.getType(), config.getTag(), user);
+      for (ClusterConfigEntity clusterConfigEntity : clusterConfigs) {
+        // unset for this config type
+        if (StringUtils.equals(clusterConfigEntity.getType(), config.getType())) {
+          clusterConfigEntity.setSelected(false);
+
+          // unless both the tag and type match, then enable it
+          if (StringUtils.equals(clusterConfigEntity.getTag(), config.getTag())) {
+            clusterConfigEntity.setSelected(true);
+          }
+        }
+      }
     }
 
+    clusterEntity = clusterDAO.merge(clusterEntity);
+
     if (serviceName == null) {
       ArrayList<String> configTypes = new ArrayList<>();
       for (Config config: configs) {
@@ -2730,29 +1920,23 @@ public class ClusterImpl implements Cluster {
 
   private List<ClusterConfigEntity> getClusterConfigEntitiesByService(String serviceName) {
     Collection<String> configTypes = serviceConfigTypes.get(serviceName);
-    return clusterDAO.getLatestClusterConfigsByTypes(getClusterId(), new ArrayList<>(configTypes));
+    return clusterDAO.getEnabledConfigsByTypes(getClusterId(), new ArrayList<>(configTypes));
   }
 
   @Override
   public Config getDesiredConfigByType(String configType) {
-    List<ClusterConfigMappingEntity> entities = clusterDAO.getLatestClusterConfigMappingsEntityByType(getClusterId(), configType);
-    if (!entities.isEmpty()) {
-      return getConfig(configType, entities.get(0).getTag());
+    ClusterConfigEntity config = clusterDAO.findEnabledConfigByType(getClusterId(), configType);
+    if (null == config) {
+      return null;
     }
 
-    return null;
+    return getConfig(configType, config.getTag());
   }
 
   @Override
   public boolean isConfigTypeExists(String configType) {
-    for (ClusterConfigMappingEntity e : clusterDAO.getClusterConfigMappingEntitiesByCluster(
-        getClusterId())) {
-      if (e.getType().equals(configType)) {
-        return true;
-      }
-    }
-
-    return false;
+    ClusterConfigEntity config = clusterDAO.findEnabledConfigByType(getClusterId(), configType);
+    return null != config;
   }
 
   @Override
@@ -2775,7 +1959,6 @@ public class ClusterImpl implements Cluster {
       DesiredConfig desiredConfig = new DesiredConfig();
       desiredConfig.setTag(mappingEntity.getVersion());
       desiredConfig.setServiceName(mappingEntity.getServiceName());
-      desiredConfig.setUser(mappingEntity.getUser());
 
       desiredConfigsByHost.get(mappingEntity.getHostId()).put(mappingEntity.getType(), desiredConfig);
     }
@@ -3150,53 +2333,74 @@ public class ClusterImpl implements Cluster {
    */
   @Override
   @Transactional
-  public void applyLatestConfigurations(StackId stackId) {
+  public void applyLatestConfigurations(StackId stackId, String serviceName) {
     clusterGlobalLock.writeLock().lock();
 
     try {
+      // grab all of the configurations and hash them so we can easily update them when picking and choosing only those from the service
       ClusterEntity clusterEntity = getClusterEntity();
-      Collection<ClusterConfigMappingEntity> configMappingEntities = clusterEntity.getConfigMappingEntities();
-
-      // hash them for easier retrieval later - these are the same entity
-      // instances which exist on the cluster entity, so modification of the CCM
-      // entity here will affect the cluster CCM entities as well
-      ImmutableMap<Object, ClusterConfigMappingEntity> ccmMap = Maps.uniqueIndex(configMappingEntities, Functions.identity());
-
-      // disable all configs
-      for (ClusterConfigMappingEntity e : configMappingEntities) {
-        LOG.debug("{} with tag {} is unselected", e.getType(), e.getTag());
-        e.setSelected(0);
+      Collection<ClusterConfigEntity> configEntities = clusterEntity.getClusterConfigEntities();
+      ImmutableMap<Object, ClusterConfigEntity> clusterConfigEntityMap = Maps.uniqueIndex(
+          configEntities, Functions.identity());
+
+      // find the latest configurations for the service
+      Set<String> configTypesForService = new HashSet<>();
+      List<ServiceConfigEntity> latestServiceConfigs = serviceConfigDAO.getLastServiceConfigsForService(
+          getClusterId(), serviceName);
+
+      // process the current service configurations
+      for (ServiceConfigEntity serviceConfig : latestServiceConfigs) {
+        List<ClusterConfigEntity> latestConfigs = serviceConfig.getClusterConfigEntities();
+        for( ClusterConfigEntity latestConfig : latestConfigs ){
+          // grab the hash'd entity from the map so we're working with the right one
+          latestConfig = clusterConfigEntityMap.get(latestConfig);
+
+          // add the config type to our list for tracking later on
+          configTypesForService.add(latestConfig.getType());
+
+          // un-select the latest configuration for the service
+          LOG.debug("Disabling configuration {} with tag {}", latestConfig.getType(), latestConfig.getTag());
+          latestConfig.setSelected(false);
+        }
       }
 
-      // work through the in-memory list, finding only the most recent mapping per type
-      Collection<ClusterConfigMappingEntity> latestConfigMappingByStack = getLatestConfigMappingsForStack(
-          clusterEntity.getClusterId(), stackId);
+      // get the latest configurations for the given stack which we're going to make active
+      Collection<ClusterConfigEntity> latestConfigsByStack = clusterDAO.getLatestConfigurations(
+          clusterId, stackId);
+
+      // set the service configuration for the specified stack to the latest
+      for (ClusterConfigEntity latestConfigByStack : latestConfigsByStack) {
+        // since we're iterating over all configuration types, only work with those that are for our service
+        if (!configTypesForService.contains(latestConfigByStack.getType())) {
+          continue;
+        }
 
-      for( ClusterConfigMappingEntity latestConfigMapping : latestConfigMappingByStack ){
-        ClusterConfigMappingEntity mapping = ccmMap.get(latestConfigMapping);
-        mapping.setSelected(1);
+        // pull the correct latest mapping for the stack out of the cached map
+        // from the cluster entity
+        ClusterConfigEntity entity = clusterConfigEntityMap.get(latestConfigByStack);
+        entity.setSelected(true);
 
-        LOG.info("Settting {} with version tag {} created on {} to selected for stack {}",
-            mapping.getType(), mapping.getTag(), new Date(mapping.getCreateTimestamp()),
-            stackId.toString());
+        LOG.info("Setting {} with version tag {} created on {} to selected for stack {}",
+            entity.getType(), entity.getTag(), new Date(entity.getTimestamp()),
+          stackId
+        );
       }
 
       // since the entities which were modified came from the cluster entity's
       // list to begin with, we can just save them right back - no need for a
-      // new collection since the CCM entity instances were modified directly
-      clusterEntity.setConfigMappingEntities(configMappingEntities);
+      // new collection since the entity instances were modified directly
       clusterEntity = clusterDAO.merge(clusterEntity);
-      clusterDAO.mergeConfigMappings(configMappingEntities);
 
       cacheConfigurations();
+
+      LOG.info(
+          "Applied latest configurations for {} on stack {}. The the following types were modified: {}",
+          serviceName, stackId, StringUtils.join(configTypesForService, ','));
+
     } finally {
       clusterGlobalLock.writeLock().unlock();
     }
 
-    LOG.info(
-        "Applied latest configurations for {} on stack {}. The desired configurations are now {}",
-        getClusterName(), stackId, getDesiredConfigs());
-
     // publish an event to instruct entity managers to clear cached instances of
     // ClusterEntity immediately - it takes EclipseLink about 1000ms to update
     // the L1 caches of other threads and the action scheduler could act upon
@@ -3206,62 +2410,6 @@ public class ClusterImpl implements Cluster {
   }
 
   /**
-   * Retrieves all of the configuration mappings (selected and unselected) for
-   * the specified stack and then iterates through them, returning the most
-   * recent mapping for every type/tag combination.
-   * <p/>
-   * Because of how configuration revert works, mappings can be created for the
-   * same type/tag combinations. The only difference being that the timestamp
-   * reflects when each mapping was created.
-   * <p/>
-   * JPQL cannot be used directly here easily because some databases cannot
-   * support the necessary grouping and IN clause. For example: <br/>
-   *
-   * <pre>
-   * SELECT mapping FROM clusterconfigmappingentity mapping
-   *   WHERE (mapping.typename, mapping.createtimestamp) IN
-   *     (SELECT latest.typename, MAX(latest.createtimestamp)
-   *      FROM clusterconfigmappingentity latest
-   *      GROUP BY latest.typename)
-   * </pre>
-   *
-   * @param clusterId
-   *          the cluster ID
-   * @param stackId
-   *          the stack to retrieve the mappings for (not {@code null}).
-   * @return the most recent mapping (selected or unselected) for the specified
-   *         stack for every type.
-   */
-  public Collection<ClusterConfigMappingEntity> getLatestConfigMappingsForStack(long clusterId,
-      StackId stackId) {
-
-    // get all mappings for the specified stack (which could include
-    // duplicates since a config revert creates a duplicate mapping with a
-    // different timestamp)
-    List<ClusterConfigMappingEntity> clusterConfigMappingsForStack = clusterDAO.getClusterConfigMappingsByStack(
-        clusterId, stackId);
-
-    Map<String, ClusterConfigMappingEntity> latestMappingsByType = new HashMap<>();
-    for (ClusterConfigMappingEntity mapping : clusterConfigMappingsForStack) {
-      String type = mapping.getType();
-
-      if (!latestMappingsByType.containsKey(type)) {
-        latestMappingsByType.put(type, mapping);
-        continue;
-      }
-
-      ClusterConfigMappingEntity entityStored = latestMappingsByType.get(type);
-      Long timestampStored = entityStored.getCreateTimestamp();
-      Long timestamp = mapping.getCreateTimestamp();
-      if (timestamp > timestampStored) {
-        latestMappingsByType.put(type, mapping);
-      }
-    }
-
-    return latestMappingsByType.values();
-  }
-
-  /**
    * {@inheritDoc}
    */
   @Override
@@ -3270,21 +2418,24 @@ public class ClusterImpl implements Cluster {
       StackId stackId = getCurrentStackVersion();
       StackInfo stackInfo = ambariMetaInfo.getStack(stackId.getStackName(), stackId.getStackVersion());
       return stackInfo.getConfigPropertiesTypes(configType);
-    } catch (AmbariException e) {
-
+    } catch (AmbariException ignored) {
     }
     return new HashMap<>();
   }
 
   /**
-   * Removes all configurations associated with the specified stack. The caller
-   * should make sure the cluster global write lock is acquired.
+   * Removes all configurations associated with the specified stack for the
+   * specified service. The caller should make sure the cluster global write
+   * lock is acquired.
    *
    * @param stackId
-   * @see Cluster#getClusterGlobalLock()
+   *          the stack to remove configurations for (not {@code null}).
+   * @param serviceName
+   *          the service name (not {@code null}).
+   * @see #clusterGlobalLock
    */
   @Transactional
-  void removeAllConfigsForStack(StackId stackId) {
+  void removeAllConfigsForStack(StackId stackId, String serviceName) {
     ClusterEntity clusterEntity = getClusterEntity();
 
     // make sure the entity isn't stale in the current unit of work.
@@ -3292,78 +2443,50 @@ public class ClusterImpl implements Cluster {
 
     long clusterId = clusterEntity.getClusterId();
 
+    // keep track of any types removed for logging purposes
+    Set<String> removedConfigurationTypes = new HashSet<>();
+
     // this will keep track of cluster config mappings that need removal
     // since there is no relationship between configs and their mappings, we
     // have to do it manually
     List<ClusterConfigEntity> removedClusterConfigs = new ArrayList<>(50);
-    Collection<ClusterConfigEntity> clusterConfigEntities = clusterEntity.getClusterConfigEntities();
+    Collection<ClusterConfigEntity> allClusterConfigEntities = clusterEntity.getClusterConfigEntities();
+    Collection<ServiceConfigEntity> allServiceConfigEntities = clusterEntity.getServiceConfigEntities();
 
-    List<ServiceConfigEntity> serviceConfigs = serviceConfigDAO.getAllServiceConfigsForClusterAndStack(
-      clusterId, stackId);
+    // get the service configs only for the service
+    List<ServiceConfigEntity> serviceConfigs = serviceConfigDAO.getServiceConfigsForServiceAndStack(
+        clusterId, stackId, serviceName);
 
     // remove all service configurations and associated configs
-    Collection<ServiceConfigEntity> serviceConfigEntities = clusterEntity.getServiceConfigEntities();
-
     for (ServiceConfigEntity serviceConfig : serviceConfigs) {
       for (ClusterConfigEntity configEntity : serviceConfig.getClusterConfigEntities()) {
-        clusterConfigEntities.remove(configEntity);
+        removedConfigurationTypes.add(configEntity.getType());
+
+        allClusterConfigEntities.remove(configEntity);
         clusterDAO.removeConfig(configEntity);
         removedClusterConfigs.add(configEntity);
       }
 
       serviceConfig.getClusterConfigEntities().clear();
       serviceConfigDAO.remove(serviceConfig);
-      serviceConfigEntities.remove(serviceConfig);
-    }
-
-    // remove any leftover cluster configurations that don't have a service
-    // configuration (like cluster-env)
-    List<ClusterConfigEntity> clusterConfigs = clusterDAO.getAllConfigurations(
-      clusterId, stackId);
-
-    for (ClusterConfigEntity clusterConfig : clusterConfigs) {
-      clusterConfigEntities.remove(clusterConfig);
-      clusterDAO.removeConfig(clusterConfig);
-      removedClusterConfigs.add(clusterConfig);
+      allServiceConfigEntities.remove(serviceConfig);
     }
 
-    clusterEntity.setClusterConfigEntities(clusterConfigEntities);
+    clusterEntity.setClusterConfigEntities(allClusterConfigEntities);
     clusterEntity = clusterDAO.merge(clusterEntity);
 
-    // remove config mappings
-    Collection<ClusterConfigMappingEntity> configMappingEntities =
-        clusterDAO.getClusterConfigMappingEntitiesByCluster(getClusterId());
-
-    for (ClusterConfigEntity removedClusterConfig : removedClusterConfigs) {
-      String removedClusterConfigType = removedClusterConfig.getType();
-      String removedClusterConfigTag = removedClusterConfig.getTag();
-
-      Iterator<ClusterConfigMappingEntity> clusterConfigMappingIterator = configMappingEntities.iterator();
-      while (clusterConfigMappingIterator.hasNext()) {
-        ClusterConfigMappingEntity clusterConfigMapping = clusterConfigMappingIterator.next();
-        String mappingType = clusterConfigMapping.getType();
-        String mappingTag = clusterConfigMapping.getTag();
-
-        if (removedClusterConfigTag.equals(mappingTag)
-          && removedClusterConfigType.equals(mappingType)) {
-          clusterConfigMappingIterator.remove();
-          clusterDAO.removeConfigMapping(clusterConfigMapping);
-        }
-      }
-    }
-
-    clusterEntity.setConfigMappingEntities(configMappingEntities);
-    clusterEntity = clusterDAO.merge(clusterEntity);
+    LOG.info("Removed the following configuration types for {} on stack {}: {}", serviceName,
+        stackId, StringUtils.join(removedConfigurationTypes, ','));
   }
 
   /**
    * {@inheritDoc}
    */
   @Override
-  public void removeConfigurations(StackId stackId) {
+  public void removeConfigurations(StackId stackId, String serviceName) {
     clusterGlobalLock.writeLock().lock();
     try {
-      removeAllConfigsForStack(stackId);
+      removeAllConfigsForStack(stackId, serviceName);
       cacheConfigurations();
     } finally {
       clusterGlobalLock.writeLock().unlock();

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/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 d61a1c8..018f8d8 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
@@ -43,10 +43,8 @@ import org.apache.ambari.server.events.HostsAddedEvent;
 import org.apache.ambari.server.events.HostsRemovedEvent;
 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.HostConfigMappingDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
-import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
 import org.apache.ambari.server.orm.dao.HostStateDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.KerberosPrincipalHostDAO;
@@ -57,7 +55,6 @@ import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.dao.TopologyHostInfoDAO;
 import org.apache.ambari.server.orm.dao.TopologyLogicalTaskDAO;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.HostRoleCommandEntity;
 import org.apache.ambari.server.orm.entities.PermissionEntity;
@@ -97,26 +94,22 @@ public class ClustersImpl implements Clusters {
 
   private static final Logger LOG = LoggerFactory.getLogger(ClustersImpl.class);
 
-  private final ConcurrentHashMap<String, Cluster> clusters = new ConcurrentHashMap<String, Cluster>();
-  private final ConcurrentHashMap<Long, Cluster> clustersById = new ConcurrentHashMap<Long, Cluster>();
-  private final ConcurrentHashMap<String, Host> hosts = new ConcurrentHashMap<String, Host>();
-  private final ConcurrentHashMap<Long, Host> hostsById = new ConcurrentHashMap<Long, Host>();
-  private final ConcurrentHashMap<String, Set<Cluster>> hostClusterMap = new ConcurrentHashMap<String, Set<Cluster>>();
-  private final ConcurrentHashMap<String, Set<Host>> clusterHostMap = new ConcurrentHashMap<String, Set<Host>>();
+  private final ConcurrentHashMap<String, Cluster> clusters = new ConcurrentHashMap<>();
+  private final ConcurrentHashMap<Long, Cluster> clustersById = new ConcurrentHashMap<>();
+  private final ConcurrentHashMap<String, Host> hosts = new ConcurrentHashMap<>();
+  private final ConcurrentHashMap<Long, Host> hostsById = new ConcurrentHashMap<>();
+  private final ConcurrentHashMap<String, Set<Cluster>> hostClusterMap = new ConcurrentHashMap<>();
+  private final ConcurrentHashMap<String, Set<Host>> clusterHostMap = new ConcurrentHashMap<>();
 
   @Inject
   private ClusterDAO clusterDAO;
   @Inject
   private HostDAO hostDAO;
   @Inject
-  private ClusterVersionDAO clusterVersionDAO;
-  @Inject
   private HostVersionDAO hostVersionDAO;
   @Inject
   private HostStateDAO hostStateDAO;
   @Inject
-  private HostRoleCommandDAO hostRoleCommandDAO;
-  @Inject
   private ResourceTypeDAO resourceTypeDAO;
   @Inject
   private RequestOperationLevelDAO requestOperationLevelDAO;
@@ -318,7 +311,7 @@ public class ClustersImpl implements Clusters {
 
   @Override
   public List<Host> getHosts() {
-    return new ArrayList<Host>(hosts.values());
+    return new ArrayList<>(hosts.values());
   }
 
   @Override
@@ -407,7 +400,7 @@ public class ClustersImpl implements Clusters {
     // not stored to DB
     Host host = hostFactory.create(hostEntity);
     host.setAgentVersion(new AgentVersion(""));
-    List<DiskInfo> emptyDiskList = new CopyOnWriteArrayList<DiskInfo>();
+    List<DiskInfo> emptyDiskList = new CopyOnWriteArrayList<>();
     host.setDisksInfo(emptyDiskList);
     host.setHealthStatus(new HostHealthStatus(HealthStatus.UNKNOWN, ""));
     host.setHostAttributes(new ConcurrentHashMap<String, String>());
@@ -480,7 +473,7 @@ public class ClustersImpl implements Clusters {
   private Map<String, Host> getHostsMap(Collection<String> hostSet) throws
       HostNotFoundException {
 
-    Map<String, Host> hostMap = new HashMap<String, Host>();
+    Map<String, Host> hostMap = new HashMap<>();
     Host host = null;
     for (String hostName : hostSet) {
       if (null != hostName) {
@@ -606,7 +599,7 @@ public class ClustersImpl implements Clusters {
   public Map<String, Host> getHostsForCluster(String clusterName)
       throws AmbariException {
 
-    Map<String, Host> hosts = new HashMap<String, Host>();
+    Map<String, Host> hosts = new HashMap<>();
     for (Host h : clusterHostMap.get(clusterName)) {
       hosts.put(h.getHostName(), h);
     }
@@ -617,7 +610,7 @@ public class ClustersImpl implements Clusters {
   @Override
   public Map<Long, Host> getHostIdsForCluster(String clusterName)
       throws AmbariException {
-    Map<Long, Host> hosts = new HashMap<Long, Host>();
+    Map<Long, Host> hosts = new HashMap<>();
 
     for (Host h : clusterHostMap.get(clusterName)) {
       HostEntity hostEntity = hostDAO.findByName(h.getHostName());
@@ -644,11 +637,6 @@ public class ClustersImpl implements Clusters {
     }
     clusterHostMap.remove(cluster.getClusterName());
 
-    Collection<ClusterVersionEntity> clusterVersions = cluster.getAllClusterVersions();
-    for (ClusterVersionEntity clusterVersion : clusterVersions) {
-      clusterVersionDAO.remove(clusterVersion);
-    }
-
     clusters.remove(clusterName);
   }
 


[24/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
index 328fe22..1a8b7e7 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
@@ -148,7 +148,7 @@ public class HostImpl implements Host {
 
   private long lastHeartbeatTime = 0L;
   private AgentEnv lastAgentEnv = null;
-  private List<DiskInfo> disksInfo = new CopyOnWriteArrayList<DiskInfo>();
+  private List<DiskInfo> disksInfo = new CopyOnWriteArrayList<>();
   private RecoveryReport recoveryReport = new RecoveryReport();
   private Integer currentPingPort = null;
 
@@ -481,7 +481,7 @@ public class HostImpl implements Host {
     // FIXME add all other information into host attributes
     setAgentVersion(new AgentVersion(hostInfo.getAgentUserId()));
 
-    Map<String, String> attrs = new HashMap<String, String>();
+    Map<String, String> attrs = new HashMap<>();
     if (hostInfo.getHardwareIsa() != null) {
       attrs.put(HARDWAREISA, hostInfo.getHardwareIsa());
     }
@@ -828,7 +828,7 @@ public class HostImpl implements Host {
     Map<String, String> hostAttrs = gson.fromJson(hostEntity.getHostAttributes(), hostAttributesType);
 
     if (hostAttrs == null) {
-      hostAttrs = new ConcurrentHashMap<String, String>();
+      hostAttrs = new ConcurrentHashMap<>();
     }
 
     hostAttrs.putAll(hostAttributes);
@@ -939,24 +939,20 @@ public class HostImpl implements Host {
     HostResponse r = new HostResponse(getHostName());
 
     r.setAgentVersion(getAgentVersion());
-    r.setAvailableMemBytes(getAvailableMemBytes());
     r.setPhCpuCount(getPhCpuCount());
     r.setCpuCount(getCpuCount());
     r.setDisksInfo(getDisksInfo());
     r.setHealthStatus(getHealthStatus());
     r.setHostAttributes(getHostAttributes());
     r.setIpv4(getIPv4());
-    r.setIpv6(getIPv6());
     r.setLastHeartbeatTime(getLastHeartbeatTime());
     r.setLastAgentEnv(lastAgentEnv);
     r.setLastRegistrationTime(getLastRegistrationTime());
     r.setOsArch(getOsArch());
-    r.setOsInfo(getOsInfo());
     r.setOsType(getOsType());
     r.setRackInfo(getRackInfo());
     r.setTotalMemBytes(getTotalMemBytes());
     r.setPublicHostName(getPublicHostName());
-    r.setHostState(getState().toString());
     r.setStatus(getStatus());
     r.setRecoveryReport(getRecoveryReport());
     r.setRecoverySummary(getRecoveryReport().getSummary());
@@ -1024,7 +1020,7 @@ public class HostImpl implements Host {
 
   @Override
   public Map<String, DesiredConfig> getDesiredConfigs(long clusterId) {
-    Map<String, DesiredConfig> map = new HashMap<String, DesiredConfig>();
+    Map<String, DesiredConfig> map = new HashMap<>();
 
     for (HostConfigMapping e : hostConfigMappingDAO.findSelected(
         clusterId, getHostId())) {
@@ -1045,10 +1041,10 @@ public class HostImpl implements Host {
   @Override
   public Map<String, HostConfig> getDesiredHostConfigs(Cluster cluster,
       Map<String, DesiredConfig> clusterDesiredConfigs) throws AmbariException {
-    Map<String, HostConfig> hostConfigMap = new HashMap<String, HostConfig>();
+    Map<String, HostConfig> hostConfigMap = new HashMap<>();
 
     if( null == cluster ){
-      clusterDesiredConfigs = new HashMap<String, DesiredConfig>();
+      clusterDesiredConfigs = new HashMap<>();
     }
 
     // per method contract, fetch if not supplied

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/repository/VersionDefinitionXml.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/repository/VersionDefinitionXml.java b/ambari-server/src/main/java/org/apache/ambari/server/state/repository/VersionDefinitionXml.java
index 69399d6..97f391e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/repository/VersionDefinitionXml.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/repository/VersionDefinitionXml.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -143,6 +143,27 @@ public class VersionDefinitionXml {
   }
 
   /**
+   * Gets the set of services that are included in this XML
+   * @return an empty set for STANDARD repositories, or a non-empty set for PATCH type.
+   */
+  public Set<String> getAvailableServiceNames() {
+    if (availableServices.isEmpty()) {
+      return Collections.emptySet();
+    } else {
+      Set<String> serviceNames = new HashSet<>();
+
+      Map<String, ManifestService> manifest = buildManifest();
+
+      for (AvailableServiceReference ref : availableServices) {
+        ManifestService ms = manifest.get(ref.serviceIdReference);
+        serviceNames.add(ms.serviceName);
+      }
+
+      return serviceNames;
+    }
+  }
+
+  /**
    * Gets if the version definition was built as the default for a stack
    * @return {@code true} if default for a stack
    */
@@ -353,7 +374,6 @@ public class VersionDefinitionXml {
 
   /**
    * Builds a Version Definition that is the default for the stack
-   * @param stack
    * @return the version definition
    */
   public static VersionDefinitionXml build(StackInfo stackInfo) {
@@ -441,4 +461,4 @@ public class VersionDefinitionXml {
     }
   }
 
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/services/RetryUpgradeActionService.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/services/RetryUpgradeActionService.java b/ambari-server/src/main/java/org/apache/ambari/server/state/services/RetryUpgradeActionService.java
index 91898dc..a19e83e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/services/RetryUpgradeActionService.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/services/RetryUpgradeActionService.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -30,11 +30,12 @@ import org.apache.ambari.server.AmbariService;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostRoleCommandEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.stack.upgrade.Direction;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -165,24 +166,21 @@ public class RetryUpgradeActionService extends AbstractScheduledService {
    * @return Request Id of active stack upgrade.
    */
   private Long getActiveUpgradeRequestId(Cluster cluster) {
-    ClusterVersionEntity currentVersion = cluster.getCurrentClusterVersion();
-
-    if (currentVersion == null) {
-      LOG.debug("No Cluster Version exists as CURRENT. Skip retrying failed tasks.");
-      return null;
-    }
 
     // May be null, and either upgrade or downgrade
     UpgradeEntity currentUpgrade = cluster.getUpgradeInProgress();
     if (currentUpgrade == null) {
-      LOG.debug("There is no active stack upgrade in progress. Skip retrying failed tasks.");
+      LOG.debug("There is no active upgrade in progress. Skip retrying failed tasks.");
       return null;
     }
 
-    LOG.debug("Found an active stack upgrade with id: {}, direction: {}, type: {}, from version: {}, to version: {}",
-        currentUpgrade.getId(), currentUpgrade.getDirection(), currentUpgrade.getUpgradeType(),
-        currentUpgrade.getFromRepositoryVersion().getVersion(),
-        currentUpgrade.getToRepositoryVersion().getVersion());
+    Direction direction = currentUpgrade.getDirection();
+    RepositoryVersionEntity repositoryVersion = currentUpgrade.getRepositoryVersion();
+
+    LOG.debug(
+        "Found an active upgrade with id: {}, direction: {}, {} {}", currentUpgrade.getId(),
+        direction, currentUpgrade.getUpgradeType(), direction.getPreposition(),
+        repositoryVersion.getVersion());
 
     return currentUpgrade.getRequestId();
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/ClusterGrouping.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/ClusterGrouping.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/ClusterGrouping.java
index c45ec24..5ac7752 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/ClusterGrouping.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/ClusterGrouping.java
@@ -224,7 +224,7 @@ public class ClusterGrouping extends Grouping {
       if (null == hosts || hosts.hosts.isEmpty()) {
         return null;
       } else {
-        realHosts = new LinkedHashSet<String>(hosts.hosts);
+        realHosts = new LinkedHashSet<>(hosts.hosts);
       }
     }
 
@@ -269,7 +269,7 @@ public class ClusterGrouping extends Grouping {
 
       if (hosts != null) {
 
-        Set<String> realHosts = new LinkedHashSet<String>(hosts.hosts);
+        Set<String> realHosts = new LinkedHashSet<>(hosts.hosts);
         if (ExecuteHostType.MASTER == et.hosts && null != hosts.master) {
           realHosts = Collections.singleton(hosts.master);
         }
@@ -292,7 +292,7 @@ public class ClusterGrouping extends Grouping {
         }
 
         return new StageWrapper(
-            StageWrapper.Type.RU_TASKS,
+            StageWrapper.Type.UPGRADE_TASKS,
             execution.title,
             new TaskWrapper(service, component, realHosts, et));
       }
@@ -300,7 +300,7 @@ public class ClusterGrouping extends Grouping {
       // no service and no component will distributed the task to all healthy
       // hosts not in maintenance mode
       Cluster cluster = ctx.getCluster();
-      Set<String> hostNames = new HashSet<String>();
+      Set<String> hostNames = new HashSet<>();
       for (Host host : ctx.getCluster().getHosts()) {
         MaintenanceState maintenanceState = host.getMaintenanceState(cluster.getClusterId());
         if (maintenanceState == MaintenanceState.OFF) {
@@ -309,7 +309,7 @@ public class ClusterGrouping extends Grouping {
       }
 
       return new StageWrapper(
-          StageWrapper.Type.RU_TASKS,
+          StageWrapper.Type.UPGRADE_TASKS,
           execution.title,
           new TaskWrapper(service, component, hostNames, et));
     }
@@ -349,7 +349,7 @@ public class ClusterGrouping extends Grouping {
   @Override
   public void merge(Iterator<Grouping> iterator) throws AmbariException {
     if (executionStages == null) {
-      executionStages = new ArrayList<ExecuteStage>();
+      executionStages = new ArrayList<>();
     }
     Map<String, List<ExecuteStage>> skippedStages = new HashMap<>();
     while (iterator.hasNext()) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Direction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Direction.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Direction.java
index cb4f501..d6edd56 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Direction.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Direction.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -79,4 +79,13 @@ public enum Direction {
   }
 
 
+  /**
+   * Gets the preposition based on the direction. Since the repository is
+   * singular, it will either be "to repo" or "from repo".
+   *
+   * @return "to" or "from"
+   */
+  public String getPreposition() {
+    return (this == UPGRADE) ? "to" : "from";
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/ExecuteTask.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/ExecuteTask.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/ExecuteTask.java
index 3995cb6..0716481 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/ExecuteTask.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/ExecuteTask.java
@@ -68,7 +68,7 @@ public class ExecuteTask extends Task {
 
   @Override
   public StageWrapper.Type getStageWrapperType() {
-    return StageWrapper.Type.RU_TASKS;
+    return StageWrapper.Type.UPGRADE_TASKS;
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Grouping.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Grouping.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Grouping.java
index 9ea4de3..fed5b77 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Grouping.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Grouping.java
@@ -71,7 +71,7 @@ public class Grouping {
   public boolean allowRetry = true;
 
   @XmlElement(name="service")
-  public List<UpgradePack.OrderService> services = new ArrayList<UpgradePack.OrderService>();
+  public List<UpgradePack.OrderService> services = new ArrayList<>();
 
   @XmlElement(name="service-check", defaultValue="true")
   public boolean performServiceCheck = true;
@@ -101,8 +101,8 @@ public class Grouping {
 
   private static class DefaultBuilder extends StageWrapperBuilder {
 
-    private List<StageWrapper> m_stages = new ArrayList<StageWrapper>();
-    private Set<String> m_servicesToCheck = new HashSet<String>();
+    private List<StageWrapper> m_stages = new ArrayList<>();
+    private Set<String> m_servicesToCheck = new HashSet<>();
     private boolean m_serviceCheck = true;
 
     private DefaultBuilder(Grouping grouping, boolean serviceCheck) {
@@ -167,7 +167,7 @@ public class Grouping {
      * @return List of list of TaskWrappers, where each outer list is a separate stage.
      */
     private List<List<TaskWrapper>> organizeTaskWrappersBySyncRules(List<TaskWrapper> tasks) {
-      List<List<TaskWrapper>> groupedTasks = new ArrayList<List<TaskWrapper>>();
+      List<List<TaskWrapper>> groupedTasks = new ArrayList<>();
 
       List<TaskWrapper> subTasks = new ArrayList<>();
       for (TaskWrapper tw : tasks) {
@@ -247,8 +247,8 @@ public class Grouping {
         m_stages.addAll(0, stageWrappers);
       }
 
-      List<TaskWrapper> tasks = new ArrayList<TaskWrapper>();
-      List<String> displays = new ArrayList<String>();
+      List<TaskWrapper> tasks = new ArrayList<>();
+      List<String> displays = new ArrayList<>();
       for (String service : m_servicesToCheck) {
         tasks.add(new TaskWrapper(
             service, "", Collections.<String>emptySet(), new ServiceCheckTask()));
@@ -277,7 +277,7 @@ public class Grouping {
       return Collections.emptyList();
     }
 
-    List<TaskBucket> holders = new ArrayList<TaskBucket>();
+    List<TaskBucket> holders = new ArrayList<>();
 
     TaskBucket current = null;
 
@@ -301,7 +301,7 @@ public class Grouping {
 
   private static class TaskBucket {
     private StageWrapper.Type type;
-    private List<Task> tasks = new ArrayList<Task>();
+    private List<Task> tasks = new ArrayList<>();
     private TaskBucket(Task initial) {
       switch (initial.getType()) {
         case CONFIGURE:
@@ -310,7 +310,7 @@ public class Grouping {
           type = StageWrapper.Type.SERVER_SIDE_ACTION;
           break;
         case EXECUTE:
-          type = StageWrapper.Type.RU_TASKS;
+          type = StageWrapper.Type.UPGRADE_TASKS;
           break;
         case CONFIGURE_FUNCTION:
           type = StageWrapper.Type.CONFIGURE;

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/HostOrderGrouping.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/HostOrderGrouping.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/HostOrderGrouping.java
index b55d185..dd2dd02 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/HostOrderGrouping.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/HostOrderGrouping.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -33,6 +33,7 @@ import org.apache.ambari.server.actionmanager.HostRoleCommand;
 import org.apache.ambari.server.actionmanager.HostRoleCommandFactory;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.metadata.RoleCommandOrder;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.stack.HostsType;
 import org.apache.ambari.server.stageplanner.RoleGraph;
 import org.apache.ambari.server.stageplanner.RoleGraphFactory;
@@ -55,7 +56,7 @@ import com.google.gson.JsonObject;
 public class HostOrderGrouping extends Grouping {
   private static final String TYPE = "type";
   private static final String HOST = "host";
-  private static Logger LOG = LoggerFactory.getLogger(HostOrderGrouping.class);
+  private static final Logger LOG = LoggerFactory.getLogger(HostOrderGrouping.class);
 
   /**
    * Contains the ordered actions to schedule for this grouping.
@@ -173,10 +174,14 @@ public class HostOrderGrouping extends Grouping {
           // either doesn't exist or the downgrade is to the current target version.
           // hostsType better not be null either, but check anyway
           if (null != hostsType && !hostsType.hosts.contains(hostName)) {
+            RepositoryVersionEntity targetRepositoryVersion = upgradeContext.getTargetRepositoryVersion(
+                sch.getServiceName());
+
             LOG.warn("Host {} could not be orchestrated. Either there are no components for {}/{} " +
                 "or the target version {} is already current.",
                 hostName, sch.getServiceName(), sch.getServiceComponentName(),
-                upgradeContext.getTargetRepositoryVersion().getVersion());
+                targetRepositoryVersion.getVersion());
+
             continue;
           }
 
@@ -226,7 +231,7 @@ public class HostOrderGrouping extends Grouping {
           // create task wrappers
           List<TaskWrapper> taskWrappers = new ArrayList<>();
           for (HostRoleCommand command : stageCommandsForHost) {
-            StackId stackId = upgradeContext.getTargetStackId();
+            StackId stackId = upgradeContext.getRepositoryVersion().getStackId();
             String componentName = command.getRole().name();
 
             String serviceName = null;
@@ -288,7 +293,6 @@ public class HostOrderGrouping extends Grouping {
 
     /**
      * @param upgradeContext  the context
-     * @param hosts           the list of hostnames
      * @return  the wrappers for a host
      */
     private List<StageWrapper> buildServiceChecks(UpgradeContext upgradeContext, List<String> serviceChecks) {
@@ -330,7 +334,10 @@ public class HostOrderGrouping extends Grouping {
      * @return                {@code true} if the host component advertises its version
      */
     private boolean isVersionAdvertised(UpgradeContext upgradeContext, ServiceComponentHost sch) {
-      StackId targetStack = upgradeContext.getTargetStackId();
+      RepositoryVersionEntity targetRepositoryVersion = upgradeContext.getTargetRepositoryVersion(
+          sch.getServiceName());
+
+      StackId targetStack = targetRepositoryVersion.getStackId();
 
       try {
         ComponentInfo component = upgradeContext.getAmbariMetaInfo().getComponent(

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelper.java
index ef40211..471e30f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/RepositoryVersionHelper.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,18 +19,33 @@ package org.apache.ambari.server.state.stack.upgrade;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Set;
 
 import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.agent.CommandRepository;
+import org.apache.ambari.server.agent.ExecutionCommand;
+import org.apache.ambari.server.agent.ExecutionCommand.KeyNames;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.controller.ActionExecutionContext;
+import org.apache.ambari.server.controller.ActionExecutionContext.ExecutionCommandVisitor;
+import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.internal.OperatingSystemResourceProvider;
 import org.apache.ambari.server.controller.internal.RepositoryResourceProvider;
 import org.apache.ambari.server.controller.internal.RepositoryVersionResourceProvider;
+import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
 import org.apache.ambari.server.orm.entities.RepositoryEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.RepositoryInfo;
+import org.apache.ambari.server.state.ServiceInfo;
+import org.apache.ambari.server.state.ServiceOsSpecific;
+import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.state.repository.VersionDefinitionXml;
 import org.apache.ambari.server.state.stack.UpgradePack;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
@@ -44,6 +59,7 @@ import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 import com.google.inject.Inject;
+import com.google.inject.Provider;
 import com.google.inject.Singleton;
 
 /**
@@ -57,8 +73,11 @@ public class RepositoryVersionHelper {
   @Inject
   private Gson gson;
 
-  @Inject(optional = true)
-  private AmbariMetaInfo ambariMetaInfo;
+  @Inject
+  private Provider<AmbariMetaInfo> ami;
+
+  @Inject
+  private Provider<Configuration> configuration;
 
   /**
    * Parses operating systems json to a list of entities. Expects json like:
@@ -86,7 +105,7 @@ public class RepositoryVersionHelper {
    * @throws Exception if any kind of json parsing error happened
    */
   public List<OperatingSystemEntity> parseOperatingSystems(String repositoriesJson) throws Exception {
-    final List<OperatingSystemEntity> operatingSystems = new ArrayList<OperatingSystemEntity>();
+    final List<OperatingSystemEntity> operatingSystems = new ArrayList<>();
     final JsonArray rootJson = new JsonParser().parse(repositoriesJson).getAsJsonArray();
     for (JsonElement operatingSystemJson: rootJson) {
       JsonObject osObj = operatingSystemJson.getAsJsonObject();
@@ -106,8 +125,11 @@ public class RepositoryVersionHelper {
         repositoryEntity.setBaseUrl(repositoryJson.get(RepositoryResourceProvider.REPOSITORY_BASE_URL_PROPERTY_ID).getAsString());
         repositoryEntity.setName(repositoryJson.get(RepositoryResourceProvider.REPOSITORY_REPO_NAME_PROPERTY_ID).getAsString());
         repositoryEntity.setRepositoryId(repositoryJson.get(RepositoryResourceProvider.REPOSITORY_REPO_ID_PROPERTY_ID).getAsString());
-        if (repositoryJson.get(RepositoryResourceProvider.REPOSITORY_UNIQUE_PROPERTY_ID) != null) {
-          repositoryEntity.setUnique(repositoryJson.get(RepositoryResourceProvider.REPOSITORY_UNIQUE_PROPERTY_ID).getAsBoolean());
+        if (repositoryJson.get(RepositoryResourceProvider.REPOSITORY_MIRRORS_LIST_PROPERTY_ID) != null) {
+          repositoryEntity.setMirrorsList(repositoryJson.get(RepositoryResourceProvider.REPOSITORY_MIRRORS_LIST_PROPERTY_ID).getAsString());
+        }
+        if (repositoryJson.getAsJsonObject().get(RepositoryResourceProvider.REPOSITORY_UNIQUE_PROPERTY_ID) != null) {
+          repositoryEntity.setUnique(repositoryJson.getAsJsonObject().get(RepositoryResourceProvider.REPOSITORY_UNIQUE_PROPERTY_ID).getAsBoolean());
         }
         operatingSystemEntity.getRepositories().add(repositoryEntity);
       }
@@ -156,6 +178,7 @@ public class RepositoryVersionHelper {
         repositoryJson.addProperty(RepositoryResourceProvider.REPOSITORY_BASE_URL_PROPERTY_ID, repository.getBaseUrl());
         repositoryJson.addProperty(RepositoryResourceProvider.REPOSITORY_REPO_NAME_PROPERTY_ID, repository.getRepoName());
         repositoryJson.addProperty(RepositoryResourceProvider.REPOSITORY_REPO_ID_PROPERTY_ID, repository.getRepoId());
+        repositoryJson.addProperty(RepositoryResourceProvider.REPOSITORY_MIRRORS_LIST_PROPERTY_ID, repository.getMirrorsList());
         repositoryJson.addProperty(RepositoryResourceProvider.REPOSITORY_UNIQUE_PROPERTY_ID, repository.isUnique());
         repositoriesJson.add(repositoryJson);
         operatingSystemJson.addProperty(OperatingSystemResourceProvider.OPERATING_SYSTEM_AMBARI_MANAGED_REPOS, repository.isAmbariManagedRepositories());
@@ -194,7 +217,7 @@ public class RepositoryVersionHelper {
    * @throws AmbariException if no upgrade packs suit the requirements
    */
   public String getUpgradePackageName(String stackName, String stackVersion, String repositoryVersion, UpgradeType upgradeType) throws AmbariException {
-    final Map<String, UpgradePack> upgradePacks = ambariMetaInfo.getUpgradePacks(stackName, stackVersion);
+    final Map<String, UpgradePack> upgradePacks = ami.get().getUpgradePacks(stackName, stackVersion);
     for (UpgradePack upgradePack : upgradePacks.values()) {
       final String upgradePackName = upgradePack.getName();
 
@@ -214,4 +237,107 @@ public class RepositoryVersionHelper {
     throw new AmbariException("There were no suitable upgrade packs for stack " + stackName + " " + stackVersion +
         ((null != upgradeType) ? " and upgrade type " + upgradeType : ""));
   }
+
+  /**
+   * Build the role parameters for an install command.
+   *
+   * @param amc           the management controller.  Tests don't use the same instance that gets injected.
+   * @param repoVersion   the repository version
+   * @param osFamily      the os family
+   * @param servicesOnHost the set of services to check for packages
+   * @return a Map<String, String> to use in
+   */
+  public Map<String, String> buildRoleParams(AmbariManagementController amc, RepositoryVersionEntity repoVersion, String osFamily, Set<String> servicesOnHost)
+    throws SystemException {
+
+    StackId stackId = repoVersion.getStackId();
+
+    List<ServiceOsSpecific.Package> packages = new ArrayList<>();
+
+    for (String serviceName : servicesOnHost) {
+      ServiceInfo info;
+
+      try {
+        if (ami.get().isServiceRemovedInStack(stackId.getStackName(), stackId.getStackVersion(), serviceName)) {
+          LOG.info(String.format("%s has been removed from stack %s-%s. Skip calculating its installation packages", stackId.getStackName(), stackId.getStackVersion(), serviceName));
+          continue; //No need to calculate install packages for removed services
+        }
+
+        info = ami.get().getService(stackId.getStackName(), stackId.getStackVersion(), serviceName);
+      } catch (AmbariException e) {
+        throw new SystemException(String.format("Cannot obtain stack information for %s-%s", stackId.getStackName(), stackId.getStackVersion()), e);
+      }
+
+      List<ServiceOsSpecific.Package> packagesForService = amc.getPackagesForServiceHost(info,
+        new HashMap<String, String>(), osFamily);
+
+      List<String> blacklistedPackagePrefixes = configuration.get().getRollingUpgradeSkipPackagesPrefixes();
+
+      for (ServiceOsSpecific.Package aPackage : packagesForService) {
+        if (!aPackage.getSkipUpgrade()) {
+          boolean blacklisted = false;
+          for (String prefix : blacklistedPackagePrefixes) {
+            if (aPackage.getName().startsWith(prefix)) {
+              blacklisted = true;
+              break;
+            }
+          }
+          if (! blacklisted) {
+            packages.add(aPackage);
+          }
+        }
+      }
+    }
+
+    Map<String, String> roleParams = new HashMap<>();
+    roleParams.put("stack_id", stackId.getStackId());
+    roleParams.put("repository_version", repoVersion.getVersion());
+    // !!! TODO make roleParams <String, Object> so we don't have to do this awfulness.
+    roleParams.put(KeyNames.PACKAGE_LIST, gson.toJson(packages));
+    roleParams.put(KeyNames.REPO_VERSION_ID, repoVersion.getId().toString());
+
+    VersionDefinitionXml xml = null;
+    try {
+      xml = repoVersion.getRepositoryXml();
+    } catch (Exception e) {
+      throw new SystemException(String.format("Could not load xml from repo version %s",
+          repoVersion.getVersion()));
+    }
+
+    if (null != xml && StringUtils.isNotBlank(xml.getPackageVersion(osFamily))) {
+      roleParams.put(KeyNames.PACKAGE_VERSION, xml.getPackageVersion(osFamily));
+    }
+
+    return roleParams;
+  }
+
+  /**
+   * Adds a command repository to the action context
+   * @param context       the context
+   * @param osFamily      the OS family
+   * @param repoVersion   the repository version entity
+   * @param repos         the repository entities
+   */
+  public void addCommandRepository(ActionExecutionContext context, String osFamily,
+      RepositoryVersionEntity repoVersion, List<RepositoryEntity> repos) {
+    StackId stackId = repoVersion.getStackId();
+
+    final CommandRepository commandRepo = new CommandRepository();
+    commandRepo.setRepositories(osFamily, repos);
+    commandRepo.setRepositoryVersion(repoVersion.getVersion());
+    commandRepo.setRepositoryVersionId(repoVersion.getId());
+    commandRepo.setStackName(stackId.getStackName());
+    commandRepo.setUniqueSuffix(String.format("-repo-%s", repoVersion.getId()));
+
+    context.addVisitor(new ExecutionCommandVisitor() {
+      @Override
+      public void visit(ExecutionCommand command) {
+        if (null == command.getRepositoryFile()) {
+          command.setRepositoryFile(commandRepo);
+        }
+      }
+    });
+  }
+
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/StageWrapper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/StageWrapper.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/StageWrapper.java
index 77b82e7..70589ec 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/StageWrapper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/StageWrapper.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -93,7 +93,7 @@ public class StageWrapper {
    * Gets the tasks json.
    */
   public String getTasksJson() {
-    List<Task> realTasks = new ArrayList<Task>();
+    List<Task> realTasks = new ArrayList<>();
     for (TaskWrapper tw : tasks) {
       realTasks.addAll(tw.getTasks());
     }
@@ -105,7 +105,7 @@ public class StageWrapper {
    * @return the set of hosts for all tasks
    */
   public Set<String> getHosts() {
-    Set<String> hosts = new HashSet<String>();
+    Set<String> hosts = new HashSet<>();
     for (TaskWrapper tw : tasks) {
       hosts.addAll(tw.getHosts());
     }
@@ -155,7 +155,7 @@ public class StageWrapper {
   public enum Type {
     SERVER_SIDE_ACTION,
     RESTART,
-    RU_TASKS,
+    UPGRADE_TASKS,
     SERVICE_CHECK,
     STOP,
     START,

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/StageWrapperBuilder.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/StageWrapperBuilder.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/StageWrapperBuilder.java
index 3cf3f8b..7fd8938 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/StageWrapperBuilder.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/StageWrapperBuilder.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Task.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Task.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Task.java
index 5c7cb6c..6ab2fd2 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Task.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/Task.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/TaskWrapper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/TaskWrapper.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/TaskWrapper.java
index dfa6159..8a87ff1 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/TaskWrapper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/TaskWrapper.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -42,10 +42,10 @@ public class TaskWrapper {
   private Set<String> timeoutKeys = new HashSet<>();
 
   /**
-   * @param s the service name for the tasks
-   * @param c the component name for the tasks
-   * @param hosts the set of hosts that the tasks are for
-   * @param tasks an array of tasks as a convenience
+   * @param s the service name for the task
+   * @param c the component name for the task
+   * @param hosts the set of hosts that the task is for
+   * @param task a single task
    */
   public TaskWrapper(String s, String c, Set<String> hosts, Task task) {
     this(s, c, hosts, null, task);

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/TaskWrapperBuilder.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/TaskWrapperBuilder.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/TaskWrapperBuilder.java
index 73ff2ca..23f2557 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/TaskWrapperBuilder.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/TaskWrapperBuilder.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -37,7 +37,7 @@ import org.slf4j.LoggerFactory;
  */
 public class TaskWrapperBuilder {
 
-  private static Logger LOG = LoggerFactory.getLogger(TaskWrapperBuilder.class);
+  private static final Logger LOG = LoggerFactory.getLogger(TaskWrapperBuilder.class);
 
   /**
    * Creates a collection of task wrappers based on the set of hosts they are allowed to run on
@@ -56,7 +56,7 @@ public class TaskWrapperBuilder {
     // to create.
     String ambariServerHostname = StageUtils.getHostName();
 
-    List<TaskWrapper> collection = new ArrayList<TaskWrapper>();
+    List<TaskWrapper> collection = new ArrayList<>();
     for (Task t : tasks) {
 
       // only add the server-side task if there are actual hosts for the service/component
@@ -114,7 +114,7 @@ public class TaskWrapperBuilder {
    * @return Returns the union of the hosts scheduled to perform the tasks.
    */
   public static Set<String> getEffectiveHosts(List<TaskWrapper> tasks) {
-    Set<String> effectiveHosts = new HashSet<String>();
+    Set<String> effectiveHosts = new HashSet<>();
     for(TaskWrapper t : tasks) {
       effectiveHosts.addAll(t.getHosts());
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
index 0ff4dad..f9d3cfc 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -24,7 +24,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.locks.Lock;
@@ -43,12 +42,14 @@ import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
 import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO;
 import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
+import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
 import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
+import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
@@ -62,6 +63,7 @@ import org.apache.ambari.server.state.HostComponentAdminState;
 import org.apache.ambari.server.state.HostConfig;
 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.SecurityState;
 import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentHost;
@@ -82,6 +84,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.util.concurrent.Striped;
 import com.google.inject.Inject;
 import com.google.inject.assistedinject.Assisted;
 import com.google.inject.assistedinject.AssistedInject;
@@ -108,6 +111,9 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
   @Inject
   private RepositoryVersionDAO repositoryVersionDAO;
 
+  @Inject
+  private HostVersionDAO hostVersionDAO;
+
   private final ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO;
 
   private final Clusters clusters;
@@ -156,6 +162,12 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
   private ImmutableList<Map<String, String>> processes = ImmutableList.of();
 
   /**
+   * Used for preventing multiple components on the same host from trying to
+   * recalculate versions concurrently.
+   */
+  private static final Striped<Lock> HOST_VERSION_LOCK = Striped.lazyWeakLock(20);
+
+  /**
    * The name of the host (which should never, ever change)
    */
   private final String hostName;
@@ -678,10 +690,8 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
         ServiceComponentHostInstallEvent e =
             (ServiceComponentHostInstallEvent) event;
         if (LOG.isDebugEnabled()) {
-          LOG.debug("Updating live stack version during INSTALL event"
-              + ", new stack version=" + e.getStackId());
+          LOG.debug("Updating live stack version during INSTALL event, new stack version={}", e.getStackId());
         }
-        impl.setStackVersion(new StackId(e.getStackId()));
       }
     }
   }
@@ -770,7 +780,7 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
       throw new RuntimeException(e);
     }
 
-    StackId stackId = serviceComponent.getDesiredStackVersion();
+    StackId stackId = serviceComponent.getDesiredStackId();
     StackEntity stackEntity = stackDAO.find(stackId.getStackName(),
         stackId.getStackVersion());
 
@@ -782,7 +792,6 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
     stateEntity.setHostEntity(hostEntity);
     stateEntity.setCurrentState(stateMachine.getCurrentState());
     stateEntity.setUpgradeState(UpgradeState.NONE);
-    stateEntity.setCurrentStack(stackEntity);
 
     HostComponentDesiredStateEntity desiredStateEntity = new HostComponentDesiredStateEntity();
     desiredStateEntity.setClusterId(serviceComponent.getClusterId());
@@ -790,7 +799,6 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
     desiredStateEntity.setServiceName(serviceComponent.getServiceName());
     desiredStateEntity.setHostEntity(hostEntity);
     desiredStateEntity.setDesiredState(State.INIT);
-    desiredStateEntity.setDesiredStack(stackEntity);
 
     if(!serviceComponent.isMasterComponent() && !serviceComponent.isClientComponent()) {
       desiredStateEntity.setAdminState(HostComponentAdminState.INSERVICE);
@@ -1002,9 +1010,7 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
   public void handleEvent(ServiceComponentHostEvent event)
       throws InvalidStateTransitionException {
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Handling ServiceComponentHostEvent event,"
-          + " eventType=" + event.getType().name()
-          + ", event=" + event.toString());
+      LOG.debug("Handling ServiceComponentHostEvent event, eventType={}, event={}", event.getType().name(), event);
     }
     State oldState = getState();
     try {
@@ -1036,13 +1042,8 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
                + ", oldState=" + oldState
                + ", currentState=" + getState());
       if (LOG.isDebugEnabled()) {
-        LOG.debug("ServiceComponentHost transitioned to a new state"
-            + ", serviceComponentName=" + getServiceComponentName()
-            + ", hostName=" + getHostName()
-            + ", oldState=" + oldState
-            + ", currentState=" + getState()
-            + ", eventType=" + event.getType().name()
-            + ", event=" + event);
+        LOG.debug("ServiceComponentHost transitioned to a new state, serviceComponentName={}, hostName={}, oldState={}, currentState={}, eventType={}, event={}",
+          getServiceComponentName(), getHostName(), oldState, getState(), event.getType().name(), event);
       }
     }
   }
@@ -1125,36 +1126,6 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
   }
 
   @Override
-  public StackId getStackVersion() {
-    HostComponentStateEntity schStateEntity = getStateEntity();
-    return getStackVersionFromSCHStateEntity(schStateEntity);
-  }
-
-  private StackId getStackVersionFromSCHStateEntity(HostComponentStateEntity schStateEntity) {
-    if (schStateEntity == null) {
-      return new StackId();
-    }
-
-    StackEntity currentStackEntity = schStateEntity.getCurrentStack();
-    return new StackId(currentStackEntity.getStackName(), currentStackEntity.getStackVersion());
-  }
-
-  @Override
-  public void setStackVersion(StackId stackId) {
-    StackEntity stackEntity = stackDAO.find(stackId.getStackName(), stackId.getStackVersion());
-
-    HostComponentStateEntity stateEntity = getStateEntity();
-    if (stateEntity != null) {
-      stateEntity.setCurrentStack(stackEntity);
-      stateEntity = hostComponentStateDAO.merge(stateEntity);
-    } else {
-      LOG.warn("Setting a member on an entity object that may have been "
-          + "previously deleted, serviceName = " + getServiceName() + ", " + "componentName = "
-          + getServiceComponentName() + ", " + "hostName = " + getHostName());
-    }
-  }
-
-  @Override
   public State getDesiredState() {
     HostComponentDesiredStateEntity desiredStateEntity = getDesiredStateEntity();
     if (desiredStateEntity != null) {
@@ -1185,38 +1156,6 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
   }
 
   @Override
-  public StackId getDesiredStackVersion() {
-    HostComponentDesiredStateEntity desiredStateEntity = getDesiredStateEntity();
-    return getDesiredStackVersionFromHostComponentDesiredStateEntity(desiredStateEntity);
-  }
-
-  private StackId getDesiredStackVersionFromHostComponentDesiredStateEntity(HostComponentDesiredStateEntity desiredStateEntity) {
-    if (desiredStateEntity != null) {
-      StackEntity desiredStackEntity = desiredStateEntity.getDesiredStack();
-      return new StackId(desiredStackEntity.getStackName(), desiredStackEntity.getStackVersion());
-    } else {
-      LOG.warn("Trying to fetch a member from an entity object that may "
-              + "have been previously deleted, serviceName = " + getServiceName() + ", "
-              + "componentName = " + getServiceComponentName() + ", " + "hostName = " + getHostName());
-    }
-    return null;
-  }
-
-  @Override
-  public void setDesiredStackVersion(StackId stackId) {
-    LOG.debug("Set DesiredStackVersion on serviceName = {} componentName = {} hostName = {} to {}",
-        getServiceName(), getServiceComponentName(), getHostName(), stackId);
-
-    HostComponentDesiredStateEntity desiredStateEntity = getDesiredStateEntity();
-    if (desiredStateEntity != null) {
-      StackEntity stackEntity = stackDAO.find(stackId.getStackName(), stackId.getStackVersion());
-
-      desiredStateEntity.setDesiredStack(stackEntity);
-      hostComponentDesiredStateDAO.merge(desiredStateEntity);
-    }
-  }
-
-  @Override
   public HostComponentAdminState getComponentAdminState() {
     HostComponentDesiredStateEntity desiredStateEntity = getDesiredStateEntity();
     return getComponentAdminStateFromDesiredStateEntity(desiredStateEntity);
@@ -1255,14 +1194,7 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
   public ServiceComponentHostResponse convertToResponse(Map<String, DesiredConfig> desiredConfigs) {
     HostComponentStateEntity hostComponentStateEntity = getStateEntity();
     HostEntity hostEntity = hostComponentStateEntity.getHostEntity();
-    if (null == hostComponentStateEntity) {
-      LOG.warn(
-          "Could not convert ServiceComponentHostResponse to a response. It's possible that Host {} was deleted.",
-          getHostName());
-      return null;
-    }
 
-    StackId stackVersion = getStackVersionFromSCHStateEntity(hostComponentStateEntity);
     HostComponentDesiredStateEntity hostComponentDesiredStateEntity = getDesiredStateEntity();
 
     String clusterName = serviceComponent.getClusterName();
@@ -1271,14 +1203,14 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
     String hostName = getHostName();
     String publicHostName = hostEntity.getPublicHostName();
     String state = getState().toString();
-    String stackId = stackVersion.getStackId();
     String desiredState = (hostComponentDesiredStateEntity == null) ? null : hostComponentDesiredStateEntity.getDesiredState().toString();
-    String desiredStackId = getDesiredStackVersionFromHostComponentDesiredStateEntity(hostComponentDesiredStateEntity).getStackId();
+    String desiredStackId = serviceComponent.getDesiredStackId().getStackId();
     HostComponentAdminState componentAdminState = getComponentAdminStateFromDesiredStateEntity(hostComponentDesiredStateEntity);
     UpgradeState upgradeState = hostComponentStateEntity.getUpgradeState();
 
     String displayName = null;
     try {
+      StackId stackVersion = serviceComponent.getDesiredStackId();
       ComponentInfo compInfo = ambariMetaInfo.getComponent(stackVersion.getStackName(),
               stackVersion.getStackVersion(), serviceName, serviceComponentName);
       displayName = compInfo.getDisplayName();
@@ -1286,9 +1218,15 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
       displayName = serviceComponentName;
     }
 
+    String desiredRepositoryVersion = null;
+    RepositoryVersionEntity repositoryVersion = serviceComponent.getDesiredRepositoryVersion();
+    if (null != repositoryVersion) {
+      desiredRepositoryVersion = repositoryVersion.getVersion();
+    }
+
     ServiceComponentHostResponse r = new ServiceComponentHostResponse(clusterName, serviceName,
-        serviceComponentName, displayName, hostName, publicHostName, state, stackId,
-        desiredState, desiredStackId, componentAdminState);
+        serviceComponentName, displayName, hostName, publicHostName, state, getVersion(),
+        desiredState, desiredStackId, desiredRepositoryVersion, componentAdminState);
 
     r.setActualConfigs(actualConfigs);
     r.setUpgradeState(upgradeState);
@@ -1317,11 +1255,11 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
     .append(", serviceName=")
     .append(serviceComponent.getServiceName())
     .append(", desiredStackVersion=")
-    .append(getDesiredStackVersion())
+    .append(serviceComponent.getDesiredStackId())
     .append(", desiredState=")
     .append(getDesiredState())
-    .append(", stackVersion=")
-    .append(getStackVersion())
+    .append(", version=")
+    .append(getVersion())
     .append(", state=")
     .append(getState())
     .append(", securityState=")
@@ -1382,7 +1320,7 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
     // completed, but only if it was persisted
     if (fireRemovalEvent) {
       long clusterId = getClusterId();
-      StackId stackId = getStackVersion();
+      StackId stackId = serviceComponent.getDesiredStackId();
       String stackVersion = stackId.getStackVersion();
       String stackName = stackId.getStackName();
       String serviceName = getServiceName();
@@ -1447,7 +1385,7 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
           Long groupId = Long.parseLong(overrideEntry.getKey());
           hc.getConfigGroupOverrides().put(groupId, overrideEntry.getValue());
           if (!configGroupMap.containsKey(groupId)) {
-            LOG.debug("Config group does not exist, id = " + groupId);
+            LOG.debug("Config group does not exist, id = {}", groupId);
           }
         }
       }
@@ -1557,46 +1495,46 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
   }
 
   /**
-   * Bootstrap any Repo Version, and potentially transition the Host Version across states.
-   * If a Host Component has a valid version, then create a Host Version if it does not already exist.
-   * If a Host Component does not have a version, return right away because no information is known.
-   * @return Return the Repository Version object
-   * @throws AmbariException
+   * {@inheritDoc}
    */
   @Override
-  public RepositoryVersionEntity recalculateHostVersionState() throws AmbariException {
-    RepositoryVersionEntity repositoryVersion = null;
-    String version = getVersion();
-    if (getUpgradeState().equals(UpgradeState.IN_PROGRESS) ||
-      getUpgradeState().equals(UpgradeState.VERSION_MISMATCH) ||
-        State.UNKNOWN.toString().equals(version)) {
-      // TODO: we still recalculate host version if upgrading component failed. It seems to be ok
-      // Recalculate only if no upgrade in progress/no version mismatch
-      return null;
-    }
-
-    final String hostName = getHostName();
-    final long hostId = getHost().getHostId();
-    final Set<Cluster> clustersForHost = clusters.getClustersForHost(hostName);
-    if (clustersForHost.size() != 1) {
-      throw new AmbariException("Host " + hostName + " should be assigned only to one cluster");
-    }
-    final Cluster cluster = clustersForHost.iterator().next();
-    final StackId stackId = cluster.getDesiredStackVersion();
-    final StackInfo stackInfo = ambariMetaInfo.getStack(stackId.getStackName(), stackId.getStackVersion());
+  @Transactional
+  public HostVersionEntity recalculateHostVersionState() throws AmbariException {
+    RepositoryVersionEntity repositoryVersion = serviceComponent.getDesiredRepositoryVersion();
+    HostEntity hostEntity = host.getHostEntity();
+    HostVersionEntity hostVersionEntity = hostVersionDAO.findHostVersionByHostAndRepository(
+        hostEntity, repositoryVersion);
+
+    Lock lock = HOST_VERSION_LOCK.get(host.getHostName());
+    lock.lock();
+    try {
+      // Create one if it doesn't already exist. It will be possible to make
+      // further transitions below.
+      if (hostVersionEntity == null) {
+        hostVersionEntity = new HostVersionEntity(hostEntity, repositoryVersion,
+            RepositoryVersionState.INSTALLING);
+
+        LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
+            hostVersionEntity.getHostName(), hostVersionEntity.getState(),
+            hostVersionEntity.getRepositoryVersion().getVersion(),
+            hostVersionEntity.getRepositoryVersion().getId());
+
+        hostVersionDAO.create(hostVersionEntity);
+      }
 
-    // Check if there is a Repo Version already for the version.
-    // If it doesn't exist, will have to create it.
-    repositoryVersion = repositoryVersionDAO.findByStackNameAndVersion(stackId.getStackName(), version);
+      final ServiceComponentHostSummary hostSummary = new ServiceComponentHostSummary(
+          ambariMetaInfo, hostEntity, repositoryVersion);
 
-    if (null == repositoryVersion) {
-      repositoryVersion = createRepositoryVersion(version, stackId, stackInfo);
+      if (hostSummary.isVersionCorrectForAllHosts(repositoryVersion)) {
+        if (hostVersionEntity.getState() != RepositoryVersionState.CURRENT) {
+          hostVersionEntity.setState(RepositoryVersionState.CURRENT);
+          hostVersionEntity = hostVersionDAO.merge(hostVersionEntity);
+        }
+      }
+    } finally {
+      lock.unlock();
     }
-
-    final HostEntity host = hostDAO.findById(hostId);
-    cluster.transitionHostVersionState(host, repositoryVersion, stackId);
-
-    return repositoryVersion;
+    return hostVersionEntity;
   }
 
   /**
@@ -1619,4 +1557,20 @@ public class ServiceComponentHostImpl implements ServiceComponentHost {
     return hostComponentStateDAO.findById(hostComponentStateId);
   }
 
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public ServiceComponent getServiceComponent() {
+    return serviceComponent;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public StackId getDesiredStackId() {
+    return serviceComponent.getDesiredStackId();
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostSummary.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostSummary.java b/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostSummary.java
index bccdb25..e9359ef 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostSummary.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostSummary.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,19 +19,19 @@
 package org.apache.ambari.server.state.svccomphost;
 
 
+import java.util.Collection;
+import java.util.HashSet;
+
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
-import org.apache.ambari.server.orm.entities.UpgradeEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
+import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
 import org.apache.ambari.server.state.ComponentInfo;
-import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.State;
 import org.apache.ambari.server.state.UpgradeState;
-
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
+import org.apache.commons.lang.StringUtils;
 
 
 /**
@@ -43,14 +43,28 @@ public class ServiceComponentHostSummary {
   private Collection<HostComponentStateEntity> haveAdvertisedVersion;
   private Collection<HostComponentStateEntity> waitingToAdvertiseVersion;
   private Collection<HostComponentStateEntity> noVersionToAdvertise;
-  private Set<String> versions;
 
-  public ServiceComponentHostSummary(AmbariMetaInfo ambariMetaInfo, HostEntity host, String stackName, String stackVersion) throws AmbariException {
+  /**
+   * Constructor.
+   *
+   * @param ambariMetaInfo
+   *          used to lookup whether a component advertises a version (not
+   *          {@code null}).
+   * @param host
+   *          the host to generate a component summary for (not {@code null}).
+   * @param repositoryVersion
+   *          the repository to generate a summary for (not {@code null}).
+   * @throws AmbariException
+   */
+  public ServiceComponentHostSummary(AmbariMetaInfo ambariMetaInfo, HostEntity host,
+      RepositoryVersionEntity repositoryVersion) throws AmbariException {
     allHostComponents = host.getHostComponentStateEntities();
     haveAdvertisedVersion = new HashSet<>();
     waitingToAdvertiseVersion = new HashSet<>();
     noVersionToAdvertise = new HashSet<>();
-    versions = new HashSet<>();
+
+    String stackName = repositoryVersion.getStackName();
+    String stackVersion = repositoryVersion.getStackVersion();
 
     for (HostComponentStateEntity hostComponentStateEntity : allHostComponents) {
       ComponentInfo compInfo = ambariMetaInfo.getComponent(
@@ -60,64 +74,50 @@ public class ServiceComponentHostSummary {
       if (!compInfo.isVersionAdvertised()) {
         // Some Components cannot advertise a version. E.g., ZKF, AMBARI_METRICS, Kerberos
         noVersionToAdvertise.add(hostComponentStateEntity);
-      } else {
-        if (hostComponentStateEntity.getUpgradeState().equals(UpgradeState.IN_PROGRESS) ||
-            hostComponentStateEntity.getVersion().equalsIgnoreCase(State.UNKNOWN.toString())) {
-          waitingToAdvertiseVersion.add(hostComponentStateEntity);
-        } else {
-          haveAdvertisedVersion.add(hostComponentStateEntity);
-          versions.add(hostComponentStateEntity.getVersion());
-        } // TODO: what if component reported wrong version?
+        continue;
       }
-    }
-  }
-
-  public ServiceComponentHostSummary(AmbariMetaInfo ambariMetaInfo, HostEntity host, StackId stackId) throws AmbariException {
-    this(ambariMetaInfo, host, stackId.getStackName(), stackId.getStackVersion());
-  }
-
-  public Collection<HostComponentStateEntity> getHaveAdvertisedVersion() {
-    return haveAdvertisedVersion;
-  }
 
-  public boolean isUpgradeFinished() {
-    return haveAllComponentsFinishedAdvertisingVersion() && noComponentVersionMismatches(getHaveAdvertisedVersion());
-  }
-
-  /**
-   * @param upgradeEntity Upgrade info about update on given host
-   * @return Return true if multiple component versions are found for this host, or if it does not coincide with the
-   * CURRENT repo version.
-   */
-  public boolean isUpgradeInProgress(UpgradeEntity upgradeEntity) {
-    // Exactly one CURRENT version must exist
-    // We can only detect an upgrade if the Host has at least one component that advertises a version and has done so already
-    // If distinct versions have been advertises, then an upgrade is in progress.
-    // If exactly one version has been advertises, but it doesn't coincide with the CURRENT HostVersion, then an upgrade is in progress.
-    return upgradeEntity != null;
-  }
+      String versionAdvertised = hostComponentStateEntity.getVersion();
+      if (hostComponentStateEntity.getUpgradeState() == UpgradeState.IN_PROGRESS
+          || StringUtils.equals(versionAdvertised, State.UNKNOWN.name())) {
+        waitingToAdvertiseVersion.add(hostComponentStateEntity);
+        continue;
+      }
 
-  /**
-   * Determine if all of the components on that need to advertise a version have finished doing so.
-   * @return Return a bool indicating if all components that can report a version have done so.
-   */
-  public boolean haveAllComponentsFinishedAdvertisingVersion() {
-    return waitingToAdvertiseVersion.isEmpty();
+      haveAdvertisedVersion.add(hostComponentStateEntity);
+    }
   }
 
   /**
-   * Checks that every component has really advertised version (in other words, we are not waiting
-   * for version advertising), and that no version mismatch occurred
+   * Gets whether all hosts for a service component have reported the correct
+   * version.
    *
-   * @param hostComponents host components
-   * @return true if components have advertised the same version, or collection is empty, false otherwise.
+   * @param repositoryVersion
+   *          the version to report (not {@code null}).
+   * @return {@code true} if all hosts for this service component have reported
+   *         the correct version, {@code false} othwerise.
    */
-  public static boolean noComponentVersionMismatches(Collection<HostComponentStateEntity> hostComponents) {
-    for (HostComponentStateEntity hostComponent : hostComponents) {
+  public boolean isVersionCorrectForAllHosts(RepositoryVersionEntity repositoryVersion) {
+    if (!waitingToAdvertiseVersion.isEmpty()) {
+      return false;
+    }
+
+    for (HostComponentStateEntity hostComponent : haveAdvertisedVersion) {
       if (UpgradeState.VERSION_NON_ADVERTISED_STATES.contains(hostComponent.getUpgradeState())) {
         return false;
       }
+
+      ServiceComponentDesiredStateEntity desiredState = hostComponent.getServiceComponentDesiredStateEntity();
+      RepositoryVersionEntity desiredRepositoryVersion = desiredState.getDesiredRepositoryVersion();
+      if (!desiredRepositoryVersion.equals(repositoryVersion)) {
+        continue;
+      }
+
+      if (!StringUtils.equals(hostComponent.getVersion(), desiredRepositoryVersion.getVersion())) {
+        return false;
+      }
     }
+
     return true;
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/AbstractUpgradeCatalog.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/AbstractUpgradeCatalog.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/AbstractUpgradeCatalog.java
index 04f5ec3..e1e4ef6 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/AbstractUpgradeCatalog.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/AbstractUpgradeCatalog.java
@@ -17,10 +17,6 @@
  */
 package org.apache.ambari.server.upgrade;
 
-import javax.persistence.EntityManager;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
 import java.io.File;
 import java.io.FileReader;
 import java.io.StringReader;
@@ -42,6 +38,10 @@ import java.util.Stack;
 import java.util.StringTokenizer;
 import java.util.TreeMap;
 
+import javax.persistence.EntityManager;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.configuration.Configuration;
@@ -102,8 +102,6 @@ public abstract class AbstractUpgradeCatalog implements UpgradeCatalog {
   protected DBAccessor dbAccessor;
   @Inject
   protected Configuration configuration;
-  @Inject
-  protected StackUpgradeUtil stackUpgradeUtil;
 
   protected Injector injector;
 
@@ -139,7 +137,7 @@ public abstract class AbstractUpgradeCatalog implements UpgradeCatalog {
   private static final Logger LOG = LoggerFactory.getLogger
     (AbstractUpgradeCatalog.class);
   private static final Map<String, UpgradeCatalog> upgradeCatalogMap =
-    new HashMap<String, UpgradeCatalog>();
+    new HashMap<>();
 
   protected String ambariUpgradeConfigUpdatesFileName;
 
@@ -374,9 +372,9 @@ public abstract class AbstractUpgradeCatalog implements UpgradeCatalog {
 
     if (clusterMap != null && !clusterMap.isEmpty()) {
       for (Cluster cluster : clusterMap.values()) {
-        Map<String, Set<String>> toAddProperties = new HashMap<String, Set<String>>();
-        Map<String, Set<String>> toUpdateProperties = new HashMap<String, Set<String>>();
-        Map<String, Set<String>> toRemoveProperties = new HashMap<String, Set<String>>();
+        Map<String, Set<String>> toAddProperties = new HashMap<>();
+        Map<String, Set<String>> toUpdateProperties = new HashMap<>();
+        Map<String, Set<String>> toRemoveProperties = new HashMap<>();
 
 
         Set<PropertyInfo> stackProperties = configHelper.getStackProperties(cluster);
@@ -494,7 +492,7 @@ public abstract class AbstractUpgradeCatalog implements UpgradeCatalog {
 
     if (clusterMap != null && !clusterMap.isEmpty()) {
       for (Cluster cluster : clusterMap.values()) {
-        Map<String, String> properties = new HashMap<String, String>();
+        Map<String, String> properties = new HashMap<>();
 
         for(String propertyName:propertyNames) {
           String propertyValue = configHelper.getPropertyValueFromStackDefinitions(cluster, configType, propertyName);
@@ -548,7 +546,7 @@ public abstract class AbstractUpgradeCatalog implements UpgradeCatalog {
               "Skipping configuration properties update");
           return;
         } else if (oldConfig == null) {
-          oldConfigProperties = new HashMap<String, String>();
+          oldConfigProperties = new HashMap<>();
         } else {
           oldConfigProperties = oldConfig.getProperties();
         }
@@ -655,7 +653,7 @@ public abstract class AbstractUpgradeCatalog implements UpgradeCatalog {
                                Map<String, String> newProperties,
                                boolean updateIfExists, Multimap<AbstractUpgradeCatalog.ConfigUpdateType, Entry<String, String>> propertiesToLog) {
 
-    Map<String, String> properties = new HashMap<String, String>(originalProperties);
+    Map<String, String> properties = new HashMap<>(originalProperties);
     for (Map.Entry<String, String> entry : newProperties.entrySet()) {
       if (!properties.containsKey(entry.getKey())) {
         properties.put(entry.getKey(), entry.getValue());
@@ -671,12 +669,12 @@ public abstract class AbstractUpgradeCatalog implements UpgradeCatalog {
 
   private Map<String, String> removeProperties(Map<String, String> originalProperties,
                                                Set<String> removeList, Multimap<AbstractUpgradeCatalog.ConfigUpdateType, Entry<String, String>> propertiesToLog){
-    Map<String, String> properties = new HashMap<String, String>();
+    Map<String, String> properties = new HashMap<>();
     properties.putAll(originalProperties);
     for (String removeProperty: removeList){
       if (originalProperties.containsKey(removeProperty)){
         properties.remove(removeProperty);
-        propertiesToLog.put(ConfigUpdateType.REMOVED, new AbstractMap.SimpleEntry<String, String>(removeProperty, ""));
+        propertiesToLog.put(ConfigUpdateType.REMOVED, new AbstractMap.SimpleEntry<>(removeProperty, ""));
       }
     }
     return properties;
@@ -780,7 +778,7 @@ public abstract class AbstractUpgradeCatalog implements UpgradeCatalog {
     ArtifactDAO artifactDAO = injector.getInstance(ArtifactDAO.class);
     KerberosDescriptor artifactDescriptor = null;
     ArtifactEntity artifactEntity = artifactDAO.findByNameAndForeignKeys("kerberos_descriptor",
-        new TreeMap<String, String>(Collections.singletonMap("cluster", String.valueOf(cluster.getClusterId()))));
+        new TreeMap<>(Collections.singletonMap("cluster", String.valueOf(cluster.getClusterId()))));
     if (artifactEntity != null) {
       Map<String, Object> data = artifactEntity.getArtifactData();
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java
index af2a7d1..a1d2b87 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java
@@ -39,13 +39,13 @@ import org.apache.ambari.server.utils.EventBusSynchronizer;
 import org.apache.ambari.server.utils.VersionUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.jdbc.support.JdbcUtils;
 
 import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
 import com.google.inject.multibindings.Multibinder;
 import com.google.inject.persist.PersistService;
-import org.springframework.jdbc.support.JdbcUtils;
 
 public class SchemaUpgradeHelper {
   private static final Logger LOG = LoggerFactory.getLogger
@@ -178,19 +178,6 @@ public class SchemaUpgradeHelper {
       // Add binding to each newly created catalog
       Multibinder<UpgradeCatalog> catalogBinder =
         Multibinder.newSetBinder(binder(), UpgradeCatalog.class);
-      catalogBinder.addBinding().to(UpgradeCatalog200.class);
-      catalogBinder.addBinding().to(UpgradeCatalog210.class);
-      catalogBinder.addBinding().to(UpgradeCatalog211.class);
-      catalogBinder.addBinding().to(UpgradeCatalog212.class);
-      catalogBinder.addBinding().to(UpgradeCatalog2121.class);
-      catalogBinder.addBinding().to(UpgradeCatalog220.class);
-      catalogBinder.addBinding().to(UpgradeCatalog221.class);
-      catalogBinder.addBinding().to(UpgradeCatalog222.class);
-      catalogBinder.addBinding().to(UpgradeCatalog230.class);
-      catalogBinder.addBinding().to(UpgradeCatalog240.class);
-      catalogBinder.addBinding().to(UpgradeCatalog2402.class);
-      catalogBinder.addBinding().to(UpgradeCatalog242.class);
-      catalogBinder.addBinding().to(UpgradeCatalog250.class);
       catalogBinder.addBinding().to(UpgradeCatalog251.class);
       catalogBinder.addBinding().to(UpgradeCatalog252.class);
       catalogBinder.addBinding().to(FinalUpgradeCatalog.class);

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/StackUpgradeHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/StackUpgradeHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/StackUpgradeHelper.java
deleted file mode 100644
index b1c643a..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/StackUpgradeHelper.java
+++ /dev/null
@@ -1,172 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.ambari.server.upgrade;
-
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.InputMismatchException;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import javax.ws.rs.core.UriInfo;
-
-import org.apache.ambari.server.controller.ControllerModule;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.dao.MetainfoDAO;
-import org.apache.ambari.server.orm.entities.MetainfoEntity;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.gson.Gson;
-import com.google.inject.Guice;
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
-import com.google.inject.persist.Transactional;
-
-public class StackUpgradeHelper {
-  private static final Logger LOG = LoggerFactory.getLogger
-    (StackUpgradeHelper.class);
-
-  private static final String STACK_ID_UPDATE_ACTION = "updateStackId";
-  private static final String METAINFO_UPDATE_ACTION = "updateMetaInfo";
-  private static final String STACK_ID_STACK_NAME_KEY = "stackName";
-
-  @Inject
-  private DBAccessor dbAccessor;
-  @Inject
-  private PersistService persistService;
-  @Inject
-  private MetainfoDAO metainfoDAO;
-  @Inject
-  private StackUpgradeUtil stackUpgradeUtil;
-
-  private void startPersistenceService() {
-    persistService.start();
-  }
-
-  private void stopPersistenceService() {
-    persistService.stop();
-  }
-
-  /**
-   * Add key value to the metainfo table.
-   * @param data
-   * @throws SQLException
-   */
-  @Transactional
-  void updateMetaInfo(Map<String, String> data) throws SQLException {
-    if (data != null && !data.isEmpty()) {
-      for (Map.Entry<String, String> entry : data.entrySet()) {
-        MetainfoEntity metainfoEntity = metainfoDAO.findByKey(entry.getKey());
-        if (metainfoEntity != null) {
-          metainfoEntity.setMetainfoName(entry.getKey());
-          metainfoEntity.setMetainfoValue(entry.getValue());
-          metainfoDAO.merge(metainfoEntity);
-        } else {
-          metainfoEntity = new MetainfoEntity();
-          metainfoEntity.setMetainfoName(entry.getKey());
-          metainfoEntity.setMetainfoValue(entry.getValue());
-          metainfoDAO.create(metainfoEntity);
-        }
-      }
-    }
-  }
-
-  /**
-   * Change the stack id in the Ambari DB.
-   * @param stackInfo
-   * @throws SQLException
-   */
-  public void updateStackVersion(Map<String, String> stackInfo) throws Exception {
-    if (stackInfo == null || stackInfo.isEmpty()) {
-      throw new IllegalArgumentException("Empty stack id. " + stackInfo);
-    }
-    
-    String repoUrl = stackInfo.remove("repo_url");
-    String repoUrlOs = stackInfo.remove("repo_url_os");
-    
-    Iterator<Map.Entry<String, String>> stackIdEntry = stackInfo.entrySet().iterator();
-    Map.Entry<String, String> stackEntry = stackIdEntry.next();
-
-    String stackName = stackEntry.getKey();
-    String stackVersion = stackEntry.getValue();
-
-    LOG.info("Updating stack id, stackName = " + stackName + ", " +
-      "stackVersion = "+ stackVersion);
-
-    stackUpgradeUtil.updateStackDetails(stackName, stackVersion);
-    
-    if (null != repoUrl) {
-      stackUpgradeUtil.updateLocalRepo(stackName, stackVersion, repoUrl, repoUrlOs);  
-    }
-
-    dbAccessor.updateTable("hostcomponentstate", "current_state", "INSTALLED", "where current_state = 'UPGRADING'");
-  }
-
-  private List<String> getValidActions() {
-    return new ArrayList<String>() {{
-      add(STACK_ID_UPDATE_ACTION);
-      add(METAINFO_UPDATE_ACTION);
-    }};
-  }
-
-  /**
-   * Support changes need to support upgrade of Stack
-   * @param args Simple key value json map
-   */
-  public static void main(String[] args) {
-    try {
-      if (args.length < 2) {
-        throw new InputMismatchException("Need to provide action, " +
-          "stack name and stack version.");
-      }
-
-      String action = args[0];
-      String valueMap = args[1];
-
-      Injector injector = Guice.createInjector(new ControllerModule());
-      StackUpgradeHelper stackUpgradeHelper = injector.getInstance(StackUpgradeHelper.class);
-      Gson gson = injector.getInstance(Gson.class);
-
-      if (!stackUpgradeHelper.getValidActions().contains(action)) {
-        throw new IllegalArgumentException("Unsupported action. Allowed " +
-          "actions: " + stackUpgradeHelper.getValidActions());
-      }
-
-      
-      stackUpgradeHelper.startPersistenceService();
-      Map values = gson.fromJson(valueMap, Map.class);
-
-      if (action.equals(STACK_ID_UPDATE_ACTION)) {
-        stackUpgradeHelper.updateStackVersion(values);
-        
-      } else if (action.equals(METAINFO_UPDATE_ACTION)) {
-
-        stackUpgradeHelper.updateMetaInfo(values);
-      }
-
-      stackUpgradeHelper.stopPersistenceService();
-
-    } catch (Throwable t) {
-      LOG.error("Caught exception on upgrade. Exiting...", t);
-      System.exit(1);
-    }
-  }
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/StackUpgradeUtil.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/StackUpgradeUtil.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/StackUpgradeUtil.java
deleted file mode 100644
index 8c629ca..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/StackUpgradeUtil.java
+++ /dev/null
@@ -1,197 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.ambari.server.upgrade;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.ClusterStateDAO;
-import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
-import org.apache.ambari.server.orm.dao.MetainfoDAO;
-import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.ServiceDesiredStateDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterStateEntity;
-import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
-import org.apache.ambari.server.orm.entities.MetainfoEntity;
-import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.ServiceDesiredStateEntity;
-import org.apache.ambari.server.orm.entities.StackEntity;
-import org.apache.ambari.server.state.OperatingSystemInfo;
-import org.apache.ambari.server.state.stack.OsFamily;
-
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import com.google.inject.persist.Transactional;
-
-public class StackUpgradeUtil {
-  @Inject
-  private Injector injector;
-
-  @Transactional
-  public void updateStackDetails(String stackName, String stackVersion) {
-    ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
-    StackDAO stackDAO = injector.getInstance(StackDAO.class);
-    List<Long> clusterIds = new ArrayList<Long>();
-
-    StackEntity stackEntity = stackDAO.find(stackName, stackVersion);
-
-    List<ClusterEntity> clusterEntities = clusterDAO.findAll();
-    if (clusterEntities != null && !clusterEntities.isEmpty()) {
-      for (ClusterEntity entity : clusterEntities) {
-        clusterIds.add(entity.getClusterId());
-        entity.setDesiredStack(stackEntity);
-        clusterDAO.merge(entity);
-      }
-    }
-
-    ClusterStateDAO clusterStateDAO = injector.getInstance(ClusterStateDAO.class);
-
-    for (Long clusterId : clusterIds) {
-      ClusterStateEntity clusterStateEntity = clusterStateDAO.findByPK(clusterId);
-      clusterStateEntity.setCurrentStack(stackEntity);
-      clusterStateDAO.merge(clusterStateEntity);
-    }
-
-    HostComponentStateDAO hostComponentStateDAO = injector.getInstance
-      (HostComponentStateDAO.class);
-    List<HostComponentStateEntity> hcEntities = hostComponentStateDAO.findAll();
-
-    if (hcEntities != null) {
-      for (HostComponentStateEntity hc : hcEntities) {
-        hc.setCurrentStack(stackEntity);
-        hostComponentStateDAO.merge(hc);
-      }
-    }
-
-    HostComponentDesiredStateDAO hostComponentDesiredStateDAO =
-      injector.getInstance(HostComponentDesiredStateDAO.class);
-
-    List<HostComponentDesiredStateEntity> hcdEntities = hostComponentDesiredStateDAO.findAll();
-
-    if (hcdEntities != null) {
-      for (HostComponentDesiredStateEntity hcd : hcdEntities) {
-        hcd.setDesiredStack(stackEntity);
-        hostComponentDesiredStateDAO.merge(hcd);
-      }
-    }
-
-    ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO =
-      injector.getInstance(ServiceComponentDesiredStateDAO.class);
-
-    List<ServiceComponentDesiredStateEntity> scdEntities =
-      serviceComponentDesiredStateDAO.findAll();
-
-    if (scdEntities != null) {
-      for (ServiceComponentDesiredStateEntity scd : scdEntities) {
-        scd.setDesiredStack(stackEntity);
-        serviceComponentDesiredStateDAO.merge(scd);
-      }
-    }
-
-    ServiceDesiredStateDAO serviceDesiredStateDAO = injector.getInstance(ServiceDesiredStateDAO.class);
-
-    List<ServiceDesiredStateEntity> sdEntities = serviceDesiredStateDAO.findAll();
-
-    if (sdEntities != null) {
-      for (ServiceDesiredStateEntity sd : sdEntities) {
-        sd.setDesiredStack(stackEntity);
-        serviceDesiredStateDAO.merge(sd);
-      }
-    }
-  }
-
-  /**
-   * @param stackName
-   * @param stackVersion
-   * @param repoUrl
-   * @param repoUrlOs
-   * @throws Exception
-   */
-  public void updateLocalRepo(String stackName, String stackVersion,
-      String repoUrl, String repoUrlOs) throws Exception {
-
-    if (null == repoUrl ||
-        repoUrl.isEmpty() ||
-        !repoUrl.startsWith("http")) {
-      return;
-    }
-
-    String[] oses = new String[0];
-
-    if (null != repoUrlOs) {
-      oses = repoUrlOs.split(",");
-    }
-
-    AmbariMetaInfo ami = injector.getInstance(AmbariMetaInfo.class);
-    MetainfoDAO metaDao = injector.getInstance(MetainfoDAO.class);
-    OsFamily os_family = injector.getInstance(OsFamily.class);
-
-    String stackRepoId = stackName + "-" + stackVersion;
-
-    if (0 == oses.length) {
-      // do them all
-      for (OperatingSystemInfo osi : ami.getOperatingSystems(stackName, stackVersion)) {
-        ami.updateRepoBaseURL(stackName, stackVersion, osi.getOsType(),
-            stackRepoId, repoUrl);
-      }
-
-    } else {
-      for (String os : oses) {
-
-        String family = os_family.find(os);
-        if (null != family) {
-          String key = ami.generateRepoMetaKey(stackName, stackVersion, os,
-              stackRepoId, AmbariMetaInfo.REPOSITORY_XML_PROPERTY_BASEURL);
-
-          String familyKey = ami.generateRepoMetaKey(stackName, stackVersion, family,
-              stackRepoId, AmbariMetaInfo.REPOSITORY_XML_PROPERTY_BASEURL);
-
-          // need to use (for example) redhat6 if the os is centos6
-          MetainfoEntity entity = metaDao.findByKey(key);
-          if (null == entity) {
-            entity = new MetainfoEntity();
-            entity.setMetainfoName(key);
-            entity.setMetainfoValue(repoUrl);
-            metaDao.merge(entity);
-          } else {
-            entity.setMetainfoValue(repoUrl);
-            metaDao.merge(entity);
-          }
-
-          entity = metaDao.findByKey(familyKey);
-          if (null == entity) {
-            entity = new MetainfoEntity();
-            entity.setMetainfoName(familyKey);
-            entity.setMetainfoValue(repoUrl);
-            metaDao.merge(entity);
-          } else {
-            entity.setMetainfoValue(repoUrl);
-            metaDao.merge(entity);
-          }
-        }
-      }
-    }
-  }
-
-}
\ No newline at end of file


[15/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/StateRecoveryManagerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/StateRecoveryManagerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/StateRecoveryManagerTest.java
index 3cb6b4b..b048d04 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/StateRecoveryManagerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/StateRecoveryManagerTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -18,46 +18,47 @@
 
 package org.apache.ambari.server;
 
-import com.google.inject.AbstractModule;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.util.Modules;
+import static org.easymock.EasyMock.capture;
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.replay;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import java.sql.SQLException;
+
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
+import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
+import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
 import org.apache.ambari.server.state.RepositoryVersionState;
 import org.easymock.Capture;
+import org.easymock.EasyMock;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
-import java.sql.SQLException;
-import java.util.ArrayList;
-
-import static org.easymock.EasyMock.and;
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.createNiceMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.replay;
-import static org.junit.Assert.*;
+import com.google.common.collect.Lists;
+import com.google.inject.AbstractModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.util.Modules;
 
 public class StateRecoveryManagerTest {
 
   private Injector injector;
   private HostVersionDAO hostVersionDAOMock;
-  private ClusterVersionDAO clusterVersionDAOMock;
+  private ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAOMock;
 
   @Before
   public void setup() throws Exception {
     // Create instances of mocks
-    clusterVersionDAOMock = createNiceMock(ClusterVersionDAO.class);
     hostVersionDAOMock = createNiceMock(HostVersionDAO.class);
+    serviceComponentDesiredStateDAOMock = createNiceMock(ServiceComponentDesiredStateDAO.class);
     // Initialize injector
     InMemoryDefaultTestModule module = new InMemoryDefaultTestModule();
     injector = Guice.createInjector(Modules.override(module).with(new MockModule()));
@@ -75,43 +76,41 @@ public class StateRecoveryManagerTest {
 
     // Adding all possible host version states
 
-    final Capture<RepositoryVersionState> installFailedHostVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> installingHostVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> installedHostVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> outOfSyncHostVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> upgradeFailedHostVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> upgradingHostVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> upgradedHostVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> currentHostVersionCapture = new Capture<RepositoryVersionState>();
-
-    expect(hostVersionDAOMock.findAll()).andReturn(new ArrayList<HostVersionEntity>() {{
-      add(getHostVersionMock("install_failed_version", RepositoryVersionState.INSTALL_FAILED, installFailedHostVersionCapture));
-      add(getHostVersionMock("installing_version", RepositoryVersionState.INSTALLING, installingHostVersionCapture));
-      add(getHostVersionMock("installed_version", RepositoryVersionState.INSTALLED, installedHostVersionCapture));
-      add(getHostVersionMock("out_of_sync_version", RepositoryVersionState.OUT_OF_SYNC, outOfSyncHostVersionCapture));
-      add(getHostVersionMock("current_version", RepositoryVersionState.CURRENT, currentHostVersionCapture));
-    }});
+    final Capture<RepositoryVersionState> installFailedHostVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> installingHostVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> installedHostVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> outOfSyncHostVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> upgradeFailedHostVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> upgradingHostVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> upgradedHostVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> currentHostVersionCapture = EasyMock.newCapture();
+
+    expect(hostVersionDAOMock.findAll()).andReturn(Lists.newArrayList(
+      getHostVersionMock("install_failed_version", RepositoryVersionState.INSTALL_FAILED, installFailedHostVersionCapture),
+      getHostVersionMock("installing_version", RepositoryVersionState.INSTALLING, installingHostVersionCapture),
+      getHostVersionMock("installed_version", RepositoryVersionState.INSTALLED, installedHostVersionCapture),
+      getHostVersionMock("out_of_sync_version", RepositoryVersionState.OUT_OF_SYNC, outOfSyncHostVersionCapture),
+      getHostVersionMock("current_version", RepositoryVersionState.CURRENT, currentHostVersionCapture)));
 
     // Adding all possible cluster version states
 
-    final Capture<RepositoryVersionState> installFailedClusterVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> installingClusterVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> installedClusterVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> outOfSyncClusterVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> upgradeFailedClusterVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> upgradingClusterVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> upgradedClusterVersionCapture = new Capture<RepositoryVersionState>();
-    final Capture<RepositoryVersionState> currentClusterVersionCapture = new Capture<RepositoryVersionState>();
-
-    expect(clusterVersionDAOMock.findAll()).andReturn(new ArrayList<ClusterVersionEntity>() {{
-      add(getClusterVersionMock("install_failed_version", RepositoryVersionState.INSTALL_FAILED, installFailedClusterVersionCapture));
-      add(getClusterVersionMock("installing_version", RepositoryVersionState.INSTALLING, installingClusterVersionCapture));
-      add(getClusterVersionMock("installed_version", RepositoryVersionState.INSTALLED, installedClusterVersionCapture));
-      add(getClusterVersionMock("out_of_sync_version", RepositoryVersionState.OUT_OF_SYNC, outOfSyncClusterVersionCapture));
-      add(getClusterVersionMock("current_version", RepositoryVersionState.CURRENT, currentClusterVersionCapture));
-    }});
-
-    replay(hostVersionDAOMock, clusterVersionDAOMock);
+    final Capture<RepositoryVersionState> installFailedClusterVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> installingClusterVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> installedClusterVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> outOfSyncClusterVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> upgradeFailedClusterVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> upgradingClusterVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> upgradedClusterVersionCapture = EasyMock.newCapture();
+    final Capture<RepositoryVersionState> currentClusterVersionCapture = EasyMock.newCapture();
+
+    expect(serviceComponentDesiredStateDAOMock.findAll()).andReturn(Lists.newArrayList(
+      getDesiredStateEntityMock("install_failed_version", RepositoryVersionState.INSTALL_FAILED, installFailedClusterVersionCapture),
+      getDesiredStateEntityMock("installing_version", RepositoryVersionState.INSTALLING, installingClusterVersionCapture),
+      getDesiredStateEntityMock("installed_version", RepositoryVersionState.INSTALLED, installedClusterVersionCapture),
+      getDesiredStateEntityMock("out_of_sync_version", RepositoryVersionState.OUT_OF_SYNC, outOfSyncClusterVersionCapture),
+      getDesiredStateEntityMock("current_version", RepositoryVersionState.CURRENT, currentClusterVersionCapture)));
+
+    replay(hostVersionDAOMock, serviceComponentDesiredStateDAOMock);
 
     stateRecoveryManager.checkHostAndClusterVersions();
 
@@ -156,35 +155,30 @@ public class StateRecoveryManagerTest {
     return hvMock;
   }
 
+  private ServiceComponentDesiredStateEntity getDesiredStateEntityMock(String name, RepositoryVersionState state, Capture<RepositoryVersionState> newStateCapture) {
 
-  private ClusterVersionEntity getClusterVersionMock(String name, RepositoryVersionState state,
-                                               Capture<RepositoryVersionState> newStateCaptor) {
-    ClusterVersionEntity cvMock = createNiceMock(ClusterVersionEntity.class);
-    expect(cvMock.getState()).andReturn(state);
-
-    cvMock.setState(capture(newStateCaptor));
+    ServiceComponentDesiredStateEntity mock = createNiceMock(ServiceComponentDesiredStateEntity.class);
+    expect(mock.getRepositoryState()).andReturn(state);
+    mock.setRepositoryState(capture(newStateCapture));
     expectLastCall();
 
-    RepositoryVersionEntity rvMock = createNiceMock(RepositoryVersionEntity.class);
-    expect(rvMock.getDisplayName()).andReturn(name);
+    RepositoryVersionEntity repositoryVersionMock = createNiceMock(RepositoryVersionEntity.class);
+    expect(repositoryVersionMock.getVersion()).andReturn(name);
 
-    expect(cvMock.getRepositoryVersion()).andReturn(rvMock);
+    expect(mock.getDesiredRepositoryVersion()).andReturn(repositoryVersionMock);
 
-    ClusterEntity ceMock = createNiceMock(ClusterEntity.class);
-    expect(ceMock.getClusterName()).andReturn("somecluster");
+    replay(mock, repositoryVersionMock);
 
-    expect(cvMock.getClusterEntity()).andReturn(ceMock);
+    return mock;
+  }
 
-    replay(cvMock, rvMock, ceMock);
 
-    return cvMock;
-  }
 
   public class MockModule extends AbstractModule {
     @Override
     protected void configure() {
       bind(HostVersionDAO.class).toInstance(hostVersionDAOMock);
-      bind(ClusterVersionDAO.class).toInstance(clusterVersionDAOMock);
+      bind(ServiceComponentDesiredStateDAO.class).toInstance(serviceComponentDesiredStateDAOMock);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapperTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapperTest.java b/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapperTest.java
index c28ba5d..64a1e3a 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapperTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/ExecutionCommandWrapperTest.java
@@ -36,6 +36,8 @@ import org.apache.ambari.server.agent.AgentCommand.AgentCommandType;
 import org.apache.ambari.server.agent.ExecutionCommand;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.orm.OrmTestHelper;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ConfigFactory;
@@ -45,14 +47,13 @@ import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStartEvent
 import org.apache.ambari.server.utils.StageUtils;
 import org.codehaus.jettison.json.JSONException;
 import org.junit.AfterClass;
+import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 
-import junit.framework.Assert;
-
 public class ExecutionCommandWrapperTest {
 
   private static final String HOST1 = "dev01.ambari.apache.org";
@@ -109,25 +110,25 @@ public class ExecutionCommandWrapperTest {
 
     Cluster cluster1 = clusters.getCluster(CLUSTER1);
 
-    SERVICE_SITE_CLUSTER = new HashMap<String, String>();
+    SERVICE_SITE_CLUSTER = new HashMap<>();
     SERVICE_SITE_CLUSTER.put(SERVICE_SITE_NAME1, SERVICE_SITE_VAL1);
     SERVICE_SITE_CLUSTER.put(SERVICE_SITE_NAME2, SERVICE_SITE_VAL2);
     SERVICE_SITE_CLUSTER.put(SERVICE_SITE_NAME3, SERVICE_SITE_VAL3);
     SERVICE_SITE_CLUSTER.put(SERVICE_SITE_NAME4, SERVICE_SITE_VAL4);
 
-    SERVICE_SITE_SERVICE = new HashMap<String, String>();
+    SERVICE_SITE_SERVICE = new HashMap<>();
     SERVICE_SITE_SERVICE.put(SERVICE_SITE_NAME1, SERVICE_SITE_VAL1_S);
     SERVICE_SITE_SERVICE.put(SERVICE_SITE_NAME5, SERVICE_SITE_VAL5_S);
 
-    SERVICE_SITE_HOST = new HashMap<String, String>();
+    SERVICE_SITE_HOST = new HashMap<>();
     SERVICE_SITE_HOST.put(SERVICE_SITE_NAME2, SERVICE_SITE_VAL2_H);
     SERVICE_SITE_HOST.put(SERVICE_SITE_NAME6, SERVICE_SITE_VAL6_H);
 
-    GLOBAL_CLUSTER = new HashMap<String, String>();
+    GLOBAL_CLUSTER = new HashMap<>();
     GLOBAL_CLUSTER.put(GLOBAL_NAME1, GLOBAL_CLUSTER_VAL1);
     GLOBAL_CLUSTER.put(GLOBAL_NAME2, GLOBAL_CLUSTER_VAL2);
 
-    CONFIG_ATTRIBUTES = new HashMap<String, Map<String,String>>();
+    CONFIG_ATTRIBUTES = new HashMap<>();
 
     //Cluster level global config
     configFactory.createNew(cluster1, GLOBAL_CONFIG, CLUSTER_VERSION_TAG, GLOBAL_CLUSTER, CONFIG_ATTRIBUTES);
@@ -155,7 +156,7 @@ public class ExecutionCommandWrapperTest {
         RoleCommand.START,
         new ServiceComponentHostStartEvent(Role.NAMENODE.toString(),
             hostName, System.currentTimeMillis()), clusterName, "HDFS", false, false);
-    List<Stage> stages = new ArrayList<Stage>();
+    List<Stage> stages = new ArrayList<>();
     stages.add(s);
     Request request = new Request(stages, "clusterHostInfo", clusters);
     db.persistActions(request);
@@ -164,14 +165,20 @@ public class ExecutionCommandWrapperTest {
   @Test
   public void testGetExecutionCommand() throws JSONException, AmbariException {
 
+    Cluster cluster = clusters.getCluster(CLUSTER1);
+
+    OrmTestHelper helper = injector.getInstance(OrmTestHelper.class);
+    RepositoryVersionEntity repositoryVersion = helper.getOrCreateRepositoryVersion(cluster);
+
+    cluster.addService("HDFS", repositoryVersion);
 
-    Map<String, Map<String, String>> confs = new HashMap<String, Map<String, String>>();
-    Map<String, String> configurationsGlobal = new HashMap<String, String>();
+    Map<String, Map<String, String>> confs = new HashMap<>();
+    Map<String, String> configurationsGlobal = new HashMap<>();
     configurationsGlobal.put(GLOBAL_NAME1, GLOBAL_VAL1);
     confs.put(GLOBAL_CONFIG, configurationsGlobal);
 
-    Map<String, Map<String, String>> confTags = new HashMap<String, Map<String, String>>();
-    Map<String, String> confTagServiceSite = new HashMap<String, String>();
+    Map<String, Map<String, String>> confTags = new HashMap<>();
+    Map<String, String> confTagServiceSite = new HashMap<>();
 
     confTagServiceSite.put("tag", CLUSTER_VERSION_TAG);
     confTagServiceSite.put("service_override_tag", SERVICE_VERSION_TAG);
@@ -223,7 +230,7 @@ public class ExecutionCommandWrapperTest {
 
 
     //Union of all keys of service site configs
-    Set<String> serviceSiteKeys = new HashSet<String>();
+    Set<String> serviceSiteKeys = new HashSet<>();
     serviceSiteKeys.addAll(SERVICE_SITE_CLUSTER.keySet());
     serviceSiteKeys.addAll(SERVICE_SITE_SERVICE.keySet());
     serviceSiteKeys.addAll(SERVICE_SITE_HOST.keySet());
@@ -234,7 +241,7 @@ public class ExecutionCommandWrapperTest {
 
   @Test
   public void testGetMergedConfig() {
-    Map<String, String> baseConfig = new HashMap<String, String>();
+    Map<String, String> baseConfig = new HashMap<>();
 
     baseConfig.put(SERVICE_SITE_NAME1, SERVICE_SITE_VAL1);
     baseConfig.put(SERVICE_SITE_NAME2, SERVICE_SITE_VAL2);
@@ -242,7 +249,7 @@ public class ExecutionCommandWrapperTest {
     baseConfig.put(SERVICE_SITE_NAME4, SERVICE_SITE_VAL4);
     baseConfig.put(SERVICE_SITE_NAME5, SERVICE_SITE_VAL5);
 
-    Map<String, String> overrideConfig = new HashMap<String, String>();
+    Map<String, String> overrideConfig = new HashMap<>();
 
     overrideConfig.put(SERVICE_SITE_NAME2, SERVICE_SITE_VAL2_H);
     overrideConfig.put(SERVICE_SITE_NAME6, SERVICE_SITE_VAL6_H);
@@ -252,7 +259,7 @@ public class ExecutionCommandWrapperTest {
       overrideConfig);
 
 
-    Set<String> configsKeys = new HashSet<String>();
+    Set<String> configsKeys = new HashSet<>();
     configsKeys.addAll(baseConfig.keySet());
     configsKeys.addAll(overrideConfig.keySet());
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/alerts/ComponentVersionAlertRunnableTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/alerts/ComponentVersionAlertRunnableTest.java b/ambari-server/src/test/java/org/apache/ambari/server/alerts/ComponentVersionAlertRunnableTest.java
index 6c07138..e41f568 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/alerts/ComponentVersionAlertRunnableTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/alerts/ComponentVersionAlertRunnableTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -38,7 +38,6 @@ import org.apache.ambari.server.events.publishers.AlertEventPublisher;
 import org.apache.ambari.server.orm.DBAccessor;
 import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
 import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
 import org.apache.ambari.server.stack.StackManagerFactory;
@@ -48,9 +47,12 @@ import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ComponentInfo;
 import org.apache.ambari.server.state.Host;
+import org.apache.ambari.server.state.Service;
+import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentHost;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.stack.OsFamily;
+import org.apache.ambari.server.state.stack.upgrade.Direction;
 import org.easymock.EasyMock;
 import org.easymock.EasyMockSupport;
 import org.junit.After;
@@ -140,6 +142,17 @@ public class ComponentVersionAlertRunnableTest extends EasyMockSupport {
     expect(m_desidredStackId.getStackName()).andReturn("SOME-STACK").atLeastOnce();
     expect(m_desidredStackId.getStackVersion()).andReturn("STACK-VERSION").atLeastOnce();
 
+    RepositoryVersionEntity repositoryVersionEntity = createNiceMock(RepositoryVersionEntity.class);
+    expect(repositoryVersionEntity.getVersion()).andReturn(EXPECTED_VERSION).anyTimes();
+
+    // services
+    Service service = createNiceMock(Service.class);
+    expect(service.getDesiredRepositoryVersion()).andReturn(repositoryVersionEntity).atLeastOnce();
+
+    ServiceComponent serviceComponent = createNiceMock(ServiceComponent.class);
+    expect(serviceComponent.getDesiredStackId()).andReturn(m_desidredStackId).atLeastOnce();
+    expect(service.getServiceComponent(EasyMock.anyString())).andReturn(serviceComponent).atLeastOnce();
+
     // components
     ServiceComponentHost sch1_1 = createNiceMock(ServiceComponentHost.class);
     ServiceComponentHost sch1_2 = createNiceMock(ServiceComponentHost.class);
@@ -149,19 +162,15 @@ public class ComponentVersionAlertRunnableTest extends EasyMockSupport {
     expect(sch1_1.getServiceName()).andReturn("FOO").atLeastOnce();
     expect(sch1_1.getServiceComponentName()).andReturn("FOO_COMPONENT").atLeastOnce();
     expect(sch1_1.getVersion()).andReturn(EXPECTED_VERSION).atLeastOnce();
-    expect(sch1_1.getDesiredStackVersion()).andReturn(m_desidredStackId).atLeastOnce();
     expect(sch1_2.getServiceName()).andReturn("BAR").atLeastOnce();
     expect(sch1_2.getServiceComponentName()).andReturn("BAR_COMPONENT").atLeastOnce();
     expect(sch1_2.getVersion()).andReturn(EXPECTED_VERSION).atLeastOnce();
-    expect(sch1_2.getDesiredStackVersion()).andReturn(m_desidredStackId).atLeastOnce();
     expect(sch2_1.getServiceName()).andReturn("FOO").atLeastOnce();
     expect(sch2_1.getServiceComponentName()).andReturn("FOO_COMPONENT").atLeastOnce();
     expect(sch2_1.getVersion()).andReturn(EXPECTED_VERSION).atLeastOnce();
-    expect(sch2_1.getDesiredStackVersion()).andReturn(m_desidredStackId).atLeastOnce();
     expect(sch2_2.getServiceName()).andReturn("BAZ").atLeastOnce();
     expect(sch2_2.getServiceComponentName()).andReturn("BAZ_COMPONENT").atLeastOnce();
     expect(sch2_2.getVersion()).andReturn(EXPECTED_VERSION).atLeastOnce();
-    expect(sch2_2.getDesiredStackVersion()).andReturn(m_desidredStackId).atLeastOnce();
 
     m_hostComponentMap.get(HOSTNAME_1).add(sch1_1);
     m_hostComponentMap.get(HOSTNAME_1).add(sch1_2);
@@ -179,14 +188,7 @@ public class ComponentVersionAlertRunnableTest extends EasyMockSupport {
     expect(m_cluster.getClusterId()).andReturn(CLUSTER_ID).atLeastOnce();
     expect(m_cluster.getClusterName()).andReturn(CLUSTER_NAME).atLeastOnce();
     expect(m_cluster.getHosts()).andReturn(m_hosts).atLeastOnce();
-
-    ClusterVersionEntity clusterVersionEntity = createNiceMock(ClusterVersionEntity.class);
-    RepositoryVersionEntity repositoryVersionEntity = createNiceMock(RepositoryVersionEntity.class);
-    expect(clusterVersionEntity.getRepositoryVersion()).andReturn(
-        repositoryVersionEntity).anyTimes();
-
-    expect(repositoryVersionEntity.getVersion()).andReturn(EXPECTED_VERSION).anyTimes();
-    expect(m_cluster.getCurrentClusterVersion()).andReturn(clusterVersionEntity).anyTimes();
+    expect(m_cluster.getService(EasyMock.anyString())).andReturn(service).atLeastOnce();
 
     // mock clusters
     expect(m_clusters.getClusters()).andReturn(clusterMap).atLeastOnce();
@@ -224,9 +226,8 @@ public class ComponentVersionAlertRunnableTest extends EasyMockSupport {
   @Test
   public void testUpgradeInProgress() throws Exception {
     UpgradeEntity upgrade = createNiceMock(UpgradeEntity.class);
-    expect(upgrade.getToRepositoryVersion()).andReturn(EasyMock.createNiceMock(RepositoryVersionEntity.class)).atLeastOnce();
-    expect(upgrade.getFromRepositoryVersion()).andReturn(EasyMock.createNiceMock(RepositoryVersionEntity.class)).atLeastOnce();
-    expect(m_cluster.getUpgradeInProgress()).andReturn(upgrade).atLeastOnce();
+    expect(upgrade.getDirection()).andReturn(Direction.UPGRADE).atLeastOnce();
+    expect(m_cluster.getUpgradeInProgress()).andReturn(upgrade).once();
 
     replayAll();
 
@@ -305,7 +306,6 @@ public class ComponentVersionAlertRunnableTest extends EasyMockSupport {
     expect(sch.getServiceName()).andReturn("FOO").atLeastOnce();
     expect(sch.getServiceComponentName()).andReturn("FOO_COMPONENT").atLeastOnce();
     expect(sch.getVersion()).andReturn(WRONG_VERSION).atLeastOnce();
-    expect(sch.getDesiredStackVersion()).andReturn(m_desidredStackId).atLeastOnce();
 
     replayAll();
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/UpgradeEventCreatorTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/UpgradeEventCreatorTest.java b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/UpgradeEventCreatorTest.java
index fdc4752..a8e21f6 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/UpgradeEventCreatorTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/UpgradeEventCreatorTest.java
@@ -40,7 +40,7 @@ public class UpgradeEventCreatorTest extends AuditEventCreatorTestBase{
     UpgradeEventCreator creator = new UpgradeEventCreator();
 
     Map<String,Object> properties = new HashMap<>();
-    properties.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION, "1.9");
+    properties.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, "1234");
     properties.put(UpgradeResourceProvider.UPGRADE_TYPE, "ROLLING");
     properties.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "mycluster");
 
@@ -50,7 +50,7 @@ public class UpgradeEventCreatorTest extends AuditEventCreatorTestBase{
     AuditEvent event = AuditEventCreatorTestHelper.getEvent(creator, request, result);
 
     String actual = event.getAuditMessage();
-    String expected = "User(" + userName + "), RemoteIp(1.2.3.4), Operation(Upgrade addition), RequestType(POST), url(http://example.com:8080/api/v1/test), ResultStatus(200 OK), Repository version(1.9), Upgrade type(ROLLING), Cluster name(mycluster)";
+    String expected = "User(" + userName + "), RemoteIp(1.2.3.4), Operation(Upgrade addition), RequestType(POST), url(http://example.com:8080/api/v1/test), ResultStatus(200 OK), Repository version ID(1234), Upgrade type(ROLLING), Cluster name(mycluster)";
 
     Assert.assertTrue("Class mismatch", event instanceof AddUpgradeRequestAuditEvent);
     Assert.assertEquals(expected, actual);

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/checks/HardcodedStackVersionPropertiesCheckTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/HardcodedStackVersionPropertiesCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/HardcodedStackVersionPropertiesCheckTest.java
index 92d79a1..6582cf7 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/checks/HardcodedStackVersionPropertiesCheckTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/HardcodedStackVersionPropertiesCheckTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -18,11 +18,13 @@
 
 package org.apache.ambari.server.checks;
 
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import java.util.regex.Pattern;
 
-import static org.junit.Assert.*;
+import org.junit.Test;
 
 public class HardcodedStackVersionPropertiesCheckTest {
 
@@ -57,4 +59,4 @@ public class HardcodedStackVersionPropertiesCheckTest {
     content = "kgflkfld fdf\nld;ls;f d hdp.version=2.3.4.0-1234 \n sfdfdsfds \n fdsfds";
     assertTrue(HardcodedStackVersionPropertiesCheck.stringContainsVersionHardcode(content, pattern));
   }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/checks/InstallPackagesCheckTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/InstallPackagesCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/InstallPackagesCheckTest.java
index c45c59d..986a0f1 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/checks/InstallPackagesCheckTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/InstallPackagesCheckTest.java
@@ -24,10 +24,8 @@ import java.util.List;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.PrereqCheckRequest;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
@@ -58,7 +56,7 @@ import com.google.inject.Provider;
 @PrepareForTest(HostComponentSummary.class)   // This class has a static method that will be mocked
 public class InstallPackagesCheckTest {
   private final Clusters clusters = Mockito.mock(Clusters.class);
-  private final ClusterVersionDAO clusterVersionDAO = Mockito.mock(ClusterVersionDAO.class);
+
   private final HostVersionDAO hostVersionDAO = Mockito.mock(HostVersionDAO.class);
   private final RepositoryVersionDAO repositoryVersionDAO = Mockito.mock(RepositoryVersionDAO.class);
   private AmbariMetaInfo ambariMetaInfo = Mockito.mock(AmbariMetaInfo.class);
@@ -101,13 +99,6 @@ public class InstallPackagesCheckTest {
       }
     };
 
-    installPackagesCheck.clusterVersionDAOProvider = new Provider<ClusterVersionDAO>() {
-      @Override
-      public ClusterVersionDAO get() {
-        return clusterVersionDAO;
-      }
-    };
-
     installPackagesCheck.hostVersionDaoProvider = new Provider<HostVersionDAO>() {
       @Override
       public HostVersionDAO get() {
@@ -132,17 +123,13 @@ public class InstallPackagesCheckTest {
 
     Mockito.when(cluster.getCurrentStackVersion()).thenReturn(stackId);
     Mockito.when(clusters.getCluster(clusterName)).thenReturn(cluster);
-    ClusterVersionEntity clusterVersionEntity = Mockito.mock(ClusterVersionEntity.class);
-    Mockito.when(clusterVersionEntity.getState()).thenReturn(RepositoryVersionState.INSTALLED);
-    Mockito.when(clusterVersionDAO.findByClusterAndStackAndVersion(
-        clusterName, targetStackId, repositoryVersion)).thenReturn(clusterVersionEntity);
-    final List<String> hostNames = new ArrayList<String>();
+    final List<String> hostNames = new ArrayList<>();
     hostNames.add("host1");
     hostNames.add("host2");
     hostNames.add("host3");
 
-    final List<Host> hosts = new ArrayList<Host>();
-    final List<HostVersionEntity> hostVersionEntities = new ArrayList<HostVersionEntity>();
+    final List<Host> hosts = new ArrayList<>();
+    final List<HostVersionEntity> hostVersionEntities = new ArrayList<>();
     for(String hostName : hostNames) {
       Host host =  Mockito.mock(Host.class);
       Mockito.when(host.getHostName()).thenReturn(hostName);
@@ -168,21 +155,10 @@ public class InstallPackagesCheckTest {
 
     // Case 2: Install Packages failed on host1
     Mockito.when(hostVersionEntities.get(0).getState()).thenReturn(RepositoryVersionState.INSTALL_FAILED);
-    Mockito.when(clusterVersionEntity.getState()).thenReturn(RepositoryVersionState.INSTALL_FAILED);
     check = new PrerequisiteCheck(null, null);
     installPackagesCheck.perform(check, checkRequest);
     Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus());
     Assert.assertNotNull(check.getFailedOn());
     Assert.assertTrue(check.getFailedOn().contains("host1"));
-
-    // Case 3: Install Packages failed on host1 and host1 was put in Maintenance Mode
-    Mockito.when(hostVersionEntities.get(0).getState()).thenReturn(RepositoryVersionState.INSTALL_FAILED);
-    Mockito.when(hosts.get(0).getMaintenanceState(1L)).thenReturn(MaintenanceState.ON);
-    Mockito.when(clusterVersionEntity.getState()).thenReturn(RepositoryVersionState.INSTALL_FAILED);
-    check = new PrerequisiteCheck(null, null);
-    installPackagesCheck.perform(check, checkRequest);
-    Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus());
-    Assert.assertNotNull(check.getFailedOn());
-    Assert.assertTrue(check.getFailedOn().contains(clusterName));
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/checks/PreviousUpgradeCompletedTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/PreviousUpgradeCompletedTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/PreviousUpgradeCompletedTest.java
index 21b2514..3233e55 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/checks/PreviousUpgradeCompletedTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/PreviousUpgradeCompletedTest.java
@@ -44,14 +44,14 @@ public class PreviousUpgradeCompletedTest {
   private final Cluster cluster = Mockito.mock(Cluster.class);
   private StackId sourceStackId = new StackId("HDP", "2.2");
   private StackId targetStackId = new StackId("HDP", "2.2");
-  private final RepositoryVersionEntity sourceRepositoryVersion = Mockito.mock(RepositoryVersionEntity.class);
-  private final RepositoryVersionEntity targetRepositoryVersion = Mockito.mock(RepositoryVersionEntity.class);
-  private String sourceVersion = "2.2.6.0-1234";
-  private String destVersion = "2.2.8.0-5678";
+  private String sourceRepositoryVersion = "2.2.6.0-1234";
+  private String destRepositoryVersion = "2.2.8.0-5678";
   private String clusterName = "cluster";
   private PrereqCheckRequest checkRequest = new PrereqCheckRequest(clusterName);
   private PreviousUpgradeCompleted puc = new PreviousUpgradeCompleted();
 
+  private RepositoryVersionEntity toRepsitoryVersion;
+
   /**
    *
    */
@@ -67,7 +67,7 @@ public class PreviousUpgradeCompletedTest {
     stack.setStackName(stackId.getStackName());
     stack.setStackVersion(stackId.getStackVersion());
 
-    checkRequest.setRepositoryVersion(sourceVersion);
+    checkRequest.setRepositoryVersion(sourceRepositoryVersion);
     checkRequest.setSourceStackId(sourceStackId);
     checkRequest.setTargetStackId(targetStackId);
 
@@ -78,6 +78,8 @@ public class PreviousUpgradeCompletedTest {
       }
     };
 
+    toRepsitoryVersion = Mockito.mock(RepositoryVersionEntity.class);
+    Mockito.when(toRepsitoryVersion.getVersion()).thenReturn(destRepositoryVersion);
   }
 
   @Test
@@ -92,8 +94,7 @@ public class PreviousUpgradeCompletedTest {
     UpgradeEntity upgradeInProgress = Mockito.mock(UpgradeEntity.class);
     Mockito.when(upgradeInProgress.getDirection()).thenReturn(Direction.UPGRADE);
     Mockito.when(upgradeInProgress.getClusterId()).thenReturn(1L);
-    Mockito.when(upgradeInProgress.getFromRepositoryVersion()).thenReturn(sourceRepositoryVersion);
-    Mockito.when(upgradeInProgress.getToRepositoryVersion()).thenReturn(targetRepositoryVersion);
+    Mockito.when(upgradeInProgress.getRepositoryVersion()).thenReturn(toRepsitoryVersion);
 
     Mockito.when(cluster.getUpgradeInProgress()).thenReturn(upgradeInProgress);
     check = new PrerequisiteCheck(null, null);

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheckTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheckTest.java
index 613e47f..5c423b5 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheckTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/YarnTimelineServerStatePreservingCheckTest.java
@@ -22,7 +22,6 @@ import java.util.Map;
 
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.PrereqCheckRequest;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
@@ -77,17 +76,13 @@ public class YarnTimelineServerStatePreservingCheckTest {
     Mockito.when(m_clusters.getCluster("cluster")).thenReturn(cluster);
     Mockito.when(cluster.getCurrentStackVersion()).thenReturn(new StackId("HDP-2.2"));
 
-    Map<String, Service> services = new HashMap<String, Service>();
+    Map<String, Service> services = new HashMap<>();
     Mockito.when(cluster.getServices()).thenReturn(services);
 
-    ClusterVersionEntity clusterVersionEntity = Mockito.mock(ClusterVersionEntity.class);
-    Mockito.when(cluster.getCurrentClusterVersion()).thenReturn(clusterVersionEntity);
-
     RepositoryVersionEntity repositoryVersionEntity = Mockito.mock(RepositoryVersionEntity.class);
-    Mockito.when(clusterVersionEntity.getRepositoryVersion()).thenReturn(repositoryVersionEntity);
     Mockito.when(repositoryVersionEntity.getVersion()).thenReturn("2.2.4.2");
 
-    Map<String, String> checkProperties = new HashMap<String, String>();
+    Map<String, String> checkProperties = new HashMap<>();
     checkProperties.put("min-applicable-stack-version","HDP-2.2.4.2");
     PrerequisiteCheckConfig prerequisiteCheckConfig = Mockito.mock(PrerequisiteCheckConfig.class);
     Mockito.when(prerequisiteCheckConfig.getCheckProperties(
@@ -119,14 +114,14 @@ public class YarnTimelineServerStatePreservingCheckTest {
 
     final DesiredConfig desiredConfig = Mockito.mock(DesiredConfig.class);
     Mockito.when(desiredConfig.getTag()).thenReturn("tag");
-    Map<String, DesiredConfig> configMap = new HashMap<String, DesiredConfig>();
+    Map<String, DesiredConfig> configMap = new HashMap<>();
     configMap.put("yarn-site", desiredConfig);
     configMap.put("core-site", desiredConfig);
 
     Mockito.when(cluster.getDesiredConfigs()).thenReturn(configMap);
     final Config config = Mockito.mock(Config.class);
     Mockito.when(cluster.getConfig(Mockito.anyString(), Mockito.anyString())).thenReturn(config);
-    final Map<String, String> properties = new HashMap<String, String>();
+    final Map<String, String> properties = new HashMap<>();
     Mockito.when(config.getProperties()).thenReturn(properties);
 
     PrerequisiteCheck check = new PrerequisiteCheck(null, null);
@@ -150,13 +145,10 @@ public class YarnTimelineServerStatePreservingCheckTest {
       }
     });
     Mockito.when(cluster.getCurrentStackVersion()).thenReturn(new StackId("HDP-2.2"));
-    ClusterVersionEntity clusterVersionEntity = Mockito.mock(ClusterVersionEntity.class);
-    Mockito.when(cluster.getCurrentClusterVersion()).thenReturn(clusterVersionEntity);
     RepositoryVersionEntity repositoryVersionEntity = Mockito.mock(RepositoryVersionEntity.class);
-    Mockito.when(clusterVersionEntity.getRepositoryVersion()).thenReturn(repositoryVersionEntity);
     Mockito.when(m_clusters.getCluster("c1")).thenReturn(cluster);
 
-    Map<String, String> checkProperties = new HashMap<String, String>();
+    Map<String, String> checkProperties = new HashMap<>();
     checkProperties.put("min-applicable-stack-version","HDP-2.2.4.2");
     PrerequisiteCheckConfig prerequisiteCheckConfig = Mockito.mock(PrerequisiteCheckConfig.class);
     Mockito.when(prerequisiteCheckConfig.getCheckProperties(
@@ -198,13 +190,10 @@ public class YarnTimelineServerStatePreservingCheckTest {
       }
     });
     Mockito.when(cluster.getCurrentStackVersion()).thenReturn(new StackId("MYSTACK-12.2"));
-    ClusterVersionEntity clusterVersionEntity = Mockito.mock(ClusterVersionEntity.class);
-    Mockito.when(cluster.getCurrentClusterVersion()).thenReturn(clusterVersionEntity);
     RepositoryVersionEntity repositoryVersionEntity = Mockito.mock(RepositoryVersionEntity.class);
-    Mockito.when(clusterVersionEntity.getRepositoryVersion()).thenReturn(repositoryVersionEntity);
     Mockito.when(m_clusters.getCluster("c1")).thenReturn(cluster);
 
-    Map<String, String> checkProperties = new HashMap<String, String>();
+    Map<String, String> checkProperties = new HashMap<>();
     checkProperties.put("min-applicable-stack-version", "HDP-2.2.4.2");
     PrerequisiteCheckConfig prerequisiteCheckConfig = Mockito.mock(PrerequisiteCheckConfig.class);
     Mockito.when(prerequisiteCheckConfig.getCheckProperties(

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelperTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelperTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelperTest.java
index 79ae063..66a6e98 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelperTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelperTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -37,6 +37,7 @@ import org.apache.ambari.server.actionmanager.HostRoleCommand;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.actionmanager.Request;
 import org.apache.ambari.server.actionmanager.Stage;
+import org.apache.ambari.server.agent.CommandRepository;
 import org.apache.ambari.server.agent.ExecutionCommand;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.internal.ComponentResourceProviderTest;
@@ -47,6 +48,14 @@ import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.metadata.ActionMetadata;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.orm.OrmTestHelper;
+import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
+import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
+import org.apache.ambari.server.orm.dao.StackDAO;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
+import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
+import org.apache.ambari.server.orm.entities.ServiceComponentVersionEntity;
+import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.security.TestAuthenticationFactory;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.state.Cluster;
@@ -57,17 +66,19 @@ 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.PropertyInfo;
+import org.apache.ambari.server.state.RepositoryInfo;
 import org.apache.ambari.server.state.SecurityType;
 import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponent;
-import org.apache.ambari.server.state.ServiceInfo;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.StackInfo;
 import org.apache.ambari.server.state.State;
 import org.apache.ambari.server.state.UserGroupInfo;
 import org.apache.ambari.server.state.ValueAttributesInfo;
+import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
 import org.apache.ambari.server.topology.TopologyManager;
 import org.apache.ambari.server.utils.StageUtils;
+import org.apache.commons.collections.MapUtils;
 import org.easymock.Capture;
 import org.easymock.EasyMock;
 import org.easymock.EasyMockRule;
@@ -131,7 +142,7 @@ public class AmbariCustomCommandExecutionHelperTest {
     StageUtils.setConfiguration(injector.getInstance(Configuration.class));
 
     SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator());
-    createClusterFixture("c1", "HDP-2.0.6", "c1");
+    createClusterFixture("c1", new StackId("HDP-2.0.6"), "2.0.6-1234", "c1");
 
     EasyMock.expect(hostRoleCommand.getTaskId()).andReturn(1L);
     EasyMock.expect(hostRoleCommand.getStageId()).andReturn(1L);
@@ -146,7 +157,7 @@ public class AmbariCustomCommandExecutionHelperTest {
     stackInfo.setName("HDP");
     stackInfo.setVersion("2.0.6");
     StackId stackId = new StackId(stackInfo);
-    Map<String, DesiredConfig> desiredConfigMap = new HashMap<String, DesiredConfig>();
+    Map<String, DesiredConfig> desiredConfigMap = new HashMap<>();
     Map<PropertyInfo, String> userProperties = new HashMap<>();
     Map<PropertyInfo, String> groupProperties = new HashMap<>();
     PropertyInfo userProperty = new PropertyInfo();
@@ -169,7 +180,7 @@ public class AmbariCustomCommandExecutionHelperTest {
     userProperties.put(userProperty, "zookeeperUser");
     groupProperties.put(groupProperty, "zookeeperGroup");
     Map<String, Set<String>> userGroupsMap = new HashMap<>();
-    userGroupsMap.put("zookeeperUser", new HashSet<String>(Arrays.asList("zookeeperGroup")));
+    userGroupsMap.put("zookeeperUser", new HashSet<>(Arrays.asList("zookeeperGroup")));
     Cluster cluster = clusters.getCluster("c1");
     EasyMock.expect(configHelper.getPropertiesWithPropertyType(
       stackId, PropertyInfo.PropertyType.USER, cluster, desiredConfigMap)).andReturn(userProperties).anyTimes();
@@ -247,11 +258,7 @@ public class AmbariCustomCommandExecutionHelperTest {
            new RequestResourceFilter("GANGLIA", "GANGLIA_MONITOR", Collections.singletonList("c1-c6402"))
        ),
        new RequestOperationLevel(Resource.Type.Service, "c1", "GANGLIA", null, null),
-        new HashMap<String, String>() {
-          {
-          }
-        },
-       false);
+        new HashMap<String, String>(), false);
 
     EasyMock.replay(hostRoleCommand, actionManager, configHelper);
 
@@ -286,10 +293,7 @@ public class AmbariCustomCommandExecutionHelperTest {
             new RequestResourceFilter("GANGLIA", "GANGLIA_MONITOR", Collections.singletonList("c1-c6401")),
             new RequestResourceFilter("GANGLIA", "GANGLIA_MONITOR", Collections.singletonList("c1-c6402"))),
         new RequestOperationLevel(Resource.Type.Service, "c1", "GANGLIA", null, null),
-        new HashMap<String, String>() {
-          {
-          }
-        }, false);
+        new HashMap<String, String>(), false);
 
     EasyMock.replay(hostRoleCommand, actionManager, configHelper);
 
@@ -326,10 +330,7 @@ public class AmbariCustomCommandExecutionHelperTest {
             new RequestResourceFilter("GANGLIA", "GANGLIA_MONITOR", Collections.singletonList("c1-c6401")),
             new RequestResourceFilter("GANGLIA", "GANGLIA_MONITOR", Collections.singletonList("c1-c6402"))),
         new RequestOperationLevel(Resource.Type.Host, "c1", "GANGLIA", null, null),
-        new HashMap<String, String>() {
-          {
-          }
-        }, false);
+        new HashMap<String, String>(), false);
 
     EasyMock.replay(hostRoleCommand, actionManager, configHelper);
 
@@ -375,10 +376,7 @@ public class AmbariCustomCommandExecutionHelperTest {
         Collections.singletonList("c6402"))),
 
         new RequestOperationLevel(Resource.Type.Service, "c1", "ZOOKEEPER", null, null),
-        new HashMap<String, String>() {
-          {
-          }
-        }, false);
+        new HashMap<String, String>(), false);
 
     EasyMock.replay(hostRoleCommand, actionManager, configHelper);
     ambariManagementController.createAction(actionRequest, requestProperties);
@@ -418,10 +416,7 @@ public class AmbariCustomCommandExecutionHelperTest {
         Collections.singletonList("c6402"))),
 
         new RequestOperationLevel(Resource.Type.Service, "c1", "ZOOKEEPER", null, null),
-        new HashMap<String, String>() {
-          {
-          }
-        }, false);
+        new HashMap<String, String>(), false);
 
     EasyMock.replay(hostRoleCommand, actionManager, configHelper);
     ambariManagementController.createAction(actionRequest, requestProperties);
@@ -472,7 +467,7 @@ public class AmbariCustomCommandExecutionHelperTest {
 
     HashSet<String> localComponents = new HashSet<>();
     EasyMock.expect(execCmd.getLocalComponents()).andReturn(localComponents).anyTimes();
-    EasyMock.replay(configHelper,stage, execCmdWrapper, execCmd);
+    EasyMock.replay(configHelper, stage, execCmdWrapper, execCmd);
 
     ambariCustomCommandExecutionHelper.addExecutionCommandsToStage(actionExecutionContext, stage, new HashMap<String, String>(), null);
     Map<String, String> configMap = timeOutCapture.getValues().get(0);
@@ -522,7 +517,7 @@ public class AmbariCustomCommandExecutionHelperTest {
 
     HashSet<String> localComponents = new HashSet<>();
     EasyMock.expect(execCmd.getLocalComponents()).andReturn(localComponents).anyTimes();
-    EasyMock.replay(stage, execCmdWrapper, execCmd);
+    EasyMock.replay(configHelper, stage, execCmdWrapper, execCmd);
 
     ambariCustomCommandExecutionHelper.addExecutionCommandsToStage(actionExecutionContext, stage, new HashMap<String, String>(), null);
   }
@@ -531,7 +526,7 @@ public class AmbariCustomCommandExecutionHelperTest {
   public void testIsTopologyRefreshRequired() throws Exception {
     AmbariCustomCommandExecutionHelper helper = injector.getInstance(AmbariCustomCommandExecutionHelper.class);
 
-    createClusterFixture("c2", "HDP-2.1.1", "c2");
+    createClusterFixture("c2", new StackId("HDP-2.1.1"), "2.1.1.0-1234", "c2");
 
     Assert.assertTrue(helper.isTopologyRefreshRequired("START", "c2", "HDFS"));
     Assert.assertTrue(helper.isTopologyRefreshRequired("RESTART", "c2", "HDFS"));
@@ -557,31 +552,103 @@ public class AmbariCustomCommandExecutionHelperTest {
     EasyMock.replay(hostRoleCommand, actionManager, configHelper);
 
     ambariManagementController.createAction(actionRequest, requestProperties);
-    StackId stackId = clusters.getCluster("c1").getDesiredStackVersion();
-    Map<String, ServiceInfo> services = ambariManagementController.getAmbariMetaInfo().getServices(stackId.getStackName(), stackId.getStackVersion());
+
     Request request = requestCapture.getValue();
     Stage stage = request.getStages().iterator().next();
     List<ExecutionCommandWrapper> commands = stage.getExecutionCommands("c1-c6401");
     ExecutionCommand command = commands.get(0).getExecutionCommand();
-    for (String service : services.keySet()) {
-      Assert.assertEquals(command.getAvailableServices().get(service), services.get(service).getVersion());
-    }
+
+    // ZK is the only service that is versionable
+    Assert.assertFalse(MapUtils.isEmpty(command.getComponentVersionMap()));
+    Assert.assertEquals(1, command.getComponentVersionMap().size());
+    Assert.assertTrue(command.getComponentVersionMap().containsKey("ZOOKEEPER"));
   }
 
-  private void createClusterFixture(String clusterName, String stackVersion, String hostPrefix) throws AmbariException, AuthorizationException {
+  @Test
+  public void testCommandRepository() throws Exception {
+    Cluster cluster = clusters.getCluster("c1");
+    Service serviceYARN = cluster.getService("YARN");
+    Service serviceZK = cluster.getService("ZOOKEEPER");
+    ServiceComponent componentRM = serviceYARN.getServiceComponent("RESOURCEMANAGER");
+    ServiceComponent componentZKC = serviceZK.getServiceComponent("ZOOKEEPER_CLIENT");
+    Host host = clusters.getHost("c1-c6401");
+
+    AmbariCustomCommandExecutionHelper helper = injector.getInstance(AmbariCustomCommandExecutionHelper.class);
+    StackDAO stackDAO = injector.getInstance(StackDAO.class);
+    RepositoryVersionDAO repoVersionDAO = injector.getInstance(RepositoryVersionDAO.class);
+    ServiceComponentDesiredStateDAO componentDAO = injector.getInstance(ServiceComponentDesiredStateDAO.class);
+    RepositoryVersionHelper repoVersionHelper = injector.getInstance(RepositoryVersionHelper.class);
+
+    CommandRepository commandRepo = helper.getCommandRepository(cluster, componentRM, host);
+
+    Assert.assertEquals(1, commandRepo.getRepositories().size());
+    CommandRepository.Repository repo = commandRepo.getRepositories().iterator().next();
+    Assert.assertEquals("http://public-repo-1.hortonworks.com/HDP/centos6/2.x/updates/2.0.6.0", repo.getBaseUrl());
+
+    RepositoryInfo ri = new RepositoryInfo();
+    ri.setBaseUrl("http://foo");
+    ri.setRepoName("HDP");
+    ri.setRepoId("new-id");
+    ri.setOsType("redhat6");
+    String operatingSystems = repoVersionHelper.serializeOperatingSystems(Collections.singletonList(ri));
+
+
+    StackEntity stackEntity = stackDAO.find(cluster.getDesiredStackVersion().getStackName(),
+        cluster.getDesiredStackVersion().getStackVersion());
+
+    RepositoryVersionEntity repositoryVersion = new RepositoryVersionEntity(stackEntity,
+        "2.1.1.1-1234", "2.1.1.1-1234", operatingSystems);
+    repositoryVersion = repoVersionDAO.merge(repositoryVersion);
+
+    // add a repo version associated with a component
+    ServiceComponentDesiredStateEntity componentEntity = componentDAO.findByName(cluster.getClusterId(),
+        serviceYARN.getName(), componentRM.getName());
+
+    ServiceComponentVersionEntity componentVersionEntity = new ServiceComponentVersionEntity();
+    componentVersionEntity.setRepositoryVersion(repositoryVersion);
+    componentVersionEntity.setUserName("admin");
+
+    componentEntity.setDesiredRepositoryVersion(repositoryVersion);
+    componentEntity.addVersion(componentVersionEntity);
+    componentEntity = componentDAO.merge(componentEntity);
+
+    // !!! make sure the override is set
+    commandRepo = helper.getCommandRepository(cluster, componentRM, host);
+
+    Assert.assertEquals(1, commandRepo.getRepositories().size());
+    repo = commandRepo.getRepositories().iterator().next();
+    Assert.assertEquals("http://foo", repo.getBaseUrl());
+
+    // verify that ZK is NOT overwritten
+    commandRepo = helper.getCommandRepository(cluster, componentZKC, host);
+
+    Assert.assertEquals(1, commandRepo.getRepositories().size());
+    repo = commandRepo.getRepositories().iterator().next();
+    Assert.assertEquals("http://public-repo-1.hortonworks.com/HDP/centos6/2.x/updates/2.0.6.0", repo.getBaseUrl());
+  }
+
+  private void createClusterFixture(String clusterName, StackId stackId,
+    String respositoryVersion, String hostPrefix) throws AmbariException, AuthorizationException {
+
     String hostC6401 = hostPrefix + "-c6401";
     String hostC6402 = hostPrefix + "-c6402";
 
-    createCluster(clusterName, stackVersion);
+    OrmTestHelper ormTestHelper = injector.getInstance(OrmTestHelper.class);
+    RepositoryVersionEntity repositoryVersion = ormTestHelper.getOrCreateRepositoryVersion(stackId,
+        respositoryVersion);
+
+    createCluster(clusterName, stackId.getStackId());
 
     addHost(hostC6401, clusterName);
     addHost(hostC6402, clusterName);
 
-    clusters.getCluster(clusterName);
-    createService(clusterName, "YARN", null);
-    createService(clusterName, "GANGLIA", null);
-    createService(clusterName, "ZOOKEEPER", null);
-    createService(clusterName, "FLUME", null);
+    Cluster cluster = clusters.getCluster(clusterName);
+    Assert.assertNotNull(cluster);
+
+    createService(clusterName, "YARN", repositoryVersion);
+    createService(clusterName, "GANGLIA", repositoryVersion);
+    createService(clusterName, "ZOOKEEPER", repositoryVersion);
+    createService(clusterName, "FLUME", repositoryVersion);
 
     createServiceComponent(clusterName, "YARN", "RESOURCEMANAGER", State.INIT);
     createServiceComponent(clusterName, "YARN", "NODEMANAGER", State.INIT);
@@ -592,7 +659,6 @@ public class AmbariCustomCommandExecutionHelperTest {
     // this component should be not installed on any host
     createServiceComponent(clusterName, "FLUME", "FLUME_HANDLER", State.INIT);
 
-
     createServiceComponentHost(clusterName, "YARN", "RESOURCEMANAGER", hostC6401, null);
     createServiceComponentHost(clusterName, "YARN", "NODEMANAGER", hostC6401, null);
     createServiceComponentHost(clusterName, "GANGLIA", "GANGLIA_SERVER", hostC6401, State.INIT);
@@ -612,7 +678,7 @@ public class AmbariCustomCommandExecutionHelperTest {
     }
   }
   private void setOsFamily(Host host, String osFamily, String osVersion) {
-    Map<String, String> hostAttributes = new HashMap<String, String>();
+    Map<String, String> hostAttributes = new HashMap<>();
     hostAttributes.put("os_family", osFamily);
     hostAttributes.put("os_release_version", osVersion);
 
@@ -625,17 +691,17 @@ public class AmbariCustomCommandExecutionHelperTest {
     ambariManagementController.createCluster(r);
   }
 
-  private void createService(String clusterName,
-      String serviceName, State desiredState) throws AmbariException, AuthorizationException {
-    String dStateStr = null;
-    if (desiredState != null) {
-      dStateStr = desiredState.toString();
-    }
-    ServiceRequest r1 = new ServiceRequest(clusterName, serviceName, dStateStr);
-    Set<ServiceRequest> requests = new HashSet<ServiceRequest>();
+  private void createService(String clusterName, String serviceName,
+      RepositoryVersionEntity repositoryVersion) throws AmbariException, AuthorizationException {
+
+    ServiceRequest r1 = new ServiceRequest(clusterName, serviceName,
+        repositoryVersion.getId(), null, "false");
+
+    Set<ServiceRequest> requests = new HashSet<>();
     requests.add(r1);
 
-    ServiceResourceProviderTest.createServices(ambariManagementController, requests);
+    ServiceResourceProviderTest.createServices(ambariManagementController,
+        injector.getInstance(RepositoryVersionDAO.class), requests);
   }
 
   private void createServiceComponent(String clusterName,
@@ -648,7 +714,7 @@ public class AmbariCustomCommandExecutionHelperTest {
     ServiceComponentRequest r = new ServiceComponentRequest(clusterName,
         serviceName, componentName, dStateStr);
     Set<ServiceComponentRequest> requests =
-        new HashSet<ServiceComponentRequest>();
+      new HashSet<>();
     requests.add(r);
     ComponentResourceProviderTest.createComponents(ambariManagementController, requests);
   }
@@ -662,7 +728,7 @@ public class AmbariCustomCommandExecutionHelperTest {
     ServiceComponentHostRequest r = new ServiceComponentHostRequest(clusterName,
         serviceName, componentName, hostname, dStateStr);
     Set<ServiceComponentHostRequest> requests =
-        new HashSet<ServiceComponentHostRequest>();
+      new HashSet<>();
     requests.add(r);
     ambariManagementController.createHostComponents(requests);
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/controller/RootServiceResponseFactoryTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/RootServiceResponseFactoryTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/RootServiceResponseFactoryTest.java
index e9b4aa7..61a5ed6 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/RootServiceResponseFactoryTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/RootServiceResponseFactoryTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -109,8 +109,7 @@ public class RootServiceResponseFactoryTest {
     RootServiceResponseFactory.Components ambariServerComponent = RootServiceResponseFactory.Components.AMBARI_SERVER;
 
     // Request null service name, not-null component name
-    request = new RootServiceComponentRequest(null,
-        ambariServerComponent.name());
+    request = new RootServiceComponentRequest(null, ambariServerComponent.name());
 
     try {
       rootServiceComponents = responseFactory.getRootServiceComponents(request);
@@ -119,8 +118,8 @@ public class RootServiceResponseFactoryTest {
     }
 
     // Request existent service name, null component name
-    request = new RootServiceComponentRequest(
-        RootServiceResponseFactory.Services.AMBARI.name(), null);
+    String serviceName = RootServiceResponseFactory.Services.AMBARI.name();
+    request = new RootServiceComponentRequest(serviceName, null);
 
     rootServiceComponents = responseFactory.getRootServiceComponents(request);
     assertEquals(
@@ -142,7 +141,7 @@ public class RootServiceResponseFactoryTest {
         }
       } else {
         assertTrue(rootServiceComponents.contains(new RootServiceComponentResponse(
-            component.name(), RootServiceResponseFactory.NOT_APPLICABLE,
+            serviceName, component.name(), RootServiceResponseFactory.NOT_APPLICABLE,
             Collections.<String, String> emptyMap())));
       }
     }


[29/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
index 9b93517..e0f52c6 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -20,6 +20,7 @@ package org.apache.ambari.server.orm.entities;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Objects;
 
 import javax.persistence.CascadeType;
 import javax.persistence.Column;
@@ -40,7 +41,9 @@ import javax.persistence.Table;
 import javax.persistence.TableGenerator;
 import javax.persistence.UniqueConstraint;
 
+import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.State;
+import org.apache.commons.lang.builder.EqualsBuilder;
 
 @Entity
 @Table(
@@ -84,19 +87,21 @@ public class ServiceComponentDesiredStateEntity {
   @Column(name = "recovery_enabled", nullable = false, insertable = true, updatable = true)
   private Integer recoveryEnabled = 0;
 
-  /**
-   * Unidirectional one-to-one association to {@link StackEntity}
-   */
-  @OneToOne
-  @JoinColumn(name = "desired_stack_id", unique = false, nullable = false, insertable = true, updatable = true)
-  private StackEntity desiredStack;
+  @Column(name = "repo_state", nullable = false, insertable = true, updatable = true)
+  @Enumerated(EnumType.STRING)
+  private RepositoryVersionState repoState = RepositoryVersionState.NOT_REQUIRED;
 
   /**
-   * Version string that should be followed by instances
-   * of component on hosts. Includes both stack version and build
+   * Unidirectional one-to-one association to {@link RepositoryVersionEntity}
    */
-  @Column(name = "desired_version", nullable = false, insertable = true, updatable = true)
-  private String desiredVersion = State.UNKNOWN.toString();
+  @OneToOne
+  @JoinColumn(
+      name = "desired_repo_version_id",
+      unique = false,
+      nullable = false,
+      insertable = true,
+      updatable = true)
+  private RepositoryVersionEntity desiredRepositoryVersion;
 
   @ManyToOne
   @JoinColumns({@javax.persistence.JoinColumn(name = "cluster_id", referencedColumnName = "cluster_id", nullable = false), @JoinColumn(name = "service_name", referencedColumnName = "service_name", nullable = false)})
@@ -108,17 +113,8 @@ public class ServiceComponentDesiredStateEntity {
   @OneToMany(mappedBy = "serviceComponentDesiredStateEntity")
   private Collection<HostComponentDesiredStateEntity> hostComponentDesiredStateEntities;
 
-  /**
-   * All of the upgrades and downgrades which have occurred for this component.
-   * Can be {@code null} for none.
-   */
-  @OneToMany(
-      mappedBy = "m_serviceComponentDesiredStateEntity",
-      cascade = { CascadeType.ALL })
-  private Collection<ServiceComponentHistoryEntity> serviceComponentHistory;
-
   @OneToMany(mappedBy = "m_serviceComponentDesiredStateEntity", cascade = { CascadeType.ALL })
-  private Collection<ServiceComponentVersionEntity> serviceComponentVersion;
+  private Collection<ServiceComponentVersionEntity> serviceComponentVersions;
 
   public Long getId() {
     return id;
@@ -156,66 +152,39 @@ public class ServiceComponentDesiredStateEntity {
     this.desiredState = desiredState;
   }
 
-  public StackEntity getDesiredStack() {
-    return desiredStack;
-  }
-
-  public void setDesiredStack(StackEntity desiredStack) {
-    this.desiredStack = desiredStack;
+  public RepositoryVersionEntity getDesiredRepositoryVersion() {
+    return desiredRepositoryVersion;
   }
 
-  public String getDesiredVersion() {
-    return desiredVersion;
+  public void setDesiredRepositoryVersion(RepositoryVersionEntity desiredRepositoryVersion) {
+    this.desiredRepositoryVersion = desiredRepositoryVersion;
   }
 
-  public void setDesiredVersion(String desiredVersion) {
-    this.desiredVersion = desiredVersion;
-  }
-
-  /**
-   * Adds a historical entry for the version of this service component. New
-   * entries are automatically created when this entity is merged via a
-   * {@link CascadeType#MERGE}.
-   *
-   * @param historicalEntry
-   *          the entry to add.
-   */
-  public void addHistory(ServiceComponentHistoryEntity historicalEntry) {
-    if (null == serviceComponentHistory) {
-      serviceComponentHistory = new ArrayList<>();
-    }
-
-    serviceComponentHistory.add(historicalEntry);
-    historicalEntry.setServiceComponentDesiredState(this);
+  public StackEntity getDesiredStack() {
+    return desiredRepositoryVersion.getStack();
   }
 
-  /**
-   * Gets the history of this component's upgrades and downgrades.
-   *
-   * @return the component history, or {@code null} if none.
-   */
-  public Collection<ServiceComponentHistoryEntity> getHistory() {
-    return serviceComponentHistory;
+  public String getDesiredVersion() {
+    return desiredRepositoryVersion.getVersion();
   }
 
-
   /**
-   * @param versionEntry the version to add
+   * @param versionEntity the version to add
    */
-  public void addVersion(ServiceComponentVersionEntity versionEntry) {
-    if (null == serviceComponentVersion) {
-      serviceComponentVersion = new ArrayList<>();
+  public void addVersion(ServiceComponentVersionEntity versionEntity) {
+    if (null == serviceComponentVersions) {
+      serviceComponentVersions = new ArrayList<>();
     }
 
-    serviceComponentVersion.add(versionEntry);
-    versionEntry.setServiceComponentDesiredState(this);
+    serviceComponentVersions.add(versionEntity);
+    versionEntity.setServiceComponentDesiredState(this);
   }
 
   /**
    * @return the collection of versions for the component
    */
   public Collection<ServiceComponentVersionEntity> getVersions() {
-    return serviceComponentVersion;
+    return serviceComponentVersions;
   }
 
 
@@ -227,6 +196,9 @@ public class ServiceComponentDesiredStateEntity {
     this.recoveryEnabled = (recoveryEnabled == false) ? 0 : 1;
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
   public boolean equals(Object o) {
     if (this == o) {
@@ -238,39 +210,21 @@ public class ServiceComponentDesiredStateEntity {
     }
 
     ServiceComponentDesiredStateEntity that = (ServiceComponentDesiredStateEntity) o;
-
-    if (id != null ? !id.equals(that.id) : that.id != null) {
-      return false;
-    }
-    if (clusterId != null ? !clusterId.equals(that.clusterId) : that.clusterId != null) {
-      return false;
-    }
-    if (componentName != null ? !componentName.equals(that.componentName) : that.componentName != null) {
-      return false;
-    }
-    if (desiredState != null ? !desiredState.equals(that.desiredState) : that.desiredState != null) {
-      return false;
-    }
-    if (serviceName != null ? !serviceName.equals(that.serviceName) : that.serviceName != null) {
-      return false;
-    }
-    if (desiredStack != null ? !desiredStack.equals(that.desiredStack)
-        : that.desiredStack != null) {
-      return false;
-    }
-    return true;
+    EqualsBuilder equalsBuilder = new EqualsBuilder();
+    equalsBuilder.append(id, that.id);
+    equalsBuilder.append(clusterId, that.clusterId);
+    equalsBuilder.append(componentName, that.componentName);
+    equalsBuilder.append(desiredState, that.desiredState);
+    equalsBuilder.append(serviceName, that.serviceName);
+    equalsBuilder.append(desiredRepositoryVersion, that.desiredRepositoryVersion);
+
+    return equalsBuilder.isEquals();
   }
 
   @Override
   public int hashCode() {
-    int result = id != null ? id.hashCode() : 0;
-    result = 31 * result + (clusterId != null ? clusterId.hashCode() : 0);
-    result = 31 * result + (serviceName != null ? serviceName.hashCode() : 0);
-    result = 31 * result + (componentName != null ? componentName.hashCode() : 0);
-    result = 31 * result + (desiredState != null ? desiredState.hashCode() : 0);
-    result = 31 * result + (desiredStack != null ? desiredStack.hashCode() : 0);
-
-    return result;
+    return Objects.hash(id, clusterId, serviceName, componentName, desiredState,
+        desiredRepositoryVersion);
   }
 
   public ClusterServiceEntity getClusterServiceEntity() {
@@ -297,4 +251,18 @@ public class ServiceComponentDesiredStateEntity {
     this.hostComponentDesiredStateEntities = hostComponentDesiredStateEntities;
   }
 
+  /**
+   * @param state the repository state for {@link #getDesiredVersion()}
+   */
+  public void setRepositoryState(RepositoryVersionState state) {
+    repoState = state;
+  }
+
+  /**
+   * @return the state of the repository for {@link #getDesiredVersion()}
+   */
+  public RepositoryVersionState getRepositoryState() {
+    return repoState;
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentVersionEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentVersionEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentVersionEntity.java
index 5085d18..ffb3b82 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentVersionEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentVersionEntity.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -49,9 +49,22 @@ import org.apache.ambari.server.state.RepositoryVersionState;
     valueColumnName = "sequence_value",
     pkColumnValue = "servicecomponent_version_id_seq",
     initialValue = 0)
-@NamedQueries({ @NamedQuery(
+@NamedQueries({
+  @NamedQuery(
     name = "ServiceComponentVersionEntity.findByComponent",
-    query = "SELECT version FROM ServiceComponentVersionEntity version WHERE version.m_serviceComponentDesiredStateEntity.clusterId = :clusterId AND version.m_serviceComponentDesiredStateEntity.serviceName = :serviceName AND version.m_serviceComponentDesiredStateEntity.componentName = :componentName") })
+    query = "SELECT version FROM ServiceComponentVersionEntity version WHERE " +
+      "version.m_serviceComponentDesiredStateEntity.clusterId = :clusterId AND " +
+      "version.m_serviceComponentDesiredStateEntity.serviceName = :serviceName AND " +
+      "version.m_serviceComponentDesiredStateEntity.componentName = :componentName"),
+  @NamedQuery(
+    name = "ServiceComponentVersionEntity.findByComponentAndVersion",
+    query = "SELECT version FROM ServiceComponentVersionEntity version WHERE " +
+        "version.m_serviceComponentDesiredStateEntity.clusterId = :clusterId AND " +
+        "version.m_serviceComponentDesiredStateEntity.serviceName = :serviceName AND " +
+        "version.m_serviceComponentDesiredStateEntity.componentName = :componentName AND " +
+        "version.m_repositoryVersion.version = :repoVersion")
+})
+
 public class ServiceComponentVersionEntity {
 
   @Id
@@ -66,7 +79,7 @@ public class ServiceComponentVersionEntity {
   private ServiceComponentDesiredStateEntity m_serviceComponentDesiredStateEntity;
 
   @ManyToOne
-  @JoinColumn(name = "repo_version_id", referencedColumnName = "repo_version_id", nullable = false)
+  @JoinColumn(name  = "repo_version_id", referencedColumnName = "repo_version_id", nullable = false)
   private RepositoryVersionEntity m_repositoryVersion;
 
   @Column(name = "state", nullable = false, insertable = true, updatable = true)
@@ -99,6 +112,13 @@ public class ServiceComponentVersionEntity {
   }
 
   /**
+   * @return the repository
+   */
+  public RepositoryVersionEntity getRepositoryVersion() {
+    return m_repositoryVersion;
+  }
+
+  /**
    * @return the id
    */
   public long getId() {

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntity.java
index 885f995..0124d3e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntity.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -18,6 +18,8 @@
 
 package org.apache.ambari.server.orm.entities;
 
+import java.util.Objects;
+
 import javax.persistence.Basic;
 import javax.persistence.Column;
 import javax.persistence.Entity;
@@ -25,11 +27,13 @@ import javax.persistence.EnumType;
 import javax.persistence.Enumerated;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
 import javax.persistence.OneToOne;
 
 import org.apache.ambari.server.state.MaintenanceState;
 import org.apache.ambari.server.state.SecurityState;
 import org.apache.ambari.server.state.State;
+import org.apache.commons.lang.builder.EqualsBuilder;
 
 @javax.persistence.IdClass(ServiceDesiredStateEntityPK.class)
 @javax.persistence.Table(name = "servicedesiredstate")
@@ -52,13 +56,6 @@ public class ServiceDesiredStateEntity {
   @Basic
   private int desiredHostRoleMapping = 0;
 
-  /**
-   * Unidirectional one-to-one association to {@link StackEntity}
-   */
-  @OneToOne
-  @JoinColumn(name = "desired_stack_id", unique = false, nullable = false, insertable = true, updatable = true)
-  private StackEntity desiredStack;
-
   @Column(name = "maintenance_state", nullable = false, insertable = true, updatable = true)
   @Enumerated(value = EnumType.STRING)
   private MaintenanceState maintenanceState = MaintenanceState.OFF;
@@ -78,6 +75,13 @@ public class ServiceDesiredStateEntity {
       })
   private ClusterServiceEntity clusterServiceEntity;
 
+  /**
+   * The desired repository that the service should be on.
+   */
+  @ManyToOne
+  @JoinColumn(name = "desired_repo_version_id", unique = false, nullable = false, insertable = true, updatable = true)
+  private RepositoryVersionEntity desiredRepositoryVersion;
+
   public Long getClusterId() {
     return clusterId;
   }
@@ -111,11 +115,7 @@ public class ServiceDesiredStateEntity {
   }
 
   public StackEntity getDesiredStack() {
-    return desiredStack;
-  }
-
-  public void setDesiredStack(StackEntity desiredStack) {
-    this.desiredStack = desiredStack;
+    return desiredRepositoryVersion.getStack();
   }
 
   public MaintenanceState getMaintenanceState() {
@@ -152,6 +152,9 @@ public class ServiceDesiredStateEntity {
     this.credentialStoreEnabled = (short)((credentialStoreEnabled == false) ? 0 : 1);
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
   public boolean equals(Object o) {
     if (this == o) {
@@ -163,37 +166,23 @@ public class ServiceDesiredStateEntity {
     }
 
     ServiceDesiredStateEntity that = (ServiceDesiredStateEntity) o;
+    EqualsBuilder equalsBuilder = new EqualsBuilder();
+    equalsBuilder.append(clusterId, that.clusterId);
+    equalsBuilder.append(desiredState, that.desiredState);
+    equalsBuilder.append(desiredHostRoleMapping, that.desiredHostRoleMapping);
+    equalsBuilder.append(serviceName, that.serviceName);
+    equalsBuilder.append(desiredRepositoryVersion, that.desiredRepositoryVersion);
 
-    if (clusterId != null ? !clusterId.equals(that.clusterId) : that.clusterId != null) {
-      return false;
-    }
-
-    if (desiredState != null ? !desiredState.equals(that.desiredState) : that.desiredState != null) {
-      return false;
-    }
-
-    if (desiredHostRoleMapping != that.desiredHostRoleMapping) {
-      return false;
-    }
-
-    if (serviceName != null ? !serviceName.equals(that.serviceName) : that.serviceName != null) {
-      return false;
-    }
-
-    if (desiredStack != null ? !desiredStack.equals(that.desiredStack) : that.desiredStack != null) {
-      return false;
-    }
-    return true;
+    return equalsBuilder.isEquals();
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
   public int hashCode() {
-    int result = clusterId != null ? clusterId.intValue() : 0;
-    result = 31 * result + (serviceName != null ? serviceName.hashCode() : 0);
-    result = 31 * result + (desiredState != null ? desiredState.hashCode() : 0);
-    result = 31 * result + desiredHostRoleMapping;
-    result = 31 * result + (desiredStack != null ? desiredStack.hashCode() : 0);
-    return result;
+    return Objects.hash(clusterId, serviceName, desiredState, desiredHostRoleMapping,
+        desiredRepositoryVersion);
   }
 
   public ClusterServiceEntity getClusterServiceEntity() {
@@ -203,4 +192,24 @@ public class ServiceDesiredStateEntity {
   public void setClusterServiceEntity(ClusterServiceEntity clusterServiceEntity) {
     this.clusterServiceEntity = clusterServiceEntity;
   }
+
+  /**
+   * Gets the desired repository version.
+   *
+   * @return the desired repository (never {@code null}).
+   */
+  public RepositoryVersionEntity getDesiredRepositoryVersion() {
+    return desiredRepositoryVersion;
+  }
+
+  /**
+   * Sets the desired repository for this service.
+   *
+   * @param desiredRepositoryVersion
+   *          the desired repository (not {@code null}).
+   */
+  public void setDesiredRepositoryVersion(RepositoryVersionEntity desiredRepositoryVersion) {
+    this.desiredRepositoryVersion = desiredRepositoryVersion;
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntityPK.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntityPK.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntityPK.java
index 54dc8b6..3fa5289 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntityPK.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceDesiredStateEntityPK.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -18,9 +18,10 @@
 
 package org.apache.ambari.server.orm.entities;
 
+import java.io.Serializable;
+
 import javax.persistence.Column;
 import javax.persistence.Id;
-import java.io.Serializable;
 
 @SuppressWarnings("serial")
 public class ServiceDesiredStateEntityPK implements Serializable {

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeEntity.java
index 8c37b51..7f4824f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeEntity.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -17,6 +17,7 @@
  */
 package org.apache.ambari.server.orm.entities;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import javax.persistence.CascadeType;
@@ -37,8 +38,12 @@ import javax.persistence.Table;
 import javax.persistence.TableGenerator;
 
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
+import org.apache.ambari.server.state.RepositoryType;
 import org.apache.ambari.server.state.stack.upgrade.Direction;
 import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
+import org.apache.commons.lang.builder.EqualsBuilder;
+
+import com.google.common.base.Objects;
 
 /**
  * Models the data representation of an upgrade
@@ -91,18 +96,6 @@ public class UpgradeEntity {
   @JoinColumn(name = "request_id", nullable = false, insertable = true, updatable = false)
   private RequestEntity requestEntity = null;
 
-  @JoinColumn(
-      name = "from_repo_version_id",
-      referencedColumnName = "repo_version_id",
-      nullable = false)
-  private RepositoryVersionEntity fromRepositoryVersion;
-
-  @JoinColumn(
-      name = "to_repo_version_id",
-      referencedColumnName = "repo_version_id",
-      nullable = false)
-  private RepositoryVersionEntity toRepositoryVersion;
-
   @Column(name="direction", nullable = false)
   @Enumerated(value = EnumType.STRING)
   private Direction direction = Direction.UPGRADE;
@@ -114,6 +107,9 @@ public class UpgradeEntity {
   @Enumerated(value = EnumType.STRING)
   private UpgradeType upgradeType;
 
+  @JoinColumn(name = "repo_version_id", referencedColumnName = "repo_version_id", nullable = false)
+  private RepositoryVersionEntity repositoryVersion;
+
   @Column(name = "skip_failures", nullable = false)
   private Integer skipFailures = 0;
 
@@ -123,6 +119,10 @@ public class UpgradeEntity {
   @Column(name="downgrade_allowed", nullable = false)
   private Short downgrade_allowed = 1;
 
+  @Column(name="orchestration", nullable = false)
+  @Enumerated(value = EnumType.STRING)
+  private RepositoryType orchestration = RepositoryType.STANDARD;
+
   /**
    * {@code true} if the upgrade has been marked as suspended.
    */
@@ -133,6 +133,14 @@ public class UpgradeEntity {
   private List<UpgradeGroupEntity> upgradeGroupEntities;
 
   /**
+   * Uni-directional relationship between an upgrade an all of the components in
+   * that upgrade.
+   */
+  @OneToMany(orphanRemoval=true, cascade = { CascadeType.ALL })
+  @JoinColumn(name = "upgrade_id")
+  private List<UpgradeHistoryEntity> upgradeHistory;
+
+  /**
    * @return the id
    */
   public Long getId() {
@@ -184,45 +192,12 @@ public class UpgradeEntity {
     return requestId;
   }
 
-  /**
-   * @param id the request id
-   */
   public void setRequestEntity(RequestEntity requestEntity) {
     this.requestEntity = requestEntity;
     requestId = requestEntity.getRequestId();
   }
 
   /**
-   * @return the "from" version
-   */
-  public RepositoryVersionEntity getFromRepositoryVersion() {
-    return fromRepositoryVersion;
-  }
-
-  /**
-   * @param repositoryVersion
-   *          the "from" version
-   */
-  public void setFromRepositoryVersion(RepositoryVersionEntity repositoryVersion) {
-    fromRepositoryVersion = repositoryVersion;
-  }
-
-  /**
-   * @return the "to" version
-   */
-  public RepositoryVersionEntity getToRepositoryVersion() {
-    return toRepositoryVersion;
-  }
-
-  /**
-   * @param repositoryVersion
-   *          the "to" version
-   */
-  public void setToRepositoryVersion(RepositoryVersionEntity repositoryVersion) {
-    toRepositoryVersion = repositoryVersion;
-  }
-
-  /**
    * @return the direction of the upgrade
    */
   public Direction getDirection() {
@@ -344,60 +319,112 @@ public class UpgradeEntity {
     this.suspended = suspended ? (short) 1 : (short) 0;
   }
 
+  /**
+   * Adds a historical entry for a service component in this upgrade.
+   *
+   * @param historicalEntry
+   *          the entry to add.
+   */
+  public void addHistory(UpgradeHistoryEntity historicalEntry) {
+    if (null == upgradeHistory) {
+      upgradeHistory = new ArrayList<>();
+    }
+
+    upgradeHistory.add(historicalEntry);
+  }
+
+  /**
+   * Gets the history of this component's upgrades and downgrades.
+   *
+   * @return the component history, or {@code null} if none.
+   */
+  public List<UpgradeHistoryEntity> getHistory() {
+    return upgradeHistory;
+  }
+
+  /**
+   * Upgrades will always have a single version being upgraded to and downgrades
+   * will have a single version being downgraded from. This repository
+   * represents that version.
+   * <p/>
+   * When the direction is {@link Direction#UPGRADE}, this represents the target
+   * repository. <br/>
+   * When the direction is {@link Direction#DOWNGRADE}, this represents the
+   * repository being downgraded from.
+   *
+   * @return the repository version being upgraded to or downgraded from (never
+   *         {@code null}).
+   */
+  public RepositoryVersionEntity getRepositoryVersion() {
+    return repositoryVersion;
+  }
+
+  /**
+   * Sets the repository version for this upgrade. This value will change
+   * depending on the direction of the upgrade.
+   * <p/>
+   * When the direction is {@link Direction#UPGRADE}, this represents the target
+   * repository. <br/>
+   * When the direction is {@link Direction#DOWNGRADE}, this represents the
+   * repository being downgraded from.
+   *
+   * @param repositoryVersion
+   *          the repository version being upgraded to or downgraded from (not
+   *          {@code null}).
+   */
+  public void setRepositoryVersion(RepositoryVersionEntity repositoryVersion) {
+    this.repositoryVersion = repositoryVersion;
+  }
+
+  /**
+   * Sets the orchestration for the upgrade.  Only different when an upgrade is a revert of a patch.
+   * In that case, the orchestration is set to PATCH even if the target repository is type STANDARD.
+   *
+   * @param type  the orchestration
+   */
+  public void setOrchestration(RepositoryType type) {
+    orchestration = type;
+  }
+
+  /**
+   * @return  the orchestration type
+   */
+  public RepositoryType getOrchestration() {
+    return orchestration;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
   @Override
   public boolean equals(Object o) {
     if (this == o) {
       return true;
     }
+
     if (o == null || getClass() != o.getClass()) {
       return false;
     }
 
     UpgradeEntity that = (UpgradeEntity) o;
-
-    if (upgradeId != null ? !upgradeId.equals(that.upgradeId) : that.upgradeId != null) {
-      return false;
-    }
-    if (clusterId != null ? !clusterId.equals(that.clusterId) : that.clusterId != null) {
-      return false;
-    }
-    if (requestId != null ? !requestId.equals(that.requestId) : that.requestId != null) {
-      return false;
-    }
-    if (fromRepositoryVersion != null ? !fromRepositoryVersion.equals(that.fromRepositoryVersion) : that.fromRepositoryVersion != null) {
-      return false;
-    }
-    if (toRepositoryVersion != null ? !toRepositoryVersion.equals(that.toRepositoryVersion) : that.toRepositoryVersion != null) {
-      return false;
-    }
-    if (direction != null ? !direction.equals(that.direction) : that.direction != null) {
-      return false;
-    }
-    if (suspended != null ? !suspended.equals(that.suspended) : that.suspended != null) {
-      return false;
-    }
-    if (upgradeType != null ? !upgradeType.equals(that.upgradeType) : that.upgradeType != null) {
-      return false;
-    }
-    if (upgradePackage != null ? !upgradePackage.equals(that.upgradePackage) : that.upgradePackage != null) {
-      return false;
-    }
-
-    return true;
+    return new EqualsBuilder()
+        .append(upgradeId, that.upgradeId)
+        .append(clusterId, that.clusterId)
+        .append(requestId, that.requestId)
+        .append(direction, that.direction)
+        .append(suspended, that.suspended)
+        .append(upgradeType, that.upgradeType)
+        .append(upgradePackage, that.upgradePackage)
+        .isEquals();
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
   public int hashCode() {
-    int result = upgradeId != null ? upgradeId.hashCode() : 0;
-    result = 31 * result + (clusterId != null ? clusterId.hashCode() : 0);
-    result = 31 * result + (requestId != null ? requestId.hashCode() : 0);
-    result = 31 * result + (fromRepositoryVersion != null ? fromRepositoryVersion.hashCode() : 0);
-    result = 31 * result + (toRepositoryVersion != null ? toRepositoryVersion.hashCode() : 0);
-    result = 31 * result + (direction != null ? direction.hashCode() : 0);
-    result = 31 * result + (suspended != null ? suspended.hashCode() : 0);
-    result = 31 * result + (upgradeType != null ? upgradeType.hashCode() : 0);
-    result = 31 * result + (upgradePackage != null ? upgradePackage.hashCode() : 0);
-    return result;
+    return Objects.hashCode(upgradeId, clusterId, requestId, direction, suspended, upgradeType,
+        upgradePackage);
   }
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeHistoryEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeHistoryEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeHistoryEntity.java
new file mode 100644
index 0000000..0f7ac72
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/UpgradeHistoryEntity.java
@@ -0,0 +1,233 @@
+/**
+ * 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.orm.entities;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.Table;
+import javax.persistence.TableGenerator;
+import javax.persistence.UniqueConstraint;
+
+import org.apache.commons.lang.builder.EqualsBuilder;
+
+import com.google.common.base.Objects;
+
+/**
+ * The {@link UpgradeHistoryEntity} represents the version history of components
+ * participating in an upgrade or a downgrade.
+ */
+@Entity
+@Table(
+    name = "upgrade_history",
+    uniqueConstraints = @UniqueConstraint(
+        columnNames = { "upgrade_id", "component_name", "service_name" }))
+@TableGenerator(
+    name = "upgrade_history_id_generator",
+    table = "ambari_sequences",
+    pkColumnName = "sequence_name",
+    valueColumnName = "sequence_value",
+    pkColumnValue = "upgrade_history_id_seq",
+    initialValue = 0)
+@NamedQueries({
+    @NamedQuery(
+        name = "UpgradeHistoryEntity.findAll",
+        query = "SELECT upgradeHistory FROM UpgradeHistoryEntity upgradeHistory"),
+    @NamedQuery(
+        name = "UpgradeHistoryEntity.findByUpgradeId",
+        query = "SELECT upgradeHistory FROM UpgradeHistoryEntity upgradeHistory WHERE upgradeHistory.upgradeId = :upgradeId")
+})
+public class UpgradeHistoryEntity {
+
+  @Id
+  @Column(name = "id", nullable = false, insertable = true, updatable = false)
+  @GeneratedValue(strategy = GenerationType.TABLE, generator = "upgrade_history_id_generator")
+  private Long id;
+
+  @Column(name = "upgrade_id", nullable = false, insertable = false, updatable = false)
+  private Long upgradeId;
+
+  @JoinColumn(name = "upgrade_id", nullable = false)
+  private UpgradeEntity upgrade;
+
+  @Column(name = "service_name", nullable = false, insertable = true, updatable = true)
+  private String serviceName;
+
+  @Column(name = "component_name", nullable = false, insertable = true, updatable = true)
+  private String componentName;
+
+  @ManyToOne
+  @JoinColumn(name = "from_repo_version_id", unique = false, nullable = false, insertable = true, updatable = true)
+  private RepositoryVersionEntity fromRepositoryVersion = null;
+
+  @ManyToOne
+  @JoinColumn(name = "target_repo_version_id", unique = false, nullable = false, insertable = true, updatable = true)
+  private RepositoryVersionEntity targetRepositoryVersion = null;
+
+  /**
+   * @return the id
+   */
+  public Long getId() {
+    return id;
+  }
+
+  /**
+   * Gets the ID of the upgrade associated with this historical entry.
+   *
+   * @return the upgrade ID (never {@code null}).
+   */
+  public Long getUpgradeId() {
+    return upgradeId;
+  }
+
+  /**
+   * @return
+   */
+  public String getServiceName() {
+    return serviceName;
+  }
+
+  /**
+   * @param serviceName
+   */
+  public void setServiceName(String serviceName) {
+    this.serviceName = serviceName;
+  }
+
+  /**
+   * @return
+   */
+  public String getComponentName() {
+    return componentName;
+  }
+
+  /**
+   * @param componentName
+   */
+  public void setComponentName(String componentName) {
+    this.componentName = componentName;
+  }
+
+  /**
+   * Gets the repository that the upgrade is coming from.
+   *
+   * @return the repository that the upgrade is coming from (not {@code null}).
+   */
+  public RepositoryVersionEntity getFromReposistoryVersion() {
+    return fromRepositoryVersion;
+  }
+
+  /**
+   * Sets the repository that the services in the upgrade are CURRENT on.
+   *
+   * @param repositoryVersionEntity
+   *          the repository entity (not {@code null}).
+   */
+  public void setFromRepositoryVersion(RepositoryVersionEntity repositoryVersionEntity) {
+    fromRepositoryVersion = repositoryVersionEntity;
+  }
+
+  /**
+   * Gets the target repository version for this upgrade.
+   *
+   * @return the target repository for the services in the upgrade (not
+   *         {@code null}).
+   */
+  public RepositoryVersionEntity getTargetRepositoryVersion() {
+    return targetRepositoryVersion;
+  }
+
+  /**
+   * Gets the version of the target repository.
+   *
+   * @return the target version string (never {@code null}).
+   * @see #getTargetRepositoryVersion()
+   */
+  public String getTargetVersion() {
+    return targetRepositoryVersion.getVersion();
+  }
+
+  /**
+   * Sets the target repository of the upgrade.
+   *
+   * @param repositoryVersionEntity
+   *          the target repository (not {@code null}).
+   */
+  public void setTargetRepositoryVersion(RepositoryVersionEntity repositoryVersionEntity) {
+    targetRepositoryVersion = repositoryVersionEntity;
+  }
+
+  /**
+   * Sets the associated upgrade entity.
+   *
+   * @param upgrade
+   */
+  public void setUpgrade(UpgradeEntity upgrade) {
+    this.upgrade = upgrade;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    UpgradeHistoryEntity that = (UpgradeHistoryEntity) o;
+    return new EqualsBuilder()
+        .append(id, that.id)
+        .append(upgradeId, that.upgradeId)
+        .append(serviceName, that.serviceName)
+        .append(componentName, that.componentName)
+        .isEquals();
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public int hashCode() {
+    return Objects.hashCode(id, upgradeId, serviceName, componentName);
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String toString() {
+    return Objects.toStringHelper(this)
+        .add("id", id)
+        .add("upgradeId", upgradeId)
+        .add("serviceName", serviceName)
+        .add("componentName", componentName)
+        .add("from", fromRepositoryVersion)
+        .add("to", targetRepositoryVersion).toString();
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/PrepareDisableKerberosServerAction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/PrepareDisableKerberosServerAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/PrepareDisableKerberosServerAction.java
index e0d16af..54ba784 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/PrepareDisableKerberosServerAction.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/PrepareDisableKerberosServerAction.java
@@ -91,7 +91,7 @@ public class PrepareDisableKerberosServerAction extends AbstractPrepareKerberosS
           }
         });
 
-    Map<String, Map<String, String>> kerberosConfigurations = new HashMap<String, Map<String, String>>();
+    Map<String, Map<String, String>> kerberosConfigurations = new HashMap<>();
     Map<String, String> commandParameters = getCommandParameters();
     String dataDirectory = getCommandParameterValue(commandParameters, DATA_DIRECTORY);
 
@@ -143,7 +143,7 @@ public class PrepareDisableKerberosServerAction extends AbstractPrepareKerberosS
     // Ensure the cluster-env/security_enabled flag is set properly
     Map<String, String> clusterEnvProperties = kerberosConfigurations.get(KerberosHelper.SECURITY_ENABLED_CONFIG_TYPE);
     if (clusterEnvProperties == null) {
-      clusterEnvProperties = new HashMap<String, String>();
+      clusterEnvProperties = new HashMap<>();
       kerberosConfigurations.put(KerberosHelper.SECURITY_ENABLED_CONFIG_TYPE, clusterEnvProperties);
     }
     clusterEnvProperties.put(KerberosHelper.SECURITY_ENABLED_PROPERTY_NAME, "false");
@@ -157,13 +157,13 @@ public class PrepareDisableKerberosServerAction extends AbstractPrepareKerberosS
         throw new AmbariException(message);
       }
 
-      Map<String, Set<String>> configurationsToRemove = new HashMap<String, Set<String>>();
+      Map<String, Set<String>> configurationsToRemove = new HashMap<>();
 
       // Fill the configurationsToRemove map with all Kerberos-related configurations.  Values
       // needed to be kept will have new values from the stack definition and thus pruned from
       // this map.
       for (Map.Entry<String, Map<String, String>> entry : kerberosConfigurations.entrySet()) {
-        configurationsToRemove.put(entry.getKey(), new HashSet<String>(entry.getValue().keySet()));
+        configurationsToRemove.put(entry.getKey(), new HashSet<>(entry.getValue().keySet()));
       }
 
       // Remove cluster-env from the set of configurations to remove since it has no default set
@@ -171,13 +171,13 @@ public class PrepareDisableKerberosServerAction extends AbstractPrepareKerberosS
       configurationsToRemove.remove("cluster-env");
 
       if (!schToProcess.isEmpty()) {
-        Set<String> visitedServices = new HashSet<String>();
+        Set<String> visitedServices = new HashSet<>();
 
         for (ServiceComponentHost sch : schToProcess) {
           String serviceName = sch.getServiceName();
 
           if (!visitedServices.contains(serviceName)) {
-            StackId stackVersion = sch.getStackVersion();
+            StackId stackVersion = sch.getDesiredStackId();
 
             visitedServices.add(serviceName);
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/UpdateKerberosConfigsServerAction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/UpdateKerberosConfigsServerAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/UpdateKerberosConfigsServerAction.java
index 2177c04..1307f23 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/UpdateKerberosConfigsServerAction.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/UpdateKerberosConfigsServerAction.java
@@ -18,7 +18,15 @@
 
 package org.apache.ambari.server.serveraction.kerberos;
 
-import com.google.inject.Inject;
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentMap;
+
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.agent.CommandReport;
@@ -31,14 +39,7 @@ import org.apache.ambari.server.state.SecurityType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.File;
-import java.io.IOException;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentMap;
+import com.google.inject.Inject;
 
 /**
  * UpdateKerberosConfigServerAction is implementation of ServerAction that updates service configs
@@ -85,8 +86,8 @@ public class UpdateKerberosConfigsServerAction extends AbstractServerAction {
 
     String authenticatedUserName = getCommandParameterValue(getCommandParameters(), KerberosServerAction.AUTHENTICATED_USER_NAME);
     String dataDirectoryPath = getCommandParameterValue(getCommandParameters(), KerberosServerAction.DATA_DIRECTORY);
-    HashMap<String, Map<String, String>> propertiesToSet = new HashMap<String, Map<String, String>>();
-    HashMap<String, Collection<String>> propertiesToRemove = new HashMap<String, Collection<String>>();
+    HashMap<String, Map<String, String>> propertiesToSet = new HashMap<>();
+    HashMap<String, Collection<String>> propertiesToRemove = new HashMap<>();
 
     // If the data directory path is set, attempt to process further, else assume there is no work to do
     if (dataDirectoryPath != null) {
@@ -95,7 +96,7 @@ public class UpdateKerberosConfigsServerAction extends AbstractServerAction {
       // If the data directory exists, attempt to process further, else assume there is no work to do
       if (dataDirectory.exists()) {
         KerberosConfigDataFileReader configReader = null;
-        Set<String> configTypes = new HashSet<String>();
+        Set<String> configTypes = new HashSet<>();
 
         try {
           // If the config data file exists, iterate over the records to find the (explicit)
@@ -146,7 +147,7 @@ public class UpdateKerberosConfigsServerAction extends AbstractServerAction {
             }
 
             for (String configType : configTypes) {
-              configHelper.updateConfigType(cluster, controller, configType,
+              configHelper.updateConfigType(cluster, cluster.getDesiredStackVersion(), controller, configType,
                   propertiesToSet.get(configType),
                   propertiesToRemove.get(configType),
                   authenticatedUserName, configNote);
@@ -200,7 +201,7 @@ public class UpdateKerberosConfigsServerAction extends AbstractServerAction {
   private void addConfigTypePropVal(HashMap<String, Map<String, String>> configurations, String configType, String prop, String val) {
     Map<String, String> configTypePropsVal = configurations.get(configType);
     if (configTypePropsVal == null) {
-      configTypePropsVal = new HashMap<String, String>();
+      configTypePropsVal = new HashMap<>();
       configurations.put(configType, configTypePropsVal);
     }
     configTypePropsVal.put(prop, val);
@@ -217,7 +218,7 @@ public class UpdateKerberosConfigsServerAction extends AbstractServerAction {
   private void removeConfigTypeProp(HashMap<String, Collection<String>> configurations, String configType, String prop) {
     Collection<String> configTypeProps = configurations.get(configType);
     if (configTypeProps == null) {
-      configTypeProps = new HashSet<String>();
+      configTypeProps = new HashSet<>();
       configurations.put(configType, configTypeProps);
     }
     configTypeProps.add(prop);

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/AbstractUpgradeServerAction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/AbstractUpgradeServerAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/AbstractUpgradeServerAction.java
new file mode 100644
index 0000000..e012dac
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/AbstractUpgradeServerAction.java
@@ -0,0 +1,59 @@
+/*
+ * 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.serveraction.upgrades;
+
+import org.apache.ambari.server.orm.entities.UpgradeEntity;
+import org.apache.ambari.server.serveraction.AbstractServerAction;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.UpgradeContext;
+import org.apache.ambari.server.state.UpgradeContextFactory;
+import org.apache.ambari.server.state.UpgradeHelper;
+
+import com.google.inject.Inject;
+
+/**
+ * Abstract class that reads values from command params in a consistent way.
+ */
+public abstract class AbstractUpgradeServerAction extends AbstractServerAction {
+
+  @Inject
+  protected Clusters m_clusters;
+
+  /**
+   * Used to move desired repo versions forward.
+   */
+  @Inject
+  protected UpgradeHelper m_upgradeHelper;
+
+  /**
+   * Used to create instances of {@link UpgradeContext} with injected
+   * dependencies.
+   */
+  @Inject
+  private UpgradeContextFactory m_upgradeContextFactory;
+
+  /**
+   * Gets an initialized {@link UpgradeContext} for the in-progress upgrade.
+   */
+  protected UpgradeContext getUpgradeContext(Cluster cluster) {
+    UpgradeEntity upgrade = cluster.getUpgradeInProgress();
+    UpgradeContext upgradeContext = m_upgradeContextFactory.create(cluster, upgrade);
+    return upgradeContext;
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckAction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckAction.java
index 9ae9844..44d2b4d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckAction.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckAction.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -18,7 +18,6 @@
 package org.apache.ambari.server.serveraction.upgrades;
 
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -28,16 +27,13 @@ import java.util.concurrent.ConcurrentMap;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.agent.CommandReport;
-import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.UpgradeContext;
-import org.apache.ambari.server.state.UpgradeContextFactory;
 import org.apache.commons.lang.StringUtils;
 
 import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonPrimitive;
-import com.google.inject.Inject;
 
 /**
  * Action that checks component versions to ensure {@link FinalizeUpgradeAction} will
@@ -45,30 +41,23 @@ import com.google.inject.Inject;
  */
 public class ComponentVersionCheckAction extends FinalizeUpgradeAction {
 
-  /**
-   * Used for building {@link UpgradeContext} instances.
-   */
-  @Inject
-  private UpgradeContextFactory upgradeContextFactory;
 
   @Override
   public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext)
       throws AmbariException, InterruptedException {
 
     String clusterName = getExecutionCommand().getClusterName();
-    Cluster cluster = clusters.getCluster(clusterName);
-    UpgradeContext context = upgradeContextFactory.create(cluster, cluster.getUpgradeInProgress());
-    RepositoryVersionEntity targetRepositoryVersion = context.getTargetRepositoryVersion();
 
-    List<InfoTuple> errors = checkHostComponentVersions(cluster,
-        targetRepositoryVersion.getVersion(), context.getTargetStackId());
+    Cluster cluster = m_clusters.getCluster(clusterName);
+
+    UpgradeContext upgradeContext = getUpgradeContext(cluster);
+    Set<InfoTuple> errors = validateComponentVersions(upgradeContext);
 
     StringBuilder outSB = new StringBuilder();
     StringBuilder errSB = new StringBuilder();
 
     if (errors.isEmpty()) {
-      outSB.append("No version mismatches found for components");
-      errSB.append("No errors found for components");
+      outSB.append("All service components are reporting the correct version.");
       return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", outSB.toString(), errSB.toString());
     } else {
       String structuredOut = getErrors(outSB, errSB, errors);
@@ -76,19 +65,21 @@ public class ComponentVersionCheckAction extends FinalizeUpgradeAction {
     }
   }
 
-  private String getErrors(StringBuilder outSB, StringBuilder errSB, List<InfoTuple> errors) {
+  private String getErrors(StringBuilder outSB, StringBuilder errSB, Set<InfoTuple> errors) {
 
-    errSB.append("The following components were found to have version mismatches.  ");
-    errSB.append("Finalize will not complete successfully:\n");
+    errSB.append("Finalization will not be able to completed because of the following version inconsistencies:");
 
     Set<String> hosts = new TreeSet<>();
     Map<String, JsonArray> hostDetails = new HashMap<>();
 
     for (InfoTuple tuple : errors) {
+      errSB.append("  ");
       errSB.append(tuple.hostName).append(": ");
+      errSB.append(System.lineSeparator()).append("    ");
       errSB.append(tuple.serviceName).append('/').append(tuple.componentName);
       errSB.append(" reports ").append(StringUtils.trimToEmpty(tuple.currentVersion));
-      errSB.append('\n');
+      errSB.append(" but expects ").append(tuple.targetVersion);
+      errSB.append(System.lineSeparator());
 
       hosts.add(tuple.hostName);
 
@@ -100,6 +91,7 @@ public class ComponentVersionCheckAction extends FinalizeUpgradeAction {
       obj.addProperty("service", tuple.serviceName);
       obj.addProperty("component", tuple.componentName);
       obj.addProperty("version", tuple.currentVersion);
+      obj.addProperty("targetVersion", tuple.targetVersion);
 
       hostDetails.get(tuple.hostName).add(obj);
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ConfigureAction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ConfigureAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ConfigureAction.java
index a42e667..eb432df 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ConfigureAction.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ConfigureAction.java
@@ -536,7 +536,7 @@ public class ConfigureAction extends AbstractServerAction {
       auditName = m_configuration.getAnonymousAuditName();
     }
 
-    m_configHelper.createConfigType(cluster, m_controller, configType,
+    m_configHelper.createConfigType(cluster, cluster.getDesiredStackVersion(), m_controller, configType,
         newValues, auditName, serviceVersionNote);
 
     String message = "Finished updating configuration ''{0}''";


[36/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariActionExecutionHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariActionExecutionHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariActionExecutionHelper.java
index f75fb41..1b0e0e0 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariActionExecutionHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariActionExecutionHelper.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -47,13 +47,14 @@ import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.internal.RequestResourceFilter;
 import org.apache.ambari.server.customactions.ActionDefinition;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
 import org.apache.ambari.server.orm.entities.RepositoryEntity;
+import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ComponentInfo;
+import org.apache.ambari.server.state.Service;
+import org.apache.ambari.server.state.ServiceComponent;
 import org.apache.ambari.server.state.ServiceComponentHost;
 import org.apache.ambari.server.state.ServiceInfo;
 import org.apache.ambari.server.state.StackId;
@@ -91,8 +92,6 @@ public class AmbariActionExecutionHelper {
   private MaintenanceStateHelper maintenanceStateHelper;
   @Inject
   private Configuration configs;
-  @Inject
-  private ClusterVersionDAO clusterVersionDAO;
 
   /**
    * Validates the request to execute an action.
@@ -148,8 +147,6 @@ public class AmbariActionExecutionHelper {
           actionRequest.getClusterName());
       }
 
-      StackId stackId = cluster.getCurrentStackVersion();
-
       String expectedService = actionDef.getTargetService() == null ? "" : actionDef.getTargetService();
 
       String actualService = resourceFilter == null || resourceFilter.getServiceName() == null ? "" : resourceFilter.getServiceName();
@@ -159,11 +156,14 @@ public class AmbariActionExecutionHelper {
       }
 
       targetService = expectedService;
-      if (targetService == null || targetService.isEmpty()) {
+      if (StringUtils.isBlank(targetService)) {
         targetService = actualService;
       }
 
-      if (targetService != null && !targetService.isEmpty()) {
+      if (StringUtils.isNotBlank(targetService)) {
+        Service service = cluster.getService(targetService);
+        StackId stackId = service.getDesiredStackId();
+
         ServiceInfo serviceInfo;
         try {
           serviceInfo = ambariMetaInfo.getService(stackId.getStackName(), stackId.getStackVersion(),
@@ -186,16 +186,20 @@ public class AmbariActionExecutionHelper {
       }
 
       targetComponent = expectedComponent;
-      if (targetComponent == null || targetComponent.isEmpty()) {
+      if (StringUtils.isBlank(targetComponent)) {
         targetComponent = actualComponent;
       }
 
-      if (!targetComponent.isEmpty() && targetService.isEmpty()) {
+      if (StringUtils.isNotBlank(targetComponent) && StringUtils.isBlank(targetService)) {
         throw new AmbariException("Action " + actionRequest.getActionName() + " targets component " + targetComponent +
           " without specifying the target service.");
       }
 
-      if (targetComponent != null && !targetComponent.isEmpty()) {
+      if (StringUtils.isNotBlank(targetComponent)) {
+        Service service = cluster.getService(targetService);
+        ServiceComponent component = service.getServiceComponent(targetComponent);
+        StackId stackId = component.getDesiredStackId();
+
         ComponentInfo compInfo;
         try {
           compInfo = ambariMetaInfo.getComponent(stackId.getStackName(), stackId.getStackVersion(),
@@ -283,13 +287,16 @@ public class AmbariActionExecutionHelper {
     }
 
     if (null != cluster) {
-      StackId stackId = cluster.getCurrentStackVersion();
+//      StackId stackId = cluster.getCurrentStackVersion();
       if (serviceName != null && !serviceName.isEmpty()) {
         if (componentName != null && !componentName.isEmpty()) {
-          Map<String, ServiceComponentHost> componentHosts =
-            cluster.getService(serviceName)
-              .getServiceComponent(componentName).getServiceComponentHosts();
+          Service service = cluster.getService(serviceName);
+          ServiceComponent component = service.getServiceComponent(componentName);
+          StackId stackId = component.getDesiredStackId();
+
+          Map<String, ServiceComponentHost> componentHosts = component.getServiceComponentHosts();
           candidateHosts.addAll(componentHosts.keySet());
+
           try {
             componentInfo = ambariMetaInfo.getComponent(stackId.getStackName(),
                 stackId.getStackVersion(), serviceName, componentName);
@@ -299,8 +306,7 @@ public class AmbariActionExecutionHelper {
           }
         } else {
           for (String component : cluster.getService(serviceName).getServiceComponents().keySet()) {
-            Map<String, ServiceComponentHost> componentHosts =
-              cluster.getService(serviceName)
+            Map<String, ServiceComponentHost> componentHosts = cluster.getService(serviceName)
                 .getServiceComponent(component).getServiceComponentHosts();
             candidateHosts.addAll(componentHosts.keySet());
           }
@@ -325,12 +331,9 @@ public class AmbariActionExecutionHelper {
       );
 
       if (! ignoredHosts.isEmpty()) {
-        LOG.debug("Hosts to ignore: {}.", StringUtils.join(ignoredHosts, ", "));
-        LOG.debug("Ignoring action for hosts due to maintenance state." +
-            "Ignored hosts =" + ignoredHosts + ", component="
-            + componentName + ", service=" + serviceName
-            + ", cluster=" + cluster.getClusterName() + ", " +
-            "actionName=" + actionContext.getActionName());
+        LOG.debug("Hosts to ignore: {}.", ignoredHosts);
+        LOG.debug("Ignoring action for hosts due to maintenance state.Ignored hosts ={}, component={}, service={}, cluster={}, actionName={}",
+          ignoredHosts, componentName, serviceName, cluster.getClusterName(), actionContext.getActionName());
       }
     }
 
@@ -454,12 +457,15 @@ public class AmbariActionExecutionHelper {
       for (Map.Entry<String, String> dbConnectorName : configs.getDatabaseConnectorNames().entrySet()) {
         hostLevelParams.put(dbConnectorName.getKey(), dbConnectorName.getValue());
       }
-
       for (Map.Entry<String, String> previousDBConnectorName : configs.getPreviousDatabaseConnectorNames().entrySet()) {
         hostLevelParams.put(previousDBConnectorName.getKey(), previousDBConnectorName.getValue());
       }
 
-      addRepoInfoToHostLevelParams(cluster, actionContext, hostLevelParams, hostName);
+      if (StringUtils.isNotBlank(serviceName)) {
+        Service service = cluster.getService(serviceName);
+        addRepoInfoToHostLevelParams(service.getDesiredRepositoryVersion(), hostLevelParams, hostName);
+      }
+
 
       Map<String, String> roleParams = execCmd.getRoleParams();
       if (roleParams == null) {
@@ -489,6 +495,8 @@ public class AmbariActionExecutionHelper {
           execCmd.getLocalComponents().add(sch.getServiceComponentName());
         }
       }
+
+      actionContext.visitAll(execCmd);
     }
   }
 
@@ -519,47 +527,35 @@ public class AmbariActionExecutionHelper {
   *
   * */
 
-  private void addRepoInfoToHostLevelParams(Cluster cluster, ActionExecutionContext actionContext,
+  private void addRepoInfoToHostLevelParams(RepositoryVersionEntity repositoryVersion,
       Map<String, String> hostLevelParams, String hostName) throws AmbariException {
-    if (null == cluster) {
+    if (null == repositoryVersion) {
       return;
     }
 
     JsonObject rootJsonObject = new JsonObject();
     JsonArray repositories = new JsonArray();
-    ClusterVersionEntity clusterVersionEntity = clusterVersionDAO.findByClusterAndStateCurrent(
-        cluster.getClusterName());
-
-    if (clusterVersionEntity != null && clusterVersionEntity.getRepositoryVersion() != null) {
-      String hostOsFamily = clusters.getHost(hostName).getOsFamily();
-      for (OperatingSystemEntity operatingSystemEntity : clusterVersionEntity.getRepositoryVersion().getOperatingSystems()) {
-        // ostype in OperatingSystemEntity it's os family. That should be fixed
-        // in OperatingSystemEntity.
-        if (operatingSystemEntity.getOsType().equals(hostOsFamily)) {
-          for (RepositoryEntity repositoryEntity : operatingSystemEntity.getRepositories()) {
-            JsonObject repositoryInfo = new JsonObject();
-            repositoryInfo.addProperty("base_url", repositoryEntity.getBaseUrl());
-            repositoryInfo.addProperty("repo_name", repositoryEntity.getName());
-            repositoryInfo.addProperty("repo_id", repositoryEntity.getRepositoryId());
-
-            repositories.add(repositoryInfo);
-          }
-          rootJsonObject.add("repositories", repositories);
+
+    String hostOsFamily = clusters.getHost(hostName).getOsFamily();
+    for (OperatingSystemEntity operatingSystemEntity : repositoryVersion.getOperatingSystems()) {
+      // ostype in OperatingSystemEntity it's os family. That should be fixed
+      // in OperatingSystemEntity.
+      if (operatingSystemEntity.getOsType().equals(hostOsFamily)) {
+        for (RepositoryEntity repositoryEntity : operatingSystemEntity.getRepositories()) {
+          JsonObject repositoryInfo = new JsonObject();
+          repositoryInfo.addProperty("base_url", repositoryEntity.getBaseUrl());
+          repositoryInfo.addProperty("repo_name", repositoryEntity.getName());
+          repositoryInfo.addProperty("repo_id", repositoryEntity.getRepositoryId());
+
+          repositories.add(repositoryInfo);
         }
+        rootJsonObject.add("repositories", repositories);
       }
     }
 
     hostLevelParams.put(REPO_INFO, rootJsonObject.toString());
 
-    // set the host level params if not already set by whoever is creating this command
-    if (!hostLevelParams.containsKey(STACK_NAME) || !hostLevelParams.containsKey(STACK_VERSION)) {
-      // see if the action context has a stack ID set to use, otherwise use the
-      // cluster's current stack ID
-      StackId stackId = actionContext.getStackId() != null ? actionContext.getStackId()
-          : cluster.getCurrentStackVersion();
-
-      hostLevelParams.put(STACK_NAME, stackId.getStackName());
-      hostLevelParams.put(STACK_VERSION, stackId.getStackVersion());
-    }
+    hostLevelParams.put(STACK_NAME, repositoryVersion.getStackName());
+    hostLevelParams.put(STACK_VERSION, repositoryVersion.getStackVersion());
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelper.java
index 9b19ade..6360a04 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelper.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -29,11 +29,7 @@ import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.DB_NAME;
 import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.GROUP_LIST;
 import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.HOOKS_FOLDER;
 import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.HOST_SYS_PREPPED;
-import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.JAVA_HOME;
-import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.JAVA_VERSION;
-import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.JCE_NAME;
 import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.JDK_LOCATION;
-import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.JDK_NAME;
 import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.MYSQL_JDBC_URL;
 import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.NOT_MANAGED_HDFS_PATH_LIST;
 import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.ORACLE_JDBC_URL;
@@ -58,6 +54,8 @@ import java.util.Random;
 import java.util.Set;
 import java.util.TreeMap;
 
+import org.apache.ambari.annotations.Experimental;
+import org.apache.ambari.annotations.ExperimentalFeature;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.Role;
 import org.apache.ambari.server.RoleCommand;
@@ -65,6 +63,7 @@ import org.apache.ambari.server.actionmanager.HostRoleCommand;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.actionmanager.Stage;
 import org.apache.ambari.server.agent.AgentCommand.AgentCommandType;
+import org.apache.ambari.server.agent.CommandRepository;
 import org.apache.ambari.server.agent.ExecutionCommand;
 import org.apache.ambari.server.agent.ExecutionCommand.KeyNames;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
@@ -73,9 +72,7 @@ import org.apache.ambari.server.controller.internal.RequestOperationLevel;
 import org.apache.ambari.server.controller.internal.RequestResourceFilter;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.metadata.ActionMetadata;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
 import org.apache.ambari.server.orm.entities.RepositoryEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
@@ -83,6 +80,7 @@ import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.CommandScriptDefinition;
 import org.apache.ambari.server.state.ComponentInfo;
+import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.ConfigHelper;
 import org.apache.ambari.server.state.CustomCommandDefinition;
 import org.apache.ambari.server.state.DesiredConfig;
@@ -92,7 +90,6 @@ import org.apache.ambari.server.state.HostState;
 import org.apache.ambari.server.state.MaintenanceState;
 import org.apache.ambari.server.state.PropertyInfo.PropertyType;
 import org.apache.ambari.server.state.RepositoryInfo;
-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.ServiceComponentHost;
@@ -108,6 +105,7 @@ import org.apache.commons.lang.math.NumberUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Function;
 import com.google.gson.Gson;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
@@ -143,6 +141,8 @@ public class AmbariCustomCommandExecutionHelper {
 
   private final static String ALIGN_MAINTENANCE_STATE = "align_maintenance_state";
 
+  public final static int MIN_STRICT_SERVICE_CHECK_TIMEOUT = 120;
+
   @Inject
   private ActionMetadata actionMetadata;
 
@@ -171,9 +171,6 @@ public class AmbariCustomCommandExecutionHelper {
   private OsFamily os_family;
 
   @Inject
-  private ClusterVersionDAO clusterVersionDAO;
-
-  @Inject
   private HostRoleCommandDAO hostRoleCommandDAO;
 
   private Map<String, Map<String, Map<String, String>>> configCredentialsForService = new HashMap<>();
@@ -195,12 +192,15 @@ public class AmbariCustomCommandExecutionHelper {
       String serviceName, String componentName, String commandName)
       throws AmbariException {
 
-    Cluster cluster = clusters.getCluster(clusterName);
-    StackId stackId = cluster.getDesiredStackVersion();
-
     if (componentName == null) {
       return false;
     }
+
+    Cluster cluster = clusters.getCluster(clusterName);
+    Service service = cluster.getService(serviceName);
+    ServiceComponent component = service.getServiceComponent(componentName);
+    StackId stackId = component.getDesiredStackId();
+
     ComponentInfo componentInfo = ambariMetaInfo.getComponent(
         stackId.getStackName(), stackId.getStackVersion(),
         serviceName, componentName);
@@ -267,8 +267,8 @@ public class AmbariCustomCommandExecutionHelper {
    * @throws AmbariException
    */
   private void addCustomCommandAction(final ActionExecutionContext actionExecutionContext,
-      final RequestResourceFilter resourceFilter, Stage stage,
-      Map<String, String> additionalCommandParams, String commandDetail) throws AmbariException {
+      final RequestResourceFilter resourceFilter, Stage stage, Map<String, String> additionalCommandParams,
+      String commandDetail, Map<String, String> requestParams) throws AmbariException {
     final String serviceName = resourceFilter.getServiceName();
     final String componentName = resourceFilter.getComponentName();
     final String commandName = actionExecutionContext.getActionName();
@@ -320,10 +320,12 @@ public class AmbariCustomCommandExecutionHelper {
       throw new AmbariException(message);
     }
 
-    StackId stackId = cluster.getDesiredStackVersion();
+    Service service = cluster.getService(serviceName);
+    StackId stackId = service.getDesiredStackId();
+
     AmbariMetaInfo ambariMetaInfo = managementController.getAmbariMetaInfo();
-    ServiceInfo serviceInfo = ambariMetaInfo.getService(
-        stackId.getStackName(), stackId.getStackVersion(), serviceName);
+    ServiceInfo serviceInfo = ambariMetaInfo.getService(service);
+    StackInfo stackInfo = ambariMetaInfo.getStack(stackId);
 
     CustomCommandDefinition customCommandDefinition = null;
     ComponentInfo ci = serviceInfo.getComponentByName(componentName);
@@ -376,16 +378,21 @@ public class AmbariCustomCommandExecutionHelper {
         execCmd.setCommandType(AgentCommandType.BACKGROUND_EXECUTION_COMMAND);
       }
 
+      execCmd.setComponentVersions(cluster);
+
       execCmd.setConfigurations(configurations);
       execCmd.setConfigurationAttributes(configurationAttributes);
       execCmd.setConfigurationTags(configTags);
 
-      execCmd.setAvailableServicesFromServiceInfoMap(ambariMetaInfo.getServices(stackId.getStackName(), stackId.getStackVersion()));
-
       // Get the value of credential store enabled from the DB
       Service clusterService = cluster.getService(serviceName);
       execCmd.setCredentialStoreEnabled(String.valueOf(clusterService.isCredentialStoreEnabled()));
 
+      ServiceComponent component = null;
+      if (StringUtils.isNotBlank(componentName)) {
+        component = clusterService.getServiceComponent(componentName);
+      }
+
       // Get the map of service config type to password properties for the service
       Map<String, Map<String, String>> configCredentials;
       configCredentials = configCredentialsForService.get(clusterService.getName());
@@ -401,7 +408,7 @@ public class AmbariCustomCommandExecutionHelper {
       hostLevelParams.put(CUSTOM_COMMAND, commandName);
 
       // Set parameters required for re-installing clients on restart
-      hostLevelParams.put(REPO_INFO, getRepoInfo(cluster, host));
+      hostLevelParams.put(REPO_INFO, getRepoInfo(cluster, component, host));
       hostLevelParams.put(STACK_NAME, stackId.getStackName());
       hostLevelParams.put(STACK_VERSION, stackId.getStackVersion());
 
@@ -466,7 +473,22 @@ public class AmbariCustomCommandExecutionHelper {
         commandTimeout = Math.max(60, commandTimeout);
       }
 
+      if (requestParams != null && requestParams.containsKey("context")) {
+        String requestContext = requestParams.get("context");
+        if (StringUtils.isNotEmpty(requestContext) && requestContext.toLowerCase().contains("rolling-restart")) {
+          Config clusterEnvConfig = cluster.getDesiredConfigByType("cluster-env");
+          if (clusterEnvConfig != null) {
+            String componentRollingRestartTimeout = clusterEnvConfig.getProperties().get("namenode_rolling_restart_timeout");
+            if (StringUtils.isNotEmpty(componentRollingRestartTimeout)) {
+              commandTimeout = Integer.parseInt(componentRollingRestartTimeout);
+            }
+          }
+        }
+      }
+
       commandParams.put(COMMAND_TIMEOUT, "" + commandTimeout);
+      commandParams.put(SERVICE_PACKAGE_FOLDER, serviceInfo.getServicePackageFolder());
+      commandParams.put(HOOKS_FOLDER, stackInfo.getStackHooksFolder());
 
       Map<String, String> roleParams = execCmd.getRoleParams();
       if (roleParams == null) {
@@ -485,6 +507,8 @@ public class AmbariCustomCommandExecutionHelper {
       execCmd.setCommandParams(commandParams);
       execCmd.setRoleParams(roleParams);
 
+      execCmd.setRepositoryFile(getCommandRepository(cluster, component, host));
+
       // perform any server side command related logic - eg - set desired states on restart
       applyCustomCommandBackendLogic(cluster, serviceName, componentName, commandName, hostName);
     }
@@ -677,7 +701,13 @@ public class AmbariCustomCommandExecutionHelper {
 
     String clusterName = stage.getClusterName();
     Cluster cluster = clusters.getCluster(clusterName);
-    StackId stackId = cluster.getDesiredStackVersion();
+    Service service = cluster.getService(serviceName);
+    ServiceComponent component = null;
+    if (null != componentName) {
+      component = service.getServiceComponent(componentName);
+    }
+    StackId stackId = (null != component) ? component.getDesiredStackId() : service.getDesiredStackId();
+
     AmbariMetaInfo ambariMetaInfo = managementController.getAmbariMetaInfo();
     ServiceInfo serviceInfo = ambariMetaInfo.getService(stackId.getStackName(),
         stackId.getStackVersion(), serviceName);
@@ -770,6 +800,12 @@ public class AmbariCustomCommandExecutionHelper {
     }
 
     commandParams.put(COMMAND_TIMEOUT, commandTimeout);
+    String checkType = configHelper.getValueFromDesiredConfigurations(cluster, ConfigHelper.CLUSTER_ENV, ConfigHelper.SERVICE_CHECK_TYPE);
+    if (ConfigHelper.SERVICE_CHECK_MINIMAL.equals(checkType)) {
+      int actualTimeout = Integer.parseInt(commandParams.get(COMMAND_TIMEOUT)) / 2;
+      actualTimeout = actualTimeout < MIN_STRICT_SERVICE_CHECK_TIMEOUT ? MIN_STRICT_SERVICE_CHECK_TIMEOUT : actualTimeout;
+      commandParams.put(COMMAND_TIMEOUT, Integer.toString(actualTimeout));
+    }
     commandParams.put(SERVICE_PACKAGE_FOLDER, serviceInfo.getServicePackageFolder());
     commandParams.put(HOOKS_FOLDER, stackInfo.getStackHooksFolder());
 
@@ -822,7 +858,7 @@ public class AmbariCustomCommandExecutionHelper {
     cloneSet.retainAll(includedHosts);
     if (cloneSet.size() > 0) {
       throw new AmbariException("Same host cannot be specified for inclusion " +
-        "as well as exclusion. Hosts: " + cloneSet.toString());
+        "as well as exclusion. Hosts: " + cloneSet);
     }
 
     Service service = cluster.getService(serviceName);
@@ -1009,7 +1045,7 @@ public class AmbariCustomCommandExecutionHelper {
 
       if (!serviceName.equals(Service.Type.HBASE.name()) || hostName.equals(primaryCandidate)) {
         commandParams.put(UPDATE_EXCLUDE_FILE_ONLY, "false");
-        addCustomCommandAction(commandContext, commandFilter, stage, commandParams, commandDetail.toString());
+        addCustomCommandAction(commandContext, commandFilter, stage, commandParams, commandDetail.toString(), null);
       }
     }
   }
@@ -1082,10 +1118,8 @@ public class AmbariCustomCommandExecutionHelper {
     List<RequestResourceFilter> resourceFilters = actionExecutionContext.getResourceFilters();
 
     for (RequestResourceFilter resourceFilter : resourceFilters) {
-      LOG.debug("Received a command execution request"
-        + ", clusterName=" + actionExecutionContext.getClusterName()
-        + ", serviceName=" + resourceFilter.getServiceName()
-        + ", request=" + actionExecutionContext.toString());
+      LOG.debug("Received a command execution request, clusterName={}, serviceName={}, request={}",
+        actionExecutionContext.getClusterName(), resourceFilter.getServiceName(), actionExecutionContext);
 
       String actionName = actionExecutionContext.getActionName();
       if (actionName.contains(SERVICE_CHECK_COMMAND_NAME)) {
@@ -1136,7 +1170,7 @@ public class AmbariCustomCommandExecutionHelper {
           }
         }
 
-        addCustomCommandAction(actionExecutionContext, resourceFilter, stage, extraParams, commandDetail);
+        addCustomCommandAction(actionExecutionContext, resourceFilter, stage, extraParams, commandDetail, requestParams);
       } else {
         throw new AmbariException("Unsupported action " + actionName);
       }
@@ -1151,106 +1185,208 @@ public class AmbariCustomCommandExecutionHelper {
    *
    * @return the repo info
    *
+   * @deprecated use {@link #getCommandRepository(Cluster, ServiceComponent, Host)} instead.
    * @throws AmbariException if the repository information can not be obtained
    */
-  public String getRepoInfo(Cluster cluster, Host host) throws AmbariException {
+  @Deprecated
+  public String getRepoInfo(Cluster cluster, ServiceComponent component, Host host) throws AmbariException {
+
+    Function<List<RepositoryInfo>, JsonArray> function = new Function<List<RepositoryInfo>, JsonArray>() {
+      @Override
+      public JsonArray apply(List<RepositoryInfo> input) {
+        return null == input ? null : (JsonArray) gson.toJsonTree(input);
+      }
+    };
+
+    final JsonArray gsonList = getBaseUrls(cluster, component, host, function);
+
+    if (null == gsonList) {
+      return "";
+    }
+
+    BaseUrlUpdater<JsonArray> updater = new BaseUrlUpdater<JsonArray>(gsonList) {
+      @Override
+      public JsonArray apply(final RepositoryVersionEntity rve) {
+
+        JsonArray result = new JsonArray();
+
+        for (JsonElement e : gsonList) {
+          JsonObject obj = e.getAsJsonObject();
+
+          String repoId = obj.has("repoId") ? obj.get("repoId").getAsString() : null;
+          String repoName = obj.has("repoName") ? obj.get("repoName").getAsString() : null;
+          String baseUrl = obj.has("baseUrl") ? obj.get("baseUrl").getAsString() : null;
+          String osType = obj.has("osType") ? obj.get("osType").getAsString() : null;
+
+          if (null == repoId || null == baseUrl || null == osType || null == repoName) {
+            continue;
+          }
+
+          for (OperatingSystemEntity ose : rve.getOperatingSystems()) {
+            if (ose.getOsType().equals(osType) && ose.isAmbariManagedRepos()) {
+              for (RepositoryEntity re : ose.getRepositories()) {
+                if (re.getName().equals(repoName) &&
+                    !re.getBaseUrl().equals(baseUrl)) {
+                  obj.addProperty("baseUrl", re.getBaseUrl());
+                }
+              }
+            result.add(e);
+            }
+          }
+        }
+
+        return result;
+      }
+    };
+
+    return updateBaseUrls(cluster, component, updater).toString();
+  }
+
+  /**
+   * Builds repository information for inclusion in a command.  This replaces escaping json on
+   * a command.
+   *
+   * @param cluster the cluster
+   * @param host    the host
+   * @return  the command repository
+   * @throws AmbariException
+   */
+  @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES)
+  public CommandRepository getCommandRepository(final Cluster cluster, ServiceComponent component, Host host) throws AmbariException {
+
+    Function<List<RepositoryInfo>, List<RepositoryInfo>> function = new Function<List<RepositoryInfo>, List<RepositoryInfo>>() {
+      @Override
+      public List<RepositoryInfo> apply(List<RepositoryInfo> input) {
+        // !!! just return what is given
+        return input;
+      }
+    };
+
+    final List<RepositoryInfo> repoInfos = getBaseUrls(cluster, component, host, function);
+
+    if (null == repoInfos) {
+      return null;
+    }
+
+    final CommandRepository command = new CommandRepository();
+    StackId stackId = component.getDesiredStackId();
+    command.setRepositories(repoInfos);
+    command.setStackName(stackId.getStackName());
+
+    final BaseUrlUpdater<Void> updater = new BaseUrlUpdater<Void>(null) {
+      @Override
+      public Void apply(RepositoryVersionEntity rve) {
+
+        command.setRepositoryVersionId(rve.getId());
+        command.setRepositoryVersion(rve.getVersion());
+        command.setStackName(rve.getStackName());
+        command.setUniqueSuffix(String.format("-repo-%s", rve.getId()));
+
+        for (CommandRepository.Repository commandRepo : command.getRepositories()) {
+          String osType = commandRepo.getOsType();
+          String repoName = commandRepo.getRepoName();
+          String baseUrl = commandRepo.getBaseUrl();
+
+          for (OperatingSystemEntity ose : rve.getOperatingSystems()) {
+            if (ose.getOsType().equals(osType) && ose.isAmbariManagedRepos()) {
+              for (RepositoryEntity re : ose.getRepositories()) {
+                if (re.getName().equals(repoName) &&
+                    !re.getBaseUrl().equals(baseUrl)) {
+                  commandRepo.setBaseUrl(re.getBaseUrl());
+                }
+              }
+            }
+          }
+        }
 
-    return getRepoInfo(cluster, host.getOsType(), host.getOsFamily(), host.getHostName());
+        return null;
+      }
+    };
+
+    updateBaseUrls(cluster, component, updater);
+
+    return command;
   }
 
-  public String getRepoInfo(Cluster cluster, String hostOSType, String hostOSFamily, String hostName) throws AmbariException {
+  /**
+   * Executed by two different representations of repos.  When we are comfortable with the new
+   * implementation, this may be removed and called inline in {@link #getCommandRepository(Cluster, ServiceComponent, Host)}
+   *
+   * @param cluster   the cluster to isolate the stack
+   * @param component the component
+   * @param host      used to resolve the family for the repositories
+   * @param function  function that will transform the supplied repositories for specific use.
+   * @return <T> the type as defined by the supplied {@code function}.
+   * @throws AmbariException
+   */
+  @Experimental(feature = ExperimentalFeature.PATCH_UPGRADES)
+  private <T> T getBaseUrls(Cluster cluster, ServiceComponent component, Host host,
+      Function<List<RepositoryInfo>, T> function) throws AmbariException {
+
+    String hostOsType = host.getOsType();
+    String hostOsFamily = host.getOsFamily();
+    String hostName = host.getHostName();
 
-    StackId stackId = cluster.getDesiredStackVersion();
+    StackId stackId = component.getDesiredStackId();
 
     Map<String, List<RepositoryInfo>> repos = ambariMetaInfo.getRepository(
             stackId.getStackName(), stackId.getStackVersion());
 
-    String family = os_family.find(hostOSType);
+    String family = os_family.find(hostOsType);
     if (null == family) {
-      family = hostOSFamily;
+      family = hostOsFamily;
     }
 
-    JsonElement gsonList = null;
+    final List<RepositoryInfo> repoInfos;
 
     // !!! check for the most specific first
-    if (repos.containsKey(hostOSType)) {
-      gsonList = gson.toJsonTree(repos.get(hostOSType));
+    if (repos.containsKey(hostOsType)) {
+      repoInfos = repos.get(hostOsType);
     } else if (null != family && repos.containsKey(family)) {
-      gsonList = gson.toJsonTree(repos.get(family));
+      repoInfos = repos.get(family);
     } else {
+      repoInfos = null;
       LOG.warn("Could not retrieve repo information for host"
               + ", hostname=" + hostName
               + ", clusterName=" + cluster.getClusterName()
               + ", stackInfo=" + stackId.getStackId());
     }
 
-    if (null != gsonList) {
-      gsonList = updateBaseUrls(cluster, JsonArray.class.cast(gsonList));
-      return gsonList.toString();
-    } else {
-      return "";
-    }
+    // leave it to function implementation to handle null.
+    return function.apply(repoInfos);
   }
 
   /**
    * Checks repo URLs against the current version for the cluster and makes
    * adjustments to the Base URL when the current is different.
-   * @param cluster   the cluster to load the current version
-   * @param jsonArray the array containing stack repo data
+   *
+   * @param <T> the result after appling the repository version, if found.
    */
-  private JsonArray updateBaseUrls(Cluster cluster, JsonArray jsonArray) throws AmbariException {
-    ClusterVersionEntity cve = cluster.getCurrentClusterVersion();
-
-    if (null == cve) {
-      List<ClusterVersionEntity> list = clusterVersionDAO.findByClusterAndState(cluster.getClusterName(),
-          RepositoryVersionState.INIT);
+  @Experimental(feature = ExperimentalFeature.PATCH_UPGRADES)
+  private <T> T updateBaseUrls(Cluster cluster, ServiceComponent component, BaseUrlUpdater<T> function) throws AmbariException {
 
-      if (!list.isEmpty()) {
-        if (list.size() > 1) {
-          throw new AmbariException(String.format("The cluster can only be initialized by one version: %s found",
-              list.size()));
-        } else {
-          cve = list.get(0);
-        }
-      }
-    }
+    RepositoryVersionEntity repositoryEntity = null;
 
-    if (null == cve || null == cve.getRepositoryVersion()) {
-      LOG.info("Cluster {} has no specific Repository Versions.  Using stack-defined values", cluster.getClusterName());
-      return jsonArray;
+    // !!! try to find the component repo first
+    if (null != component) {
+      repositoryEntity = component.getDesiredRepositoryVersion();
+    } else {
+      LOG.info("Service component not passed in, attempt to resolve the repository for cluster {}",
+          cluster.getClusterName());
     }
 
-    RepositoryVersionEntity rve = cve.getRepositoryVersion();
+    if (null == repositoryEntity && null != component) {
+      Service service = cluster.getService(component.getServiceName());
 
-    JsonArray result = new JsonArray();
-
-    for (JsonElement e : jsonArray) {
-      JsonObject obj = e.getAsJsonObject();
-
-      String repoId = obj.has("repoId") ? obj.get("repoId").getAsString() : null;
-      String repoName = obj.has("repoName") ? obj.get("repoName").getAsString() : null;
-      String baseUrl = obj.has("baseUrl") ? obj.get("baseUrl").getAsString() : null;
-      String osType = obj.has("osType") ? obj.get("osType").getAsString() : null;
-
-      if (null == repoId || null == baseUrl || null == osType || null == repoName) {
-        continue;
-      }
+      repositoryEntity = service.getDesiredRepositoryVersion();
+    }
 
-      for (OperatingSystemEntity ose : rve.getOperatingSystems()) {
-        if (ose.getOsType().equals(osType) && ose.isAmbariManagedRepos()) {
-          for (RepositoryEntity re : ose.getRepositories()) {
-            if (re.getName().equals(repoName) &&
-                re.getRepositoryId().equals(repoId) &&
-                !re.getBaseUrl().equals(baseUrl)) {
-              obj.addProperty("baseUrl", re.getBaseUrl());
-            }
-          }
-        result.add(e);
-        }
-      }
+    if (null == repositoryEntity) {
+      LOG.info("Cluster {} has no specific Repository Versions.  Using stack-defined values", cluster.getClusterName());
+      return function.getDefault();
     }
 
-    return result;
+    return function.apply(repositoryEntity);
   }
 
 
@@ -1259,30 +1395,40 @@ public class AmbariCustomCommandExecutionHelper {
    *
    * @param actionExecContext  the context
    * @param cluster            the cluster for the command
-   * @param stackId            the effective stack id to use.
    *
-   * @return a wrapper of the imporant JSON structures to add to a stage
+   * @return a wrapper of the important JSON structures to add to a stage
    */
   public ExecuteCommandJson getCommandJson(ActionExecutionContext actionExecContext,
-      Cluster cluster, StackId stackId) throws AmbariException {
+      Cluster cluster, RepositoryVersionEntity repositoryVersion) throws AmbariException {
 
     Map<String, String> commandParamsStage = StageUtils.getCommandParamsStage(actionExecContext);
     Map<String, String> hostParamsStage = new HashMap<>();
     Map<String, Set<String>> clusterHostInfo;
     String clusterHostInfoJson = "{}";
 
+    StackId stackId = null;
+    if (null != repositoryVersion) {
+      stackId = repositoryVersion.getStackId();
+    }
+
     if (null != cluster) {
-      clusterHostInfo = StageUtils.getClusterHostInfo(
-          cluster);
+      clusterHostInfo = StageUtils.getClusterHostInfo(cluster);
+
       // Important, because this runs during Stack Uprade, it needs to use the effective Stack Id.
-      hostParamsStage = createDefaultHostParams(cluster, stackId);
+      hostParamsStage = createDefaultHostParams(cluster, repositoryVersion);
+
       String componentName = null;
       String serviceName = null;
       if (actionExecContext.getOperationLevel() != null) {
         componentName = actionExecContext.getOperationLevel().getHostComponentName();
         serviceName = actionExecContext.getOperationLevel().getServiceName();
       }
-      if (serviceName != null && componentName != null) {
+
+      if (serviceName != null && componentName != null && null != stackId) {
+        Service service = cluster.getService(serviceName);
+        ServiceComponent component = service.getServiceComponent(componentName);
+        stackId = component.getDesiredStackId();
+
         ComponentInfo componentInfo = ambariMetaInfo.getComponent(
                 stackId.getStackName(), stackId.getStackVersion(),
                 serviceName, componentName);
@@ -1294,17 +1440,22 @@ public class AmbariCustomCommandExecutionHelper {
         String clientsToUpdateConfigs = gson.toJson(clientsToUpdateConfigsList);
         hostParamsStage.put(CLIENTS_TO_UPDATE_CONFIGS, clientsToUpdateConfigs);
       }
+
       clusterHostInfoJson = StageUtils.getGson().toJson(clusterHostInfo);
 
       //Propogate HCFS service type info to command params
-      Map<String, ServiceInfo> serviceInfos = ambariMetaInfo.getServices(stackId.getStackName(), stackId.getStackVersion());
-      for (ServiceInfo serviceInfoInstance : serviceInfos.values()) {
-        if (serviceInfoInstance.getServiceType() != null) {
-          LOG.debug("Adding {} to command parameters for {}", serviceInfoInstance.getServiceType(),
-              serviceInfoInstance.getName());
-
-          commandParamsStage.put("dfs_type", serviceInfoInstance.getServiceType());
-          break;
+      if (null != stackId) {
+        Map<String, ServiceInfo> serviceInfos = ambariMetaInfo.getServices(stackId.getStackName(),
+            stackId.getStackVersion());
+
+        for (ServiceInfo serviceInfoInstance : serviceInfos.values()) {
+          if (serviceInfoInstance.getServiceType() != null) {
+            LOG.debug("Adding {} to command parameters for {}",
+                serviceInfoInstance.getServiceType(), serviceInfoInstance.getName());
+
+            commandParamsStage.put("dfs_type", serviceInfoInstance.getServiceType());
+            break;
+          }
         }
       }
     }
@@ -1316,18 +1467,14 @@ public class AmbariCustomCommandExecutionHelper {
         hostParamsStageJson);
   }
 
-  Map<String, String> createDefaultHostParams(Cluster cluster) throws AmbariException {
-    StackId stackId = cluster.getDesiredStackVersion();
-    return createDefaultHostParams(cluster, stackId);
+  Map<String, String> createDefaultHostParams(Cluster cluster, RepositoryVersionEntity repositoryVersion) throws AmbariException {
+    return createDefaultHostParams(cluster, repositoryVersion.getStackId());
   }
 
-  Map<String, String> createDefaultHostParams(Cluster cluster, StackId stackId) throws AmbariException{
+  Map<String, String> createDefaultHostParams(Cluster cluster, StackId stackId) throws AmbariException {
+
     TreeMap<String, String> hostLevelParams = new TreeMap<>();
     hostLevelParams.put(JDK_LOCATION, managementController.getJdkResourceUrl());
-    hostLevelParams.put(JAVA_HOME, managementController.getJavaHome());
-    hostLevelParams.put(JAVA_VERSION, String.valueOf(configs.getJavaVersion()));
-    hostLevelParams.put(JDK_NAME, managementController.getJDKName());
-    hostLevelParams.put(JCE_NAME, managementController.getJCEName());
     hostLevelParams.put(STACK_NAME, stackId.getStackName());
     hostLevelParams.put(STACK_VERSION, stackId.getStackVersion());
     hostLevelParams.put(DB_NAME, managementController.getServerDB());
@@ -1344,24 +1491,14 @@ public class AmbariCustomCommandExecutionHelper {
     String notManagedHdfsPathList = gson.toJson(notManagedHdfsPathSet);
     hostLevelParams.put(NOT_MANAGED_HDFS_PATH_LIST, notManagedHdfsPathList);
 
-    ClusterVersionEntity clusterVersionEntity = clusterVersionDAO.findByClusterAndStateCurrent(cluster.getClusterName());
-    if (clusterVersionEntity == null) {
-      List<ClusterVersionEntity> clusterVersionEntityList = clusterVersionDAO
-              .findByClusterAndState(cluster.getClusterName(), RepositoryVersionState.INSTALLING);
-      if (!clusterVersionEntityList.isEmpty()) {
-        clusterVersionEntity = clusterVersionEntityList.iterator().next();
-      }
-    }
     for (Map.Entry<String, String> dbConnectorName : configs.getDatabaseConnectorNames().entrySet()) {
       hostLevelParams.put(dbConnectorName.getKey(), dbConnectorName.getValue());
     }
+
     for (Map.Entry<String, String> previousDBConnectorName : configs.getPreviousDatabaseConnectorNames().entrySet()) {
       hostLevelParams.put(previousDBConnectorName.getKey(), previousDBConnectorName.getValue());
     }
 
-    if (clusterVersionEntity != null) {
-      hostLevelParams.put("current_version", clusterVersionEntity.getRepositoryVersion().getVersion());
-    }
 
     return hostLevelParams;
   }
@@ -1377,9 +1514,18 @@ public class AmbariCustomCommandExecutionHelper {
    */
   public boolean isTopologyRefreshRequired(String actionName, String clusterName, String serviceName)
       throws AmbariException {
+
     if (actionName.equals(START_COMMAND_NAME) || actionName.equals(RESTART_COMMAND_NAME)) {
       Cluster cluster = clusters.getCluster(clusterName);
-      StackId stackId = cluster.getDesiredStackVersion();
+      StackId stackId = null;
+      try {
+        Service service = cluster.getService(serviceName);
+        stackId = service.getDesiredStackId();
+      } catch (AmbariException e) {
+        LOG.debug("Could not load service {}, skipping topology check", serviceName);
+        stackId = cluster.getDesiredStackVersion();
+      }
+
 
       AmbariMetaInfo ambariMetaInfo = managementController.getAmbariMetaInfo();
 
@@ -1409,7 +1555,7 @@ public class AmbariCustomCommandExecutionHelper {
 
       return service.getServiceComponent(resourceFilter.getComponentName());
     } catch (Exception e) {
-      LOG.debug(String.format( "Unknown error appears during getting service component: %s", e.getMessage()));
+      LOG.debug("Unknown error appears during getting service component: {}", e.getMessage());
     }
     return null;
   }
@@ -1470,4 +1616,22 @@ public class AmbariCustomCommandExecutionHelper {
     hosts.removeAll(removedHosts);
     return removedHosts;
   }
+
+  /**
+   * Class that is used to update base urls.  There are two implementations of this - when we no
+   * longer are sure the deprecated repo info can be removed, so too can this class.
+   */
+  @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES)
+  abstract static class BaseUrlUpdater<T> implements Function<RepositoryVersionEntity, T> {
+    private T m_default;
+
+    private BaseUrlUpdater(T defaultValue) {
+      m_default = defaultValue;
+    }
+
+    private T getDefault() {
+      return m_default;
+    }
+
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
index 82171f6..3ea9030 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
@@ -111,7 +111,6 @@ import org.apache.ambari.server.metadata.ActionMetadata;
 import org.apache.ambari.server.metadata.RoleCommandOrder;
 import org.apache.ambari.server.metadata.RoleCommandOrderProvider;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.ExtensionDAO;
 import org.apache.ambari.server.orm.dao.ExtensionLinkDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
@@ -120,7 +119,6 @@ import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.dao.WidgetDAO;
 import org.apache.ambari.server.orm.dao.WidgetLayoutDAO;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.ExtensionEntity;
 import org.apache.ambari.server.orm.entities.ExtensionLinkEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
@@ -172,7 +170,6 @@ import org.apache.ambari.server.state.PropertyDependencyInfo;
 import org.apache.ambari.server.state.PropertyInfo;
 import org.apache.ambari.server.state.PropertyInfo.PropertyType;
 import org.apache.ambari.server.state.RepositoryInfo;
-import org.apache.ambari.server.state.RepositoryVersionState;
 import org.apache.ambari.server.state.SecurityType;
 import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.ServiceComponent;
@@ -308,8 +305,6 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
   @Inject
   private CredentialStoreService credentialStoreService;
   @Inject
-  private ClusterVersionDAO clusterVersionDAO;
-  @Inject
   private SettingDAO settingDAO;
 
   private MaintenanceStateHelper maintenanceStateHelper;
@@ -502,17 +497,6 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
     }
     // Create cluster widgets and layouts
     initializeWidgetsAndLayouts(c, null);
-
-    if (null != versionEntity) {
-      ClusterVersionDAO clusterVersionDAO = injector.getInstance(ClusterVersionDAO.class);
-
-      ClusterVersionEntity clusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(request.getClusterName(), stackId,
-          request.getRepositoryVersion());
-
-      if (null == clusterVersion) {
-        c.createClusterVersion(stackId, versionEntity.getVersion(), getAuthName(), RepositoryVersionState.INIT);
-      }
-    }
   }
 
   @Override
@@ -704,8 +688,6 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
         sch.setDesiredState(state);
       }
 
-      sch.setDesiredStackVersion(sc.getDesiredStackVersion());
-
       schMap.put(cluster, sch);
     }
 
@@ -1048,7 +1030,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
   }
 
   private Stage createNewStage(long id, Cluster cluster, long requestId,
-                               String requestContext,
+                               String requestContext, String clusterHostInfo,
                                String commandParamsStage, String hostParamsStage) {
     String logDir = BASE_LOG_DIR + File.pathSeparator + requestId;
     Stage stage =
@@ -2194,7 +2176,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
         changedComponentCount.get(serviceName).keySet()) {
         ServiceComponent sc = cluster.getService(serviceName).
           getServiceComponent(componentName);
-        StackId stackId = sc.getDesiredStackVersion();
+        StackId stackId = sc.getDesiredStackId();
         ComponentInfo compInfo = ambariMetaInfo.getComponent(
           stackId.getStackName(), stackId.getStackVersion(), serviceName,
           componentName);
@@ -2317,7 +2299,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
                                 Map<String, String> commandParamsInp,
                                 ServiceComponentHostEvent event,
                                 boolean skipFailure,
-                                ClusterVersionEntity effectiveClusterVersion,
+                                RepositoryVersionEntity repoVersion,
                                 boolean isUpgradeSuspended,
                                 DatabaseType databaseType,
                                 Map<String, DesiredConfig> clusterDesiredConfigs
@@ -2356,6 +2338,8 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
     Service clusterService = cluster.getService(serviceName);
     execCmd.setCredentialStoreEnabled(String.valueOf(clusterService.isCredentialStoreEnabled()));
 
+    ServiceComponent component = clusterService.getServiceComponent(componentName);
+
     // Get the map of service config type to password properties for the service
     Map<String, Map<String, String>> configCredentials;
     configCredentials = configCredentialsForService.get(clusterService.getName());
@@ -2438,9 +2422,10 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
         commandParams.put(MAX_DURATION_OF_RETRIES, Integer.toString(retryMaxTime));
         commandParams.put(COMMAND_RETRY_ENABLED, Boolean.toString(retryEnabled));
 
-        if (effectiveClusterVersion != null) {
-         commandParams.put(VERSION, effectiveClusterVersion.getRepositoryVersion().getVersion());
+        if (repoVersion != null) {
+         commandParams.put(VERSION, repoVersion.getVersion());
         }
+
         if (script.getTimeout() > 0) {
           scriptCommandTimeout = String.valueOf(script.getTimeout());
         }
@@ -2478,7 +2463,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
       commandParams.put(ExecutionCommand.KeyNames.REFRESH_TOPOLOGY, "True");
     }
 
-    String repoInfo = customCommandExecutionHelper.getRepoInfo(cluster, hostEntity.getOsType(), osFamily , hostname);
+    String repoInfo = customCommandExecutionHelper.getRepoInfo(cluster, component, host);
     if (LOG.isDebugEnabled()) {
       LOG.debug("Sending repo information to agent"
         + ", hostname=" + scHost.getHostName()
@@ -2491,20 +2476,6 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
     hostParams.put(REPO_INFO, repoInfo);
     hostParams.putAll(getRcaParameters());
 
-    // use the effective cluster version here since this command might happen
-    // in the context of an upgrade and we should send the repo ID which matches
-    // the version being send down
-    RepositoryVersionEntity repoVersion = null;
-    if (null != effectiveClusterVersion) {
-      repoVersion = effectiveClusterVersion.getRepositoryVersion();
-    } else {
-      List<ClusterVersionEntity> list = clusterVersionDAO.findByClusterAndState(cluster.getClusterName(),
-          RepositoryVersionState.INIT);
-      if (1 == list.size()) {
-        repoVersion = list.get(0).getRepositoryVersion();
-      }
-    }
-
     if (null != repoVersion) {
       try {
         VersionDefinitionXml xml = repoVersion.getRepositoryXml();
@@ -2593,8 +2564,8 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
     execCmd.setRoleParams(roleParams);
     execCmd.setCommandParams(commandParams);
 
-    execCmd.setAvailableServicesFromServiceInfoMap(ambariMetaInfo.getServices(stackId.getStackName(), stackId.getStackVersion()));
-
+    execCmd.setRepositoryFile(customCommandExecutionHelper.getCommandRepository(cluster, component, host));
+    hostParams.put(KeyNames.CURRENT_VERSION, repoVersion.getVersion());
 
     if ((execCmd != null) && (execCmd.getConfigurationTags().containsKey("cluster-env"))) {
       LOG.debug("AmbariManagementControllerImpl.createHostAction: created ExecutionCommand for host {}, role {}, roleCommand {}, and command ID {}, with cluster-env tags {}",
@@ -2725,9 +2696,6 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
       return requestStages;
     }
 
-    // caching effective cluster version
-    ClusterVersionEntity effectiveClusterVersion = cluster.getEffectiveClusterVersion();
-
     // caching upgrade suspended
     boolean isUpgradeSuspended = cluster.isUpgradeSuspended();
 
@@ -2753,11 +2721,10 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
       Map<String, Set<String>> clusterHostInfo = StageUtils.getClusterHostInfo(cluster);
 
       String clusterHostInfoJson = StageUtils.getGson().toJson(clusterHostInfo);
-      String hostParamsJson = StageUtils.getGson().toJson(
-          customCommandExecutionHelper.createDefaultHostParams(cluster));
 
       Stage stage = createNewStage(requestStages.getLastStageId(), cluster,
-          requestStages.getId(), requestProperties.get(REQUEST_CONTEXT_PROPERTY),"{}", hostParamsJson);
+          requestStages.getId(), requestProperties.get(REQUEST_CONTEXT_PROPERTY),
+          clusterHostInfoJson, "{}", null);
       boolean skipFailure = false;
       if (requestProperties.containsKey(Setting.SETTING_NAME_SKIP_FAILURE) && requestProperties.get(Setting.SETTING_NAME_SKIP_FAILURE).equalsIgnoreCase("true")) {
         skipFailure = true;
@@ -2867,6 +2834,16 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
           for (ServiceComponentHost scHost :
               changedScHosts.get(compName).get(newState)) {
 
+            Service service = cluster.getService(scHost.getServiceName());
+            ServiceComponent serviceComponent = service.getServiceComponent(compName);
+
+            if (StringUtils.isBlank(stage.getHostParamsStage())) {
+              RepositoryVersionEntity repositoryVersion = serviceComponent.getDesiredRepositoryVersion();
+              stage.setHostParamsStage(StageUtils.getGson().toJson(
+                  customCommandExecutionHelper.createDefaultHostParams(cluster, repositoryVersion)));
+            }
+
+
             // Do not create role command for hosts that are not responding
             if (scHost.getHostState().equals(HostState.HEARTBEAT_LOST)) {
               LOG.info("Command is not created for servicecomponenthost "
@@ -2905,7 +2882,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
                     event = new ServiceComponentHostInstallEvent(
                         scHost.getServiceComponentName(), scHost.getHostName(),
                         nowTimestamp,
-                        scHost.getDesiredStackVersion().getStackId());
+                        scHost.getDesiredStackId().getStackId());
                   }
                 } else if (oldSchState == State.STARTED
                       // TODO: oldSchState == State.INSTALLED is always false, looks like a bug
@@ -2919,7 +2896,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
                   roleCommand = RoleCommand.UPGRADE;
                   event = new ServiceComponentHostUpgradeEvent(
                       scHost.getServiceComponentName(), scHost.getHostName(),
-                      nowTimestamp, scHost.getDesiredStackVersion().getStackId());
+                      nowTimestamp, scHost.getDesiredStackId().getStackId());
                 } else {
                   throw new AmbariException("Invalid transition for"
                       + " servicecomponenthost"
@@ -2933,7 +2910,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
                 }
                 break;
               case STARTED:
-                StackId stackId = scHost.getDesiredStackVersion();
+                StackId stackId = scHost.getDesiredStackId();
                 ComponentInfo compInfo = ambariMetaInfo.getComponent(
                     stackId.getStackName(), stackId.getStackVersion(), scHost.getServiceName(),
                     scHost.getServiceComponentName());
@@ -3074,8 +3051,11 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
                 LOG.error("Error transitioning ServiceComponentHost state to INSTALLED", e);
               }
             } else {
+              // !!! can never be null
+              RepositoryVersionEntity repoVersion = serviceComponent.getDesiredRepositoryVersion();
+
               createHostAction(cluster, stage, scHost, configurations, configurationAttributes, configTags,
-                roleCommand, requestParameters, event, skipFailure, effectiveClusterVersion, isUpgradeSuspended,
+                roleCommand, requestParameters, event, skipFailure, repoVersion, isUpgradeSuspended,
                 databaseType, clusterDesiredConfigs);
             }
 
@@ -3200,23 +3180,30 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
                                               RoleCommand roleCommand) throws AmbariException {
     Map<String, Set<String>> clusterHostInfo = StageUtils.getClusterHostInfo(cluster);
     String clusterHostInfoJson = StageUtils.getGson().toJson(clusterHostInfo);
-    Map<String, String> hostParamsCmd = customCommandExecutionHelper.createDefaultHostParams(cluster);
-    Stage stage = createNewStage(0, cluster,1, "","{}", "");
 
+    Map<String, String> hostParamsCmd = customCommandExecutionHelper.createDefaultHostParams(
+        cluster, scHost.getServiceComponent().getDesiredRepositoryVersion());
+
+    Stage stage = createNewStage(0, cluster, 1, "", clusterHostInfoJson, "{}", "");
 
     Map<String, Map<String, String>> configTags = configHelper.getEffectiveDesiredTags(cluster, scHost.getHostName());
     Map<String, Map<String, String>> configurations = configHelper.getEffectiveConfigProperties(cluster, configTags);
 
-    Map<String, Map<String, Map<String, String>>>
-        configurationAttributes =
-        new TreeMap<>();
+    Map<String, Map<String, Map<String, String>>> configurationAttributes = new TreeMap<>();
+
+    RepositoryVersionEntity repoVersion = null;
+    if (null != scHost.getServiceComponent().getDesiredRepositoryVersion()) {
+      repoVersion = scHost.getServiceComponent().getDesiredRepositoryVersion();
+    } else {
+      Service service = cluster.getService(scHost.getServiceName());
+      repoVersion = service.getDesiredRepositoryVersion();
+    }
 
-    ClusterVersionEntity effectiveClusterVersion = cluster.getEffectiveClusterVersion();
     boolean isUpgradeSuspended = cluster.isUpgradeSuspended();
     DatabaseType databaseType = configs.getDatabaseType();
     Map<String, DesiredConfig> clusterDesiredConfigs = cluster.getDesiredConfigs();
     createHostAction(cluster, stage, scHost, configurations, configurationAttributes, configTags,
-                     roleCommand, null, null, false, effectiveClusterVersion, isUpgradeSuspended, databaseType,
+                     roleCommand, null, null, false, repoVersion, isUpgradeSuspended, databaseType,
                      clusterDesiredConfigs);
     ExecutionCommand ec = stage.getExecutionCommands().get(scHost.getHostName()).get(0).getExecutionCommand();
 
@@ -3995,7 +3982,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
      * that has client component is in maintenance state
      */
 
-    StackId stackId = service.getDesiredStackVersion();
+    StackId stackId = service.getDesiredStackId();
     ComponentInfo compInfo =
         ambariMetaInfo.getService(stackId.getStackName(),
             stackId.getStackVersion(), service.getName()).getClientComponent();
@@ -4136,11 +4123,58 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
         actionManager,
         actionRequest);
 
-    StackId stackId = null;
-    if (null != cluster) {
-      stackId = cluster.getDesiredStackVersion();
+//    StackId stackId = null;
+//    if (null != cluster) {
+//      stackId = cluster.getDesiredStackVersion();
+//    }
+    RepositoryVersionEntity desiredRepositoryVersion = null;
+
+    RequestOperationLevel operationLevel = actionExecContext.getOperationLevel();
+    if (null != operationLevel && StringUtils.isNotBlank(operationLevel.getServiceName())) {
+      Service service = cluster.getService(operationLevel.getServiceName());
+      if (null != service) {
+        desiredRepositoryVersion = service.getDesiredRepositoryVersion();
+      }
+    }
+
+    if (null == desiredRepositoryVersion && CollectionUtils.isNotEmpty(actionExecContext.getResourceFilters())) {
+      Set<RepositoryVersionEntity> versions = new HashSet<>();
+
+      for (RequestResourceFilter filter : actionExecContext.getResourceFilters()) {
+        RepositoryVersionEntity repoVersion = null;
+
+        if (StringUtils.isNotBlank(filter.getServiceName())) {
+          Service service = cluster.getService(filter.getServiceName());
+
+          if (StringUtils.isNotBlank(filter.getComponentName())) {
+            ServiceComponent serviceComponent = service.getServiceComponent(filter.getComponentName());
+
+            repoVersion = serviceComponent.getDesiredRepositoryVersion();
+          }
+
+          if (null == repoVersion) {
+            repoVersion = service.getDesiredRepositoryVersion();
+          }
+        }
+
+        if (null != repoVersion) {
+          versions.add(repoVersion);
+        }
+      }
+
+      if (1 == versions.size()) {
+        desiredRepositoryVersion = versions.iterator().next();
+      } else if (versions.size() > 1) {
+        Set<String> errors = new HashSet<>();
+        for (RepositoryVersionEntity version : versions) {
+          errors.add(String.format("%s/%s", version.getStackId(), version.getVersion()));
+        }
+        throw new IllegalArgumentException(String.format("More than one repository is resolved with this Action: %s",
+            StringUtils.join(errors, ';')));
+      }
     }
-    ExecuteCommandJson jsons = customCommandExecutionHelper.getCommandJson(actionExecContext, cluster, stackId);
+
+    ExecuteCommandJson jsons = customCommandExecutionHelper.getCommandJson(actionExecContext, cluster, desiredRepositoryVersion);
     String commandParamsForStage = jsons.getCommandParamsForStage();
 
     Map<String, String> commandParamsStage = gson.fromJson(commandParamsForStage, new TypeToken<Map<String, String>>()
@@ -4169,7 +4203,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
     commandParamsForStage = gson.toJson(commandParamsStage);
 
     Stage stage = createNewStage(requestStageContainer.getLastStageId(), cluster, requestId, requestContext,
-        commandParamsForStage, jsons.getHostParamsForStage());
+        jsons.getClusterHostInfo(), commandParamsForStage, jsons.getHostParamsForStage());
 
     if (actionRequest.isCommand()) {
       customCommandExecutionHelper.addExecutionCommandsToStage(actionExecContext, stage,
@@ -4503,7 +4537,6 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
         if (rr.getRepositoryVersionId() != null) {
           throw new AmbariException("Can't directly update repositories in repository_version, update the repository_version instead");
         }
-        ambariMetaInfo.updateRepoBaseURL(rr.getStackName(), rr.getStackVersion(), rr.getOsType(), rr.getRepoId(), rr.getBaseUrl());
       }
     }
   }
@@ -4971,10 +5004,6 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
       try {
         Set<RootServiceComponentResponse> rootServiceComponents = getRootServiceComponents(request);
 
-        for (RootServiceComponentResponse serviceComponentResponse : rootServiceComponents) {
-          serviceComponentResponse.setServiceName(serviceName);
-        }
-
         response.addAll(rootServiceComponents);
       } catch (AmbariException e) {
         if (requests.size() == 1) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/HostRequest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/HostRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/HostRequest.java
index a616839..e19e121 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/HostRequest.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/HostRequest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,26 +19,21 @@
 package org.apache.ambari.server.controller;
 
 import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
 
 public class HostRequest {
 
   private String hostname;
   private String publicHostname;
   private String clusterName; // CREATE/UPDATE
-  private Map<String, String> hostAttributes; // CREATE/UPDATE
   private String rackInfo;
   private List<ConfigurationRequest> desiredConfigs; // UPDATE
   private String maintenanceState; // UPDATE
   private String blueprint;
-  private String hostgroup;
-  private String hostToClone;
+  private String hostGroup;
 
-  public HostRequest(String hostname, String clusterName, Map<String, String> hostAttributes) {
+  public HostRequest(String hostname, String clusterName) {
     this.hostname = hostname;
     this.clusterName = clusterName;
-    this.hostAttributes = hostAttributes;
   }
 
   public String getHostname() {
@@ -57,42 +52,34 @@ public class HostRequest {
     this.clusterName = clusterName;
   }
 
-  public Map<String, String> getHostAttributes() {
-    return hostAttributes;
-  }
-
-  public void setHostAttributes(Map<String, String> hostAttributes) {
-    this.hostAttributes = hostAttributes;
-  }
-  
   public String getRackInfo() {
     return rackInfo;
   }
-  
+
   public void setRackInfo(String info) {
     rackInfo = info;
   }
-  
+
   public String getPublicHostName() {
     return publicHostname;
   }
-  
+
   public void setPublicHostName(String name) {
     publicHostname = name;
   }
-  
+
   public void setDesiredConfigs(List<ConfigurationRequest> request) {
     desiredConfigs = request;
   }
-  
+
   public List<ConfigurationRequest> getDesiredConfigs() {
     return desiredConfigs;
   }
-  
+
   public void setMaintenanceState(String state) {
     maintenanceState = state;
   }
-  
+
   public String getMaintenanceState() {
     return maintenanceState;
   }
@@ -105,40 +92,16 @@ public class HostRequest {
     return blueprint;
   }
 
-  public void setHostGroupName(String hostgroupName) {
-    hostgroup = hostgroupName;
+  public void setHostGroupName(String hostGroupName) {
+    hostGroup = hostGroupName;
   }
 
   public String getHostGroupName() {
-    return hostgroup;
-  }
-
-  public void setHostToClone(String hostname) {
-    hostToClone = hostname;
-  }
-
-  public String getHostToClone() {
-    return hostToClone;
+    return hostGroup;
   }
 
+  @Override
   public String toString() {
-    StringBuilder sb = new StringBuilder();
-    sb.append("{ hostname=").append(hostname).append(", clusterName=").append(clusterName);
-    if (hostAttributes != null) {
-      sb.append(", hostAttributes=[");
-      int i = 0;
-      for (Entry<String, String> attr : hostAttributes.entrySet()) {
-        if (i != 0) {
-          sb.append(",");
-        }
-        ++i;
-        sb.append(attr.getKey());
-        sb.append("=");
-        sb.append(attr.getValue());
-      }
-      sb.append(']');
-    }
-    sb.append(" }");
-    return sb.toString();
+    return "{ hostname=" + hostname + ", clusterName=" + clusterName + " }";
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/controller/HostResponse.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/HostResponse.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/HostResponse.java
index 8032801..3218bbd 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/HostResponse.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/HostResponse.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -22,14 +22,15 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import org.apache.ambari.server.agent.AgentEnv;
 import org.apache.ambari.server.agent.DiskInfo;
 import org.apache.ambari.server.agent.RecoveryReport;
 import org.apache.ambari.server.state.AgentVersion;
-import org.apache.ambari.server.state.DesiredConfig;
 import org.apache.ambari.server.state.HostConfig;
 import org.apache.ambari.server.state.HostHealthStatus;
+import org.apache.ambari.server.state.HostState;
 import org.apache.ambari.server.state.MaintenanceState;
 
 public class HostResponse {
@@ -44,42 +45,28 @@ public class HostResponse {
   private String ipv4;
 
   /**
-   * Host IP if ipv6 interface available
-   */
-  private String ipv6;
-
-  /**
    * Count of cores on Host
    */
-  private int cpuCount;
-  
+  private long cpuCount;
+
   /**
    * Count of physical cores on Host
    */
-  private int phCpuCount;
-  
-  
+  private long phCpuCount;
+
   /**
    * Os Architecture
    */
   private String osArch;
 
+  private String osFamily;
+
   /**
    * OS Type
    */
   private String osType;
 
   /**
-   * OS Information
-   */
-  private String osInfo;
-
-  /**
-   * Amount of available memory for the Host
-   */
-  private long availableMemBytes;
-
-  /**
    * Amount of physical memory for the Host
    */
   private long totalMemBytes;
@@ -93,7 +80,7 @@ public class HostResponse {
    * Last heartbeat timestamp from the Host
    */
   private long lastHeartbeatTime;
-  
+
   /**
    * Last environment information
    */
@@ -133,18 +120,16 @@ public class HostResponse {
    * Summary of node recovery
    */
   private String recoverySummary = "DISABLED";
-  
+
   /**
    * Public name.
    */
-  private String publicHostname = null;
+  private String publicHostname;
 
   /**
    * Host State
    */
-  private String hostState;
-
-  private Map<String, DesiredConfig> desiredConfigs;
+  private HostState hostState;
 
   /**
    * Configs derived from Config groups
@@ -156,25 +141,22 @@ public class HostResponse {
    */
   private String status;
 
-  private MaintenanceState maintenanceState = null;
+  private MaintenanceState maintenanceState;
 
   public HostResponse(String hostname, String clusterName,
-                      String ipv4, String ipv6, int cpuCount, int phCpuCount, String osArch, String osType,
-                      String osInfo, long availableMemBytes, long totalMemBytes,
+                      String ipv4, int cpuCount, int phCpuCount, String osArch, String osType,
+                      long totalMemBytes,
                       List<DiskInfo> disksInfo, long lastHeartbeatTime,
                       long lastRegistrationTime, String rackInfo,
                       Map<String, String> hostAttributes, AgentVersion agentVersion,
-                      HostHealthStatus healthStatus, String hostState, String status) {
+                      HostHealthStatus healthStatus, HostState hostState, String status) {
     this.hostname = hostname;
     this.clusterName = clusterName;
     this.ipv4 = ipv4;
-    this.ipv6 = ipv6;
     this.cpuCount = cpuCount;
     this.phCpuCount = phCpuCount;
     this.osArch = osArch;
     this.osType = osType;
-    this.osInfo = osInfo;
-    this.availableMemBytes = availableMemBytes;
     this.totalMemBytes = totalMemBytes;
     this.disksInfo = disksInfo;
     this.lastHeartbeatTime = lastHeartbeatTime;
@@ -183,37 +165,28 @@ public class HostResponse {
     this.hostAttributes = hostAttributes;
     this.agentVersion = agentVersion;
     this.healthStatus = healthStatus;
-    this.setHostState(hostState);
+    this.hostState = hostState;
     this.status = status;
   }
 
   //todo: why are we passing in empty strings for host/cluster name instead of null?
   public HostResponse(String hostname) {
-    this(hostname, "", "", "",
-        0, 0, "", "",
-        "", 0, 0, new ArrayList<DiskInfo>(),
+    this(hostname, "", "",
+      0, 0, "", "",
+      0, new ArrayList<DiskInfo>(),
         0, 0, "",
         new HashMap<String, String>(),
         null, null, null, null);
   }
 
-  /**
-   * @return the hostname
-   */
   public String getHostname() {
     return hostname;
   }
 
-  /**
-   * @param hostname the hostname to set
-   */
   public void setHostname(String hostname) {
     this.hostname = hostname;
   }
 
-  /**
-   * @return the clusterNames
-   */
   public String getClusterName() {
     return clusterName;
   }
@@ -225,299 +198,156 @@ public class HostResponse {
     this.clusterName = clusterName;
   }
 
-  /**
-   * @return the ipv4
-   */
   public String getIpv4() {
     return ipv4;
   }
 
-  /**
-   * @param ipv4 the ipv4 to set
-   */
   public void setIpv4(String ipv4) {
     this.ipv4 = ipv4;
   }
 
-  /**
-   * @return the ipv6
-   */
-  public String getIpv6() {
-    return ipv6;
-  }
-
-  /**
-   * @param ipv6 the ipv6 to set
-   */
-  public void setIpv6(String ipv6) {
-    this.ipv6 = ipv6;
-  }
-
-  /**
-   * @return the cpuCount
-   */
-  public int getCpuCount() {
+  public long getCpuCount() {
     return cpuCount;
   }
 
-  /**
-   * @param cpuCount the cpuCount to set
-   */
-  public void setCpuCount(int cpuCount) {
+  public void setCpuCount(long cpuCount) {
     this.cpuCount = cpuCount;
   }
 
-  /**
-  * @return the phCpuCount
-  */
-  public int getPhCpuCount() {
+  public long getPhCpuCount() {
     return phCpuCount;
   }
 
-  /**
-  * @param phCpuCount the physical cpu count to set
-  */
-  public void setPhCpuCount(int phCpuCount) {
+  public void setPhCpuCount(long phCpuCount) {
     this.phCpuCount = phCpuCount;
   }
 
-  
-  
-  /**
-   * @return the osArch
-   */
   public String getOsArch() {
     return osArch;
   }
 
-  /**
-   * @param osArch the osArch to set
-   */
   public void setOsArch(String osArch) {
     this.osArch = osArch;
   }
 
-  /**
-   * @return the osType
-   */
-  public String getOsType() {
-    return osType;
-  }
-
-  /**
-   * @param osType the osType to set
-   */
-  public void setOsType(String osType) {
-    this.osType = osType;
+  public String getOsFamily() {
+    return osFamily;
   }
 
-  /**
-   * @return the osInfo
-   */
-  public String getOsInfo() {
-    return osInfo;
+  public void setOsFamily(String osFamily) {
+    this.osFamily = osFamily;
   }
 
-  /**
-   * @param osInfo the osInfo to set
-   */
-  public void setOsInfo(String osInfo) {
-    this.osInfo = osInfo;
-  }
-
-  /**
-   * @return the availableMemBytes
-   */
-  public long getAvailableMemBytes() {
-    return availableMemBytes;
+  public String getOsType() {
+    return osType;
   }
 
-  /**
-   * @param availableMemBytes the availableMemBytes to set
-   */
-  public void setAvailableMemBytes(long availableMemBytes) {
-    this.availableMemBytes = availableMemBytes;
+  public void setOsType(String osType) {
+    this.osType = osType;
   }
 
-  /**
-   * @return the totalMemBytes
-   */
   public long getTotalMemBytes() {
     return totalMemBytes;
   }
 
-  /**
-   * @param totalMemBytes the totalMemBytes to set
-   */
   public void setTotalMemBytes(long totalMemBytes) {
     this.totalMemBytes = totalMemBytes;
   }
 
-  /**
-   * @return the disksInfo
-   */
   public List<DiskInfo> getDisksInfo() {
     return disksInfo;
   }
 
-  /**
-   * @param disksInfo the disksInfo to set
-   */
   public void setDisksInfo(List<DiskInfo> disksInfo) {
     this.disksInfo = disksInfo;
   }
 
-  /**
-   * @return the lastHeartbeatTime
-   */
   public long getLastHeartbeatTime() {
     return lastHeartbeatTime;
   }
 
-  /**
-   * @param lastHeartbeatTime the lastHeartbeatTime to set
-   */
   public void setLastHeartbeatTime(long lastHeartbeatTime) {
     this.lastHeartbeatTime = lastHeartbeatTime;
   }
 
-  /**
-   * @return the lastRegistrationTime
-   */
   public long getLastRegistrationTime() {
     return lastRegistrationTime;
   }
 
-  /**
-   * @param lastRegistrationTime the lastRegistrationTime to set
-   */
   public void setLastRegistrationTime(long lastRegistrationTime) {
     this.lastRegistrationTime = lastRegistrationTime;
   }
 
-  /**
-   * @return the rackInfo
-   */
   public String getRackInfo() {
     return rackInfo;
   }
 
-  /**
-   * @param rackInfo the rackInfo to set
-   */
   public void setRackInfo(String rackInfo) {
     this.rackInfo = rackInfo;
   }
 
-  /**
-   * @return the hostAttributes
-   */
   public Map<String, String> getHostAttributes() {
     return hostAttributes;
   }
 
-  /**
-   * @param hostAttributes the hostAttributes to set
-   */
   public void setHostAttributes(Map<String, String> hostAttributes) {
     this.hostAttributes = hostAttributes;
   }
 
-  /**
-   * @return the agentVersion
-   */
   public AgentVersion getAgentVersion() {
     return agentVersion;
   }
 
-  /**
-   * @param agentVersion the agentVersion to set
-   */
   public void setAgentVersion(AgentVersion agentVersion) {
     this.agentVersion = agentVersion;
   }
 
-  /**
-   * @return the healthStatus
-   */
-  public HostHealthStatus getHealthStatus() {
-    return healthStatus;
+  public String getHealthReport() {
+    return healthStatus.getHealthReport();
   }
 
-  /**
-   * @param healthStatus the healthStatus to set
-   */
   public void setHealthStatus(HostHealthStatus healthStatus) {
     this.healthStatus = healthStatus;
   }
 
-
-
   @Override
   public boolean equals(Object o) {
     if (this == o) return true;
     if (o == null || getClass() != o.getClass()) return false;
 
-    HostResponse that = (HostResponse) o;
-
-    if (hostname != null ?
-        !hostname.equals(that.hostname) : that.hostname != null) {
-      return false;
-    }
+    HostResponse other = (HostResponse) o;
 
-    return true;
+    return Objects.equals(hostname, other.hostname);
   }
 
   @Override
   public int hashCode() {
-    int result = hostname != null ? hostname.hashCode() : 0;
-    return result;
+    return Objects.hashCode(hostname);
   }
 
   public String getPublicHostName() {
     return publicHostname;
   }
-  
+
   public void setPublicHostName(String name) {
     publicHostname = name;
   }
 
-  /**
-   * @return the hostState
-   */
-  public String getHostState() {
+  public HostState getHostState() {
     return hostState;
   }
 
-  /**
-   * @param hostState the hostState to set
-   */
-  public void setHostState(String hostState) {
+  public void setHostState(HostState hostState) {
     this.hostState = hostState;
   }
 
-  
   public AgentEnv getLastAgentEnv() {
     return lastAgentEnv;
   }
-  
-  /**
-   * @param agentEnv
-   */
+
   public void setLastAgentEnv(AgentEnv agentEnv) {
     lastAgentEnv = agentEnv;
   }
-  
-  /**
-   * @param desired
-   */
-  public void setDesiredConfigs(Map<String, DesiredConfig> desired) {
-    desiredConfigs = desired;
-  }
-  
-  public Map<String, DesiredConfig> getDesiredConfigs() {
-    return desiredConfigs;
-  }
 
   public Map<String, HostConfig> getDesiredHostConfigs() {
     return desiredHostConfigs;
@@ -535,23 +365,16 @@ public class HostResponse {
     this.status = status;
   }
 
-  /**
-   * @param state the maintenance state
-   */
   public void setMaintenanceState(MaintenanceState state) {
     maintenanceState = state;
   }
-  
-  /**
-   * @return the maintenance state
-   */
+
   public MaintenanceState getMaintenanceState() {
     return maintenanceState;
   }
 
   /**
    * Get the recovery summary for the host
-   * @return
    */
   public String getRecoverySummary() {
     return recoverySummary;
@@ -559,7 +382,6 @@ public class HostResponse {
 
   /**
    * Set the recovery summary for the host
-   * @return
    */
   public void setRecoverySummary(String recoverySummary) {
     this.recoverySummary = recoverySummary;
@@ -567,7 +389,6 @@ public class HostResponse {
 
   /**
    * Get the detailed recovery report
-   * @return
    */
   public RecoveryReport getRecoveryReport() {
     return recoveryReport;
@@ -575,9 +396,9 @@ public class HostResponse {
 
   /**
    * Set the detailed recovery report
-   * @param recoveryReport
    */
   public void setRecoveryReport(RecoveryReport recoveryReport) {
     this.recoveryReport = recoveryReport;
   }
+
 }


[11/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostComponentStateDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostComponentStateDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostComponentStateDAOTest.java
index 5ea0b1e..7896c34 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostComponentStateDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostComponentStateDAOTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -60,4 +60,4 @@ public class HostComponentStateDAOTest {
 
     verify(entityManagerProvider, entityManager, hostDAO, hostEntity, hostComponentStateEntity);
   }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostVersionDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostVersionDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostVersionDAOTest.java
index 6219a69..bb077d6 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostVersionDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostVersionDAOTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -30,7 +30,6 @@ import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
@@ -58,7 +57,6 @@ public class HostVersionDAOTest {
   private ResourceTypeDAO resourceTypeDAO;
   private ClusterDAO clusterDAO;
   private StackDAO stackDAO;
-  private ClusterVersionDAO clusterVersionDAO;
   private HostDAO hostDAO;
   private HostVersionDAO hostVersionDAO;
   private OrmTestHelper helper;
@@ -79,7 +77,6 @@ public class HostVersionDAOTest {
     resourceTypeDAO = injector.getInstance(ResourceTypeDAO.class);
     clusterDAO = injector.getInstance(ClusterDAO.class);
     stackDAO = injector.getInstance(StackDAO.class);
-    clusterVersionDAO = injector.getInstance(ClusterVersionDAO.class);
     hostDAO = injector.getInstance(HostDAO.class);
     hostVersionDAO = injector.getInstance(HostVersionDAO.class);
     helper = injector.getInstance(OrmTestHelper.class);
@@ -116,17 +113,6 @@ public class HostVersionDAOTest {
 
     RepositoryVersionEntity repoVersionEntity = helper.getOrCreateRepositoryVersion(HDP_22_STACK, repoVersion_2200);
 
-    // Create the Cluster Version and link it to the cluster
-    ClusterVersionEntity clusterVersionEntity = new ClusterVersionEntity(
-        clusterEntity, repoVersionEntity, RepositoryVersionState.CURRENT,
-        System.currentTimeMillis(), System.currentTimeMillis(), "admin");
-    List<ClusterVersionEntity> clusterVersionEntities = new ArrayList<ClusterVersionEntity>();
-    clusterVersionEntities.add(clusterVersionEntity);
-    clusterEntity.setClusterVersionEntities(clusterVersionEntities);
-
-    clusterVersionDAO.create(clusterVersionEntity);
-    clusterDAO.merge(clusterEntity);
-
     // Create the hosts
     HostEntity host1 = new HostEntity();
     HostEntity host2 = new HostEntity();
@@ -139,7 +125,7 @@ public class HostVersionDAOTest {
     host2.setIpv4("192.168.0.2");
     host3.setIpv4("192.168.0.3");
 
-    List<HostEntity> hostEntities = new ArrayList<HostEntity>();
+    List<HostEntity> hostEntities = new ArrayList<>();
     hostEntities.add(host1);
     hostEntities.add(host2);
     hostEntities.add(host3);
@@ -157,9 +143,9 @@ public class HostVersionDAOTest {
     clusterDAO.merge(clusterEntity);
 
     // Create the Host Versions
-    HostVersionEntity hostVersionEntity1 = new HostVersionEntity(host1, clusterVersionEntity.getRepositoryVersion(), RepositoryVersionState.CURRENT);
-    HostVersionEntity hostVersionEntity2 = new HostVersionEntity(host2, clusterVersionEntity.getRepositoryVersion(), RepositoryVersionState.INSTALLED);
-    HostVersionEntity hostVersionEntity3 = new HostVersionEntity(host3, clusterVersionEntity.getRepositoryVersion(), RepositoryVersionState.INSTALLED);
+    HostVersionEntity hostVersionEntity1 = new HostVersionEntity(host1, repoVersionEntity, RepositoryVersionState.CURRENT);
+    HostVersionEntity hostVersionEntity2 = new HostVersionEntity(host2, repoVersionEntity, RepositoryVersionState.INSTALLED);
+    HostVersionEntity hostVersionEntity3 = new HostVersionEntity(host3, repoVersionEntity, RepositoryVersionState.INSTALLED);
 
     hostVersionDAO.create(hostVersionEntity1);
     hostVersionDAO.create(hostVersionEntity2);
@@ -172,20 +158,8 @@ public class HostVersionDAOTest {
   private void addMoreVersions() {
     ClusterEntity clusterEntity = clusterDAO.findByName("test_cluster1");
 
-    // Create another Cluster Version and mark the old one as INSTALLED
-    if (clusterEntity.getClusterVersionEntities() != null && clusterEntity.getClusterVersionEntities().size() > 0) {
-      ClusterVersionEntity installedClusterVersion = clusterVersionDAO.findByClusterAndStateCurrent(clusterEntity.getClusterName());
-      installedClusterVersion.setState(RepositoryVersionState.INSTALLED);
-      clusterVersionDAO.merge(installedClusterVersion);
-    } else {
-      Assert.fail("Cluster is expected to have at least one cluster version");
-    }
-
     RepositoryVersionEntity repositoryVersionEnt_2_2_0_1 = helper.getOrCreateRepositoryVersion(HDP_22_STACK, repoVersion_2201);
 
-    ClusterVersionEntity newClusterVersionEntity = new ClusterVersionEntity(clusterEntity, repositoryVersionEnt_2_2_0_1, RepositoryVersionState.CURRENT, System.currentTimeMillis(), System.currentTimeMillis(), "admin");
-    clusterEntity.addClusterVersionEntity(newClusterVersionEntity);
-    clusterVersionDAO.create(newClusterVersionEntity);
 
     HostEntity[] hostEntities = clusterEntity.getHostEntities().toArray(new HostEntity[clusterEntity.getHostEntities().size()]);
     // Must sort by host name in ascending order to ensure that state is accurately set later on.
@@ -193,7 +167,7 @@ public class HostVersionDAOTest {
 
     // For each of the hosts, add a host version
     for (HostEntity host : hostEntities) {
-      HostVersionEntity hostVersionEntity = new HostVersionEntity(host, helper.getOrCreateRepositoryVersion(HDP_22_STACK, repoVersion_2201), RepositoryVersionState.INSTALLED);
+      HostVersionEntity hostVersionEntity = new HostVersionEntity(host, repositoryVersionEnt_2_2_0_1, RepositoryVersionState.INSTALLED);
       hostVersionDAO.create(hostVersionEntity);
     }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
index 9760214..406349a 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -29,7 +29,6 @@ import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
-import org.apache.ambari.server.orm.entities.ClusterConfigMappingEntity;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupConfigMappingEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupEntity;
@@ -41,7 +40,6 @@ import org.apache.ambari.server.security.authorization.ResourceType;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.cluster.ClusterImpl;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -271,23 +269,43 @@ public class ServiceConfigDAOTest {
   @Test
   public void testGetLastServiceConfigsForService() throws Exception {
     String serviceName = "HDFS";
+    Clusters clusters = injector.getInstance(Clusters.class);
+    clusters.addCluster("c1", HDP_01);
+    ConfigGroupEntity configGroupEntity1 = new ConfigGroupEntity();
+    ClusterEntity clusterEntity = clusterDAO.findByName("c1");
+    configGroupEntity1.setClusterEntity(clusterEntity);
+    configGroupEntity1.setClusterId(clusterEntity.getClusterId());
+    configGroupEntity1.setGroupName("group1");
+    configGroupEntity1.setDescription("group1_desc");
+    configGroupEntity1.setTag("HDFS");
+    configGroupEntity1.setServiceName("HDFS");
+    configGroupDAO.create(configGroupEntity1);
+    ConfigGroupEntity group1 = configGroupDAO.findByName("group1");
+    ConfigGroupEntity configGroupEntity2 = new ConfigGroupEntity();
+    configGroupEntity2.setClusterEntity(clusterEntity);
+    configGroupEntity2.setClusterId(clusterEntity.getClusterId());
+    configGroupEntity2.setGroupName("group2");
+    configGroupEntity2.setDescription("group2_desc");
+    configGroupEntity2.setTag("HDFS");
+    configGroupEntity2.setServiceName("HDFS");
+    configGroupDAO.create(configGroupEntity2);
+    ConfigGroupEntity group2 = configGroupDAO.findByName("group2");
     createServiceConfig(serviceName, "admin", 1L, 1L, 1111L, null);
     createServiceConfig(serviceName, "admin", 2L, 2L, 1010L, null);
-    createServiceConfigWithGroup(serviceName, "admin", 3L, 3L, 2222L, null, 1L);
-    createServiceConfigWithGroup(serviceName, "admin", 5L, 5L, 3333L, null, 2L);
-    createServiceConfigWithGroup(serviceName, "admin", 4L, 4L, 3330L, null, 2L);
-
-    List<ServiceConfigEntity> serviceConfigEntities =
-        serviceConfigDAO.getLastServiceConfigsForService(clusterDAO.findByName("c1").getClusterId(), serviceName);
+    createServiceConfigWithGroup(serviceName, "admin", 3L, 3L, 2222L, null, group1.getGroupId());
+    createServiceConfigWithGroup(serviceName, "admin", 5L, 5L, 3333L, null, group2.getGroupId());
+    createServiceConfigWithGroup(serviceName, "admin", 4L, 4L, 3330L, null, group2.getGroupId());
 
+    List<ServiceConfigEntity> serviceConfigEntities = serviceConfigDAO
+        .getLastServiceConfigsForService(clusterDAO.findByName("c1").getClusterId(), serviceName);
     Assert.assertNotNull(serviceConfigEntities);
     Assert.assertEquals(3, serviceConfigEntities.size());
 
-    for (ServiceConfigEntity sce: serviceConfigEntities) {
-     if (sce.getGroupId() != null && sce.getGroupId().equals(2L)) {
-       // Group ID with the highest version should be selected
-       Assert.assertEquals(sce.getVersion(), Long.valueOf(5L));
-     }
+    for (ServiceConfigEntity sce : serviceConfigEntities) {
+      if (sce.getGroupId() != null && sce.getGroupId().equals(group2.getGroupId())) {
+        // Group ID with the highest version should be selected
+        Assert.assertEquals(sce.getVersion(), Long.valueOf(5L));
+      }
     }
   }
 
@@ -370,10 +388,15 @@ public class ServiceConfigDAOTest {
 
     long clusterId = serviceConfigEntity.getClusterId();
 
-    List<ServiceConfigEntity> serviceConfigs = serviceConfigDAO.getAllServiceConfigsForClusterAndStack(clusterId, HDP_01);
-    Assert.assertEquals(4, serviceConfigs.size());
+    List<ServiceConfigEntity> serviceConfigs = serviceConfigDAO.getServiceConfigsForServiceAndStack(
+        clusterId, HDP_01, "HDFS");
+
+    Assert.assertEquals(3, serviceConfigs.size());
 
-    serviceConfigs = serviceConfigDAO.getAllServiceConfigsForClusterAndStack(clusterId, HDP_02);
+    serviceConfigs = serviceConfigDAO.getServiceConfigsForServiceAndStack(clusterId, HDP_01, "YARN");
+    Assert.assertEquals(1, serviceConfigs.size());
+    
+    serviceConfigs = serviceConfigDAO.getServiceConfigsForServiceAndStack(clusterId, HDP_02, "HDFS");
     Assert.assertEquals(0, serviceConfigs.size());
   }
 
@@ -392,6 +415,16 @@ public class ServiceConfigDAOTest {
     clusterEntity.setDesiredStack(stackEntity);
     clusterDAO.merge(clusterEntity);
 
+    ConfigGroupEntity configGroupEntity1 = new ConfigGroupEntity();
+    configGroupEntity1.setClusterEntity(clusterEntity);
+    configGroupEntity1.setClusterId(clusterEntity.getClusterId());
+    configGroupEntity1.setGroupName("group1");
+    configGroupEntity1.setDescription("group1_desc");
+    configGroupEntity1.setTag("HDFS");
+    configGroupEntity1.setServiceName("HDFS");
+    configGroupDAO.create(configGroupEntity1);
+    ConfigGroupEntity group1 = configGroupDAO.findByName("group1");
+    createServiceConfigWithGroup("HDFS", "admin", 3L, 8L, 2222L, null, group1.getGroupId());
     // create some for HDP 0.2
     serviceConfigEntity = createServiceConfig("HDFS", "admin", 4L, 5L, 50L, null);
     serviceConfigEntity = createServiceConfig("HDFS", "admin", 5L, 6L, 60L, null);
@@ -400,7 +433,8 @@ public class ServiceConfigDAOTest {
     long clusterId = serviceConfigEntity.getClusterId();
 
     List<ServiceConfigEntity> serviceConfigs = serviceConfigDAO.getLatestServiceConfigs(clusterId, HDP_01);
-    Assert.assertEquals(2, serviceConfigs.size());
+    Assert.assertEquals(3, serviceConfigs.size());
+    configGroupDAO.remove(configGroupEntity1);
 
     serviceConfigs = serviceConfigDAO.getLatestServiceConfigs(clusterId, HDP_02);
     Assert.assertEquals(2, serviceConfigs.size());
@@ -412,85 +446,99 @@ public class ServiceConfigDAOTest {
     ClusterEntity clusterEntity = clusterDAO.findByName("c1");
 
     Assert.assertTrue(!clusterEntity.getClusterConfigEntities().isEmpty());
-    Assert.assertTrue(!clusterEntity.getConfigMappingEntities().isEmpty());
 
     Assert.assertEquals(5, clusterEntity.getClusterConfigEntities().size());
-    Assert.assertEquals(3, clusterEntity.getConfigMappingEntities().size());
   }
 
+  /**
+   * Tests the ability to find the latest configuration by stack, regardless of
+   * whether that configuration is enabled.
+   *
+   * @throws Exception
+   */
   @Test
-  public void testGetClusterConfigMappingByStack() throws Exception{
+  public void testGetLatestClusterConfigsByStack() throws Exception {
     initClusterEntities();
 
     ClusterEntity clusterEntity = clusterDAO.findByName("c1");
 
-    List<ClusterConfigMappingEntity> clusterConfigMappingEntities = clusterDAO.getClusterConfigMappingsByStack(clusterEntity.getClusterId(), HDP_01);
-    Assert.assertEquals(2, clusterConfigMappingEntities .size());
+    // there should be 3 configs in HDP-0.1 for this cluster, none selected
+    List<ClusterConfigEntity> clusterConfigEntities = clusterDAO.getLatestConfigurations(clusterEntity.getClusterId(), HDP_01);
+    Assert.assertEquals(1, clusterConfigEntities.size());
+
+    ClusterConfigEntity entity = clusterConfigEntities.get(0);
+    Assert.assertEquals("version3", entity.getTag());
+    Assert.assertEquals("oozie-site", entity.getType());
+    Assert.assertFalse(entity.isSelected());
 
-    ClusterConfigMappingEntity e1 = clusterConfigMappingEntities.get(0);
-    String tag1 = e1.getTag();
-    Assert.assertEquals("version1", tag1);
-    String type1 = e1.getType();
-    Assert.assertEquals("oozie-site", type1);
+    // there should be 2 configs in HDP-0.2 for this cluster, the latest being
+    // selected
+    clusterConfigEntities = clusterDAO.getLatestConfigurations(clusterEntity.getClusterId(), HDP_02);
+    Assert.assertEquals(1, clusterConfigEntities.size());
 
-    ClusterConfigMappingEntity e2 = clusterConfigMappingEntities.get(1);
-    String tag2 = e2.getTag();
-    Assert.assertEquals("version2", tag2);
-    String type2 = e2.getType();
-    Assert.assertEquals("oozie-site", type2);
+    entity = clusterConfigEntities.get(0);
+    Assert.assertEquals("version5", entity.getTag());
+    Assert.assertEquals("oozie-site", entity.getType());
+    Assert.assertTrue(entity.isSelected());
   }
 
   /**
-   * Test the get latest configuration query against clusterconfig table with configuration groups inserted
-   * */
+   * Tests getting latest and enabled configurations when there is a
+   * configuration group. Configurations for configuration groups are not
+   * "selected" as they are merged in with the selected configuration. This can
+   * cause problems if searching simply for the "latest" since it will pickup
+   * the wrong configuration.
+   *
+   */
   @Test
-  public void testGetClusterConfigMappingByStackCG() throws Exception{
+  public void testGetClusterConfigsByStackCG() throws Exception {
     initClusterEntitiesWithConfigGroups();
     ClusterEntity clusterEntity = clusterDAO.findByName("c1");
-    Long clusterId = clusterEntity.getClusterId();
 
     List<ConfigGroupEntity> configGroupEntities = configGroupDAO.findAllByTag("OOZIE");
 
+    Long clusterId = clusterDAO.findByName("c1").getClusterId();
+
     Assert.assertNotNull(configGroupEntities);
     ConfigGroupEntity configGroupEntity = configGroupEntities.get(0);
     Assert.assertNotNull(configGroupEntity);
     Assert.assertEquals("c1", configGroupEntity.getClusterEntity().getClusterName());
-    Assert.assertEquals(clusterId, configGroupEntity.getClusterEntity()
-      .getClusterId());
+    Assert.assertEquals(clusterId, configGroupEntity.getClusterEntity().getClusterId());
     Assert.assertEquals("oozie_server", configGroupEntity.getGroupName());
     Assert.assertEquals("OOZIE", configGroupEntity.getTag());
     Assert.assertEquals("oozie server", configGroupEntity.getDescription());
 
-    List<ClusterConfigMappingEntity> clusterConfigMappingEntities = clusterDAO.getClusterConfigMappingsByStack(clusterEntity.getClusterId(), HDP_01);
-    Assert.assertEquals(2, clusterConfigMappingEntities .size());
+    // all 3 are HDP-0.1, but only the 2nd one is enabled
+    List<ClusterConfigEntity> clusterConfigEntities = clusterDAO.getEnabledConfigsByStack(
+        clusterEntity.getClusterId(), HDP_01);
+
+    Assert.assertEquals(1, clusterConfigEntities.size());
+
+    ClusterConfigEntity configEntity = clusterConfigEntities.get(0);
+    Assert.assertEquals("version2", configEntity.getTag());
+    Assert.assertEquals("oozie-site", configEntity.getType());
+    Assert.assertTrue(configEntity.isSelected());
 
-    ClusterConfigMappingEntity e1 = clusterConfigMappingEntities.get(0);
-    String tag1 = e1.getTag();
-    Assert.assertEquals("version1", tag1);
-    String type1 = e1.getType();
-    Assert.assertEquals("oozie-site", type1);
+    // this should still return the 2nd one since the 3rd one has never been
+    // selected as its only for configuration groups
+    clusterConfigEntities = clusterDAO.getLatestConfigurations(clusterEntity.getClusterId(),
+        HDP_01);
 
-    ClusterConfigMappingEntity e2 = clusterConfigMappingEntities.get(1);
-    String tag2 = e2.getTag();
-    Assert.assertEquals("version2", tag2);
-    String type2 = e2.getType();
-    Assert.assertEquals("oozie-site", type2);
+    configEntity = clusterConfigEntities.get(0);
+    Assert.assertEquals("version2", configEntity.getTag());
+    Assert.assertEquals("oozie-site", configEntity.getType());
+    Assert.assertTrue(configEntity.isSelected());
   }
 
+
   /**
-   * Test
-   *
-   * When the last configuration of a given configuration type to be stored into the clusterconfig table is
-   * for a configuration group, there is no corresponding entry generated in the clusterconfigmapping.
-   *
-   * Therefore, the getlatestconfiguration query should skip configuration groups stored in the clusterconfig table.
+   * Tests that when there are multiple configurations for a stack, only the
+   * selected ones get returned.
    *
-   * Test to determine the latest configuration of a given type whose version_tag
-   * exists in the clusterconfigmapping table.
-   *
-   * */
+   * @throws Exception
+   */
   @Test
-  public void testGetLatestClusterConfigMappingByStack() throws Exception {
+  public void testGetEnabledClusterConfigByStack() throws Exception {
     Clusters clusters = injector.getInstance(Clusters.class);
     clusters.addCluster("c1", HDP_01);
 
@@ -498,30 +546,24 @@ public class ServiceConfigDAOTest {
 
     initClusterEntities();
 
-    Collection<ClusterConfigMappingEntity> latestMapingEntities = ((ClusterImpl) cluster).getLatestConfigMappingsForStack(
-        cluster.getClusterId(), HDP_01);
+    Collection<ClusterConfigEntity> latestConfigs = clusterDAO.getEnabledConfigsByStack(
+        cluster.getClusterId(), HDP_02);
 
-    Assert.assertEquals(1, latestMapingEntities.size());
-    for(ClusterConfigMappingEntity e: latestMapingEntities){
-      Assert.assertEquals("version2", e.getTag());
+    Assert.assertEquals(1, latestConfigs.size());
+    for (ClusterConfigEntity e : latestConfigs) {
+      Assert.assertEquals("version5", e.getTag());
       Assert.assertEquals("oozie-site", e.getType());
     }
   }
 
   /**
-   * Test
-   *
-   * When the last configuration of a given configuration type to be stored into the clusterconfig table is
-   * for a configuration group, there is no corresponding entry generated in the clusterconfigmapping.
-   *
-   * Therefore, the getlatestconfiguration query should skip configuration groups stored in the clusterconfig table.
-   *
-   * Test to determine the latest configuration of a given type whose version_tag
-   * exists in the clusterconfigmapping table.
-   *
-   * */
+   * When the last configuration of a given configuration type to be stored into
+   * the clusterconfig table is for a configuration group, that configuration is
+   * not enabled. Therefore, it should be skipped when getting the enabled
+   * configurations for a stack.
+   */
   @Test
-  public void testGetLatestClusterConfigMappingByStackCG() throws Exception{
+  public void testGetLatestClusterConfigByStackCG() throws Exception {
     Clusters clusters = injector.getInstance(Clusters.class);
     clusters.addCluster("c1", HDP_01);
 
@@ -529,16 +571,50 @@ public class ServiceConfigDAOTest {
 
     initClusterEntitiesWithConfigGroups();
 
-    Collection<ClusterConfigMappingEntity> latestMapingEntities = ((ClusterImpl) cluster).getLatestConfigMappingsForStack(
+    Collection<ClusterConfigEntity> latestConfigs = clusterDAO.getEnabledConfigsByStack(
         cluster.getClusterId(), HDP_01);
 
-    Assert.assertEquals(1, latestMapingEntities.size());
-    for(ClusterConfigMappingEntity e: latestMapingEntities){
+    Assert.assertEquals(1, latestConfigs.size());
+    for (ClusterConfigEntity e : latestConfigs) {
       Assert.assertEquals("version2", e.getTag());
       Assert.assertEquals("oozie-site", e.getType());
     }
   }
 
+  @Test
+  public void testGetLastServiceConfigsForServiceWhenAConfigGroupIsDeleted() throws Exception {
+    Clusters clusters = injector.getInstance(Clusters.class);
+    clusters.addCluster("c1", HDP_01);
+    initClusterEntitiesWithConfigGroups();
+    ConfigGroupEntity configGroupEntity1 = new ConfigGroupEntity();
+    ClusterEntity clusterEntity = clusterDAO.findByName("c1");
+    Long clusterId = clusterEntity.getClusterId();
+    configGroupEntity1.setClusterEntity(clusterEntity);
+    configGroupEntity1.setClusterId(clusterEntity.getClusterId());
+    configGroupEntity1.setGroupName("toTestDeleteGroup_OOZIE");
+    configGroupEntity1.setDescription("toTestDeleteGroup_OOZIE_DESC");
+    configGroupEntity1.setTag("OOZIE");
+    configGroupEntity1.setServiceName("OOZIE");
+    configGroupDAO.create(configGroupEntity1);
+    ConfigGroupEntity testDeleteGroup_OOZIE = configGroupDAO.findByName("toTestDeleteGroup_OOZIE");
+    createServiceConfigWithGroup("OOZIE", "", 2L, 2L, System.currentTimeMillis(), null,
+        testDeleteGroup_OOZIE.getGroupId());
+    Collection<ServiceConfigEntity> serviceConfigEntityList = serviceConfigDAO.getLastServiceConfigsForService(clusterId,
+        "OOZIE");
+    Assert.assertEquals(2, serviceConfigEntityList.size());
+    configGroupDAO.remove(configGroupEntity1);
+    serviceConfigEntityList = serviceConfigDAO.getLastServiceConfigsForService(clusterId, "OOZIE");
+    Assert.assertEquals(1, serviceConfigEntityList.size());
+  }
+
+  /**
+   * Createa a cluster with 5 configurations for Oozie. Each configuration will
+   * have a tag of "version" plus a count. 3 configs will be for
+   * {@link #HDP_01}, and 2 will be for {@link #HDP_02}. Only the most recent
+   * configuration, {@code version5}, will be enabled.
+   *
+   * @throws Exception
+   */
   private void initClusterEntities() throws Exception{
     String userName = "admin";
 
@@ -558,7 +634,9 @@ public class ServiceConfigDAOTest {
 
     String oozieSite = "oozie-site";
 
-    for (int i = 1; i < 6; i++){
+    // create 5 Oozie Configs, with only the latest from HDP-0.2 being enabled
+    int configsToCreate = 5;
+    for (int i = 1; i <= configsToCreate; i++) {
       Thread.sleep(1);
       ClusterConfigEntity entity = new ClusterConfigEntity();
       entity.setClusterEntity(clusterEntity);
@@ -567,58 +645,35 @@ public class ServiceConfigDAOTest {
       entity.setVersion(Long.valueOf(i));
       entity.setTag("version"+i);
       entity.setTimestamp(new Date().getTime());
-      if(i < 4) {
-        entity.setStack(stackEntityHDP01);
-      } else {
+
+      // set selected to true to get the last selected timestamp populated
+      entity.setSelected(true);
+
+      // now set it to false
+      entity.setSelected(false);
+
+      entity.setStack(stackEntityHDP01);
+      if (i >= 4) {
         entity.setStack(stackEntityHDP02);
+        if (i == configsToCreate) {
+          entity.setSelected(true);
+        }
       }
+
       entity.setData("");
       clusterDAO.createConfig(entity);
       clusterEntity.getClusterConfigEntities().add(entity);
       clusterDAO.merge(clusterEntity);
     }
-
-    Collection<ClusterConfigMappingEntity> entities = clusterEntity.getConfigMappingEntities();
-    if(null == entities){
-      entities = new ArrayList<ClusterConfigMappingEntity>();
-      clusterEntity.setConfigMappingEntities(entities);
-    }
-
-    ClusterConfigMappingEntity e1 = new ClusterConfigMappingEntity();
-    e1.setClusterEntity(clusterEntity);
-    e1.setClusterId(clusterEntity.getClusterId());
-    e1.setCreateTimestamp(System.currentTimeMillis());
-    e1.setSelected(0);
-    e1.setUser(userName);
-    e1.setType(oozieSite);
-    e1.setTag("version1");
-    entities.add(e1);
-    clusterDAO.merge(clusterEntity);
-
-    ClusterConfigMappingEntity e2 = new ClusterConfigMappingEntity();
-    e2.setClusterEntity(clusterEntity);
-    e2.setClusterId(clusterEntity.getClusterId());
-    e2.setCreateTimestamp(System.currentTimeMillis());
-    e2.setSelected(0);
-    e2.setUser(userName);
-    e2.setType(oozieSite);
-    e2.setTag("version2");
-    entities.add(e2);
-    clusterDAO.merge(clusterEntity);
-
-    Thread.sleep(1);
-    ClusterConfigMappingEntity e3 = new ClusterConfigMappingEntity();
-    e3.setClusterEntity(clusterEntity);
-    e3.setClusterId(clusterEntity.getClusterId());
-    e3.setCreateTimestamp(System.currentTimeMillis());
-    e3.setSelected(1);
-    e3.setUser(userName);
-    e3.setType(oozieSite);
-    e3.setTag("version4");
-    entities.add(e3);
-    clusterDAO.merge(clusterEntity);
   }
 
+  /**
+   * Createa a cluster with 3 configurations for Oozie in the {@link #HDP_01}
+   * stack. Only {@code version2}, will be enabled. {@code version3} will be for
+   * a new configuration group.
+   *
+   * @throws Exception
+   */
   private void initClusterEntitiesWithConfigGroups() throws Exception{
     String userName = "admin";
 
@@ -636,8 +691,9 @@ public class ServiceConfigDAOTest {
     StackEntity stackEntityHDP01 = stackDAO.find(HDP_01.getStackName(),HDP_01.getStackVersion());
     String oozieSite = "oozie-site";
 
-    int count = 3;
-    for (int i = 1; i < count; i++){
+    // create 2 configurations for HDP-0.1
+    int count = 2;
+    for (int i = 1; i <= count; i++) {
       Thread.sleep(1);
       ClusterConfigEntity entity = new ClusterConfigEntity();
       entity.setClusterEntity(clusterEntity);
@@ -648,43 +704,17 @@ public class ServiceConfigDAOTest {
       entity.setTimestamp(new Date().getTime());
       entity.setStack(stackEntityHDP01);
       entity.setData("");
+      entity.setSelected(false);
+
+      if (i == count) {
+        entity.setSelected(true);
+      }
+
       clusterDAO.createConfig(entity);
       clusterEntity.getClusterConfigEntities().add(entity);
       clusterDAO.merge(clusterEntity);
     }
 
-    Collection<ClusterConfigMappingEntity> entities = clusterEntity.getConfigMappingEntities();
-    if(null == entities){
-      entities = new ArrayList<ClusterConfigMappingEntity>();
-      clusterEntity.setConfigMappingEntities(entities);
-    }
-
-    Thread.sleep(1);
-    ClusterConfigMappingEntity e1 = new ClusterConfigMappingEntity();
-    e1.setClusterEntity(clusterEntity);
-    e1.setClusterId(clusterEntity.getClusterId());
-    e1.setCreateTimestamp(System.currentTimeMillis());
-    e1.setSelected(0);
-    e1.setUser(userName);
-    e1.setType(oozieSite);
-    e1.setTag("version1");
-    entities.add(e1);
-    clusterDAO.merge(clusterEntity);
-
-    Thread.sleep(1);
-    ClusterConfigMappingEntity e2 = new ClusterConfigMappingEntity();
-    e2.setClusterEntity(clusterEntity);
-    e2.setClusterId(clusterEntity.getClusterId());
-    e2.setCreateTimestamp(System.currentTimeMillis());
-    e2.setSelected(1);
-    e2.setUser(userName);
-    e2.setType(oozieSite);
-    e2.setTag("version2");
-    entities.add(e2);
-    clusterDAO.merge(clusterEntity);
-
-    ConfigGroupEntity configGroupEntity = new ConfigGroupEntity();
-
     ResourceTypeEntity resourceTypeEntity = resourceTypeDAO.findById(ResourceType.CLUSTER.getId());
     if (resourceTypeEntity == null) {
       resourceTypeEntity = new ResourceTypeEntity();
@@ -696,51 +726,48 @@ public class ServiceConfigDAOTest {
     ResourceEntity resourceEntity = new ResourceEntity();
     resourceEntity.setResourceType(resourceTypeEntity);
 
+    // create a configuration group for oozie
+    ConfigGroupEntity configGroupEntity = new ConfigGroupEntity();
     configGroupEntity.setClusterEntity(clusterEntity);
     configGroupEntity.setClusterId(clusterEntity.getClusterId());
     configGroupEntity.setGroupName("oozie_server");
     configGroupEntity.setDescription("oozie server");
     configGroupEntity.setTag("OOZIE");
+    configGroupDAO.create(configGroupEntity);
 
-    ClusterConfigEntity configEntity = new ClusterConfigEntity();
-    configEntity.setType("oozie-site");
-    configEntity.setTag("version3");
-    configEntity.setData("someData");
-    configEntity.setAttributes("someAttributes");
-    configEntity.setStack(stackEntityHDP01);
+    // create a new configuration for oozie, for the config group
+    ClusterConfigEntity configEntityForGroup = new ClusterConfigEntity();
+    configEntityForGroup.setSelected(false);
+    configEntityForGroup.setType("oozie-site");
+    configEntityForGroup.setTag("version3");
+    configEntityForGroup.setData("someData");
+    configEntityForGroup.setAttributes("someAttributes");
+    configEntityForGroup.setStack(stackEntityHDP01);
 
-    Thread.sleep(1);
-    List<ClusterConfigEntity> configEntities = new
-      ArrayList<ClusterConfigEntity>();
-    configEntities.add(configEntity);
+    List<ClusterConfigEntity> configEntitiesForGroup = new ArrayList<>();
+    configEntitiesForGroup.add(configEntityForGroup);
+    List<ConfigGroupConfigMappingEntity> configMappingEntities = new ArrayList<>();
 
-    configGroupDAO.create(configGroupEntity);
+    for (ClusterConfigEntity config : configEntitiesForGroup) {
+      config.setClusterEntity(clusterEntity);
+      config.setClusterId(clusterEntity.getClusterId());
+      clusterDAO.createConfig(config);
 
-    Thread.sleep(1);
-    if (configEntities != null && !configEntities.isEmpty()) {
-      List<ConfigGroupConfigMappingEntity> configMappingEntities = new
-        ArrayList<ConfigGroupConfigMappingEntity>();
-
-      for (ClusterConfigEntity config : configEntities) {
-        config.setClusterEntity(clusterEntity);
-        config.setClusterId(clusterEntity.getClusterId());
-        clusterDAO.createConfig(config);
-
-        ConfigGroupConfigMappingEntity configMappingEntity = new
-          ConfigGroupConfigMappingEntity();
-        configMappingEntity.setClusterId(clusterEntity.getClusterId());
-        configMappingEntity.setClusterConfigEntity(config);
-        configMappingEntity.setConfigGroupEntity(configGroupEntity);
-        configMappingEntity.setConfigGroupId(configGroupEntity.getGroupId());
-        configMappingEntity.setVersionTag(config.getTag());
-        configMappingEntity.setConfigType(config.getType());
-        configMappingEntity.setTimestamp(System.currentTimeMillis());
-        configMappingEntities.add(configMappingEntity);
-        configGroupConfigMappingDAO.create(configMappingEntity);
-      }
-
-      configGroupEntity.setConfigGroupConfigMappingEntities(configMappingEntities);
-      configGroupDAO.merge(configGroupEntity);
+      Thread.sleep(1);
+      ConfigGroupConfigMappingEntity configMappingEntity = new
+        ConfigGroupConfigMappingEntity();
+      configMappingEntity.setClusterId(clusterEntity.getClusterId());
+      configMappingEntity.setClusterConfigEntity(config);
+      configMappingEntity.setConfigGroupEntity(configGroupEntity);
+      configMappingEntity.setConfigGroupId(configGroupEntity.getGroupId());
+      configMappingEntity.setVersionTag(config.getTag());
+      configMappingEntity.setConfigType(config.getType());
+      configMappingEntity.setTimestamp(System.currentTimeMillis());
+      configMappingEntities.add(configMappingEntity);
+      configGroupConfigMappingDAO.create(configMappingEntity);
     }
+
+    configGroupEntity.setConfigGroupConfigMappingEntities(configMappingEntities);
+    configGroupDAO.merge(configGroupEntity);
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java
index 47d2a81..d07ac15 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ComponentVersionCheckActionTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -21,7 +21,6 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -31,7 +30,6 @@ import javax.persistence.EntityManager;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.H2DatabaseCleaner;
 import org.apache.ambari.server.ServiceComponentNotFoundException;
-import org.apache.ambari.server.ServiceNotFoundException;
 import org.apache.ambari.server.actionmanager.ExecutionCommandWrapper;
 import org.apache.ambari.server.actionmanager.HostRoleCommand;
 import org.apache.ambari.server.actionmanager.HostRoleCommandFactory;
@@ -42,19 +40,18 @@ import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.RequestDAO;
 import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.dao.UpgradeDAO;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
 import org.apache.ambari.server.orm.entities.HostVersionEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
 import org.apache.ambari.server.orm.entities.RequestEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.orm.entities.UpgradeEntity;
+import org.apache.ambari.server.orm.entities.UpgradeHistoryEntity;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ConfigFactory;
@@ -75,8 +72,6 @@ import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import com.google.inject.Guice;
 import com.google.inject.Inject;
@@ -87,18 +82,16 @@ import com.google.inject.persist.UnitOfWork;
  * Tests upgrade-related server side actions
  */
 public class ComponentVersionCheckActionTest {
-  private static final Logger LOG = LoggerFactory.getLogger(ComponentVersionCheckActionTest.class);
-
 
   private static final String HDP_2_1_1_0 = "2.1.1.0-1";
   private static final String HDP_2_1_1_1 = "2.1.1.1-2";
 
-  private static final String HDP_2_2_1_0 = "2.2.0.1-3";
+  private static final String HDP_2_2_1_0 = "2.2.1.0-1";
 
   private static final StackId HDP_21_STACK = new StackId("HDP-2.1.1");
   private static final StackId HDP_22_STACK = new StackId("HDP-2.2.0");
 
-  private static final String HDP_211_CENTOS6_REPO_URL = "http://s3.amazonaws.com/dev.hortonworks.com/HDP/centos6/2.x/BUILDS/2.1.1.0-118";
+  private static final String HDP_211_CENTOS6_REPO_URL = "http://public-repo-1.hortonworks.com/HDP/centos6/2.x/updates/2.0.6.0";
 
   private Injector m_injector;
 
@@ -109,9 +102,6 @@ public class ComponentVersionCheckActionTest {
   private RepositoryVersionDAO repoVersionDAO;
 
   @Inject
-  private ClusterVersionDAO clusterVersionDAO;
-
-  @Inject
   private HostVersionDAO hostVersionDAO;
 
   @Inject
@@ -148,6 +138,8 @@ public class ComponentVersionCheckActionTest {
     String clusterName = "c1";
     String hostName = "h1";
 
+    m_helper.createStack(sourceStack);
+
     Clusters clusters = m_injector.getInstance(Clusters.class);
     clusters.addCluster(clusterName, sourceStack);
 
@@ -174,9 +166,7 @@ public class ComponentVersionCheckActionTest {
     host.setHostAttributes(hostAttributes);
 
     // Create the starting repo version
-    RepositoryVersionEntity sourceRepositoryVersion = m_helper.getOrCreateRepositoryVersion(sourceStack, sourceRepo);
-    c.createClusterVersion(sourceStack, sourceRepo, "admin", RepositoryVersionState.INSTALLING);
-    c.transitionClusterVersion(sourceStack, sourceRepo, RepositoryVersionState.CURRENT);
+    m_helper.getOrCreateRepositoryVersion(sourceStack, sourceRepo);
 
     // Create the new repo version
     String urlInfo = "[{'repositories':["
@@ -187,13 +177,8 @@ public class ComponentVersionCheckActionTest {
         targetRepo, String.valueOf(System.currentTimeMillis()), urlInfo);
 
     // Start upgrading the newer repo
-    c.createClusterVersion(targetStack, targetRepo, "admin", RepositoryVersionState.INSTALLING);
-    c.transitionClusterVersion(targetStack, targetRepo, RepositoryVersionState.INSTALLED);
     c.setCurrentStackVersion(targetStack);
 
-    c.mapHostVersions(Collections.singleton(hostName), c.getCurrentClusterVersion(),
-        RepositoryVersionState.CURRENT);
-
     HostDAO hostDAO = m_injector.getInstance(HostDAO.class);
 
     HostVersionEntity entity = new HostVersionEntity();
@@ -214,16 +199,31 @@ public class ComponentVersionCheckActionTest {
     upgradeEntity.setClusterId(c.getClusterId());
     upgradeEntity.setRequestEntity(requestEntity);
     upgradeEntity.setUpgradePackage("");
-    upgradeEntity.setFromRepositoryVersion(sourceRepositoryVersion);
-    upgradeEntity.setToRepositoryVersion(toRepositoryVersion);
+    upgradeEntity.setRepositoryVersion(toRepositoryVersion);
     upgradeEntity.setUpgradeType(UpgradeType.NON_ROLLING);
     upgradeDAO.create(upgradeEntity);
 
     c.setUpgradeEntity(upgradeEntity);
   }
 
-  private void makeCrossStackUpgradeCluster(StackId sourceStack, String sourceRepo, StackId targetStack,
-                                            String targetRepo, String clusterName, String hostName) throws Exception {
+  /**
+   * Creates a cluster with a running upgrade. The upgrade will have no services
+   * attached to it, so those will need to be set after this is called.
+   *
+   * @param sourceStack
+   * @param sourceRepo
+   * @param targetStack
+   * @param targetRepo
+   * @param clusterName
+   * @param hostName
+   * @throws Exception
+   */
+  private void makeCrossStackUpgradeCluster(StackId sourceStack, String sourceRepo,
+      StackId targetStack, String targetRepo, String clusterName, String hostName)
+      throws Exception {
+
+    m_helper.createStack(sourceStack);
+    m_helper.createStack(targetStack);
 
     Clusters clusters = m_injector.getInstance(Clusters.class);
     clusters.addCluster(clusterName, sourceStack);
@@ -254,11 +254,11 @@ public class ComponentVersionCheckActionTest {
     clusters.mapHostToCluster(hostName, clusterName);
 
     // Create the starting repo version
-    RepositoryVersionEntity sourceRepositoryVersion = m_helper.getOrCreateRepositoryVersion(sourceStack, sourceRepo);
-    c.createClusterVersion(sourceStack, sourceRepo, "admin", RepositoryVersionState.INSTALLING);
-    c.transitionClusterVersion(sourceStack, sourceRepo, RepositoryVersionState.CURRENT);
+    m_helper.getOrCreateRepositoryVersion(sourceStack, sourceRepo);
 
-    RepositoryVersionEntity toRepositoryVersion = m_helper.getOrCreateRepositoryVersion(targetStack,targetRepo);
+    // create the new repo version
+    RepositoryVersionEntity toRepositoryVersion = m_helper.getOrCreateRepositoryVersion(targetStack,
+        targetRepo);
 
     RequestEntity requestEntity = new RequestEntity();
     requestEntity.setClusterId(c.getClusterId());
@@ -272,15 +272,13 @@ public class ComponentVersionCheckActionTest {
     upgradeEntity.setClusterId(c.getClusterId());
     upgradeEntity.setRequestEntity(requestEntity);
     upgradeEntity.setUpgradePackage("");
-    upgradeEntity.setFromRepositoryVersion(sourceRepositoryVersion);
-    upgradeEntity.setToRepositoryVersion(toRepositoryVersion);
+    upgradeEntity.setRepositoryVersion(toRepositoryVersion);
     upgradeEntity.setUpgradeType(UpgradeType.NON_ROLLING);
     upgradeDAO.create(upgradeEntity);
 
     c.setUpgradeEntity(upgradeEntity);
   }
 
-
   /**
    * Creates a new {@link HostVersionEntity} instance in the
    * {@link RepositoryVersionState#INSTALLED} for the specified host.
@@ -318,9 +316,6 @@ public class ComponentVersionCheckActionTest {
 
     // Finalize the upgrade
     Map<String, String> commandParams = new HashMap<>();
-    commandParams.put(FinalizeUpgradeAction.UPGRADE_DIRECTION_KEY, "upgrade");
-    commandParams.put(FinalizeUpgradeAction.VERSION_KEY, targetRepo);
-
     ExecutionCommand executionCommand = new ExecutionCommand();
     executionCommand.setCommandParams(commandParams);
     executionCommand.setClusterName("c1");
@@ -336,19 +331,19 @@ public class ComponentVersionCheckActionTest {
     assertNotNull(report);
     assertEquals(HostRoleStatus.COMPLETED.name(), report.getStatus());
     assertEquals(0, report.getExitCode());
-
   }
 
   @Test
   public void testMixedComponentVersions() throws Exception {
     StackId sourceStack = HDP_21_STACK;
     StackId targetStack = HDP_22_STACK;
-    String sourceRepo = HDP_2_1_1_0;
-    String targetRepo = HDP_2_2_1_0;
+    String sourceVersion = HDP_2_1_1_0;
+    String targetVersion = HDP_2_2_1_0;
     String clusterName = "c1";
     String hostName = "h1";
 
-    makeCrossStackUpgradeCluster(sourceStack, sourceRepo, targetStack, targetRepo, clusterName, hostName);
+    makeCrossStackUpgradeCluster(sourceStack, sourceVersion, targetStack, targetVersion,
+        clusterName, hostName);
 
     Clusters clusters = m_injector.getInstance(Clusters.class);
     Cluster cluster = clusters.getCluster("c1");
@@ -356,20 +351,11 @@ public class ComponentVersionCheckActionTest {
     RepositoryVersionEntity sourceRepoVersion = m_helper.getOrCreateRepositoryVersion(HDP_21_STACK, HDP_2_1_1_0);
     RepositoryVersionEntity targetRepoVersion = m_helper.getOrCreateRepositoryVersion(HDP_22_STACK, HDP_2_2_1_0);
 
-    // Start upgrading the newer repo
-    cluster.createClusterVersion(targetStack, targetRepo, "admin",
-        RepositoryVersionState.INSTALLING);
-    cluster.transitionClusterVersion(targetStack, targetRepo, RepositoryVersionState.INSTALLED);
-
-    cluster.mapHostVersions(Collections.singleton(hostName), cluster.getCurrentClusterVersion(),
-        RepositoryVersionState.CURRENT);
-
-
-    Service service = installService(cluster, "HDFS");
+    Service service = installService(cluster, "HDFS", sourceRepoVersion);
     addServiceComponent(cluster, service, "NAMENODE");
     addServiceComponent(cluster, service, "DATANODE");
-    createNewServiceComponentHost(cluster, "HDFS", "NAMENODE", "h1");
-    createNewServiceComponentHost(cluster, "HDFS", "DATANODE", "h1");
+    createNewServiceComponentHost(cluster, "HDFS", "NAMENODE", hostName);
+    createNewServiceComponentHost(cluster, "HDFS", "DATANODE", hostName);
 
     // create some configs
     createConfigs(cluster);
@@ -381,24 +367,36 @@ public class ComponentVersionCheckActionTest {
     cluster.setCurrentStackVersion(sourceStack);
     cluster.setDesiredStackVersion(targetStack);
 
-    // set the SCH versions to the new stack so that the finalize action is
-    // happy
-    cluster.getServiceComponentHosts("HDFS", "NAMENODE").get(0).setVersion(targetRepo);
-    // don't update DATANODE - we want to make the action complain
+    // tell the upgrade that HDFS is upgrading - without this, no services will
+    // be participating in the upgrade
+    UpgradeEntity upgrade = cluster.getUpgradeInProgress();
+    UpgradeHistoryEntity history = new UpgradeHistoryEntity();
+    history.setUpgrade(upgrade);
+    history.setServiceName("HDFS");
+    history.setComponentName("NAMENODE");
+    history.setFromRepositoryVersion(sourceRepoVersion);
+    history.setTargetRepositoryVersion(targetRepoVersion);
+    upgrade.addHistory(history);
+
+    history = new UpgradeHistoryEntity();
+    history.setUpgrade(upgrade);
+    history.setServiceName("HDFS");
+    history.setComponentName("DATANODE");
+    history.setFromRepositoryVersion(sourceRepoVersion);
+    history.setTargetRepositoryVersion(targetRepoVersion);
+    upgrade.addHistory(history);
 
-    // inject an unhappy path where the cluster repo version is still UPGRADING
-    // even though all of the hosts are UPGRADED
-    ClusterVersionEntity upgradingClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(
-        "c1", HDP_22_STACK, targetRepo);
+    UpgradeDAO upgradeDAO = m_injector.getInstance(UpgradeDAO.class);
+    upgrade = upgradeDAO.merge(upgrade);
 
-    upgradingClusterVersion.setState(RepositoryVersionState.INSTALLING);
-    upgradingClusterVersion = clusterVersionDAO.merge(upgradingClusterVersion);
+    // set the SCH versions to the new stack so that the finalize action is
+    // happy - don't update DATANODE - we want to make the action complain
+    cluster.getServiceComponentHosts("HDFS", "NAMENODE").get(0).setVersion(targetVersion);
 
     // verify the conditions for the test are met properly
-    upgradingClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion("c1", HDP_22_STACK, targetRepo);
-    List<HostVersionEntity> hostVersions = hostVersionDAO.findByClusterStackAndVersion("c1", HDP_22_STACK, targetRepo);
+    List<HostVersionEntity> hostVersions = hostVersionDAO.findByClusterStackAndVersion("c1",
+        HDP_22_STACK, targetVersion);
 
-    assertEquals(RepositoryVersionState.INSTALLING, upgradingClusterVersion.getState());
     assertTrue(hostVersions.size() > 0);
     for (HostVersionEntity hostVersion : hostVersions) {
       assertEquals(RepositoryVersionState.INSTALLED, hostVersion.getState());
@@ -407,11 +405,6 @@ public class ComponentVersionCheckActionTest {
     // now finalize and ensure we can transition from UPGRADING to UPGRADED
     // automatically before CURRENT
     Map<String, String> commandParams = new HashMap<>();
-    commandParams.put(FinalizeUpgradeAction.UPGRADE_DIRECTION_KEY, "upgrade");
-    commandParams.put(FinalizeUpgradeAction.VERSION_KEY, targetRepo);
-    commandParams.put(FinalizeUpgradeAction.ORIGINAL_STACK_KEY, sourceStack.getStackId());
-    commandParams.put(FinalizeUpgradeAction.TARGET_STACK_KEY, targetStack.getStackId());
-
     ExecutionCommand executionCommand = new ExecutionCommand();
     executionCommand.setCommandParams(commandParams);
     executionCommand.setClusterName("c1");
@@ -428,12 +421,85 @@ public class ComponentVersionCheckActionTest {
     assertNotNull(report);
     assertEquals(HostRoleStatus.FAILED.name(), report.getStatus());
     assertEquals(-1, report.getExitCode());
+
+    // OK, now set the datanode so it completes
+    cluster.getServiceComponentHosts("HDFS", "DATANODE").get(0).setVersion(targetVersion);
+
+    report = action.execute(null);
+    assertNotNull(report);
+    assertEquals(HostRoleStatus.COMPLETED.name(), report.getStatus());
+    assertEquals(0, report.getExitCode());
+  }
+
+  @Test
+  public void testMatchingPartialVersions() throws Exception {
+    StackId sourceStack = HDP_21_STACK;
+    StackId targetStack = HDP_21_STACK;
+    String sourceRepo = HDP_2_1_1_0;
+    String targetRepo = HDP_2_1_1_1;
+
+    makeUpgradeCluster(sourceStack, sourceRepo, targetStack, targetRepo);
+
+    Clusters clusters = m_injector.getInstance(Clusters.class);
+
+    Host host = clusters.getHost("h1");
+    Assert.assertNotNull(host);
+    host.setOsInfo("redhat6");
+
+    Cluster cluster = clusters.getCluster("c1");
+    clusters.mapHostToCluster("h1", "c1");
+
+    RepositoryVersionEntity repositoryVersion2110 = m_helper.getOrCreateRepositoryVersion(
+        HDP_21_STACK, HDP_2_1_1_0);
+
+    RepositoryVersionEntity repositoryVersion2111 = m_helper.getOrCreateRepositoryVersion(
+        HDP_21_STACK, HDP_2_1_1_1);
+
+    Service service = installService(cluster, "HDFS", repositoryVersion2110);
+    addServiceComponent(cluster, service, "NAMENODE");
+    addServiceComponent(cluster, service, "DATANODE");
+
+    ServiceComponentHost sch = createNewServiceComponentHost(cluster, "HDFS", "NAMENODE", "h1");
+    sch.setVersion(HDP_2_1_1_0);
+    sch = createNewServiceComponentHost(cluster, "HDFS", "DATANODE", "h1");
+    sch.setVersion(HDP_2_1_1_0);
+
+    service = installService(cluster, "ZOOKEEPER", repositoryVersion2111);
+    addServiceComponent(cluster, service, "ZOOKEEPER_SERVER");
+
+    sch = createNewServiceComponentHost(cluster, "ZOOKEEPER", "ZOOKEEPER_SERVER", "h1");
+    sch.setVersion(HDP_2_1_1_1);
+
+    // Verify the repo before calling Finalize
+    AmbariMetaInfo metaInfo = m_injector.getInstance(AmbariMetaInfo.class);
+
+    RepositoryInfo repo = metaInfo.getRepository(sourceStack.getStackName(), sourceStack.getStackVersion(), "redhat6", sourceStack.getStackId());
+    assertEquals(HDP_211_CENTOS6_REPO_URL, repo.getBaseUrl());
+
+    // Finalize the upgrade
+    Map<String, String> commandParams = new HashMap<>();
+    ExecutionCommand executionCommand = new ExecutionCommand();
+    executionCommand.setCommandParams(commandParams);
+    executionCommand.setClusterName("c1");
+
+    HostRoleCommand hostRoleCommand = hostRoleCommandFactory.create(null, null, null, null);
+    hostRoleCommand.setExecutionCommandWrapper(new ExecutionCommandWrapper(executionCommand));
+
+    ComponentVersionCheckAction action = m_injector.getInstance(ComponentVersionCheckAction.class);
+    action.setExecutionCommand(executionCommand);
+    action.setHostRoleCommand(hostRoleCommand);
+
+    CommandReport report = action.execute(null);
+    assertNotNull(report);
+    assertEquals(HostRoleStatus.COMPLETED.name(), report.getStatus());
+    assertEquals(0, report.getExitCode());
+
   }
 
   private ServiceComponentHost createNewServiceComponentHost(Cluster cluster, String svc,
                                                              String svcComponent, String hostName) throws AmbariException {
     Assert.assertNotNull(cluster.getConfigGroups());
-    Service s = installService(cluster, svc);
+    Service s = cluster.getService(svc);
     ServiceComponent sc = addServiceComponent(cluster, s, svcComponent);
 
     ServiceComponentHost sch = serviceComponentHostFactory.createNew(sc, hostName);
@@ -441,22 +507,13 @@ public class ComponentVersionCheckActionTest {
     sc.addServiceComponentHost(sch);
     sch.setDesiredState(State.INSTALLED);
     sch.setState(State.INSTALLED);
-    sch.setDesiredStackVersion(cluster.getDesiredStackVersion());
-    sch.setStackVersion(cluster.getCurrentStackVersion());
 
     return sch;
   }
 
-  private Service installService(Cluster cluster, String serviceName) throws AmbariException {
-    Service service = null;
-
-    try {
-      service = cluster.getService(serviceName);
-    } catch (ServiceNotFoundException e) {
-      service = serviceFactory.createNew(cluster, serviceName);
-      cluster.addService(service);
-    }
-
+  private Service installService(Cluster cluster, String serviceName, RepositoryVersionEntity repositoryVersion) throws AmbariException {
+    Service service = serviceFactory.createNew(cluster, serviceName, repositoryVersion);
+    cluster.addService(service);
     return service;
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/stack/RepoUtilTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/stack/RepoUtilTest.java b/ambari-server/src/test/java/org/apache/ambari/server/stack/RepoUtilTest.java
index 99a34f4..9216499 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/stack/RepoUtilTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/stack/RepoUtilTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -21,22 +21,20 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
 
-import com.google.common.collect.ImmutableListMultimap;
-import com.google.common.collect.ImmutableMultimap;
-import com.google.common.collect.Multimaps;
 import org.apache.ambari.server.controller.RepositoryResponse;
 import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
 import org.apache.ambari.server.orm.entities.RepositoryEntity;
 import org.apache.ambari.server.state.RepositoryInfo;
-import org.apache.ambari.server.state.stack.RepositoryXml;
 import org.junit.Assert;
 import org.junit.Test;
 
 import com.google.common.base.Optional;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableListMultimap;
 import com.google.common.collect.ListMultimap;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Multimaps;
 
 public class RepoUtilTest {
 
@@ -152,7 +150,7 @@ public class RepoUtilTest {
 
   private static ListMultimap<String, RepositoryInfo> serviceRepos(List<String> operatingSystems,
       String repoName, String repoId, String baseUrl) {
-    ArrayListMultimap multimap = ArrayListMultimap.create();
+    ListMultimap<String, RepositoryInfo> multimap = ArrayListMultimap.create();
     for (String os: operatingSystems) {
       RepositoryInfo repoInfo = new RepositoryInfo();
       repoInfo.setOsType(os);

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/test/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartupTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartupTest.java b/ambari-server/src/test/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartupTest.java
index 24ab0e8..89b2a0e 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartupTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartupTest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -18,32 +18,35 @@
 package org.apache.ambari.server.stack;
 
 
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.atLeast;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
-import java.io.*;
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.util.Collections;
 
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
-import org.apache.ambari.server.orm.entities.OperatingSystemEntity;
-import org.apache.ambari.server.orm.entities.RepositoryEntity;
+import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
 import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
+import org.apache.ambari.server.orm.entities.ServiceDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.state.RepositoryInfo;
 import org.apache.ambari.server.state.StackInfo;
 import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
-import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 import com.google.common.base.Charsets;
 import com.google.common.collect.ImmutableList;
 import com.google.common.io.Resources;
+import com.google.gson.Gson;
 import com.google.inject.Guice;
-import com.google.inject.Injector;
 import com.google.inject.Provider;
 
 /**
@@ -55,7 +58,6 @@ public class UpdateActiveRepoVersionOnStartupTest {
   private static String ADD_ON_REPO_ID = "MSFT_R-8.0";
 
   private RepositoryVersionDAO repositoryVersionDao;
-  private RepositoryVersionEntity repoVersion;
   private UpdateActiveRepoVersionOnStartup activeRepoUpdater;
 
   @Test
@@ -77,25 +79,20 @@ public class UpdateActiveRepoVersionOnStartupTest {
    * @throws Exception
    */
   private void verifyRepoIsAdded() throws Exception {
-    verify(repositoryVersionDao, times(1)).merge(repoVersion);
-
-    boolean serviceRepoAddedToJson = false;
-    outer:
-    for (OperatingSystemEntity os: repoVersion.getOperatingSystems()) if (os.getOsType().equals("redhat6")) {
-      for (RepositoryEntity repo: os.getRepositories()) if (repo.getRepositoryId().equals(ADD_ON_REPO_ID)) {
-        serviceRepoAddedToJson = true;
-        break outer;
-      }
-    }
-    Assert.assertTrue(ADD_ON_REPO_ID + " is add-on repo was not added to JSON representation", serviceRepoAddedToJson);
+    verify(repositoryVersionDao, atLeast(1)).merge(Mockito.any(RepositoryVersionEntity.class));
   }
 
   public void init(boolean addClusterVersion) throws Exception {
     ClusterDAO clusterDao = mock(ClusterDAO.class);
-    ClusterVersionDAO clusterVersionDAO = mock(ClusterVersionDAO.class);
+
     repositoryVersionDao = mock(RepositoryVersionDAO.class);
+
     final RepositoryVersionHelper repositoryVersionHelper = new RepositoryVersionHelper();
-    AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
+    Field field = RepositoryVersionHelper.class.getDeclaredField("gson");
+    field.setAccessible(true);
+    field.set(repositoryVersionHelper, new Gson());
+
+    final AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
 
     StackManager stackManager = mock(StackManager.class);
     when(metaInfo.getStackManager()).thenReturn(stackManager);
@@ -109,43 +106,64 @@ public class UpdateActiveRepoVersionOnStartupTest {
     stackEntity.setStackVersion("2.3");
     cluster.setDesiredStack(stackEntity);
 
+    RepositoryVersionEntity desiredRepositoryVersion = new RepositoryVersionEntity();
+    desiredRepositoryVersion.setStack(stackEntity);
+    desiredRepositoryVersion.setOperatingSystems(resourceAsString("org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartupTest_initialRepos.json"));
+
+    ServiceDesiredStateEntity serviceDesiredStateEntity = new ServiceDesiredStateEntity();
+    serviceDesiredStateEntity.setDesiredRepositoryVersion(desiredRepositoryVersion);
+
+    ClusterServiceEntity clusterServiceEntity = new ClusterServiceEntity();
+    clusterServiceEntity.setServiceDesiredStateEntity(serviceDesiredStateEntity);
+    cluster.setClusterServiceEntities(Collections.singletonList(clusterServiceEntity));
+
     StackInfo stackInfo = new StackInfo();
     stackInfo.setName("HDP");
     stackInfo.setVersion("2.3");
+
     RepositoryInfo repositoryInfo = new RepositoryInfo();
     repositoryInfo.setBaseUrl("http://msft.r");
     repositoryInfo.setRepoId(ADD_ON_REPO_ID);
     repositoryInfo.setRepoName("MSFT_R");
     repositoryInfo.setOsType("redhat6");
     stackInfo.getRepositories().add(repositoryInfo);
+
     when(stackManager.getStack("HDP", "2.3")).thenReturn(stackInfo);
 
-    Provider<RepositoryVersionHelper> repositoryVersionHelperProvider = mock(Provider.class);
+    final Provider<RepositoryVersionHelper> repositoryVersionHelperProvider = mock(Provider.class);
     when(repositoryVersionHelperProvider.get()).thenReturn(repositoryVersionHelper);
+
+
     InMemoryDefaultTestModule testModule = new InMemoryDefaultTestModule() {
       @Override
       protected void configure() {
-        bind(RepositoryVersionHelper.class).toInstance(repositoryVersionHelper);
+        bind(RepositoryVersionHelper.class).toProvider(repositoryVersionHelperProvider);
+        bind(AmbariMetaInfo.class).toProvider(new Provider<AmbariMetaInfo>() {
+          @Override
+          public AmbariMetaInfo get() {
+            return metaInfo;
+          }
+        });
+
         requestStaticInjection(RepositoryVersionEntity.class);
       }
     };
-    Injector injector = Guice.createInjector(testModule);
+
+    Guice.createInjector(testModule);
     if (addClusterVersion) {
-      repoVersion = new RepositoryVersionEntity();
-      repoVersion.setStack(stackEntity);
-      repoVersion.setOperatingSystems(resourceAsString("org/apache/ambari/server/stack/UpdateActiveRepoVersionOnStartupTest_initialRepos.json"));
-      ClusterVersionEntity clusterVersion = new ClusterVersionEntity();
-      clusterVersion.setRepositoryVersion(repoVersion);
-      when(clusterVersionDAO.findByClusterAndStateCurrent(CLUSTER_NAME)).thenReturn(clusterVersion);
 
+      RepositoryInfo info = new RepositoryInfo();
+      info.setBaseUrl("http://msft.r");
+      info.setRepoId(ADD_ON_REPO_ID);
+      info.setRepoName("MSFT_R1");
+      info.setOsType("redhat6");
+      stackInfo.getRepositories().add(info);
     }
 
     activeRepoUpdater = new UpdateActiveRepoVersionOnStartup(clusterDao,
-        clusterVersionDAO, repositoryVersionDao, repositoryVersionHelper, metaInfo);
+        repositoryVersionDao, repositoryVersionHelper, metaInfo);
   }
 
-
-
   private static String resourceAsString(String resourceName) throws IOException {
     return Resources.toString(Resources.getResource(resourceName), Charsets.UTF_8);
   }


[20/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog220.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog220.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog220.java
deleted file mode 100644
index 8ee7943..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog220.java
+++ /dev/null
@@ -1,1409 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import java.io.StringWriter;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.Collection;
-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;
-import java.util.regex.Matcher;
-
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
-import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
-import org.apache.ambari.server.orm.dao.ArtifactDAO;
-import org.apache.ambari.server.orm.dao.ClusterDAO;
-import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
-import org.apache.ambari.server.orm.dao.DaoUtils;
-import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
-import org.apache.ambari.server.orm.dao.StackDAO;
-import org.apache.ambari.server.orm.dao.UpgradeDAO;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.ArtifactEntity;
-import org.apache.ambari.server.orm.entities.ClusterEntity;
-import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
-import org.apache.ambari.server.orm.entities.HostEntity;
-import org.apache.ambari.server.orm.entities.HostVersionEntity;
-import org.apache.ambari.server.orm.entities.RepositoryVersionEntity;
-import org.apache.ambari.server.orm.entities.StackEntity;
-import org.apache.ambari.server.orm.entities.UpgradeEntity;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.RepositoryVersionState;
-import org.apache.ambari.server.state.SecurityType;
-import org.apache.ambari.server.state.Service;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.StackInfo;
-import org.apache.ambari.server.state.alert.SourceType;
-import org.apache.ambari.server.state.kerberos.KerberosComponentDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosDescriptorFactory;
-import org.apache.ambari.server.state.kerberos.KerberosIdentityDescriptor;
-import org.apache.ambari.server.state.kerberos.KerberosServiceDescriptor;
-import org.apache.ambari.server.state.stack.upgrade.Direction;
-import org.apache.ambari.server.state.stack.upgrade.RepositoryVersionHelper;
-import org.apache.ambari.server.state.stack.upgrade.UpgradeType;
-import org.apache.ambari.server.utils.VersionUtils;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import com.google.inject.persist.Transactional;
-
-/**
- * Upgrade catalog for version 2.2.0.
- */
-public class UpgradeCatalog220 extends AbstractUpgradeCatalog {
-
-  private static final String UPGRADE_TABLE = "upgrade";
-  private static final String STORM_SITE = "storm-site";
-  private static final String HDFS_SITE_CONFIG = "hdfs-site";
-  private static final String TOPOLOGY_CONFIG = "topology";
-  private static final String KAFKA_BROKER = "kafka-broker";
-  private static final String KAFKA_ENV_CONFIG = "kafka-env";
-  private static final String KAFKA_ENV_CONTENT_KERBEROS_PARAMS =
-    "export KAFKA_KERBEROS_PARAMS={{kafka_kerberos_params}}";
-  private static final String AMS_ENV = "ams-env";
-  private static final String AMS_HBASE_ENV = "ams-hbase-env";
-  private static final String AMS_SITE = "ams-site";
-  private static final String AMS_HBASE_SITE = "ams-hbase-site";
-  private static final String AMS_HBASE_SITE_ZK_TIMEOUT_PROPERTY =
-    "zookeeper.session.timeout.localHBaseCluster";
-  private static final String AMS_HBASE_SITE_NORMALIZER_ENABLED_PROPERTY = "hbase.normalizer.enabled";
-  private static final String AMS_HBASE_SITE_NORMALIZER_PERIOD_PROPERTY = "hbase.normalizer.period";
-  private static final String AMS_HBASE_SITE_NORMALIZER_CLASS_PROPERTY = "hbase.master.normalizer.class";
-  private static final String TIMELINE_METRICS_HBASE_FIFO_COMPACTION_ENABLED = "timeline.metrics.hbase.fifo.compaction.enabled";
-  private static final String HBASE_ENV_CONFIG = "hbase-env";
-  private static final String FLUME_ENV_CONFIG = "flume-env";
-  private static final String HIVE_SITE_CONFIG = "hive-site";
-  private static final String HIVE_ENV_CONFIG = "hive-env";
-  private static final String RANGER_ENV_CONFIG = "ranger-env";
-  private static final String RANGER_UGSYNC_SITE_CONFIG = "ranger-ugsync-site";
-  private static final String ZOOKEEPER_LOG4J_CONFIG = "zookeeper-log4j";
-  private static final String NIMBS_MONITOR_FREQ_SECS_PROPERTY = "nimbus.monitor.freq.secs";
-  private static final String STORM_METRICS_REPORTER = "metrics.reporter.register";
-  private static final String HIVE_SERVER2_OPERATION_LOG_LOCATION_PROPERTY = "hive.server2.logging.operation.log.location";
-  private static final String HADOOP_ENV_CONFIG = "hadoop-env";
-  private static final String CONTENT_PROPERTY = "content";
-  private static final String HADOOP_ENV_CONTENT_TO_APPEND = "\n{% if is_datanode_max_locked_memory_set %}\n" +
-    "# Fix temporary bug, when ulimit from conf files is not picked up, without full relogin. \n" +
-    "# Makes sense to fix only when runing DN as root \n" +
-    "if [ \"$command\" == \"datanode\" ] && [ \"$EUID\" -eq 0 ] && [ -n \"$HADOOP_SECURE_DN_USER\" ]; then\n" +
-    "  ulimit -l {{datanode_max_locked_memory}}\n" +
-    "fi\n" +
-    "{% endif %}\n";
-
-  private static final String DOWNGRADE_ALLOWED_COLUMN = "downgrade_allowed";
-  private static final String UPGRADE_SKIP_FAILURE_COLUMN = "skip_failures";
-  private static final String UPGRADE_SKIP_SC_FAILURE_COLUMN = "skip_sc_failures";
-  public static final String UPGRADE_PACKAGE_COL = "upgrade_package";
-  public static final String UPGRADE_TYPE_COL = "upgrade_type";
-  public static final String REPO_VERSION_TABLE = "repo_version";
-
-  private static final String HOST_ROLE_COMMAND_TABLE = "host_role_command";
-  private static final String HOST_ID_COL = "host_id";
-
-  private static final String KERBEROS_DESCRIPTOR_TABLE = "kerberos_descriptor";
-  private static final String KERBEROS_DESCRIPTOR_NAME_COLUMN = "kerberos_descriptor_name";
-  private static final String KERBEROS_DESCRIPTOR_COLUMN = "kerberos_descriptor";
-  private static final String RANGER_HDFS_PLUGIN_ENABLED_PROPERTY = "ranger-hdfs-plugin-enabled";
-  private static final String RANGER_HIVE_PLUGIN_ENABLED_PROPERTY = "ranger-hive-plugin-enabled";
-  private static final String RANGER_HBASE_PLUGIN_ENABLED_PROPERTY = "ranger-hbase-plugin-enabled";
-  private static final String RANGER_STORM_PLUGIN_ENABLED_PROPERTY = "ranger-storm-plugin-enabled";
-  private static final String RANGER_KNOX_PLUGIN_ENABLED_PROPERTY = "ranger-knox-plugin-enabled";
-  private static final String RANGER_YARN_PLUGIN_ENABLED_PROPERTY = "ranger-yarn-plugin-enabled";
-  private static final String RANGER_KAFKA_PLUGIN_ENABLED_PROPERTY = "ranger-kafka-plugin-enabled";
-
-  private static final String RANGER_USERSYNC_SOURCE_IMPL_CLASS_PROPERTY = "ranger.usersync.source.impl.class";
-
-  private static final String BLUEPRINT_TABLE = "blueprint";
-  private static final String SECURITY_TYPE_COLUMN = "security_type";
-  private static final String SECURITY_DESCRIPTOR_REF_COLUMN = "security_descriptor_reference";
-
-  private static final String STAGE_TABLE = "stage";
-
-  private static final String KNOX_SERVICE = "KNOX";
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog220.class);
-
-  private static final String OOZIE_SITE_CONFIG = "oozie-site";
-  private static final String OOZIE_SERVICE_HADOOP_CONFIGURATIONS_PROPERTY_NAME = "oozie.service.HadoopAccessorService.hadoop.configurations";
-  private static final String OLD_DEFAULT_HADOOP_CONFIG_PATH = "/etc/hadoop/conf";
-  private static final String NEW_DEFAULT_HADOOP_CONFIG_PATH = "{{hadoop_conf_dir}}";
-
-  @Inject
-  DaoUtils daoUtils;
-
-  @Inject
-  private RepositoryVersionDAO repositoryVersionDAO;
-
-  @Inject
-  private ClusterDAO clusterDAO;
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   *
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog220(Injector injector) {
-    super(injector);
-    this.injector = injector;
-  }
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.2.0";
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getSourceVersion() {
-    return "2.1.2.1";
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-    executeUpgradeDDLUpdates();
-
-    // Alter the host_role_command table to allow host_id to be nullable
-    dbAccessor.alterColumn(HOST_ROLE_COMMAND_TABLE, new DBColumnInfo(HOST_ID_COL, Long.class, null, null, true));
-
-    addKerberosDescriptorTable();
-    executeBlueprintDDLUpdates();
-    executeStageDDLUpdates();
-  }
-
-  protected void executeUpgradeDDLUpdates() throws AmbariException, SQLException {
-    updateUpgradesDDL();
-  }
-
-  private void addKerberosDescriptorTable() throws SQLException {
-    List<DBAccessor.DBColumnInfo> columns = new ArrayList<>();
-    columns.add(new DBAccessor.DBColumnInfo(KERBEROS_DESCRIPTOR_NAME_COLUMN, String.class, 255, null, false));
-    columns.add(new DBAccessor.DBColumnInfo(KERBEROS_DESCRIPTOR_COLUMN, char[].class, null, null, false));
-
-    LOG.debug("Creating table [ {} ] with columns [ {} ] and primary key: [ {} ]", KERBEROS_DESCRIPTOR_TABLE, columns, KERBEROS_DESCRIPTOR_NAME_COLUMN);
-    dbAccessor.createTable(KERBEROS_DESCRIPTOR_TABLE, columns, KERBEROS_DESCRIPTOR_NAME_COLUMN);
-  }
-
-  private void executeBlueprintDDLUpdates() throws AmbariException, SQLException {
-    dbAccessor.addColumn(BLUEPRINT_TABLE, new DBAccessor.DBColumnInfo(SECURITY_TYPE_COLUMN,
-      String.class, 32, "NONE", false));
-    dbAccessor.addColumn(BLUEPRINT_TABLE, new DBAccessor.DBColumnInfo(SECURITY_DESCRIPTOR_REF_COLUMN,
-      String.class, null, null, true));
-  }
-
-  /**
-   * Updates the {@code stage} table by:
-   * <ul>
-   * <li>Adding the {@code supports_auto_skip_failure} column</li>
-   * </ul>
-   *
-   * @throws SQLException
-   */
-  protected void executeStageDDLUpdates() throws SQLException {
-    dbAccessor.addColumn(STAGE_TABLE,
-      new DBAccessor.DBColumnInfo("supports_auto_skip_failure", Integer.class, 1, 0, false));
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executePreDMLUpdates() throws AmbariException, SQLException {
-    // execute DDL updates
-    executeStackUpgradeDDLUpdates();
-
-    // DDL and DML mixed code, double check here
-    bootstrapRepoVersionForHDP21();
-
-    // execute DML updates, no DDL things after this line
-    executeUpgradePreDMLUpdates();
-  }
-
-  /**
-   * Updates the following columns on the {@value #UPGRADE_TABLE} table to
-   * default values:
-   * <ul>
-   * <li>{value {@link #DOWNGRADE_ALLOWED_COLUMN}}</li>
-   * <li>{value {@link #UPGRADE_SKIP_FAILURE_COLUMN}}</li>
-   * <li>{value {@link #UPGRADE_SKIP_SC_FAILURE_COLUMN}}</li>
-   * </ul>
-   *
-   * @throws AmbariException
-   * @throws SQLException
-   */
-  protected void executeUpgradePreDMLUpdates() throws AmbariException, SQLException {
-    UpgradeDAO upgradeDAO = injector.getInstance(UpgradeDAO.class);
-    List<UpgradeEntity> upgrades = upgradeDAO.findAll();
-    for (UpgradeEntity upgrade: upgrades){
-      if (upgrade.isDowngradeAllowed() == null) {
-        upgrade.setDowngradeAllowed(true);
-      }
-
-      // ensure that these are set to false for existing upgrades
-      upgrade.setAutoSkipComponentFailures(false);
-      upgrade.setAutoSkipServiceCheckFailures(false);
-
-      // apply changes
-      upgradeDAO.merge(upgrade);
-
-      LOG.info(String.format("Updated upgrade id %s, upgrade pack %s from version %s to %s",
-        upgrade.getId(), upgrade.getUpgradePackage(), upgrade.getFromRepositoryVersion().getVersion(),
-          upgrade.getToRepositoryVersion().getVersion()));
-    }
-
-    // make the columns nullable now that they have defaults
-    dbAccessor.setColumnNullable(UPGRADE_TABLE, DOWNGRADE_ALLOWED_COLUMN, false);
-    dbAccessor.setColumnNullable(UPGRADE_TABLE, UPGRADE_SKIP_FAILURE_COLUMN, false);
-    dbAccessor.setColumnNullable(UPGRADE_TABLE, UPGRADE_SKIP_SC_FAILURE_COLUMN, false);
-  }
-
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    addNewConfigurationsFromXml();
-    updateAlertDefinitions();
-    updateStormConfigs();
-    updateAMSConfigs();
-    updateHDFSConfigs();
-    updateHbaseEnvConfig();
-    updateFlumeEnvConfig();
-    updateHadoopEnv();
-    updateKafkaConfigs();
-    updateRangerEnvConfig();
-    updateRangerUgsyncSiteConfig();
-    updateZookeeperLog4j();
-    updateHiveConfig();
-    updateAccumuloConfigs();
-    updateKerberosDescriptorArtifacts();
-    updateKnoxTopology();
-  }
-
-  protected void updateKnoxTopology() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config topology = cluster.getDesiredConfigByType(TOPOLOGY_CONFIG);
-      if (topology != null) {
-        String content = topology.getProperties().get(CONTENT_PROPERTY);
-        if (content != null) {
-          Document topologyXml = convertStringToDocument(content);
-          if (topologyXml != null) {
-            Element root = topologyXml.getDocumentElement();
-            if (root != null)  {
-              NodeList providerNodes = root.getElementsByTagName("provider");
-              boolean authorizationProviderExists = false;
-              try {
-                for (int i = 0; i < providerNodes.getLength(); i++) {
-                  Node providerNode = providerNodes.item(i);
-                  NodeList childNodes = providerNode.getChildNodes();
-                  for (int k = 0; k < childNodes.getLength(); k++) {
-                    Node child = childNodes.item(k);
-                    child.normalize();
-                    String childTextContent = child.getTextContent();
-                    if (childTextContent != null && childTextContent.toLowerCase().equals("authorization")) {
-                      authorizationProviderExists = true;
-                      break;
-                    }
-                  }
-                  if (authorizationProviderExists) {
-                    break;
-                  }
-                }
-              } catch(Exception e) {
-                e.printStackTrace();
-                LOG.error("Error occurred during check 'authorization' provider already exists in topology." + e);
-                return;
-              }
-              if (!authorizationProviderExists) {
-                NodeList nodeList = root.getElementsByTagName("gateway");
-                if (nodeList != null && nodeList.getLength() > 0) {
-                  boolean rangerPluginEnabled = isConfigEnabled(cluster,
-                    AbstractUpgradeCatalog.CONFIGURATION_TYPE_RANGER_KNOX_PLUGIN_PROPERTIES,
-                    AbstractUpgradeCatalog.PROPERTY_RANGER_KNOX_PLUGIN_ENABLED);
-
-                  Node gatewayNode = nodeList.item(0);
-                  Element newProvider = topologyXml.createElement("provider");
-
-                  Element role = topologyXml.createElement("role");
-                  role.appendChild(topologyXml.createTextNode("authorization"));
-                  newProvider.appendChild(role);
-
-                  Element name = topologyXml.createElement("name");
-                  if (rangerPluginEnabled) {
-                    name.appendChild(topologyXml.createTextNode("XASecurePDPKnox"));
-                  } else {
-                    name.appendChild(topologyXml.createTextNode("AclsAuthz"));
-                  }
-                  newProvider.appendChild(name);
-
-                  Element enabled = topologyXml.createElement("enabled");
-                  enabled.appendChild(topologyXml.createTextNode("true"));
-                  newProvider.appendChild(enabled);
-
-
-                  gatewayNode.appendChild(newProvider);
-
-                  DOMSource topologyDomSource = new DOMSource(root);
-                  StringWriter writer = new StringWriter();
-                  try {
-                    Transformer transformer = TransformerFactory.newInstance().newTransformer();
-                    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
-                    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
-                    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
-                    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
-                    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
-                    transformer.transform(topologyDomSource, new StreamResult(writer));
-                  } catch (TransformerConfigurationException e) {
-                    e.printStackTrace();
-                    LOG.error("Unable to create transformer instance, to convert Document(XML) to String. " + e);
-                    return;
-                  } catch (TransformerException e) {
-                    e.printStackTrace();
-                    LOG.error("Unable to transform Document(XML) to StringWriter. " + e);
-                    return;
-                  }
-
-                  content = writer.toString();
-                  Map<String, String> updates = Collections.singletonMap(CONTENT_PROPERTY, content);
-                  updateConfigurationPropertiesForCluster(cluster, TOPOLOGY_CONFIG, updates, true, false);
-                }
-              }
-            }
-          }
-        }
-      }
-    }
-  }
-
-  /**
-   * Move the upgrade_package column from the repo_version table to the upgrade table as follows,
-   * add column upgrade_package to upgrade table as String 255 and nullable
-   * populate column in the upgrade table
-   * drop the column in the repo_version table
-   * make the column in the upgrade table non-nullable.
-   * This has to be called as part of DML and not DDL since the persistence service has to be started.
-   * @throws AmbariException
-   * @throws SQLException
-   */
-  @Transactional
-  protected void executeStackUpgradeDDLUpdates() throws SQLException, AmbariException {
-    final Configuration.DatabaseType databaseType = configuration.getDatabaseType();
-
-    // Add columns
-    if (!dbAccessor.tableHasColumn(UPGRADE_TABLE, UPGRADE_PACKAGE_COL)) {
-      LOG.info("Adding upgrade_package column to upgrade table.");
-      dbAccessor.addColumn(UPGRADE_TABLE, new DBColumnInfo(UPGRADE_PACKAGE_COL, String.class, 255, null, true));
-    }
-    if (!dbAccessor.tableHasColumn(UPGRADE_TABLE, UPGRADE_TYPE_COL)) {
-      LOG.info("Adding upgrade_type column to upgrade table.");
-      dbAccessor.addColumn(UPGRADE_TABLE, new DBColumnInfo(UPGRADE_TYPE_COL, String.class, 32, null, true));
-    }
-
-    // Populate values in upgrade table.
-    boolean success = populateUpgradeTable();
-
-    if (!success) {
-      throw new AmbariException("Errors found while populating the upgrade table with values for columns upgrade_type and upgrade_package.");
-    }
-
-    if (dbAccessor.tableHasColumn(REPO_VERSION_TABLE, UPGRADE_PACKAGE_COL)) {
-      LOG.info("Dropping upgrade_package column from repo_version table.");
-      dbAccessor.dropColumn(REPO_VERSION_TABLE, UPGRADE_PACKAGE_COL);
-
-      // Now, make the added column non-nullable
-      // Make the hosts id non-null after all the values are populated
-      LOG.info("Making upgrade_package column in the upgrade table non-nullable.");
-      if (databaseType == Configuration.DatabaseType.DERBY) {
-        // This is a workaround for UpgradeTest.java unit test
-        dbAccessor.executeQuery("ALTER TABLE " + UPGRADE_TABLE + " ALTER column " + UPGRADE_PACKAGE_COL + " NOT NULL");
-      } else {
-        dbAccessor.alterColumn(UPGRADE_TABLE, new DBColumnInfo(UPGRADE_PACKAGE_COL, String.class, 255, null, false));
-      }
-    }
-
-    if (dbAccessor.tableHasColumn(REPO_VERSION_TABLE, UPGRADE_TYPE_COL)) {
-      // Now, make the added column non-nullable
-      // Make the hosts id non-null after all the values are populated
-      LOG.info("Making upgrade_type column in the upgrade table non-nullable.");
-      if (databaseType == Configuration.DatabaseType.DERBY) {
-        // This is a workaround for UpgradeTest.java unit test
-        dbAccessor.executeQuery("ALTER TABLE " + UPGRADE_TABLE + " ALTER column " + UPGRADE_TYPE_COL + " NOT NULL");
-      } else {
-        dbAccessor.alterColumn(UPGRADE_TABLE, new DBColumnInfo(UPGRADE_TYPE_COL, String.class, 32, null, false));
-      }
-    }
-  }
-
-  /**
-   * Populate the upgrade table with values for the columns upgrade_type and upgrade_package.
-   * The upgrade_type will default to {@code org.apache.ambari.server.state.stack.upgrade.UpgradeType.ROLLING}
-   * whereas the upgrade_package will be calculated.
-   * @return {@code} true on success, and {@code} false otherwise.
-   */
-  private boolean populateUpgradeTable() {
-    boolean success = true;
-    Statement statement = null;
-    ResultSet rs = null;
-    try {
-      statement = dbAccessor.getConnection().createStatement();
-      if (statement != null) {
-        // Need to use SQL since the schema is changing and some of the columns have not yet been added..
-        rs = statement.executeQuery("SELECT upgrade_id, cluster_id, from_version, to_version, direction, upgrade_package, upgrade_type FROM upgrade");
-        if (rs != null) {
-          try {
-            while (rs.next()) {
-              final long upgradeId = rs.getLong("upgrade_id");
-              final long clusterId = rs.getLong("cluster_id");
-              final String fromVersion = rs.getString("from_version");
-              final String toVersion = rs.getString("to_version");
-              final Direction direction = Direction.valueOf(rs.getString("direction"));
-              // These two values are likely null.
-              String upgradePackage = rs.getString("upgrade_package");
-              String upgradeType = rs.getString("upgrade_type");
-
-              LOG.info(MessageFormat.format("Populating rows for the upgrade table record with " +
-                  "upgrade_id: {0,number,#}, cluster_id: {1,number,#}, from_version: {2}, to_version: {3}, direction: {4}",
-                upgradeId, clusterId, fromVersion, toVersion, direction));
-
-              // Set all upgrades that have been done so far to type "rolling"
-              if (StringUtils.isEmpty(upgradeType)) {
-                LOG.info("Updating the record's upgrade_type to " + UpgradeType.ROLLING);
-                dbAccessor.executeQuery("UPDATE upgrade SET upgrade_type = '" + UpgradeType.ROLLING + "' WHERE upgrade_id = " + upgradeId);
-              }
-
-              if (StringUtils.isEmpty(upgradePackage)) {
-                String version = null;
-                StackEntity stack = null;
-
-                if (direction == Direction.UPGRADE) {
-                  version = toVersion;
-                } else if (direction == Direction.DOWNGRADE) {
-                  // TODO AMBARI-12698, this is going to be a problem.
-                  // During a downgrade, the "to_version" is overwritten to the source version, but the "from_version"
-                  // doesn't swap. E.g.,
-                  //  upgrade_id | from_version |  to_version  | direction
-                  // ------------+--------------+--------------+----------
-                  //           1 | 2.2.6.0-2800 | 2.3.0.0-2557 | UPGRADE
-                  //           2 | 2.2.6.0-2800 | 2.2.6.0-2800 | DOWNGRADE
-                  version = fromVersion;
-                }
-
-                ClusterEntity cluster = clusterDAO.findById(clusterId);
-
-                if (null != cluster) {
-                  stack = cluster.getDesiredStack();
-                  upgradePackage = calculateUpgradePackage(stack, version);
-                } else {
-                  LOG.error("Could not find a cluster with cluster_id " + clusterId);
-                }
-
-                if (!StringUtils.isEmpty(upgradePackage)) {
-                  LOG.info("Updating the record's upgrade_package to " + upgradePackage);
-                  dbAccessor.executeQuery("UPDATE upgrade SET upgrade_package = '" + upgradePackage + "' WHERE upgrade_id = " + upgradeId);
-                } else {
-                  success = false;
-                  LOG.error("Unable to populate column upgrade_package for record in table upgrade with id " + upgradeId);
-                }
-              }
-            }
-          } catch (Exception e) {
-            success = false;
-            e.printStackTrace();
-            LOG.error("Unable to populate the upgrade_type and upgrade_package columns of the upgrade table. " + e);
-          }
-        }
-      }
-    } catch (Exception e) {
-      success = false;
-      e.printStackTrace();
-      LOG.error("Failed to retrieve records from the upgrade table to populate the upgrade_type and upgrade_package columns. Exception: " + e);
-    } finally {
-      try {
-        if (rs != null) {
-          rs.close();
-        }
-        if (statement != null) {
-          statement.close();
-        }
-      } catch (SQLException e) {
-        ;
-      }
-    }
-    return success;
-  }
-
-  /**
-   * Find the single Repo Version for the given stack and version, and return
-   * its upgrade_package column. Because the upgrade_package column is going to
-   * be removed from this entity, must use raw SQL instead of the entity class.
-   * <p/>
-   * It's possible that there is an invalid version listed in the upgrade table.
-   * For example:
-   *
-   * <pre>
-   * upgrade
-   * 1 2 1295  2.2.0.0-2041  2.2.4.2-2     UPGRADE
-   * 2 2 1296  2.2.0.0-2041  2.2.0.0-2041  DOWNGRADE
-   * 3 2 1299  2.2.0.0-2041  2.2.4.2       UPGRADE
-   *
-   * repo_version
-   * 1  2.2.0.0-2041  HDP-2.2.0.0-2041  upgrade-2.2
-   * 2  2.2.4.2-2     HDP-2.2.4.2-2     upgrade-2.2
-   * </pre>
-   *
-   * Notice that it's possible for the {@code upgrade} table to include entries
-   * for a repo version which does not exist; {@code 2.2.4.2}. In these cases,
-   * this method will attempt a "best match".
-   *
-   * @param stack
-   *          Stack
-   * @param version
-   *          Stack version
-   * @return The value of the upgrade_package column, or null if not found.
-   */
-
-  private String calculateUpgradePackage(StackEntity stack, String version) {
-    String upgradePackage = null;
-    // Find the corresponding repo_version, and extract its upgrade_package
-    if (null != version && null != stack) {
-      RepositoryVersionEntity repoVersion = repositoryVersionDAO.findByStackNameAndVersion(stack.getStackName(), version);
-
-      // a null repoVersion means there's mismatch between the upgrade and repo_version table;
-      // use a best-guess approach based on the Stack
-      if( null == repoVersion ){
-        List<RepositoryVersionEntity> bestMatches = repositoryVersionDAO.findByStack(new StackId(stack));
-        if (!bestMatches.isEmpty()) {
-          repoVersion = bestMatches.get(0);
-        }
-      }
-
-      // our efforts have failed; we have no idea what to use; return null as per the contract of the method
-      if( null == repoVersion ) {
-        return null;
-      }
-
-      Statement statement = null;
-      ResultSet rs = null;
-      try {
-        statement = dbAccessor.getConnection().createStatement();
-        if (statement != null) {
-          // Need to use SQL since the schema is changing and the entity will no longer have the upgrade_package column.
-          rs = statement.executeQuery("SELECT upgrade_package FROM repo_version WHERE repo_version_id = " + repoVersion.getId());
-          if (rs != null && rs.next()) {
-            upgradePackage = rs.getString("upgrade_package");
-          }
-        }
-      } catch (Exception e) {
-        LOG.error("Failed to retrieve upgrade_package for repo_version record with id " + repoVersion.getId() + ". Exception: " + e.getMessage());
-      } finally {
-        try {
-          if (rs != null) {
-            rs.close();
-          }
-          if (statement != null) {
-            statement.close();
-          }
-        } catch (SQLException e) {
-          ;
-        }
-      }
-    }
-    return upgradePackage;
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void updateKerberosDescriptorArtifact(ArtifactDAO artifactDAO, ArtifactEntity artifactEntity) throws AmbariException {
-    if (artifactEntity != null) {
-      Map<String, Object> data = artifactEntity.getArtifactData();
-
-      if (data != null) {
-        final KerberosDescriptor kerberosDescriptor = new KerberosDescriptorFactory().createInstance(data);
-
-        if (kerberosDescriptor != null) {
-          KerberosServiceDescriptor hdfsService = kerberosDescriptor.getService("HDFS");
-          if(hdfsService != null) {
-            // before 2.2.0 hdfs indentity expected to be in HDFS service
-            KerberosIdentityDescriptor hdfsIdentity = hdfsService.getIdentity("hdfs");
-            if (hdfsIdentity != null) {
-              KerberosComponentDescriptor namenodeComponent = hdfsService.getComponent("NAMENODE");
-              hdfsIdentity.setName("hdfs");
-              hdfsService.removeIdentity("hdfs");
-              namenodeComponent.putIdentity(hdfsIdentity);
-            }
-          }
-          updateKerberosDescriptorIdentityReferences(kerberosDescriptor, "/HDFS/hdfs", "/HDFS/NAMENODE/hdfs");
-          updateKerberosDescriptorIdentityReferences(kerberosDescriptor.getServices(), "/HDFS/hdfs", "/HDFS/NAMENODE/hdfs");
-
-          artifactEntity.setArtifactData(kerberosDescriptor.toMap());
-          artifactDAO.merge(artifactEntity);
-        }
-      }
-    }
-  }
-
-  /**
-   * If still on HDP 2.1, then no repo versions exist, so need to bootstrap the HDP 2.1 repo version,
-   * If still on HDP 2.1, then no repo versions exist, so need to bootstrap the HDP 2.1 repo version,
-   * and mark it as CURRENT in the cluster_version table for the cluster, as well as the host_version table
-   * for all hosts.
-   */
-  @Transactional
-  public void bootstrapRepoVersionForHDP21() throws AmbariException, SQLException {
-    final String hardcodedInitialVersion = "2.1.0.0-0001";
-    AmbariManagementController amc = injector.getInstance(AmbariManagementController.class);
-    AmbariMetaInfo ambariMetaInfo = amc.getAmbariMetaInfo();
-    StackDAO stackDAO = injector.getInstance(StackDAO.class);
-    RepositoryVersionHelper repositoryVersionHelper = injector.getInstance(RepositoryVersionHelper.class);
-    RepositoryVersionDAO repositoryVersionDAO = injector.getInstance(RepositoryVersionDAO.class);
-    ClusterVersionDAO clusterVersionDAO = injector.getInstance(ClusterVersionDAO.class);
-    HostVersionDAO hostVersionDAO = injector.getInstance(HostVersionDAO.class);
-
-    Clusters clusters = amc.getClusters();
-    if (clusters == null) {
-      LOG.error("Unable to get Clusters entity.");
-      return;
-    }
-
-    for (Cluster cluster : clusters.getClusters().values()) {
-      ClusterEntity clusterEntity = clusterDAO.findByName(cluster.getClusterName());
-      final StackId stackId = cluster.getCurrentStackVersion();
-      LOG.info(MessageFormat.format("Analyzing cluster {0}, currently at stack {1} and version {2}",
-        cluster.getClusterName(), stackId.getStackName(), stackId.getStackVersion()));
-
-      if (stackId.getStackName().equalsIgnoreCase("HDP") && stackId.getStackVersion().equalsIgnoreCase("2.1")) {
-        final StackInfo stackInfo = ambariMetaInfo.getStack(stackId.getStackName(), stackId.getStackVersion());
-        StackEntity stackEntity = stackDAO.find(stackId.getStackName(), stackId.getStackVersion());
-
-        LOG.info("Bootstrapping the versions since using HDP-2.1");
-
-        // The actual value is not known, so use this.
-        String displayName = stackId.getStackName() + "-" + hardcodedInitialVersion;
-
-        // However, the Repo URLs should be correct.
-        String operatingSystems = repositoryVersionHelper.serializeOperatingSystems(stackInfo.getRepositories());
-
-        // Create the Repo Version if it doesn't already exist.
-        RepositoryVersionEntity repoVersionEntity = repositoryVersionDAO.findByDisplayName(displayName);
-        if (null != repoVersionEntity) {
-          LOG.info(MessageFormat.format("A Repo Version already exists with Display Name: {0}", displayName));
-        } else {
-          final long repoVersionIdSeq = repositoryVersionDAO.findMaxId("id");
-          // Safe to attempt to add the sequence if it doesn't exist already.
-          addSequence("repo_version_id_seq", repoVersionIdSeq, false);
-
-          repoVersionEntity = repositoryVersionDAO.create(
-            stackEntity, hardcodedInitialVersion, displayName, operatingSystems);
-          LOG.info(MessageFormat.format("Created Repo Version with ID: {0,number,#}\n, Display Name: {1}, Repo URLs: {2}\n",
-            repoVersionEntity.getId(), displayName, operatingSystems));
-        }
-
-        // Create the Cluster Version if it doesn't already exist.
-        ClusterVersionEntity clusterVersionEntity = clusterVersionDAO.findByClusterAndStackAndVersion(cluster.getClusterName(),
-          stackId, hardcodedInitialVersion);
-
-        if (null != clusterVersionEntity) {
-          LOG.info(MessageFormat.format("A Cluster Version version for cluster: {0}, version: {1}, already exists; its state is {2}.",
-            cluster.getClusterName(), clusterVersionEntity.getRepositoryVersion().getVersion(), clusterVersionEntity.getState()));
-
-          // If there are not CURRENT cluster versions, make this one the CURRENT one.
-          if (clusterVersionEntity.getState() != RepositoryVersionState.CURRENT &&
-            clusterVersionDAO.findByClusterAndState(cluster.getClusterName(), RepositoryVersionState.CURRENT).isEmpty()) {
-            clusterVersionEntity.setState(RepositoryVersionState.CURRENT);
-            clusterVersionDAO.merge(clusterVersionEntity);
-          }
-        } else {
-          final long clusterVersionIdSeq = clusterVersionDAO.findMaxId("id");
-          // Safe to attempt to add the sequence if it doesn't exist already.
-          addSequence("cluster_version_id_seq", clusterVersionIdSeq, false);
-
-          clusterVersionEntity = clusterVersionDAO.create(clusterEntity, repoVersionEntity, RepositoryVersionState.CURRENT,
-            System.currentTimeMillis(), System.currentTimeMillis(), "admin");
-          LOG.info(MessageFormat.format("Created Cluster Version with ID: {0,number,#}, cluster: {1}, version: {2}, state: {3}.",
-            clusterVersionEntity.getId(), cluster.getClusterName(), clusterVersionEntity.getRepositoryVersion().getVersion(),
-            clusterVersionEntity.getState()));
-        }
-
-        // Create the Host Versions if they don't already exist.
-        Collection<HostEntity> hosts = clusterEntity.getHostEntities();
-        boolean addedAtLeastOneHost = false;
-        if (null != hosts && !hosts.isEmpty()) {
-          for (HostEntity hostEntity : hosts) {
-            HostVersionEntity hostVersionEntity = hostVersionDAO.findByClusterStackVersionAndHost(cluster.getClusterName(),
-              stackId, hardcodedInitialVersion, hostEntity.getHostName());
-
-            if (null != hostVersionEntity) {
-              LOG.info(MessageFormat.format("A Host Version version for cluster: {0}, version: {1}, host: {2}, already exists; its state is {3}.",
-                cluster.getClusterName(), hostVersionEntity.getRepositoryVersion().getVersion(),
-                hostEntity.getHostName(), hostVersionEntity.getState()));
-
-              if (hostVersionEntity.getState() != RepositoryVersionState.CURRENT &&
-                hostVersionDAO.findByClusterHostAndState(cluster.getClusterName(), hostEntity.getHostName(),
-                  RepositoryVersionState.CURRENT).isEmpty()) {
-                hostVersionEntity.setState(RepositoryVersionState.CURRENT);
-                hostVersionDAO.merge(hostVersionEntity);
-              }
-            } else {
-              // This should only be done the first time.
-              if (!addedAtLeastOneHost) {
-                final long hostVersionIdSeq = hostVersionDAO.findMaxId("id");
-                // Safe to attempt to add the sequence if it doesn't exist already.
-                addSequence("host_version_id_seq", hostVersionIdSeq, false);
-                addedAtLeastOneHost = true;
-              }
-
-              hostVersionEntity = new HostVersionEntity(hostEntity, repoVersionEntity, RepositoryVersionState.CURRENT);
-              hostVersionDAO.create(hostVersionEntity);
-              LOG.info(MessageFormat.format("Created Host Version with ID: {0,number,#}, cluster: {1}, version: {2}, host: {3}, state: {4}.",
-                hostVersionEntity.getId(), cluster.getClusterName(), hostVersionEntity.getRepositoryVersion().getVersion(),
-                hostEntity.getHostName(), hostVersionEntity.getState()));
-            }
-          }
-        } else {
-          LOG.info(MessageFormat.format("Not inserting any Host Version records since cluster {0} does not have any hosts.",
-            cluster.getClusterName()));
-        }
-      }
-    }
-  }
-
-  /**
-   * Adds the following columns to the {@value #UPGRADE_TABLE} table:
-   * <ul>
-   * <li>{@value #DOWNGRADE_ALLOWED_COLUMN}</li>
-   * <li>{@value #UPGRADE_SKIP_FAILURE_COLUMN}</li>
-   * <li>{@value #UPGRADE_SKIP_SC_FAILURE_COLUMN}</li>
-   * </ul>
-   *
-   * @throws SQLException
-   */
-  protected void updateUpgradesDDL() throws SQLException{
-    dbAccessor.addColumn(UPGRADE_TABLE, new DBColumnInfo(DOWNGRADE_ALLOWED_COLUMN, Short.class, 1, null, true));
-    dbAccessor.addColumn(UPGRADE_TABLE, new DBColumnInfo(UPGRADE_SKIP_FAILURE_COLUMN, Short.class, 1, null, true));
-    dbAccessor.addColumn(UPGRADE_TABLE, new DBColumnInfo(UPGRADE_SKIP_SC_FAILURE_COLUMN, Short.class, 1, null, true));
-  }
-
-  /**
-   * Modifies the JSON of some of the alert definitions which have changed
-   * between Ambari versions.
-   */
-  protected void updateAlertDefinitions() {
-    LOG.info("Updating alert definitions.");
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
-    for (final Cluster cluster : clusterMap.values()) {
-      long clusterID = cluster.getClusterId();
-
-      final AlertDefinitionEntity journalNodeProcessAlertDefinitionEntity = alertDefinitionDAO.findByName(
-        clusterID, "journalnode_process");
-      final AlertDefinitionEntity hostDiskUsageAlertDefinitionEntity = alertDefinitionDAO.findByName(
-          clusterID, "ambari_agent_disk_usage");
-
-      if (journalNodeProcessAlertDefinitionEntity != null) {
-        String source = journalNodeProcessAlertDefinitionEntity.getSource();
-
-        journalNodeProcessAlertDefinitionEntity.setSource(modifyJournalnodeProcessAlertSource(source));
-        journalNodeProcessAlertDefinitionEntity.setSourceType(SourceType.WEB);
-        journalNodeProcessAlertDefinitionEntity.setHash(UUID.randomUUID().toString());
-
-        alertDefinitionDAO.merge(journalNodeProcessAlertDefinitionEntity);
-        LOG.info("journalnode_process alert definition was updated.");
-      }
-
-      if (hostDiskUsageAlertDefinitionEntity != null) {
-        hostDiskUsageAlertDefinitionEntity.setDescription("This host-level alert is triggered if the amount of disk space " +
-            "used goes above specific thresholds. The default threshold values are 50% for WARNING and 80% for CRITICAL.");
-        hostDiskUsageAlertDefinitionEntity.setLabel("Host Disk Usage");
-
-        alertDefinitionDAO.merge(hostDiskUsageAlertDefinitionEntity);
-        LOG.info("ambari_agent_disk_usage alert definition was updated.");
-      }
-
-    }
-  }
-
-  /**
-   * Modifies type of the journalnode_process alert to WEB.
-   * Changes reporting text and uri according to the WEB type.
-   * Removes default_port property.
-   */
-  String modifyJournalnodeProcessAlertSource(String source) {
-    JsonObject rootJson = new JsonParser().parse(source).getAsJsonObject();
-
-    rootJson.remove("type");
-    rootJson.addProperty("type", "WEB");
-
-    rootJson.remove("default_port");
-
-    rootJson.remove("uri");
-    JsonObject uriJson = new JsonObject();
-    uriJson.addProperty("http", "{{hdfs-site/dfs.journalnode.http-address}}");
-    uriJson.addProperty("https", "{{hdfs-site/dfs.journalnode.https-address}}");
-    uriJson.addProperty("kerberos_keytab", "{{hdfs-site/dfs.web.authentication.kerberos.keytab}}");
-    uriJson.addProperty("kerberos_principal", "{{hdfs-site/dfs.web.authentication.kerberos.principal}}");
-    uriJson.addProperty("https_property", "{{hdfs-site/dfs.http.policy}}");
-    uriJson.addProperty("https_property_value", "HTTPS_ONLY");
-    uriJson.addProperty("connection_timeout", 5.0);
-    rootJson.add("uri", uriJson);
-
-    rootJson.getAsJsonObject("reporting").getAsJsonObject("ok").remove("text");
-    rootJson.getAsJsonObject("reporting").getAsJsonObject("ok").addProperty(
-      "text", "HTTP {0} response in {2:.3f}s");
-
-    rootJson.getAsJsonObject("reporting").getAsJsonObject("warning").remove("text");
-    rootJson.getAsJsonObject("reporting").getAsJsonObject("warning").addProperty(
-      "text", "HTTP {0} response from {1} in {2:.3f}s ({3})");
-    rootJson.getAsJsonObject("reporting").getAsJsonObject("warning").remove("value");
-
-    rootJson.getAsJsonObject("reporting").getAsJsonObject("critical").remove("text");
-    rootJson.getAsJsonObject("reporting").getAsJsonObject("critical").addProperty("text",
-      "Connection failed to {1} ({3})");
-    rootJson.getAsJsonObject("reporting").getAsJsonObject("critical").remove("value");
-
-    return rootJson.toString();
-  }
-
-  protected void updateHadoopEnv() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config hadoopEnvConfig = cluster.getDesiredConfigByType(HADOOP_ENV_CONFIG);
-      if (hadoopEnvConfig != null) {
-        String content = hadoopEnvConfig.getProperties().get(CONTENT_PROPERTY);
-        if (content != null) {
-          content += HADOOP_ENV_CONTENT_TO_APPEND;
-          Map<String, String> updates = Collections.singletonMap(CONTENT_PROPERTY, content);
-          updateConfigurationPropertiesForCluster(cluster, HADOOP_ENV_CONFIG, updates, true, false);
-        }
-      }
-    }
-  }
-
-  protected void updateHDFSConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(
-      AmbariManagementController.class);
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(ambariManagementController.getClusters());
-
-    for (final Cluster cluster : clusterMap.values()) {
-      // Remove dfs.namenode.rpc-address property when NN HA is enabled
-      if (cluster.getDesiredConfigByType(HDFS_SITE_CONFIG) != null && isNNHAEnabled(cluster)) {
-        Set<String> removePropertiesSet = new HashSet<>();
-        removePropertiesSet.add("dfs.namenode.rpc-address");
-        removeConfigurationPropertiesFromCluster(cluster, HDFS_SITE_CONFIG, removePropertiesSet);
-      }
-    }
-  }
-
-  protected void updateZookeeperLog4j() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config zookeeperLog4jConfig = cluster.getDesiredConfigByType(ZOOKEEPER_LOG4J_CONFIG);
-      if (zookeeperLog4jConfig != null) {
-        String content = zookeeperLog4jConfig.getProperties().get(CONTENT_PROPERTY);
-        if (content != null) {
-          content = content.replaceAll("[\n^]\\s*log4j\\.rootLogger\\s*=\\s*INFO\\s*,\\s*CONSOLE", "\nlog4j.rootLogger=INFO, ROLLINGFILE");
-          Map<String, String> updates = Collections.singletonMap(CONTENT_PROPERTY, content);
-          updateConfigurationPropertiesForCluster(cluster, ZOOKEEPER_LOG4J_CONFIG, updates, true, false);
-        }
-      }
-    }
-  }
-
-  protected void updateStormConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config stormSiteProps = cluster.getDesiredConfigByType(STORM_SITE);
-      if (stormSiteProps != null) {
-        Map<String, String> updates = new HashMap<>();
-
-        String nimbusMonitorFreqSecs = stormSiteProps.getProperties().get(NIMBS_MONITOR_FREQ_SECS_PROPERTY);
-        if (nimbusMonitorFreqSecs != null && nimbusMonitorFreqSecs.equals("10")) {
-          updates.put(NIMBS_MONITOR_FREQ_SECS_PROPERTY, "120");
-        }
-
-        Service amsService = null;
-        try {
-          amsService = cluster.getService("AMBARI_METRICS");
-        } catch(AmbariException ambariException) {
-          LOG.info("AMBARI_METRICS service not found in cluster while updating storm-site properties");
-        }
-        String metricsReporter = stormSiteProps.getProperties().get(STORM_METRICS_REPORTER);
-        if (amsService != null && StringUtils.isEmpty(metricsReporter)) {
-          updates.put(STORM_METRICS_REPORTER, "org.apache.hadoop.metrics2.sink.storm.StormTimelineMetricsReporter");
-        }
-
-        updateConfigurationPropertiesForCluster(cluster, STORM_SITE, updates, true, false);
-      }
-    }
-  }
-
-  protected void updateHiveConfig() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config hiveSiteConfig = cluster.getDesiredConfigByType(HIVE_SITE_CONFIG);
-      if (hiveSiteConfig != null) {
-        String hiveServer2OperationLogLocation = hiveSiteConfig.getProperties().get(HIVE_SERVER2_OPERATION_LOG_LOCATION_PROPERTY);
-        if (hiveServer2OperationLogLocation != null && hiveServer2OperationLogLocation.equals("${system:java.io.tmpdir}/${system:user.name}/operation_logs")) {
-          Map<String, String> updates = Collections.singletonMap(HIVE_SERVER2_OPERATION_LOG_LOCATION_PROPERTY, "/tmp/hive/operation_logs");
-          updateConfigurationPropertiesForCluster(cluster, HIVE_SITE_CONFIG, updates, true, false);
-        }
-      }
-      StackId stackId = cluster.getCurrentStackVersion();
-      boolean isStackNotLess23 = (stackId != null && stackId.getStackName().equals("HDP") &&
-              VersionUtils.compareVersions(stackId.getStackVersion(), "2.3") >= 0);
-
-      Config hiveEnvConfig = cluster.getDesiredConfigByType(HIVE_ENV_CONFIG);
-      if (hiveEnvConfig != null) {
-        Map<String, String> hiveEnvProps = new HashMap<>();
-        String content = hiveEnvConfig.getProperties().get(CONTENT_PROPERTY);
-        // For HDP-2.3 we need to add hive heap size management to content,
-        // for others we need to update content
-        if(content != null) {
-          if(isStackNotLess23) {
-            content = updateHiveEnvContentHDP23(content);
-          } else {
-            content = updateHiveEnvContent(content);
-          }
-          hiveEnvProps.put(CONTENT_PROPERTY, content);
-          updateConfigurationPropertiesForCluster(cluster, HIVE_ENV_CONFIG, hiveEnvProps, true, true);
-        }
-      }
-
-    }
-  }
-
-  protected void updateHbaseEnvConfig() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    boolean updateConfig = false;
-
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      StackId stackId = cluster.getCurrentStackVersion();
-      Config hbaseEnvConfig = cluster.getDesiredConfigByType(HBASE_ENV_CONFIG);
-      if (hbaseEnvConfig != null) {
-        String content = hbaseEnvConfig.getProperties().get(CONTENT_PROPERTY);
-        if (content != null) {
-          if (!content.contains("-Djava.io.tmpdir")) {
-            content += "\n\nexport HBASE_OPTS=\"${HBASE_OPTS} -Djava.io.tmpdir={{java_io_tmpdir}}\"";
-            updateConfig = true;
-          }
-          if (stackId != null && stackId.getStackName().equals("HDP") &&
-              VersionUtils.compareVersions(stackId.getStackVersion(), "2.2") >= 0) {
-            if (!content.contains("MaxDirectMemorySize={{hbase_max_direct_memory_size}}m")) {
-              String newPartOfContent = "\n\nexport HBASE_REGIONSERVER_OPTS=\"$HBASE_REGIONSERVER_OPTS {% if hbase_max_direct_memory_size %} -XX:MaxDirectMemorySize={{hbase_max_direct_memory_size}}m {% endif %}\"\n\n";
-              content += newPartOfContent;
-              updateConfig = true;
-            }
-            if (updateConfig) {
-              Map<String, String> updates = Collections.singletonMap(CONTENT_PROPERTY, content);
-              updateConfigurationPropertiesForCluster(cluster, HBASE_ENV_CONFIG, updates, true, false);
-            }
-          }
-        }
-      }
-    }
-  }
-
-  protected void updateFlumeEnvConfig() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config flumeEnvConfig = cluster.getDesiredConfigByType(FLUME_ENV_CONFIG);
-      if (flumeEnvConfig != null) {
-        String content = flumeEnvConfig.getProperties().get(CONTENT_PROPERTY);
-        if (content != null && !content.contains("/usr/lib/flume/lib/ambari-metrics-flume-sink.jar")) {
-          String newPartOfContent = "\n\n" +
-            "# Note that the Flume conf directory is always included in the classpath.\n" +
-            "# Add flume sink to classpath\n" +
-            "if [ -e \"/usr/lib/flume/lib/ambari-metrics-flume-sink.jar\" ]; then\n" +
-            "  export FLUME_CLASSPATH=$FLUME_CLASSPATH:/usr/lib/flume/lib/ambari-metrics-flume-sink.jar\n" +
-            "fi\n";
-          content += newPartOfContent;
-          Map<String, String> updates = Collections.singletonMap(CONTENT_PROPERTY, content);
-          updateConfigurationPropertiesForCluster(cluster, FLUME_ENV_CONFIG, updates, true, false);
-        }
-      }
-    }
-  }
-
-  protected void updateAMSConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-
-          Config amsHbaseEnv = cluster.getDesiredConfigByType(AMS_HBASE_ENV);
-          if (amsHbaseEnv != null) {
-            Map<String, String> amsHbaseEnvProperties = amsHbaseEnv.getProperties();
-            String content = amsHbaseEnvProperties.get("content");
-            Map<String, String> newProperties = new HashMap<>();
-            newProperties.put("content", updateAmsHbaseEnvContent(content));
-            updateConfigurationPropertiesForCluster(cluster, AMS_HBASE_ENV, newProperties, true, true);
-          }
-
-          Config amsEnv = cluster.getDesiredConfigByType(AMS_ENV);
-          if (amsEnv != null) {
-            Map<String, String> amsEnvProperties = amsEnv.getProperties();
-            String content = amsEnvProperties.get("content");
-            Map<String, String> newProperties = new HashMap<>();
-            newProperties.put("content", updateAmsEnvContent(content));
-            updateConfigurationPropertiesForCluster(cluster, AMS_ENV, newProperties, true, true);
-          }
-
-          Config amsSite = cluster.getDesiredConfigByType(AMS_SITE);
-          if (amsSite != null) {
-            Map<String, String> currentAmsSiteProperties = amsSite.getProperties();
-            Map<String, String> newProperties = new HashMap<>();
-
-            //Changed AMS result set limit from 5760 to 15840.
-            if(currentAmsSiteProperties.containsKey("timeline.metrics.service.default.result.limit") &&
-              currentAmsSiteProperties.get("timeline.metrics.service.default.result.limit").equals(String.valueOf(5760))) {
-              LOG.info("Updating timeline.metrics.service.default.result.limit to 15840");
-              newProperties.put("timeline.metrics.service.default.result.limit", String.valueOf(15840));
-            }
-
-            //Interval
-            newProperties.put("timeline.metrics.cluster.aggregator.second.interval", String.valueOf(120));
-            newProperties.put("timeline.metrics.cluster.aggregator.minute.interval", String.valueOf(300));
-            newProperties.put("timeline.metrics.host.aggregator.minute.interval", String.valueOf(300));
-
-            //ttl
-            newProperties.put("timeline.metrics.cluster.aggregator.second.ttl", String.valueOf(2592000));
-            newProperties.put("timeline.metrics.cluster.aggregator.minute.ttl", String.valueOf(7776000));
-
-            //checkpoint
-            newProperties.put("timeline.metrics.cluster.aggregator.second.checkpointCutOffMultiplier", String.valueOf(2));
-
-            //disabled
-            newProperties.put("timeline.metrics.cluster.aggregator.second.disabled", String.valueOf(false));
-
-            //Add compaction policy property
-            newProperties.put(TIMELINE_METRICS_HBASE_FIFO_COMPACTION_ENABLED, String.valueOf(true));
-
-            updateConfigurationPropertiesForCluster(cluster, AMS_SITE, newProperties, true, true);
-          }
-
-          Config amsHbaseSite = cluster.getDesiredConfigByType(AMS_HBASE_SITE);
-          if (amsHbaseSite != null) {
-            Map<String, String> amsHbaseSiteProperties = amsHbaseSite.getProperties();
-            Map<String, String> newProperties = new HashMap<>();
-
-            String zkTimeout = amsHbaseSiteProperties.get(AMS_HBASE_SITE_ZK_TIMEOUT_PROPERTY);
-            // if old default, set new default
-            if ("20000".equals(zkTimeout)) {
-              newProperties.put(AMS_HBASE_SITE_ZK_TIMEOUT_PROPERTY, "120000");
-            }
-
-            //Adding hbase.normalizer.period to upgrade
-            if(!amsHbaseSiteProperties.containsKey(AMS_HBASE_SITE_NORMALIZER_ENABLED_PROPERTY)) {
-              LOG.info("Enabling " + AMS_HBASE_SITE_NORMALIZER_ENABLED_PROPERTY);
-              newProperties.put(AMS_HBASE_SITE_NORMALIZER_ENABLED_PROPERTY, String.valueOf(true));
-            }
-
-            if(!amsHbaseSiteProperties.containsKey(AMS_HBASE_SITE_NORMALIZER_PERIOD_PROPERTY)) {
-              LOG.info("Updating " + AMS_HBASE_SITE_NORMALIZER_PERIOD_PROPERTY);
-              newProperties.put(AMS_HBASE_SITE_NORMALIZER_PERIOD_PROPERTY, String.valueOf(600000));
-            }
-
-            if(!amsHbaseSiteProperties.containsKey(AMS_HBASE_SITE_NORMALIZER_CLASS_PROPERTY)) {
-              LOG.info("Updating " + AMS_HBASE_SITE_NORMALIZER_CLASS_PROPERTY);
-              newProperties.put(AMS_HBASE_SITE_NORMALIZER_CLASS_PROPERTY,
-                "org.apache.hadoop.hbase.master.normalizer.SimpleRegionNormalizer");
-            }
-            updateConfigurationPropertiesForCluster(cluster, AMS_HBASE_SITE, newProperties, true, true);
-          }
-        }
-      }
-    }
-
-  }
-
-  protected String updateAmsHbaseEnvContent(String content) {
-    if (content == null) {
-      return null;
-    }
-    String regSearch = "export HBASE_HEAPSIZE=";
-    String replacement = "#export HBASE_HEAPSIZE=";
-    content = content.replaceAll(regSearch, replacement);
-    content += "\n" +
-      "# The maximum amount of heap to use for hbase shell.\n" +
-      "export HBASE_SHELL_OPTS=\"-Xmx256m\"\n";
-    return content;
-  }
-
-  protected String updateAmsEnvContent(String content) {
-    if (content == null) {
-      return null;
-    }
-    if (!content.contains("AMS_COLLECTOR_GC_OPTS")) {
-      content += "\n" +
-        "# AMS Collector GC options\n" +
-        "export AMS_COLLECTOR_GC_OPTS=\"-XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70 " +
-        "-XX:+UseCMSInitiatingOccupancyOnly -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps " +
-        "-XX:+UseGCLogFileRotation -XX:GCLogFileSize=10M " +
-        "-Xloggc:{{ams_collector_log_dir}}/collector-gc.log-`date +'%Y%m%d%H%M'`\"\n" +
-        "export AMS_COLLECTOR_OPTS=\"$AMS_COLLECTOR_OPTS $AMS_COLLECTOR_GC_OPTS\"\n";
-    }
-
-    if (!content.contains("AMS_HBASE_NORMALIZER_ENABLED")) {
-      content += "\n" +
-        "# HBase normalizer enabled\n" +
-        "export AMS_HBASE_NORMALIZER_ENABLED={{ams_hbase_normalizer_enabled}}\n";
-    }
-
-    if (!content.contains("AMS_HBASE_FIFO_COMPACTION_ENABLED")) {
-      content += "\n" +
-        "# HBase compaction policy enabled\n" +
-        "export AMS_HBASE_FIFO_COMPACTION_ENABLED={{ams_hbase_fifo_compaction_enabled}}\n";
-    }
-
-    return content;
-  }
-
-  protected void updateKafkaConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-          Set<String> installedServices = cluster.getServices().keySet();
-          Config kafkaBroker = cluster.getDesiredConfigByType(KAFKA_BROKER);
-          if (kafkaBroker != null) {
-            Map<String, String> newProperties = new HashMap<>();
-            Map<String, String> kafkaBrokerProperties = kafkaBroker.getProperties();
-            String kafkaMetricsReporters = kafkaBrokerProperties.get("kafka.metrics.reporters");
-            if (kafkaMetricsReporters == null ||
-              "{{kafka_metrics_reporters}}".equals(kafkaMetricsReporters)) {
-
-              if (installedServices.contains("AMBARI_METRICS")) {
-                newProperties.put("kafka.metrics.reporters", "org.apache.hadoop.metrics2.sink.kafka.KafkaTimelineMetricsReporter");
-              } else if (installedServices.contains("GANGLIA")) {
-                newProperties.put("kafka.metrics.reporters", "kafka.ganglia.KafkaGangliaMetricsReporter");
-              } else {
-                newProperties.put("kafka.metrics.reporters", " ");
-              }
-
-            }
-            if (!newProperties.isEmpty()) {
-              updateConfigurationPropertiesForCluster(cluster, KAFKA_BROKER, newProperties, true, true);
-            }
-          }
-
-          Config kafkaEnv = cluster.getDesiredConfigByType(KAFKA_ENV_CONFIG);
-          if (kafkaEnv != null) {
-            String kafkaEnvContent = kafkaEnv.getProperties().get(CONTENT_PROPERTY);
-            if (kafkaEnvContent != null && !kafkaEnvContent.contains(KAFKA_ENV_CONTENT_KERBEROS_PARAMS)) {
-              kafkaEnvContent += "\n\nexport KAFKA_KERBEROS_PARAMS=\"$KAFKA_KERBEROS_PARAMS {{kafka_kerberos_params}}\"";
-              Map<String, String> updates = Collections.singletonMap(CONTENT_PROPERTY, kafkaEnvContent);
-              updateConfigurationPropertiesForCluster(cluster, KAFKA_ENV_CONFIG, updates, true, false);
-            }
-          }
-        }
-      }
-    }
-  }
-
-  protected void updateRangerEnvConfig() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Map<String, String> newRangerEnvProps = new HashMap<>();
-      Config rangerHdfsPluginProperties = cluster.getDesiredConfigByType("ranger-hdfs-plugin-properties");
-      if (rangerHdfsPluginProperties != null && rangerHdfsPluginProperties.getProperties().containsKey(RANGER_HDFS_PLUGIN_ENABLED_PROPERTY)) {
-        newRangerEnvProps.put(RANGER_HDFS_PLUGIN_ENABLED_PROPERTY, rangerHdfsPluginProperties.getProperties().get(RANGER_HDFS_PLUGIN_ENABLED_PROPERTY));
-      }
-      Config hiveEnvProperties = cluster.getDesiredConfigByType("hive-env");
-      if (hiveEnvProperties != null && hiveEnvProperties.getProperties().containsKey("hive_security_authorization")
-              && hiveEnvProperties.getProperties().get("hive_security_authorization").toLowerCase().equals("ranger")) {
-        newRangerEnvProps.put(RANGER_HIVE_PLUGIN_ENABLED_PROPERTY, "Yes");
-      }
-      Config rangerHbasePluginProperties = cluster.getDesiredConfigByType("ranger-hbase-plugin-properties");
-      if (rangerHbasePluginProperties != null && rangerHbasePluginProperties.getProperties().containsKey(RANGER_HBASE_PLUGIN_ENABLED_PROPERTY)) {
-        newRangerEnvProps.put(RANGER_HBASE_PLUGIN_ENABLED_PROPERTY, rangerHbasePluginProperties.getProperties().get(RANGER_HBASE_PLUGIN_ENABLED_PROPERTY));
-      }
-
-      Config rangerStormPluginProperties = cluster.getDesiredConfigByType("ranger-storm-plugin-properties");
-      if (rangerStormPluginProperties != null && rangerStormPluginProperties.getProperties().containsKey(RANGER_STORM_PLUGIN_ENABLED_PROPERTY)) {
-        newRangerEnvProps.put(RANGER_STORM_PLUGIN_ENABLED_PROPERTY, rangerStormPluginProperties.getProperties().get(RANGER_STORM_PLUGIN_ENABLED_PROPERTY));
-      }
-      Config rangerKnoxPluginProperties = cluster.getDesiredConfigByType("ranger-knox-plugin-properties");
-      if (rangerKnoxPluginProperties != null && rangerKnoxPluginProperties.getProperties().containsKey(RANGER_KNOX_PLUGIN_ENABLED_PROPERTY)) {
-        newRangerEnvProps.put(RANGER_KNOX_PLUGIN_ENABLED_PROPERTY, rangerKnoxPluginProperties.getProperties().get(RANGER_KNOX_PLUGIN_ENABLED_PROPERTY));
-      }
-      Config rangerYarnPluginProperties = cluster.getDesiredConfigByType("ranger-yarn-plugin-properties");
-      if (rangerYarnPluginProperties != null && rangerYarnPluginProperties.getProperties().containsKey(RANGER_YARN_PLUGIN_ENABLED_PROPERTY)) {
-        newRangerEnvProps.put(RANGER_YARN_PLUGIN_ENABLED_PROPERTY, rangerYarnPluginProperties.getProperties().get(RANGER_YARN_PLUGIN_ENABLED_PROPERTY));
-      }
-      Config rangerKafkaPluginProperties = cluster.getDesiredConfigByType("ranger-kafka-plugin-properties");
-      if (rangerKafkaPluginProperties != null && rangerKafkaPluginProperties.getProperties().containsKey(RANGER_KAFKA_PLUGIN_ENABLED_PROPERTY)) {
-        newRangerEnvProps.put(RANGER_KAFKA_PLUGIN_ENABLED_PROPERTY, rangerKafkaPluginProperties.getProperties().get(RANGER_KAFKA_PLUGIN_ENABLED_PROPERTY));
-      }
-      if (!newRangerEnvProps.isEmpty()) {
-        updateConfigurationPropertiesForCluster(cluster, RANGER_ENV_CONFIG, newRangerEnvProps, true, true);
-      }
-    }
-  }
-
-  protected void updateRangerUgsyncSiteConfig() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config rangerUgsyncSiteProperties = cluster.getDesiredConfigByType(RANGER_UGSYNC_SITE_CONFIG);
-      if (rangerUgsyncSiteProperties != null && rangerUgsyncSiteProperties.getProperties().containsKey(RANGER_USERSYNC_SOURCE_IMPL_CLASS_PROPERTY)) {
-        String sourceClassValue = rangerUgsyncSiteProperties.getProperties().get(RANGER_USERSYNC_SOURCE_IMPL_CLASS_PROPERTY);
-        if (sourceClassValue != null) {
-          if ("ldap".equals(sourceClassValue)) {
-            Map<String, String> updates = Collections.singletonMap(RANGER_USERSYNC_SOURCE_IMPL_CLASS_PROPERTY,
-                "org.apache.ranger.ldapusersync.process.LdapUserGroupBuilder");
-            updateConfigurationPropertiesForCluster(cluster, RANGER_UGSYNC_SITE_CONFIG, updates, true, false);
-          } else if ("unix".equals(sourceClassValue)) {
-            Map<String, String> updates = Collections.singletonMap(RANGER_USERSYNC_SOURCE_IMPL_CLASS_PROPERTY,
-                "org.apache.ranger.unixusersync.process.UnixUserGroupBuilder");
-            updateConfigurationPropertiesForCluster(cluster, RANGER_UGSYNC_SITE_CONFIG, updates, true, false);
-          } else if ("file".equals(sourceClassValue)) {
-            Map<String, String> updates = Collections.singletonMap(RANGER_USERSYNC_SOURCE_IMPL_CLASS_PROPERTY,
-                "org.apache.ranger.unixusersync.process.FileSourceUserGroupBuilder");
-            updateConfigurationPropertiesForCluster(cluster, RANGER_UGSYNC_SITE_CONFIG, updates, true, false);
-          }
-        }
-      }
-    }
-  }
-
-  protected String updateHiveEnvContent(String hiveEnvContent) {
-    if(hiveEnvContent == null) {
-      return null;
-    }
-    // There are two cases here
-    // We do not have "export HADOOP_CLIENT_OPTS" and we need to add it
-    // We have "export HADOOP_CLIENT_OPTS" with wrong order
-    String exportHadoopClientOpts = "(?s).*export\\s*HADOOP_CLIENT_OPTS.*";
-    if (hiveEnvContent.matches(exportHadoopClientOpts)) {
-      String oldHeapSizeRegex = "export\\s*HADOOP_CLIENT_OPTS=\"-Xmx\\$\\{HADOOP_HEAPSIZE\\}m\\s*\\$HADOOP_CLIENT_OPTS\"";
-      String newHeapSizeRegex = "export HADOOP_CLIENT_OPTS=\"$HADOOP_CLIENT_OPTS  -Xmx${HADOOP_HEAPSIZE}m\"";
-      return hiveEnvContent.replaceAll(oldHeapSizeRegex, Matcher.quoteReplacement(newHeapSizeRegex));
-    } else {
-      String oldHeapSizeRegex = "export\\s*HADOOP_HEAPSIZE\\s*=\\s*\"\\{\\{hive_heapsize\\}\\}\"\\.*\\n\\s*fi\\s*\\n";
-      String newHeapSizeRegex = "export HADOOP_HEAPSIZE={{hive_heapsize}} # Setting for HiveServer2 and Client\n" +
-              "fi\n" +
-              "\n" +
-              "export HADOOP_CLIENT_OPTS=\"$HADOOP_CLIENT_OPTS  -Xmx${HADOOP_HEAPSIZE}m\"\n";
-      return hiveEnvContent.replaceAll(oldHeapSizeRegex, Matcher.quoteReplacement(newHeapSizeRegex));
-    }
-  }
-
-  protected String updateHiveEnvContentHDP23(String hiveEnvContent) {
-    if(hiveEnvContent == null) {
-      return null;
-    }
-    String oldHeapSizeRegex = "# The heap size of the jvm stared by hive shell script can be controlled via:\\s*\\n";
-    String newHeapSizeRegex = "# The heap size of the jvm stared by hive shell script can be controlled via:\n" +
-            "\n" +
-            "if [ \"$SERVICE\" = \"metastore\" ]; then\n" +
-            "  export HADOOP_HEAPSIZE={{hive_metastore_heapsize}} # Setting for HiveMetastore\n" +
-            "else\n" +
-            "  export HADOOP_HEAPSIZE={{hive_heapsize}} # Setting for HiveServer2 and Client\n" +
-            "fi\n" +
-            "\n" +
-            "export HADOOP_CLIENT_OPTS=\"$HADOOP_CLIENT_OPTS  -Xmx${HADOOP_HEAPSIZE}m\"\n" +
-            "\n";
-    return hiveEnvContent.replaceFirst(oldHeapSizeRegex, Matcher.quoteReplacement(newHeapSizeRegex));
-  }
-
-  protected void updateAccumuloConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      // If security type is set to Kerberos, update Kerberos-related configs
-      if(cluster.getSecurityType() == SecurityType.KERBEROS) {
-        Config clientProps = cluster.getDesiredConfigByType("client");
-        if (clientProps != null) {
-          Map<String, String> properties = clientProps.getProperties();
-          if (properties == null) {
-            properties = new HashMap<>();
-          }
-          // <2.2.0 did not account for a custom service principal.
-          // Need to ensure that the client knows the server's principal (the primary) to properly authenticate.
-          properties.put("kerberos.server.primary", "{{bare_accumulo_principal}}");
-          updateConfigurationPropertiesForCluster(cluster, "client", properties, true, false);
-        }
-      } // else -- no special client-configuration is necessary.
-    }
-  }
-}


[19/37] ambari git commit: AMBARI-21450. Initial cherry-picking for feature branch (ncole)

Posted by nc...@apache.org.
http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog221.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog221.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog221.java
deleted file mode 100644
index 365526b..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog221.java
+++ /dev/null
@@ -1,448 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-import com.google.gson.JsonPrimitive;
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
-import org.apache.ambari.server.orm.dao.DaoUtils;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.utils.VersionUtils;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
-
-/**
- * Upgrade catalog for version 2.2.1.
- */
-public class UpgradeCatalog221 extends AbstractUpgradeCatalog {
-
-  private static final String AMS_HBASE_SITE = "ams-hbase-site";
-  private static final String AMS_SITE = "ams-site";
-  private static final String AMS_HBASE_SECURITY_SITE = "ams-hbase-security-site";
-  private static final String AMS_ENV = "ams-env";
-  private static final String AMS_HBASE_ENV = "ams-hbase-env";
-  private static final String AMS_MODE = "timeline.metrics.service.operation.mode";
-  private static final String ZK_ZNODE_PARENT = "zookeeper.znode.parent";
-  private static final String ZK_CLIENT_PORT = "hbase.zookeeper.property.clientPort";
-  private static final String ZK_TICK_TIME = "hbase.zookeeper.property.tickTime";
-  private static final String CLUSTER_ENV = "cluster-env";
-  private static final String SECURITY_ENABLED = "security_enabled";
-  private static final String TOPOLOGY_HOST_INFO_TABLE = "topology_host_info";
-  private static final String TOPOLOGY_HOST_INFO_RACK_INFO_COLUMN = "rack_info";
-  private static final String TEZ_SITE = "tez-site";
-
-  @Inject
-  DaoUtils daoUtils;
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog221.class);
-
-  private static final String OOZIE_SITE_CONFIG = "oozie-site";
-  private static final String OOZIE_SERVICE_HADOOP_CONFIGURATIONS_PROPERTY_NAME = "oozie.service.HadoopAccessorService.hadoop.configurations";
-  private static final String OLD_DEFAULT_HADOOP_CONFIG_PATH = "/etc/hadoop/conf";
-  private static final String NEW_DEFAULT_HADOOP_CONFIG_PATH = "{{hadoop_conf_dir}}";
-
-  private static final String BLUEPRINT_HOSTGROUP_COMPONENT_TABLE_NAME = "hostgroup_component";
-  private static final String BLUEPRINT_PROVISION_ACTION_COLUMN_NAME = "provision_action";
-
-  private static final String RANGER_KMS_DBKS_CONFIG = "dbks-site";
-  private static final String RANGER_KMS_DB_FLAVOR = "DB_FLAVOR";
-  private static final String RANGER_KMS_DB_HOST = "db_host";
-  private static final String RANGER_KMS_DB_NAME = "db_name";
-  private static final String RANGER_KMS_JDBC_URL = "ranger.ks.jpa.jdbc.url";
-  private static final String RANGER_KMS_JDBC_DRIVER = "ranger.ks.jpa.jdbc.driver";
-  private static final String RANGER_KMS_PROPERTIES = "kms-properties";
-
-  private static final String TEZ_COUNTERS_MAX = "tez.counters.max";
-  private static final String TEZ_COUNTERS_MAX_GROUPS = "tez.counters.max.groups";
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   *
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog221(Injector injector) {
-    super(injector);
-    this.injector = injector;
-  }
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.2.1";
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getSourceVersion() {
-    return "2.2.0";
-  }
-
-
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-    // indices to improve request status calc performance
-    dbAccessor.createIndex("idx_stage_request_id", "stage", "request_id");
-    dbAccessor.createIndex("idx_hrc_request_id", "host_role_command", "request_id");
-    dbAccessor.createIndex("idx_rsc_request_id", "role_success_criteria", "request_id");
-
-    executeBlueprintProvisionActionDDLUpdates();
-
-    dbAccessor.addColumn(TOPOLOGY_HOST_INFO_TABLE,
-        new DBAccessor.DBColumnInfo(TOPOLOGY_HOST_INFO_RACK_INFO_COLUMN, String.class, 255));
-
-  }
-
-  private void executeBlueprintProvisionActionDDLUpdates() throws AmbariException, SQLException {
-    // add provision_action column to the hostgroup_component table for Blueprints
-    dbAccessor.addColumn(BLUEPRINT_HOSTGROUP_COMPONENT_TABLE_NAME, new DBAccessor.DBColumnInfo(BLUEPRINT_PROVISION_ACTION_COLUMN_NAME,
-      String.class, 255, null, true));
-  }
-
-  @Override
-  protected void executePreDMLUpdates() throws AmbariException, SQLException {
-    //To change body of implemented methods use File | Settings | File Templates.
-  }
-
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    addNewConfigurationsFromXml();
-    updateAlerts();
-    updateOozieConfigs();
-    updateTezConfigs();
-    updateRangerKmsDbksConfigs();
-    updateAMSConfigs();
-  }
-
-  protected void updateAlerts() {
-    LOG.info("Updating alert definitions.");
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
-    for (final Cluster cluster : clusterMap.values()) {
-      long clusterID = cluster.getClusterId();
-      final AlertDefinitionEntity hiveMetastoreProcessAlertDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "hive_metastore_process");
-      final AlertDefinitionEntity hiveServerProcessAlertDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "hive_server_process");
-
-      List<AlertDefinitionEntity> hiveAlertDefinitions = new ArrayList();
-      if(hiveMetastoreProcessAlertDefinitionEntity != null) {
-        hiveAlertDefinitions.add(hiveMetastoreProcessAlertDefinitionEntity);
-      }
-      if(hiveServerProcessAlertDefinitionEntity != null) {
-        hiveAlertDefinitions.add(hiveServerProcessAlertDefinitionEntity);
-      }
-
-      for(AlertDefinitionEntity alertDefinition : hiveAlertDefinitions){
-        String source = alertDefinition.getSource();
-
-        alertDefinition.setScheduleInterval(3);
-        alertDefinition.setSource(addCheckCommandTimeoutParam(source));
-        alertDefinition.setHash(UUID.randomUUID().toString());
-
-        alertDefinitionDAO.merge(alertDefinition);
-      }
-
-      final AlertDefinitionEntity amsZookeeperProcessAlertDefinitionEntity = alertDefinitionDAO.findByName(
-        clusterID, "ams_metrics_collector_zookeeper_server_process");
-
-      if (amsZookeeperProcessAlertDefinitionEntity != null) {
-        LOG.info("Removing alert : ams_metrics_collector_zookeeper_server_process");
-        alertDefinitionDAO.remove(amsZookeeperProcessAlertDefinitionEntity);
-      }
-    }
-  }
-
-  protected String addCheckCommandTimeoutParam(String source) {
-    JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-    JsonArray parametersJson = sourceJson.getAsJsonArray("parameters");
-
-    boolean parameterExists = parametersJson != null && !parametersJson.isJsonNull();
-
-    if (parameterExists) {
-      Iterator<JsonElement> jsonElementIterator = parametersJson.iterator();
-      while(jsonElementIterator.hasNext()) {
-        JsonElement element = jsonElementIterator.next();
-        JsonElement name = element.getAsJsonObject().get("name");
-        if (name != null && !name.isJsonNull() && name.getAsString().equals("check.command.timeout")) {
-          return sourceJson.toString();
-        }
-      }
-    }
-
-    JsonObject checkCommandTimeoutParamJson = new JsonObject();
-    checkCommandTimeoutParamJson.add("name", new JsonPrimitive("check.command.timeout"));
-    checkCommandTimeoutParamJson.add("display_name", new JsonPrimitive("Check command timeout"));
-    checkCommandTimeoutParamJson.add("value", new JsonPrimitive(60.0));
-    checkCommandTimeoutParamJson.add("type", new JsonPrimitive("NUMERIC"));
-    checkCommandTimeoutParamJson.add("description", new JsonPrimitive("The maximum time before check command will be killed by timeout"));
-    checkCommandTimeoutParamJson.add("units", new JsonPrimitive("seconds"));
-
-    if (!parameterExists) {
-      parametersJson = new JsonArray();
-      parametersJson.add(checkCommandTimeoutParamJson);
-      sourceJson.add("parameters", parametersJson);
-    } else {
-      parametersJson.add(checkCommandTimeoutParamJson);
-      sourceJson.remove("parameters");
-      sourceJson.add("parameters", parametersJson);
-    }
-
-    return sourceJson.toString();
-  }
-
-  protected void updateAMSConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-
-          Config amsEnv = cluster.getDesiredConfigByType(AMS_ENV);
-          if (amsEnv != null) {
-            Map<String, String> amsEnvProperties = amsEnv.getProperties();
-            String content = amsEnvProperties.get("content");
-            Map<String, String> newProperties = new HashMap<>();
-            newProperties.put("content", updateAmsEnvContent(content));
-            updateConfigurationPropertiesForCluster(cluster, AMS_ENV, newProperties, true, true);
-          }
-
-          String znodeParent = null;
-          Config amsHbaseSecuritySite = cluster.getDesiredConfigByType(AMS_HBASE_SECURITY_SITE);
-          if (amsHbaseSecuritySite != null) {
-            Map<String, String> amsHbaseSecuritySiteProperties = amsHbaseSecuritySite.getProperties();
-            znodeParent = amsHbaseSecuritySiteProperties.get(ZK_ZNODE_PARENT);
-            LOG.info("Removing config zookeeper.znode.parent from ams-hbase-security-site");
-            removeConfigurationPropertiesFromCluster(cluster, AMS_HBASE_SECURITY_SITE, Collections.singleton(ZK_ZNODE_PARENT));
-          }
-
-          Config amsHbaseSite = cluster.getDesiredConfigByType(AMS_HBASE_SITE);
-          if (amsHbaseSite != null) {
-            Map<String, String> amsHbaseSiteProperties = amsHbaseSite.getProperties();
-            Map<String, String> newProperties = new HashMap<>();
-
-            if (!amsHbaseSiteProperties.containsKey(ZK_ZNODE_PARENT)) {
-              if (StringUtils.isEmpty(znodeParent) || "/hbase".equals(znodeParent)) {
-                boolean isSecurityEnabled = false;
-                Config clusterEnv = cluster.getDesiredConfigByType(CLUSTER_ENV);
-                if (clusterEnv != null) {
-                  Map<String,String> clusterEnvProperties = clusterEnv.getProperties();
-                  if (clusterEnvProperties.containsKey(SECURITY_ENABLED)) {
-                    isSecurityEnabled = Boolean.valueOf(clusterEnvProperties.get(SECURITY_ENABLED));
-                  }
-                }
-                znodeParent = "/ams-hbase-" + (isSecurityEnabled ? "secure" : "unsecure");
-              }
-
-              LOG.info("Adding config zookeeper.znode.parent=" + znodeParent + " to ams-hbase-site");
-              newProperties.put(ZK_ZNODE_PARENT, znodeParent);
-
-            }
-
-            boolean isDistributed = false;
-            Config amsSite = cluster.getDesiredConfigByType(AMS_SITE);
-            if (amsSite != null) {
-              if ("distributed".equals(amsSite.getProperties().get(AMS_MODE))) {
-                isDistributed = true;
-              }
-            }
-
-            // Skip override if custom port found in embedded mode.
-            if (amsHbaseSiteProperties.containsKey(ZK_CLIENT_PORT) &&
-               (isDistributed || amsHbaseSiteProperties.get(ZK_CLIENT_PORT).equals("61181"))) {
-              String newValue = "{{zookeeper_clientPort}}";
-              LOG.info("Replacing value of " + ZK_CLIENT_PORT + " from " +
-                amsHbaseSiteProperties.get(ZK_CLIENT_PORT) + " to " +
-                newValue + " in ams-hbase-site");
-
-              newProperties.put(ZK_CLIENT_PORT, newValue);
-            }
-
-            if (!amsHbaseSiteProperties.containsKey(ZK_TICK_TIME)) {
-              LOG.info("Adding config " + ZK_TICK_TIME + " to ams-hbase-site");
-              newProperties.put(ZK_TICK_TIME, "6000");
-            }
-
-            updateConfigurationPropertiesForCluster(cluster, AMS_HBASE_SITE, newProperties, true, true);
-          }
-
-          Config amsHbaseEnv = cluster.getDesiredConfigByType(AMS_HBASE_ENV);
-          if (amsHbaseEnv != null) {
-            Map<String, String> amsHbaseEnvProperties = amsHbaseEnv.getProperties();
-            String content = amsHbaseEnvProperties.get("content");
-            Map<String, String> newProperties = new HashMap<>();
-            newProperties.put("content", updateAmsHbaseEnvContent(content));
-            updateConfigurationPropertiesForCluster(cluster, AMS_HBASE_ENV, newProperties, true, true);
-          }
-        }
-      }
-    }
-  }
-
-  protected String updateAmsHbaseEnvContent(String content) {
-    if (content == null) {
-      return null;
-    }
-    String regSearch = "_jaas_config_file\\}\\} -Dzookeeper.sasl.client.username=\\{\\{zk_servicename\\}\\}";
-    String replacement = "_jaas_config_file}}";
-    content = content.replaceAll(regSearch, replacement);
-    return content;
-  }
-
-  protected String updateAmsEnvContent(String content) {
-
-    if (content == null) {
-      return null;
-    }
-    String regSearch = "-Djava.security.auth.login.config=\\{\\{ams_collector_jaas_config_file\\}\\} " +
-      "-Dzookeeper.sasl.client.username=\\{\\{zk_servicename\\}\\}";
-    String replacement = "-Djava.security.auth.login.config={{ams_collector_jaas_config_file}}";
-    content = content.replaceAll(regSearch, replacement);
-
-    return content;
-  }
-
-  protected void updateOozieConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config oozieSiteProps = cluster.getDesiredConfigByType(OOZIE_SITE_CONFIG);
-      if (oozieSiteProps != null) {
-        // Update oozie.service.HadoopAccessorService.hadoop.configurations
-        Map<String, String> updateProperties = new HashMap<>();
-        String oozieHadoopConfigProperty = oozieSiteProps.getProperties().get(OOZIE_SERVICE_HADOOP_CONFIGURATIONS_PROPERTY_NAME);
-        if(oozieHadoopConfigProperty != null && oozieHadoopConfigProperty.contains(OLD_DEFAULT_HADOOP_CONFIG_PATH)) {
-          String updatedOozieHadoopConfigProperty = oozieHadoopConfigProperty.replaceAll(
-              OLD_DEFAULT_HADOOP_CONFIG_PATH, NEW_DEFAULT_HADOOP_CONFIG_PATH);
-          updateProperties.put(OOZIE_SERVICE_HADOOP_CONFIGURATIONS_PROPERTY_NAME, updatedOozieHadoopConfigProperty);
-          updateConfigurationPropertiesForCluster(cluster, OOZIE_SITE_CONFIG, updateProperties, true, false);
-        }
-      }
-    }
-  }
-
-  protected void updateTezConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config tezSiteProps = cluster.getDesiredConfigByType(TEZ_SITE);
-      if (tezSiteProps != null) {
-
-        // Update tez.counters.max and tez.counters.max.groups configurations
-        String tezCountersMaxProperty = tezSiteProps.getProperties().get(TEZ_COUNTERS_MAX);
-        String tezCountersMaxGroupesProperty = tezSiteProps.getProperties().get(TEZ_COUNTERS_MAX_GROUPS);
-
-        StackId stackId = cluster.getCurrentStackVersion();
-        boolean isStackNotLess23 = (stackId != null && stackId.getStackName().equals("HDP") &&
-            VersionUtils.compareVersions(stackId.getStackVersion(), "2.3") >= 0);
-
-        if (isStackNotLess23) {
-          Map<String, String> updates = new HashMap<String, String>();
-          if (tezCountersMaxProperty != null && tezCountersMaxProperty.equals("2000")) {
-            updates.put(TEZ_COUNTERS_MAX, "10000");
-          }
-          if (tezCountersMaxGroupesProperty != null && tezCountersMaxGroupesProperty.equals("1000")) {
-            updates.put(TEZ_COUNTERS_MAX_GROUPS, "3000");
-          }
-          if (!updates.isEmpty()) {
-            updateConfigurationPropertiesForCluster(cluster, TEZ_SITE, updates, true, false);
-          }
-        }
-      }
-    }
-  }
-
-  protected void updateRangerKmsDbksConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Map<String, String> newRangerKmsProps = new HashMap<>();
-      Config rangerKmsDbConfigs = cluster.getDesiredConfigByType(RANGER_KMS_PROPERTIES);
-      if (rangerKmsDbConfigs != null) {
-        String dbFlavor = rangerKmsDbConfigs.getProperties().get(RANGER_KMS_DB_FLAVOR);
-        String dbHost = rangerKmsDbConfigs.getProperties().get(RANGER_KMS_DB_HOST);
-        String dbName = rangerKmsDbConfigs.getProperties().get(RANGER_KMS_DB_NAME);
-        String dbConnectionString = null;
-        String dbDriver = null;
-
-        if (dbFlavor != null && dbHost != null && dbName != null) {
-          if ("MYSQL".equalsIgnoreCase(dbFlavor)) {
-            dbConnectionString = "jdbc:mysql://"+dbHost+"/"+dbName;
-            dbDriver = "com.mysql.jdbc.Driver";
-          } else if ("ORACLE".equalsIgnoreCase(dbFlavor)) {
-            dbConnectionString = "jdbc:oracle:thin:@//"+dbHost;
-            dbDriver = "oracle.jdbc.driver.OracleDriver";
-          } else if ("POSTGRES".equalsIgnoreCase(dbFlavor)) {
-            dbConnectionString = "jdbc:postgresql://"+dbHost+"/"+dbName;
-            dbDriver = "org.postgresql.Driver";
-          } else if ("MSSQL".equalsIgnoreCase(dbFlavor)) {
-            dbConnectionString = "jdbc:sqlserver://"+dbHost+";databaseName="+dbName;
-            dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
-          } else if ("SQLA".equalsIgnoreCase(dbFlavor)) {
-            dbConnectionString = "jdbc:sqlanywhere:database="+dbName+";host="+dbHost;
-            dbDriver = "sap.jdbc4.sqlanywhere.IDriver";
-          }
-          newRangerKmsProps.put(RANGER_KMS_JDBC_URL, dbConnectionString);
-          newRangerKmsProps.put(RANGER_KMS_JDBC_DRIVER, dbDriver);
-          updateConfigurationPropertiesForCluster(cluster, RANGER_KMS_DBKS_CONFIG, newRangerKmsProps, true, false);
-        }
-      }
-    }
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog222.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog222.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog222.java
deleted file mode 100644
index ab3b493..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog222.java
+++ /dev/null
@@ -1,751 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import java.io.File;
-import java.io.FileReader;
-import java.lang.reflect.Type;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.api.services.AmbariMetaInfo;
-import org.apache.ambari.server.controller.AmbariManagementController;
-import org.apache.ambari.server.orm.DBAccessor;
-import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
-import org.apache.ambari.server.orm.dao.DaoUtils;
-import org.apache.ambari.server.orm.dao.WidgetDAO;
-import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
-import org.apache.ambari.server.orm.entities.WidgetEntity;
-import org.apache.ambari.server.state.Cluster;
-import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
-import org.apache.ambari.server.state.Service;
-import org.apache.ambari.server.state.ServiceComponentHost;
-import org.apache.ambari.server.state.ServiceInfo;
-import org.apache.ambari.server.state.StackId;
-import org.apache.ambari.server.state.StackInfo;
-import org.apache.ambari.server.state.stack.WidgetLayout;
-import org.apache.ambari.server.state.stack.WidgetLayoutInfo;
-import org.apache.ambari.server.utils.VersionUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.reflect.TypeToken;
-import com.google.gson.Gson;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-
-/**
- * Upgrade catalog for version 2.2.2.
- */
-public class UpgradeCatalog222 extends AbstractUpgradeCatalog {
-
-  @Inject
-  DaoUtils daoUtils;
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog222.class);
-  private static final String AMS_SITE = "ams-site";
-  private static final String AMS_HBASE_SITE = "ams-hbase-site";
-  private static final String HIVE_SITE_CONFIG = "hive-site";
-  private static final String ATLAS_APPLICATION_PROPERTIES_CONFIG = "application-properties";
-  private static final String ATLAS_HOOK_HIVE_MINTHREADS_PROPERTY = "atlas.hook.hive.minThreads";
-  private static final String ATLAS_HOOK_HIVE_MAXTHREADS_PROPERTY = "atlas.hook.hive.maxThreads";
-  private static final String ATLAS_CLUSTER_NAME_PROPERTY = "atlas.cluster.name";
-  private static final String ATLAS_ENABLETLS_PROPERTY = "atlas.enableTLS";
-  private static final String ATLAS_SERVER_HTTP_PORT_PROPERTY = "atlas.server.http.port";
-  private static final String ATLAS_SERVER_HTTPS_PORT_PROPERTY = "atlas.server.https.port";
-  private static final String ATLAS_REST_ADDRESS_PROPERTY = "atlas.rest.address";
-  private static final String HBASE_ENV_CONFIG = "hbase-env";
-  private static final String CONTENT_PROPERTY = "content";
-
-  private static final String UPGRADE_TABLE = "upgrade";
-  private static final String UPGRADE_SUSPENDED_COLUMN = "suspended";
-
-  private static final String HOST_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER_PROPERTY =
-    "timeline.metrics.host.aggregator.daily.checkpointCutOffMultiplier";
-  private static final String CLUSTER_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER_PROPERTY =
-    "timeline.metrics.cluster.aggregator.daily.checkpointCutOffMultiplier";
-  private static final String TIMELINE_METRICS_SERVICE_WATCHER_DISBALED_PROPERTY = "timeline.metrics.service.watcher.disabled";
-  private static final String AMS_MODE_PROPERTY = "timeline.metrics.service.operation.mode";
-  public static final String PRECISION_TABLE_TTL_PROPERTY = "timeline.metrics.host.aggregator.ttl";
-  public static final String CLUSTER_SECOND_TABLE_TTL_PROPERTY = "timeline.metrics.cluster.aggregator.second.ttl";
-  public static final String CLUSTER_MINUTE_TABLE_TTL_PROPERTY = "timeline.metrics.cluster.aggregator.minute.ttl";
-  public static final String AMS_WEBAPP_ADDRESS_PROPERTY = "timeline.metrics.service.webapp.address";
-  public static final String HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD_PROPERTY = "hbase.client.scanner.timeout.period";
-  public static final String HBASE_RPC_TIMEOUT_PROPERTY = "hbase.rpc.timeout";
-
-  public static final String PHOENIX_QUERY_TIMEOUT_PROPERTY = "phoenix.query.timeoutMs";
-  public static final String PHOENIX_QUERY_KEEPALIVE_PROPERTY = "phoenix.query.keepAliveMs";
-  public static final String TIMELINE_METRICS_CLUSTER_AGGREGATOR_INTERPOLATION_ENABLED
-    = "timeline.metrics.cluster.aggregator.interpolation.enabled";
-  public static final String TIMELINE_METRICS_SINK_COLLECTION_PERIOD = "timeline.metrics.sink.collection.period";
-
-  public static final String AMS_SERVICE_NAME = "AMBARI_METRICS";
-  public static final String AMS_COLLECTOR_COMPONENT_NAME = "METRICS_COLLECTOR";
-
-  protected static final String WIDGET_TABLE = "widget";
-  protected static final String WIDGET_DESCRIPTION = "description";
-  protected static final String WIDGET_NAME = "widget_name";
-  protected static final String WIDGET_CORRUPT_BLOCKS = "Corrupted Blocks";
-  protected static final String WIDGET_CORRUPT_REPLICAS = "Blocks With Corrupted Replicas";
-  protected static final String WIDGET_CORRUPT_REPLICAS_DESCRIPTION = "Number represents data blocks with at least one " +
-    "corrupted replica (but not all of them). Its indicative of HDFS bad health.";
-  protected static final String WIDGET_VALUES = "widget_values";
-  protected static final String WIDGET_VALUES_VALUE =
-    "${Hadoop:service\\" +
-    "\\u003dNameNode,name\\" +
-    "\\u003dFSNamesystem.CorruptBlocks}";
-
-  public final static String HBASE_SITE_HBASE_COPROCESSOR_MASTER_CLASSES = "hbase.coprocessor.master.classes";
-  public final static String HBASE_SITE_HBASE_COPROCESSOR_REGION_CLASSES = "hbase.coprocessor.region.classes";
-  public final static String HBASE_SITE_HBASE_COPROCESSOR_REGIONSERVER_CLASSES = "hbase.coprocessor.regionserver.classes";
-
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   *
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog222(Injector injector) {
-    super(injector);
-    this.injector = injector;
-  }
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.2.2";
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getSourceVersion() {
-    return "2.2.1";
-  }
-
-
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-    DBAccessor.DBColumnInfo columnInfo = new DBAccessor.DBColumnInfo("host_id", Long.class);
-    dbAccessor.addColumn("topology_host_info", columnInfo);
-    dbAccessor.addFKConstraint("topology_host_info", "FK_hostinfo_host_id", "host_id", "hosts", "host_id", true);
-    dbAccessor.executeUpdate("update topology_host_info set host_id = (select hosts.host_id from hosts where hosts.host_name = topology_host_info.fqdn)");
-
-    updateUpgradeTable();
-  }
-
-  @Override
-  protected void executePreDMLUpdates() throws AmbariException, SQLException {
-    //To change body of implemented methods use File | Settings | File Templates.
-  }
-
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    addNewConfigurationsFromXml();
-    updateAlerts();
-    updateStormConfigs();
-    updateAMSConfigs();
-    updateHiveConfig();
-    updateHostRoleCommands();
-    updateHDFSWidgetDefinition();
-    updateYARNWidgetDefinition();
-    updateHBASEWidgetDefinition();
-    updateHbaseEnvConfig();
-    updateCorruptedReplicaWidget();
-    updateZookeeperConfigs();
-    updateHBASEConfigs();
-    createNewSliderConfigVersion();
-    initializeStromAndKafkaWidgets();
-  }
-
-  protected void createNewSliderConfigVersion() {
-    // Here we are creating new service config version for SLIDER, to link slider-client
-    // config to SLIDER service, in serviceconfigmapping table. It could be not mapped because
-    // of bug which we had a long time ago.
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(ambariManagementController.getClusters());
-
-    for (final Cluster cluster : clusterMap.values()) {
-      Service sliderService = null;
-      try {
-        sliderService = cluster.getService("SLIDER");
-      } catch(AmbariException ambariException) {
-        LOG.info("SLIDER service not found in cluster while creating new serviceconfig version for SLIDER service.");
-      }
-      if (sliderService != null) {
-        cluster.createServiceConfigVersion("SLIDER", AUTHENTICATED_USER_NAME, "Creating new service config version for SLIDER service.", null);
-      }
-    }
-  }
-
-  protected void updateZookeeperConfigs() throws  AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(ambariManagementController.getClusters());
-
-    for (final Cluster cluster : clusterMap.values()) {
-      Config zooEnv = cluster.getDesiredConfigByType("zookeeper-env");
-      if (zooEnv != null && zooEnv.getProperties().containsKey("zk_server_heapsize")) {
-        String heapSizeValue = zooEnv.getProperties().get("zk_server_heapsize");
-        if(!heapSizeValue.endsWith("m")) {
-          Map<String, String> updates = new HashMap<String, String>();
-          updates.put("zk_server_heapsize", heapSizeValue+"m");
-          updateConfigurationPropertiesForCluster(cluster, "zookeeper-env", updates, true, false);
-        }
-
-      }
-    }
-  }
-
-  protected void updateHBASEConfigs() throws  AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(ambariManagementController.getClusters());
-
-    for (final Cluster cluster : clusterMap.values()) {
-      Config hbaseSite = cluster.getDesiredConfigByType("hbase-site");
-      boolean rangerHbasePluginEnabled = isConfigEnabled(cluster,
-        AbstractUpgradeCatalog.CONFIGURATION_TYPE_RANGER_HBASE_PLUGIN_PROPERTIES,
-        AbstractUpgradeCatalog.PROPERTY_RANGER_HBASE_PLUGIN_ENABLED);
-      if (hbaseSite != null && rangerHbasePluginEnabled) {
-        Map<String, String> updates = new HashMap<>();
-        String stackVersion = cluster.getCurrentStackVersion().getStackVersion();
-        if (VersionUtils.compareVersions(stackVersion, "2.2") == 0) {
-          if (hbaseSite.getProperties().containsKey(HBASE_SITE_HBASE_COPROCESSOR_MASTER_CLASSES)) {
-            updates.put(HBASE_SITE_HBASE_COPROCESSOR_MASTER_CLASSES,
-              "com.xasecure.authorization.hbase.XaSecureAuthorizationCoprocessor");
-          }
-          if (hbaseSite.getProperties().containsKey(HBASE_SITE_HBASE_COPROCESSOR_REGIONSERVER_CLASSES)) {
-            updates.put(HBASE_SITE_HBASE_COPROCESSOR_REGIONSERVER_CLASSES,
-              "com.xasecure.authorization.hbase.XaSecureAuthorizationCoprocessor");
-          }
-          if (hbaseSite.getProperties().containsKey(HBASE_SITE_HBASE_COPROCESSOR_REGION_CLASSES)) {
-            updates.put(HBASE_SITE_HBASE_COPROCESSOR_REGION_CLASSES,
-                "org.apache.hadoop.hbase.security.token.TokenProvider,org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint," +
-                    "com.xasecure.authorization.hbase.XaSecureAuthorizationCoprocessor");
-          }
-        } else if (VersionUtils.compareVersions(stackVersion, "2.3") == 0) {
-          if (hbaseSite.getProperties().containsKey(HBASE_SITE_HBASE_COPROCESSOR_MASTER_CLASSES)) {
-            updates.put(HBASE_SITE_HBASE_COPROCESSOR_MASTER_CLASSES,
-              "org.apache.ranger.authorization.hbase.RangerAuthorizationCoprocessor ");
-          }
-          if (hbaseSite.getProperties().containsKey(HBASE_SITE_HBASE_COPROCESSOR_REGIONSERVER_CLASSES)) {
-            updates.put(HBASE_SITE_HBASE_COPROCESSOR_REGIONSERVER_CLASSES,
-              "org.apache.ranger.authorization.hbase.RangerAuthorizationCoprocessor");
-          }
-          if (hbaseSite.getProperties().containsKey(HBASE_SITE_HBASE_COPROCESSOR_REGION_CLASSES)) {
-            updates.put(HBASE_SITE_HBASE_COPROCESSOR_REGION_CLASSES,
-              "org.apache.hadoop.hbase.security.token.TokenProvider,org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint," +
-                "org.apache.ranger.authorization.hbase.RangerAuthorizationCoprocessor");
-          }
-        }
-        if (! updates.isEmpty()) {
-          updateConfigurationPropertiesForCluster(cluster, "hbase-site", updates, true, false);
-        }
-      }
-    }
-  }
-
-  protected void updateStormConfigs() throws  AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(ambariManagementController.getClusters());
-
-    for (final Cluster cluster : clusterMap.values()) {
-      if (cluster.getDesiredConfigByType("storm-site") != null && cluster.getDesiredConfigByType("storm-site").getProperties().containsKey("storm.zookeeper.superACL")
-              && cluster.getDesiredConfigByType("storm-site").getProperties().get("storm.zookeeper.superACL").equals("sasl:{{storm_base_jaas_principal}}")) {
-        Map<String, String> newStormProps = new HashMap<String, String>();
-        newStormProps.put("storm.zookeeper.superACL", "sasl:{{storm_bare_jaas_principal}}");
-        updateConfigurationPropertiesForCluster(cluster, "storm-site", newStormProps, true, false);
-      }
-    }
-  }
-
-  protected void updateAlerts() {
-    LOG.info("Updating alert definitions.");
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
-    for (final Cluster cluster : clusterMap.values()) {
-      long clusterID = cluster.getClusterId();
-
-      final AlertDefinitionEntity regionserverHealthSummaryDefinitionEntity = alertDefinitionDAO.findByName(
-              clusterID, "regionservers_health_summary");
-
-      final AlertDefinitionEntity atsWebAlert = alertDefinitionDAO.findByName(
-              clusterID, "yarn_app_timeline_server_webui");
-
-      if (regionserverHealthSummaryDefinitionEntity != null) {
-        alertDefinitionDAO.remove(regionserverHealthSummaryDefinitionEntity);
-      }
-
-      if (atsWebAlert != null) {
-        String source = atsWebAlert.getSource();
-        JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-
-        JsonObject uriJson = sourceJson.get("uri").getAsJsonObject();
-        uriJson.remove("http");
-        uriJson.remove("https");
-        uriJson.addProperty("http", "{{yarn-site/yarn.timeline-service.webapp.address}}/ws/v1/timeline");
-        uriJson.addProperty("https", "{{yarn-site/yarn.timeline-service.webapp.https.address}}/ws/v1/timeline");
-
-        atsWebAlert.setSource(sourceJson.toString());
-        alertDefinitionDAO.merge(atsWebAlert);
-      }
-
-      //update Atlas alert
-      final AlertDefinitionEntity atlasMetadataServerWebUI = alertDefinitionDAO.findByName(
-              clusterID, "metadata_server_webui");
-      if (atlasMetadataServerWebUI != null) {
-        String source = atlasMetadataServerWebUI.getSource();
-        JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject();
-
-        JsonObject uriJson = sourceJson.get("uri").getAsJsonObject();
-        uriJson.remove("http");
-        uriJson.remove("https");
-        uriJson.addProperty("http", "{{application-properties/atlas.server.http.port}}");
-        uriJson.addProperty("https", "{{application-properties/atlas.server.https.port}}");
-
-        atlasMetadataServerWebUI.setSource(sourceJson.toString());
-        alertDefinitionDAO.merge(atlasMetadataServerWebUI);
-      }
-
-    }
-
-
-  }
-
-  protected void updateHostRoleCommands() throws SQLException {
-    dbAccessor.createIndex("idx_hrc_status_role", "host_role_command", "status", "role");
-  }
-
-  protected void updateAMSConfigs() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = ambariManagementController.getClusters();
-
-    if (clusters != null) {
-      Map<String, Cluster> clusterMap = clusters.getClusters();
-
-      if (clusterMap != null && !clusterMap.isEmpty()) {
-        for (final Cluster cluster : clusterMap.values()) {
-
-          Config amsSite = cluster.getDesiredConfigByType(AMS_SITE);
-          if (amsSite != null) {
-            Map<String, String> amsSiteProperties = amsSite.getProperties();
-            Map<String, String> newProperties = new HashMap<>();
-
-            if (amsSiteProperties.containsKey(AMS_WEBAPP_ADDRESS_PROPERTY)) {
-              Set<String> collectorHostNames = cluster.getHosts(AMS_SERVICE_NAME, AMS_COLLECTOR_COMPONENT_NAME);
-              for (String collector: collectorHostNames) {
-                String currentValue = amsSiteProperties.get(AMS_WEBAPP_ADDRESS_PROPERTY);
-
-                if (currentValue.startsWith("0.0.0.0")) {
-                  newProperties.put(AMS_WEBAPP_ADDRESS_PROPERTY, currentValue.replace("0.0.0.0", collector));
-                } else if (currentValue.startsWith("localhost")) {
-                  newProperties.put(AMS_WEBAPP_ADDRESS_PROPERTY, currentValue.replace("localhost", collector));
-                }
-              }
-            }
-
-            if (amsSiteProperties.containsKey(HOST_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER_PROPERTY) &&
-              amsSiteProperties.get(HOST_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER_PROPERTY).equals("1")) {
-
-              LOG.info("Setting value of " + HOST_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER_PROPERTY + " : 2");
-              newProperties.put(HOST_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER_PROPERTY, String.valueOf(2));
-
-            }
-
-            if (amsSiteProperties.containsKey(CLUSTER_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER_PROPERTY) &&
-              amsSiteProperties.get(CLUSTER_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER_PROPERTY).equals("1")) {
-
-              LOG.info("Setting value of " + CLUSTER_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER_PROPERTY + " : 2");
-              newProperties.put(CLUSTER_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER_PROPERTY, String.valueOf(2));
-
-            }
-
-            if (!amsSiteProperties.containsKey(TIMELINE_METRICS_SERVICE_WATCHER_DISBALED_PROPERTY)) {
-              LOG.info("Add config  " + TIMELINE_METRICS_SERVICE_WATCHER_DISBALED_PROPERTY + " = false");
-              newProperties.put(TIMELINE_METRICS_SERVICE_WATCHER_DISBALED_PROPERTY, String.valueOf(false));
-            }
-
-            boolean isDistributed = false;
-            if ("distributed".equals(amsSite.getProperties().get(AMS_MODE_PROPERTY))) {
-              isDistributed = true;
-            }
-
-            if (amsSiteProperties.containsKey(PRECISION_TABLE_TTL_PROPERTY)) {
-              String oldTtl = amsSiteProperties.get(PRECISION_TABLE_TTL_PROPERTY);
-              String newTtl = oldTtl;
-              if (isDistributed) {
-                if ("86400".equals(oldTtl)) {
-                  newTtl = String.valueOf(3 * 86400); // 3 days
-                }
-              }
-              newProperties.put(PRECISION_TABLE_TTL_PROPERTY, newTtl);
-              LOG.info("Setting value of " + PRECISION_TABLE_TTL_PROPERTY + " : " + newTtl);
-            }
-
-            if (amsSiteProperties.containsKey(CLUSTER_SECOND_TABLE_TTL_PROPERTY)) {
-              String oldTtl = amsSiteProperties.get(CLUSTER_SECOND_TABLE_TTL_PROPERTY);
-              String newTtl = oldTtl;
-
-              if ("2592000".equals(oldTtl)) {
-                newTtl = String.valueOf(7 * 86400); // 7 days
-              }
-
-              newProperties.put(CLUSTER_SECOND_TABLE_TTL_PROPERTY, newTtl);
-              LOG.info("Setting value of " + CLUSTER_SECOND_TABLE_TTL_PROPERTY + " : " + newTtl);
-            }
-
-            if (amsSiteProperties.containsKey(CLUSTER_MINUTE_TABLE_TTL_PROPERTY)) {
-              String oldTtl = amsSiteProperties.get(CLUSTER_MINUTE_TABLE_TTL_PROPERTY);
-              String newTtl = oldTtl;
-
-              if ("7776000".equals(oldTtl)) {
-                newTtl = String.valueOf(30 * 86400); // 30 days
-              }
-
-              newProperties.put(CLUSTER_MINUTE_TABLE_TTL_PROPERTY, newTtl);
-              LOG.info("Setting value of " + CLUSTER_MINUTE_TABLE_TTL_PROPERTY + " : " + newTtl);
-            }
-
-            if (!amsSiteProperties.containsKey(TIMELINE_METRICS_CLUSTER_AGGREGATOR_INTERPOLATION_ENABLED)) {
-              LOG.info("Add config  " + TIMELINE_METRICS_CLUSTER_AGGREGATOR_INTERPOLATION_ENABLED + " = true");
-              newProperties.put(TIMELINE_METRICS_CLUSTER_AGGREGATOR_INTERPOLATION_ENABLED, String.valueOf(true));
-            }
-
-            if (!amsSiteProperties.containsKey(TIMELINE_METRICS_SINK_COLLECTION_PERIOD) ||
-              "60".equals(amsSiteProperties.get(TIMELINE_METRICS_SINK_COLLECTION_PERIOD))) {
-
-              newProperties.put(TIMELINE_METRICS_SINK_COLLECTION_PERIOD, "10");
-              LOG.info("Setting value of " + TIMELINE_METRICS_SINK_COLLECTION_PERIOD + " : 10");
-            }
-
-            updateConfigurationPropertiesForCluster(cluster, AMS_SITE, newProperties, true, true);
-          }
-
-          Config amsHbaseSite = cluster.getDesiredConfigByType(AMS_HBASE_SITE);
-          if (amsHbaseSite != null) {
-            Map<String, String> amsHbaseSiteProperties = amsHbaseSite.getProperties();
-            Map<String, String> newProperties = new HashMap<>();
-
-            if (!amsHbaseSiteProperties.containsKey(HBASE_RPC_TIMEOUT_PROPERTY)) {
-              newProperties.put(HBASE_RPC_TIMEOUT_PROPERTY, String.valueOf(300000));
-            }
-
-            if (!amsHbaseSiteProperties.containsKey(PHOENIX_QUERY_KEEPALIVE_PROPERTY)) {
-              newProperties.put(PHOENIX_QUERY_KEEPALIVE_PROPERTY, String.valueOf(300000));
-            }
-
-            if (!amsHbaseSiteProperties.containsKey(HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD_PROPERTY) ||
-              amsHbaseSiteProperties.get(HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD_PROPERTY).equals("900000")) {
-              amsHbaseSiteProperties.put(HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD_PROPERTY, String.valueOf(300000));
-            }
-
-            if (!amsHbaseSiteProperties.containsKey(PHOENIX_QUERY_TIMEOUT_PROPERTY) ||
-              amsHbaseSiteProperties.get(PHOENIX_QUERY_TIMEOUT_PROPERTY).equals("1200000")) {
-              amsHbaseSiteProperties.put(PHOENIX_QUERY_TIMEOUT_PROPERTY, String.valueOf(300000));
-            }
-            updateConfigurationPropertiesForCluster(cluster, AMS_HBASE_SITE, newProperties, true, true);
-          }
-
-        }
-      }
-    }
-  }
-
-  protected void updateHDFSWidgetDefinition() throws AmbariException {
-    LOG.info("Updating HDFS widget definition.");
-
-    Map<String, List<String>> widgetMap = new HashMap<>();
-    Map<String, String> sectionLayoutMap = new HashMap<>();
-
-    List<String> hdfsSummaryWidgets = new ArrayList<>(Arrays.asList("NameNode RPC", "NN Connection Load",
-      "NameNode GC count", "NameNode GC time", "NameNode Host Load"));
-    widgetMap.put("HDFS_SUMMARY", hdfsSummaryWidgets);
-    sectionLayoutMap.put("HDFS_SUMMARY", "default_hdfs_dashboard");
-
-    List<String> hdfsHeatmapWidgets = new ArrayList<>(Arrays.asList("HDFS Bytes Read", "HDFS Bytes Written",
-      "DataNode Process Disk I/O Utilization", "DataNode Process Network I/O Utilization"));
-    widgetMap.put("HDFS_HEATMAPS", hdfsHeatmapWidgets);
-    sectionLayoutMap.put("HDFS_HEATMAPS", "default_hdfs_heatmap");
-
-    updateWidgetDefinitionsForService("HDFS", widgetMap, sectionLayoutMap);
-  }
-
-  protected void updateYARNWidgetDefinition() throws AmbariException {
-    LOG.info("Updating YARN widget definition.");
-
-    Map<String, List<String>> widgetMap = new HashMap<>();
-    Map<String, String> sectionLayoutMap = new HashMap<>();
-
-    List<String> yarnSummaryWidgets = new ArrayList<>(Arrays.asList("Container Failures", "App Failures", "Cluster Memory"));
-    widgetMap.put("YARN_SUMMARY", yarnSummaryWidgets);
-    sectionLayoutMap.put("YARN_SUMMARY", "default_yarn_dashboard");
-
-    List<String> yarnHeatmapWidgets = new ArrayList<>(Arrays.asList("Container Failures"));
-    widgetMap.put("YARN_HEATMAPS", yarnHeatmapWidgets);
-    sectionLayoutMap.put("YARN_HEATMAPS", "default_yarn_heatmap");
-
-    updateWidgetDefinitionsForService("YARN", widgetMap, sectionLayoutMap);
-
-  }
-
-  protected void updateHBASEWidgetDefinition() throws AmbariException {
-
-    LOG.info("Updating HBASE widget definition.");
-
-    Map<String, List<String>> widgetMap = new HashMap<>();
-    Map<String, String> sectionLayoutMap = new HashMap<>();
-
-    List<String> hbaseSummaryWidgets = new ArrayList<>(Arrays.asList("Reads and Writes", "Blocked Updates"));
-    widgetMap.put("HBASE_SUMMARY", hbaseSummaryWidgets);
-    sectionLayoutMap.put("HBASE_SUMMARY", "default_hbase_dashboard");
-
-    updateWidgetDefinitionsForService("HBASE", widgetMap, sectionLayoutMap);
-  }
-
-
-  protected void updateHbaseEnvConfig() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config hbaseEnvConfig = cluster.getDesiredConfigByType(HBASE_ENV_CONFIG);
-      if (hbaseEnvConfig != null) {
-        Map<String, String> updates = getUpdatedHbaseEnvProperties(hbaseEnvConfig.getProperties().get(CONTENT_PROPERTY));
-        if (!updates.isEmpty()) {
-          updateConfigurationPropertiesForCluster(cluster, HBASE_ENV_CONFIG, updates, true, false);
-        }
-
-      }
-    }
-  }
-
-  protected Map<String, String> getUpdatedHbaseEnvProperties(String content) {
-    if (content != null) {
-      //Fix bad config added in Upgrade 2.2.0.
-      String badConfig = "export HBASE_OPTS=\"-Djava.io.tmpdir={{java_io_tmpdir}}\"";
-      String correctConfig = "export HBASE_OPTS=\"${HBASE_OPTS} -Djava.io.tmpdir={{java_io_tmpdir}}\"";
-
-      if (content.contains(badConfig)) {
-        content = content.replace(badConfig, correctConfig);
-        return Collections.singletonMap(CONTENT_PROPERTY, content);
-      }
-    }
-    return Collections.emptyMap();
-  }
-
-  protected void updateWidgetDefinitionsForService(String serviceName, Map<String, List<String>> widgetMap,
-                                                 Map<String, String> sectionLayoutMap) throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    AmbariMetaInfo ambariMetaInfo = injector.getInstance(AmbariMetaInfo.class);
-    Type widgetLayoutType = new TypeToken<Map<String, List<WidgetLayout>>>(){}.getType();
-    Gson gson = injector.getInstance(Gson.class);
-    WidgetDAO widgetDAO = injector.getInstance(WidgetDAO.class);
-
-    Clusters clusters = ambariManagementController.getClusters();
-
-    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
-    for (final Cluster cluster : clusterMap.values()) {
-      long clusterID = cluster.getClusterId();
-
-      StackId stackId = cluster.getDesiredStackVersion();
-      Map<String, Object> widgetDescriptor = null;
-      StackInfo stackInfo = ambariMetaInfo.getStack(stackId.getStackName(), stackId.getStackVersion());
-      ServiceInfo serviceInfo = stackInfo.getService(serviceName);
-      if (serviceInfo == null) {
-        LOG.info("Skipping updating widget definition, because " + serviceName +  " service is not present in cluster " +
-          "cluster_name= " + cluster.getClusterName());
-        continue;
-      }
-
-      for (String section : widgetMap.keySet()) {
-        List<String> widgets = widgetMap.get(section);
-        for (String widgetName : widgets) {
-          List<WidgetEntity> widgetEntities = widgetDAO.findByName(clusterID,
-            widgetName, "ambari", section);
-
-          if (widgetEntities != null && widgetEntities.size() > 0) {
-            WidgetEntity entityToUpdate = null;
-            if (widgetEntities.size() > 1) {
-              LOG.info("Found more that 1 entity with name = "+ widgetName +
-                " for cluster = " + cluster.getClusterName() + ", skipping update.");
-            } else {
-              entityToUpdate = widgetEntities.iterator().next();
-            }
-            if (entityToUpdate != null) {
-              LOG.info("Updating widget: " + entityToUpdate.getWidgetName());
-              // Get the definition from widgets.json file
-              WidgetLayoutInfo targetWidgetLayoutInfo = null;
-              File widgetDescriptorFile = serviceInfo.getWidgetsDescriptorFile();
-              if (widgetDescriptorFile != null && widgetDescriptorFile.exists()) {
-                try {
-                  widgetDescriptor = gson.fromJson(new FileReader(widgetDescriptorFile), widgetLayoutType);
-                } catch (Exception ex) {
-                  String msg = "Error loading widgets from file: " + widgetDescriptorFile;
-                  LOG.error(msg, ex);
-                  widgetDescriptor = null;
-                }
-              }
-              if (widgetDescriptor != null) {
-                LOG.debug("Loaded widget descriptor: " + widgetDescriptor);
-                for (Object artifact : widgetDescriptor.values()) {
-                  List<WidgetLayout> widgetLayouts = (List<WidgetLayout>) artifact;
-                  for (WidgetLayout widgetLayout : widgetLayouts) {
-                    if (widgetLayout.getLayoutName().equals(sectionLayoutMap.get(section))) {
-                      for (WidgetLayoutInfo layoutInfo : widgetLayout.getWidgetLayoutInfoList()) {
-                        if (layoutInfo.getWidgetName().equals(widgetName)) {
-                          targetWidgetLayoutInfo = layoutInfo;
-                        }
-                      }
-                    }
-                  }
-                }
-              }
-              if (targetWidgetLayoutInfo != null) {
-                entityToUpdate.setMetrics(gson.toJson(targetWidgetLayoutInfo.getMetricsInfo()));
-                entityToUpdate.setWidgetValues(gson.toJson(targetWidgetLayoutInfo.getValues()));
-                if ("HBASE".equals(serviceName) && "Reads and Writes".equals(widgetName)) {
-                  entityToUpdate.setDescription(targetWidgetLayoutInfo.getDescription());
-                  LOG.info("Update description for HBase Reads and Writes widget");
-                }
-                widgetDAO.merge(entityToUpdate);
-              } else {
-                LOG.warn("Unable to find widget layout info for " + widgetName +
-                  " in the stack: " + stackId);
-              }
-            }
-          }
-        }
-      }
-    }
-  }
-
-  protected void updateHiveConfig() throws AmbariException {
-    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
-    for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) {
-      Config hiveSiteConfig = cluster.getDesiredConfigByType(HIVE_SITE_CONFIG);
-      Config atlasConfig = cluster.getDesiredConfigByType(ATLAS_APPLICATION_PROPERTIES_CONFIG);
-
-      StackId stackId = cluster.getCurrentStackVersion();
-      boolean isStackNotLess23 = (stackId != null && stackId.getStackName().equals("HDP") &&
-        VersionUtils.compareVersions(stackId.getStackVersion(), "2.3") >= 0);
-
-      List<ServiceComponentHost> atlasHost = cluster.getServiceComponentHosts("ATLAS", "ATLAS_SERVER");
-      Map<String, String> updates = new HashMap<String, String>();
-
-      if (isStackNotLess23 && atlasHost.size() != 0 && hiveSiteConfig != null) {
-
-        updates.put(ATLAS_HOOK_HIVE_MINTHREADS_PROPERTY, "1");
-        updates.put(ATLAS_HOOK_HIVE_MAXTHREADS_PROPERTY, "1");
-        updates.put(ATLAS_CLUSTER_NAME_PROPERTY, "primary");
-
-        if (atlasConfig != null && atlasConfig.getProperties().containsKey(ATLAS_ENABLETLS_PROPERTY)) {
-          String atlasEnableTLSProperty = atlasConfig.getProperties().get(ATLAS_ENABLETLS_PROPERTY);
-          String atlasScheme = "http";
-          String atlasServerHttpPortProperty = atlasConfig.getProperties().get(ATLAS_SERVER_HTTP_PORT_PROPERTY);
-          if (atlasEnableTLSProperty.toLowerCase().equals("true")) {
-            atlasServerHttpPortProperty = atlasConfig.getProperties().get(ATLAS_SERVER_HTTPS_PORT_PROPERTY);
-            atlasScheme = "https";
-          }
-          updates.put(ATLAS_REST_ADDRESS_PROPERTY, String.format("%s://%s:%s", atlasScheme, atlasHost.get(0).getHostName(), atlasServerHttpPortProperty));
-        }
-        updateConfigurationPropertiesForCluster(cluster, HIVE_SITE_CONFIG, updates, false, false);
-      }
-    }
-  }
-
-  protected void updateCorruptedReplicaWidget() throws SQLException {
-    String widgetValues = String.format("[{\"name\": \"%s\", \"value\": \"%s\"}]",
-      WIDGET_CORRUPT_REPLICAS, WIDGET_VALUES_VALUE);
-    String updateStatement = "UPDATE %s SET %s='%s', %s='%s', %s='%s' WHERE %s='%s'";
-
-    LOG.info("Update widget definition for HDFS corrupted blocks metric");
-    dbAccessor.executeUpdate(String.format(updateStatement,
-      WIDGET_TABLE,
-      WIDGET_NAME, WIDGET_CORRUPT_REPLICAS,
-      WIDGET_DESCRIPTION, WIDGET_CORRUPT_REPLICAS_DESCRIPTION,
-      WIDGET_VALUES, widgetValues,
-      WIDGET_NAME, WIDGET_CORRUPT_BLOCKS
-    ));
-  }
-
-  /**
-   * Updates the {@value #UPGRADE_TABLE} in the following ways:
-   * <ul>
-   * <li>{value {@link #UPGRADE_SUSPENDED_COLUMN} is added</li>
-   * </ul>
-   *
-   * @throws AmbariException
-   * @throws SQLException
-   */
-  protected void updateUpgradeTable() throws AmbariException, SQLException {
-    dbAccessor.addColumn(UPGRADE_TABLE,
-      new DBAccessor.DBColumnInfo(UPGRADE_SUSPENDED_COLUMN, Short.class, 1, 0, false));
-  }
-
-  /**
-   * Copy cluster & service widgets for Storm and Kafka from stack to DB.
-   */
-  protected void initializeStromAndKafkaWidgets() throws AmbariException {
-    AmbariManagementController controller = injector.getInstance(AmbariManagementController.class);
-    Clusters clusters = controller.getClusters();
-    if (clusters == null) {
-      return;
-    }
-
-    Map<String, Cluster> clusterMap = clusters.getClusters();
-
-    if (clusterMap != null && !clusterMap.isEmpty()) {
-      for (Cluster cluster : clusterMap.values()) {
-
-        Map<String, Service> serviceMap = cluster.getServices();
-        if (serviceMap != null && !serviceMap.isEmpty()) {
-          for (Service service : serviceMap.values()) {
-            if ("STORM".equals(service.getName()) || "KAFKA".equals(service.getName())) {
-              controller.initializeWidgetsAndLayouts(cluster, service);
-            }
-          }
-        }
-      }
-    }
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/48f7fb22/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog230.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog230.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog230.java
deleted file mode 100644
index 6cda590..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog230.java
+++ /dev/null
@@ -1,402 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.upgrade;
-
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
-import org.apache.ambari.server.orm.dao.PermissionDAO;
-import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
-import org.apache.ambari.server.orm.dao.RoleAuthorizationDAO;
-import org.apache.ambari.server.orm.entities.PermissionEntity;
-import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
-import org.apache.ambari.server.orm.entities.RoleAuthorizationEntity;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-
-
-/**
- * Upgrade catalog for version 2.3.0.
- */
-public class UpgradeCatalog230 extends AbstractUpgradeCatalog {
-  private static final String HOST_ROLE_COMMAND_TABLE = "host_role_command";
-  private static final String USERS_TABLE = "users";
-
-  private static final String HOST_ID_COL = "host_id";
-  private static final String USER_TYPE_COL = "user_type";
-
-  private static final String ADMIN_PERMISSION_TABLE = "adminpermission";
-  private static final String PERMISSION_ID_COL = "permission_id";
-  private static final String PERMISSION_NAME_COL = "permission_name";
-  private static final String PERMISSION_LABEL_COL = "permission_label";
-
-  private static final String ROLE_AUTHORIZATION_TABLE = "roleauthorization";
-  private static final String PERMISSION_ROLE_AUTHORIZATION_TABLE = "permission_roleauthorization";
-  private static final String ROLE_AUTHORIZATION_ID_COL = "authorization_id";
-  private static final String ROLE_AUTHORIZATION_NAME_COL = "authorization_name";
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getSourceVersion() {
-    return "2.2.1";
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  public String getTargetVersion() {
-    return "2.3.0";
-  }
-
-  /**
-   * Logger.
-   */
-  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog230.class);
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
-   * @param injector Guice injector to track dependencies and uses bindings to inject them.
-   */
-  @Inject
-  public UpgradeCatalog230(Injector injector) {
-    super(injector);
-    this.injector = injector;
-  }
-
-  // ----- AbstractUpgradeCatalog --------------------------------------------
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-  protected void executeDDLUpdates() throws AmbariException, SQLException {
-
-    dbAccessor.alterColumn(HOST_ROLE_COMMAND_TABLE, new DBColumnInfo(HOST_ID_COL, Long.class, null, null, true));
-    dbAccessor.addColumn(USERS_TABLE, new DBColumnInfo(USER_TYPE_COL, String.class, null, "LOCAL", true));
-
-    dbAccessor.executeQuery("UPDATE users SET user_type='LDAP' WHERE ldap_user=1");
-
-    dbAccessor.addUniqueConstraint(USERS_TABLE, "UNQ_users_0", "user_name", "user_type");
-
-    updateAdminPermissionTable();
-    createRoleAuthorizationTables();
-  }
-
-  @Override
-  protected void executePreDMLUpdates() throws AmbariException, SQLException {
-  }
-
-  @Override
-  protected void executeDMLUpdates() throws AmbariException, SQLException {
-    setPermissionLabels();
-    updatePermissionNames();
-    addNewPermissions();
-    createRoleAuthorizations();
-    createPermissionRoleAuthorizationMap();
-  }
-
-  private void addNewPermissions() throws SQLException {
-    LOG.info("Adding new permissions: CLUSTER.OPERATOR, SERVICE.ADMINISTRATOR, SERVICE.OPERATOR");
-
-    PermissionDAO permissionDAO = injector.getInstance(PermissionDAO.class);
-    ResourceTypeDAO resourceTypeDAO = injector.getInstance(ResourceTypeDAO.class);
-    ResourceTypeEntity clusterResourceTypeEntity = resourceTypeDAO.findByName("CLUSTER");
-
-    // CLUSTER.OPERATOR: Cluster Operator
-    if(permissionDAO.findPermissionByNameAndType("CLUSTER.OPERATOR", clusterResourceTypeEntity) == null) {
-      PermissionEntity permissionEntity = new PermissionEntity();
-      permissionEntity.setId(null);
-      permissionEntity.setPermissionName("CLUSTER.OPERATOR");
-      permissionEntity.setPermissionLabel("Cluster Operator");
-      permissionEntity.setResourceType(clusterResourceTypeEntity);
-      permissionDAO.create(permissionEntity);
-    }
-
-    // SERVICE.ADMINISTRATOR: Service Administrator
-    if(permissionDAO.findPermissionByNameAndType("SERVICE.ADMINISTRATOR", clusterResourceTypeEntity) == null) {
-      PermissionEntity permissionEntity = new PermissionEntity();
-      permissionEntity.setId(null);
-      permissionEntity.setPermissionName("SERVICE.ADMINISTRATOR");
-      permissionEntity.setPermissionLabel("Service Administrator");
-      permissionEntity.setResourceType(clusterResourceTypeEntity);
-      permissionDAO.create(permissionEntity);
-    }
-
-    // SERVICE.OPERATOR: Service Operator
-    if(permissionDAO.findPermissionByNameAndType("SERVICE.OPERATOR", clusterResourceTypeEntity) == null) {
-      PermissionEntity permissionEntity = new PermissionEntity();
-      permissionEntity.setId(null);
-      permissionEntity.setPermissionName("SERVICE.OPERATOR");
-      permissionEntity.setPermissionLabel("Service Operator");
-      permissionEntity.setResourceType(clusterResourceTypeEntity);
-      permissionDAO.create(permissionEntity);
-    }
-  }
-
-
-  private void createRoleAuthorizations() throws SQLException {
-    LOG.info("Adding authorizations");
-
-    RoleAuthorizationDAO roleAuthorizationDAO = injector.getInstance(RoleAuthorizationDAO.class);
-
-    createRoleAuthorization(roleAuthorizationDAO, "VIEW.USE", "Use View");
-
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.VIEW_METRICS", "View metrics");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.VIEW_STATUS_INFO", "View status information");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.VIEW_CONFIGS", "View configurations");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.COMPARE_CONFIGS", "Compare configurations");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.VIEW_ALERTS", "View service-level alerts");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.START_STOP", "Start/Stop/Restart Service");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.DECOMMISSION_RECOMMISSION", "Decommission/recommission");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.RUN_SERVICE_CHECK", "Run service checks");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.TOGGLE_MAINTENANCE", "Turn on/off maintenance mode");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.RUN_CUSTOM_COMMAND", "Perform service-specific tasks");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.MODIFY_CONFIGS", "Modify configurations");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.MANAGE_CONFIG_GROUPS", "Manage configuration groups");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.MANAGE_ALERTS", "Manage service-level alerts");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.MOVE", "Move to another host");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.ENABLE_HA", "Enable HA");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.TOGGLE_ALERTS", "Enable/disable service-level alerts");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.ADD_DELETE_SERVICES", "Add/delete services");
-    createRoleAuthorization(roleAuthorizationDAO, "SERVICE.SET_SERVICE_USERS_GROUPS", "Set service users and groups");
-
-    createRoleAuthorization(roleAuthorizationDAO, "HOST.VIEW_METRICS", "View metrics");
-    createRoleAuthorization(roleAuthorizationDAO, "HOST.VIEW_STATUS_INFO", "View status information");
-    createRoleAuthorization(roleAuthorizationDAO, "HOST.VIEW_CONFIGS", "View configuration");
-    createRoleAuthorization(roleAuthorizationDAO, "HOST.TOGGLE_MAINTENANCE", "Turn on/off maintenance mode");
-    createRoleAuthorization(roleAuthorizationDAO, "HOST.ADD_DELETE_COMPONENTS", "Install components");
-    createRoleAuthorization(roleAuthorizationDAO, "HOST.ADD_DELETE_HOSTS", "Add/Delete hosts");
-
-    createRoleAuthorization(roleAuthorizationDAO, "CLUSTER.VIEW_METRICS", "View metrics");
-    createRoleAuthorization(roleAuthorizationDAO, "CLUSTER.VIEW_STATUS_INFO", "View status information");
-    createRoleAuthorization(roleAuthorizationDAO, "CLUSTER.VIEW_CONFIGS", "View configuration");
-    createRoleAuthorization(roleAuthorizationDAO, "CLUSTER.VIEW_STACK_DETAILS", "View stack version details");
-    createRoleAuthorization(roleAuthorizationDAO, "CLUSTER.VIEW_ALERTS", "View cluster-level alerts");
-    createRoleAuthorization(roleAuthorizationDAO, "CLUSTER.MANAGE_CREDENTIALS", "Manage external credentials");
-    createRoleAuthorization(roleAuthorizationDAO, "CLUSTER.MODIFY_CONFIGS", "Modify cluster configurations");
-    createRoleAuthorization(roleAuthorizationDAO, "CLUSTER.MANAGE_CONFIG_GROUPS", "Manage cluster configuration groups");
-    createRoleAuthorization(roleAuthorizationDAO, "CLUSTER.MANAGE_ALERTS", "Manage cluster-level alerts");
-    createRoleAuthorization(roleAuthorizationDAO, "CLUSTER.TOGGLE_ALERTS", "Enable/disable cluster-level alerts");
-    createRoleAuthorization(roleAuthorizationDAO, "CLUSTER.TOGGLE_KERBEROS", "Enable/disable Kerberos");
-    createRoleAuthorization(roleAuthorizationDAO, "CLUSTER.UPGRADE_DOWNGRADE_STACK", "Upgrade/downgrade stack");
-
-    createRoleAuthorization(roleAuthorizationDAO, "AMBARI.ADD_DELETE_CLUSTERS", "Create new clusters");
-    createRoleAuthorization(roleAuthorizationDAO, "AMBARI.RENAME_CLUSTER", "Rename clusters");
-    createRoleAuthorization(roleAuthorizationDAO, "AMBARI.MANAGE_USERS", "Manage users");
-    createRoleAuthorization(roleAuthorizationDAO, "AMBARI.MANAGE_GROUPS", "Manage groups");
-    createRoleAuthorization(roleAuthorizationDAO, "AMBARI.MANAGE_VIEWS", "Manage Ambari Views");
-    createRoleAuthorization(roleAuthorizationDAO, "AMBARI.ASSIGN_ROLES", "Assign roles");
-    createRoleAuthorization(roleAuthorizationDAO, "AMBARI.MANAGE_STACK_VERSIONS", "Manage stack versions");
-    createRoleAuthorization(roleAuthorizationDAO, "AMBARI.EDIT_STACK_REPOS", "Edit stack repository URLs");
-  }
-
-  private void createRoleAuthorization(RoleAuthorizationDAO roleAuthorizationDAO, String id, String name) {
-    if(roleAuthorizationDAO.findById(id) == null) {
-      RoleAuthorizationEntity roleAuthorizationEntity = new RoleAuthorizationEntity();
-      roleAuthorizationEntity.setAuthorizationId(id);
-      roleAuthorizationEntity.setAuthorizationName(name);
-      roleAuthorizationDAO.create(roleAuthorizationEntity);
-    }
-  }
-
-  private void createPermissionRoleAuthorizationMap() throws SQLException {
-    LOG.info("Creating permission to authorizations map");
-
-    // Determine the role entities
-    PermissionDAO permissionDAO = injector.getInstance(PermissionDAO.class);
-    ResourceTypeDAO resourceTypeDAO = injector.getInstance(ResourceTypeDAO.class);
-
-    ResourceTypeEntity ambariResource = resourceTypeDAO.findByName("AMBARI");
-    ResourceTypeEntity clusterResource = resourceTypeDAO.findByName("CLUSTER");
-    ResourceTypeEntity viewResource = resourceTypeDAO.findByName("VIEW");
-
-    PermissionEntity viewPermission = permissionDAO.findPermissionByNameAndType("VIEW.USER", viewResource);
-    PermissionEntity administratorPermission = permissionDAO.findPermissionByNameAndType("AMBARI.ADMINISTRATOR", ambariResource);
-    PermissionEntity clusterUserPermission = permissionDAO.findPermissionByNameAndType("CLUSTER.USER", clusterResource);
-    PermissionEntity clusterOperatorPermission = permissionDAO.findPermissionByNameAndType("CLUSTER.OPERATOR", clusterResource);
-    PermissionEntity clusterAdministratorPermission = permissionDAO.findPermissionByNameAndType("CLUSTER.ADMINISTRATOR", clusterResource);
-    PermissionEntity serviceAdministratorPermission = permissionDAO.findPermissionByNameAndType("SERVICE.ADMINISTRATOR", clusterResource);
-    PermissionEntity serviceOperatorPermission = permissionDAO.findPermissionByNameAndType("SERVICE.OPERATOR", clusterResource);
-
-    // Create role groups
-    Collection<PermissionEntity> viewUserAndAdministrator = Arrays.asList(viewPermission, administratorPermission);
-    Collection<PermissionEntity> clusterUserAndUp = Arrays.asList(
-        clusterUserPermission,
-        serviceOperatorPermission,
-        serviceAdministratorPermission,
-        clusterOperatorPermission,
-        clusterAdministratorPermission,
-        administratorPermission);
-    Collection<PermissionEntity> serviceOperatorAndUp = Arrays.asList(
-        serviceOperatorPermission,
-        serviceAdministratorPermission,
-        clusterOperatorPermission,
-        clusterAdministratorPermission,
-        administratorPermission);
-    Collection<PermissionEntity> serviceAdministratorAndUp = Arrays.asList(
-        serviceAdministratorPermission,
-        clusterOperatorPermission,
-        clusterAdministratorPermission,
-        administratorPermission);
-    Collection<PermissionEntity> clusterOperatorAndUp = Arrays.asList(
-        clusterOperatorPermission,
-        clusterAdministratorPermission,
-        administratorPermission);
-    Collection<PermissionEntity> clusterAdministratorAndUp = Arrays.asList(
-        clusterAdministratorPermission,
-        administratorPermission);
-    Collection<PermissionEntity> administratorOnly = Collections.singleton(administratorPermission);
-
-    // A map of the authorizations to the relevant roles
-    Map<String, Collection<PermissionEntity>> map = new HashMap<String, Collection<PermissionEntity>>();
-    map.put("VIEW.USE", viewUserAndAdministrator);
-    map.put("SERVICE.VIEW_METRICS", clusterUserAndUp);
-    map.put("SERVICE.VIEW_STATUS_INFO", clusterUserAndUp);
-    map.put("SERVICE.VIEW_CONFIGS", clusterUserAndUp);
-    map.put("SERVICE.COMPARE_CONFIGS", clusterUserAndUp);
-    map.put("SERVICE.VIEW_ALERTS", clusterUserAndUp);
-    map.put("SERVICE.START_STOP", serviceOperatorAndUp);
-    map.put("SERVICE.DECOMMISSION_RECOMMISSION", serviceOperatorAndUp);
-    map.put("SERVICE.RUN_SERVICE_CHECK", serviceOperatorAndUp);
-    map.put("SERVICE.TOGGLE_MAINTENANCE", serviceOperatorAndUp);
-    map.put("SERVICE.RUN_CUSTOM_COMMAND", serviceOperatorAndUp);
-    map.put("SERVICE.MODIFY_CONFIGS", serviceAdministratorAndUp);
-    map.put("SERVICE.MANAGE_CONFIG_GROUPS", serviceAdministratorAndUp);
-    map.put("CLUSTER.MANAGE_CONFIG_GROUPS", serviceAdministratorAndUp);
-    map.put("SERVICE.MANAGE_ALERTS", serviceAdministratorAndUp);
-    map.put("SERVICE.MOVE", serviceAdministratorAndUp);
-    map.put("SERVICE.ENABLE_HA", serviceAdministratorAndUp);
-    map.put("SERVICE.TOGGLE_ALERTS", serviceAdministratorAndUp);
-    map.put("SERVICE.ADD_DELETE_SERVICES", clusterAdministratorAndUp);
-    map.put("SERVICE.SET_SERVICE_USERS_GROUPS", clusterAdministratorAndUp);
-    map.put("HOST.VIEW_METRICS", clusterUserAndUp);
-    map.put("HOST.VIEW_STATUS_INFO", clusterUserAndUp);
-    map.put("HOST.VIEW_CONFIGS", clusterUserAndUp);
-    map.put("HOST.TOGGLE_MAINTENANCE", clusterOperatorAndUp);
-    map.put("HOST.ADD_DELETE_COMPONENTS", clusterOperatorAndUp);
-    map.put("HOST.ADD_DELETE_HOSTS", clusterOperatorAndUp);
-    map.put("CLUSTER.VIEW_METRICS", clusterUserAndUp);
-    map.put("CLUSTER.VIEW_STATUS_INFO", clusterUserAndUp);
-    map.put("CLUSTER.VIEW_CONFIGS", clusterUserAndUp);
-    map.put("CLUSTER.VIEW_STACK_DETAILS", clusterUserAndUp);
-    map.put("CLUSTER.VIEW_ALERTS", clusterUserAndUp);
-    map.put("CLUSTER.MANAGE_CREDENTIALS", clusterAdministratorAndUp);
-    map.put("CLUSTER.MODIFY_CONFIGS", clusterAdministratorAndUp);
-    map.put("CLUSTER.MANAGE_ALERTS", clusterAdministratorAndUp);
-    map.put("CLUSTER.TOGGLE_ALERTS", clusterAdministratorAndUp);
-    map.put("CLUSTER.TOGGLE_KERBEROS", clusterAdministratorAndUp);
-    map.put("CLUSTER.UPGRADE_DOWNGRADE_STACK", clusterAdministratorAndUp);
-    map.put("AMBARI.ADD_DELETE_CLUSTERS", administratorOnly);
-    map.put("AMBARI.RENAME_CLUSTER", administratorOnly);
-    map.put("AMBARI.MANAGE_USERS", administratorOnly);
-    map.put("AMBARI.MANAGE_GROUPS", administratorOnly);
-    map.put("AMBARI.MANAGE_VIEWS", administratorOnly);
-    map.put("AMBARI.ASSIGN_ROLES", administratorOnly);
-    map.put("AMBARI.MANAGE_STACK_VERSIONS", administratorOnly);
-    map.put("AMBARI.EDIT_STACK_REPOS", administratorOnly);
-
-    // Iterate over the map of authorizations to role to find the set of roles to map to each
-    // authorization and then add the relevant record
-    for (Map.Entry<String, Collection<PermissionEntity>> entry : map.entrySet()) {
-      String authorizationId = entry.getKey();
-
-      for (PermissionEntity permission : entry.getValue()) {
-        addAuthorizationToRole(permission, authorizationId);
-      }
-    }
-  }
-
-
-  // ----- UpgradeCatalog ----------------------------------------------------
-
-  private void updateAdminPermissionTable() throws SQLException {
-    // Add the permission_label column to the adminpermission table
-    dbAccessor.addColumn(ADMIN_PERMISSION_TABLE, new DBColumnInfo(PERMISSION_LABEL_COL, String.class, 255, null, true));
-  }
-
-  private void createRoleAuthorizationTables() throws SQLException {
-
-    ArrayList<DBColumnInfo> columns;
-
-    //  Add roleauthorization table
-    LOG.info("Creating " + ROLE_AUTHORIZATION_TABLE + " table");
-    columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBColumnInfo(ROLE_AUTHORIZATION_ID_COL, String.class, 100, null, false));
-    columns.add(new DBColumnInfo(ROLE_AUTHORIZATION_NAME_COL, String.class, 255, null, false));
-    dbAccessor.createTable(ROLE_AUTHORIZATION_TABLE, columns, ROLE_AUTHORIZATION_ID_COL);
-
-    //  Add permission_roleauthorization table to map roleauthorizations to permissions (aka roles)
-    LOG.info("Creating " + PERMISSION_ROLE_AUTHORIZATION_TABLE + " table");
-    columns = new ArrayList<DBColumnInfo>();
-    columns.add(new DBColumnInfo(PERMISSION_ID_COL, Long.class, null, null, false));
-    columns.add(new DBColumnInfo(ROLE_AUTHORIZATION_ID_COL, String.class, 100, null, false));
-    dbAccessor.createTable(PERMISSION_ROLE_AUTHORIZATION_TABLE, columns, PERMISSION_ID_COL, ROLE_AUTHORIZATION_ID_COL);
-
-    dbAccessor.addFKConstraint(PERMISSION_ROLE_AUTHORIZATION_TABLE, "FK_permission_roleauth_pid",
-        PERMISSION_ID_COL, ADMIN_PERMISSION_TABLE, PERMISSION_ID_COL, false);
-
-    dbAccessor.addFKConstraint(PERMISSION_ROLE_AUTHORIZATION_TABLE, "FK_permission_roleauth_aid",
-        ROLE_AUTHORIZATION_ID_COL, ROLE_AUTHORIZATION_TABLE, ROLE_AUTHORIZATION_ID_COL, false);
-  }
-
-  private void setPermissionLabels() throws SQLException {
-    String updateStatement = "UPDATE " + ADMIN_PERMISSION_TABLE + " SET " + PERMISSION_LABEL_COL + "='%s' WHERE " + PERMISSION_ID_COL + "=%d";
-
-    LOG.info("Setting permission labels");
-    dbAccessor.executeUpdate(String.format(updateStatement,
-        "Ambari Administrator", PermissionEntity.AMBARI_ADMINISTRATOR_PERMISSION));
-    dbAccessor.executeUpdate(String.format(updateStatement,
-        "Cluster User", PermissionEntity.CLUSTER_USER_PERMISSION));
-    dbAccessor.executeUpdate(String.format(updateStatement,
-        "Cluster Administrator", PermissionEntity.CLUSTER_ADMINISTRATOR_PERMISSION));
-    dbAccessor.executeUpdate(String.format(updateStatement,
-        "View User", PermissionEntity.VIEW_USER_PERMISSION));
-  }
-
-  private void updatePermissionNames() throws SQLException {
-    String updateStatement = "UPDATE " + ADMIN_PERMISSION_TABLE + " SET " + PERMISSION_NAME_COL + "='%s' WHERE " + PERMISSION_ID_COL + "=%d";
-
-    // Update permissions names
-    LOG.info("Updating permission names");
-    dbAccessor.executeUpdate(String.format(updateStatement,
-        PermissionEntity.AMBARI_ADMINISTRATOR_PERMISSION_NAME, PermissionEntity.AMBARI_ADMINISTRATOR_PERMISSION));
-    dbAccessor.executeUpdate(String.format(updateStatement,
-        PermissionEntity.CLUSTER_USER_PERMISSION_NAME, PermissionEntity.CLUSTER_USER_PERMISSION));
-    dbAccessor.executeUpdate(String.format(updateStatement,
-        PermissionEntity.CLUSTER_ADMINISTRATOR_PERMISSION_NAME, PermissionEntity.CLUSTER_ADMINISTRATOR_PERMISSION));
-    dbAccessor.executeUpdate(String.format(updateStatement,
-        PermissionEntity.VIEW_USER_PERMISSION_NAME, PermissionEntity.VIEW_USER_PERMISSION));
-  }
-
-}