@Transactional
  CommandReport updateDesiredRepositoryVersion(
      Cluster cluster, UpgradeContext upgradeContext, String userName)
      throws AmbariException, InterruptedException {

    StringBuilder out = new StringBuilder();
    StringBuilder err = new StringBuilder();

    try {
      // 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());

        // move the cluster's desired stack as well
        StackId targetStackId = targetRepositoryVersion.getStackId();
        cluster.setDesiredStackVersion(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());

        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());
        }
      }

      // move repositories to the right version and create/revert configs
      m_upgradeHelper.updateDesiredRepositoriesAndConfigs(upgradeContext);

      // 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()));

        List<HostVersionEntity> hostVersionsToReset = m_hostVersionDAO.findHostVersionByClusterAndRepository(
            cluster.getClusterId(), downgradeFromRepositoryVersion);

        for (HostVersionEntity hostVersion : hostVersionsToReset) {
          if( hostVersion.getState() != RepositoryVersionState.NOT_REQUIRED ){
            hostVersion.setState(RepositoryVersionState.INSTALLED);
          }
        }

        // move the cluster's desired stack back to it's current stack on downgrade
        StackId targetStackId = cluster.getCurrentStackVersion();
        cluster.setDesiredStackVersion(targetStackId);
      }

      return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", out.toString(), err.toString());
    } catch (Exception e) {
      StringWriter sw = new StringWriter();
      e.printStackTrace(new PrintWriter(sw));
      err.append(sw);

      return createCommandReport(-1, HostRoleStatus.FAILED, "{}", out.toString(), err.toString());
    }
  }
Sets the desired repository version for services participating in the
upgrade.

@param cluster
         the cluster
@param upgradeContext
         the upgrade context
@param userName
         username performing the action
@return the command report to return
ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/UpdateDesiredRepositoryAction.java