class HiveDynamicServiceDiscoveryCheck extends AbstractCheckDescriptor {

  @Override
  public void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request) throws AmbariException {
    List<String> errorMessages = new ArrayList<>();

    String dynamicServiceDiscoveryEnabled = getProperty(request, "hive-site", "hive.server2.support.dynamic.service.discovery");
    String zookeeperQuorum = getProperty(request, "hive-site", "hive.zookeeper.quorum");
    String zookeeperNamespace = getProperty(request, "hive-site", "hive.server2.zookeeper.namespace");

    if (null == dynamicServiceDiscoveryEnabled || !Boolean.parseBoolean(dynamicServiceDiscoveryEnabled)) {
      errorMessages.add(getFailReason(HIVE_DYNAMIC_SERVICE_DISCOVERY_ENABLED_KEY, prerequisiteCheck, request));
    }

    if (StringUtils.isBlank(zookeeperQuorum)) {
      errorMessages.add(getFailReason(HIVE_DYNAMIC_SERVICE_ZK_QUORUM_KEY, prerequisiteCheck,
          request));
    }

    if (StringUtils.isBlank(zookeeperNamespace)) {
      errorMessages.add(getFailReason(HIVE_DYNAMIC_SERVICE_ZK_NAMESPACE_KEY, prerequisiteCheck,
          request));
    }

    String minFailureStackVersion = null;
    PrerequisiteCheckConfig prerequisiteCheckConfig = request.getPrerequisiteCheckConfig();
    Map<String, String> checkProperties = null;
    if(prerequisiteCheckConfig != null) {
      checkProperties = prerequisiteCheckConfig.getCheckProperties(this.getClass().getName());
    }
    if(checkProperties != null && checkProperties.containsKey(MIN_FAILURE_STACK_VERSION_PROPERTY_NAME)) {
      minFailureStackVersion = checkProperties.get(MIN_FAILURE_STACK_VERSION_PROPERTY_NAME);
    }

    if (!errorMessages.isEmpty()) {
      prerequisiteCheck.setFailReason(StringUtils.join(errorMessages, " "));
      prerequisiteCheck.getFailedOn().add("HIVE");
      PrereqCheckStatus checkStatus = PrereqCheckStatus.FAIL;
      if(minFailureStackVersion != null && !minFailureStackVersion.isEmpty()) {
        String[] minStack = minFailureStackVersion.split("-");
        if (minStack.length == 2) {
          String minStackName = minStack[0];
          String minStackVersion = minStack[1];
          if (minStackName.equals(request.getSourceStackId().getStackName())) {
            RepositoryVersionEntity repositoryVersion = request.getTargetRepositoryVersion();
            StackId targetStackId = repositoryVersion.getStackId();
            if (VersionUtils.compareVersions(request.getSourceStackId().getStackVersion(), minStackVersion) < 0
                && VersionUtils.compareVersions(targetStackId.getStackVersion(), minStackVersion) < 0
                && VersionUtils.compareVersions(request.getSourceStackId().getStackVersion(), targetStackId.getStackVersion()) < 0) {
              checkStatus = PrereqCheckStatus.WARNING;
            }
          }
        }
      }
      prerequisiteCheck.setStatus(checkStatus);
    }
  }

  public  HiveDynamicServiceDiscoveryCheck();

}