class UpgradeUserKerberosDescriptor extends AbstractUpgradeServerAction {

  @Override
  public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext)
      throws AmbariException, InterruptedException {
    HostRoleCommand hostRoleCommand = getHostRoleCommand();
    String clusterName = hostRoleCommand.getExecutionCommandWrapper().getExecutionCommand().getClusterName();
    Cluster cluster = getClusters().getCluster(clusterName);
    List<String> messages = new ArrayList<>();
    List<String> errorMessages = new ArrayList<>();

    UpgradeContext upgradeContext = getUpgradeContext(cluster);

    logMessage(messages, "Obtaining the user-defined Kerberos descriptor");

    TreeMap<String, String> foreignKeys = new TreeMap<>();
    foreignKeys.put("cluster", String.valueOf(cluster.getClusterId()));

    ArtifactEntity entity = artifactDAO.findByNameAndForeignKeys("kerberos_descriptor", foreignKeys);
    KerberosDescriptor userDescriptor = (entity == null) ? null : kerberosDescriptorFactory.createInstance(entity.getArtifactData());

    if (userDescriptor != null) {

      @Experimental(
          feature = ExperimentalFeature.PATCH_UPGRADES,
          comment = "This needs to be correctly done per-service")

      StackId originalStackId = cluster.getCurrentStackVersion();
      StackId targetStackId = upgradeContext.getRepositoryVersion().getStackId();

      if (upgradeContext.getDirection() == Direction.DOWNGRADE) {
        restoreDescriptor(foreignKeys, messages, errorMessages);
      } else {
        backupDescriptor(foreignKeys, messages, errorMessages);

        KerberosDescriptor newDescriptor = null;
        KerberosDescriptor previousDescriptor = null;

        if (targetStackId == null) {
          logErrorMessage(messages, errorMessages, "The new stack version information was not found.");
        } else {
          logMessage(messages, String.format("Obtaining new stack Kerberos descriptor for %s.", targetStackId.toString()));
          newDescriptor = ambariMetaInfo.getKerberosDescriptor(targetStackId.getStackName(), targetStackId.getStackVersion(), false);

          if (newDescriptor == null) {
            logErrorMessage(messages, errorMessages, String.format("The Kerberos descriptor for the new stack version, %s, was not found.", targetStackId.toString()));
          }
        }

        if (originalStackId == null) {
          logErrorMessage(messages, errorMessages, "The previous stack version information was not found.");
        } else {
          logMessage(messages, String.format("Obtaining previous stack Kerberos descriptor for %s.", originalStackId.toString()));
          previousDescriptor = ambariMetaInfo.getKerberosDescriptor(originalStackId.getStackName(), originalStackId.getStackVersion(), false);

          if (newDescriptor == null) {
            logErrorMessage(messages, errorMessages, String.format("The Kerberos descriptor for the previous stack version, %s, was not found.", originalStackId.toString()));
          }
        }

        if (errorMessages.isEmpty()) {
          logMessage(messages, "Updating the user-specified Kerberos descriptor.");

          KerberosDescriptor updatedDescriptor = KerberosDescriptorUpdateHelper.updateUserKerberosDescriptor(
              previousDescriptor,
              newDescriptor,
              userDescriptor);

          logMessage(messages, "Storing updated user-specified Kerberos descriptor.");

          entity.setArtifactData(updatedDescriptor.toMap());
          artifactDAO.merge(entity);

          logMessage(messages, "Successfully updated the user-specified Kerberos descriptor.");
        }
      }
    } else {
      logMessage(messages, "A user-specified Kerberos descriptor was not found. No updates are necessary.");
    }

    if (!errorMessages.isEmpty()) {
      logErrorMessage(messages, errorMessages, "No updates have been performed due to previous issues.");
    }

    return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", StringUtils.join(messages, "\n"), StringUtils.join(errorMessages, "\n"));
  }

  protected  UpgradeUserKerberosDescriptor();

}