package com.cloudera.cmf.service.config;

import com.cloudera.cmf.Constants;
import com.cloudera.cmf.service.ServiceDataProvider;
import com.cloudera.cmf.service.hdfs.HdfsServiceHandler.RoleNames;
import com.cloudera.cmf.service.kms.KmsConnector;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableRangeMap;
import com.google.common.collect.ImmutableSet;
import java.net.URI;
import java.util.List;

public class KmsUrlEvaluator extends AbstractGenericConfigEvaluator
{
  public KmsUrlEvaluator(String propertyName)
  {
    super(ImmutableSet.of(HdfsServiceHandler.RoleNames.NAMENODE, HdfsServiceHandler.RoleNames.DATANODE, HdfsServiceHandler.RoleNames.HTTPFS, HdfsServiceHandler.RoleNames.NFSGATEWAY, HdfsServiceHandler.RoleNames.GATEWAY), ImmutableRangeMap.of(Constants.SERVICE_ALL_VERSIONS_RANGE, propertyName));
  }

  protected List<EvaluatedConfig> evaluateConfig(ConfigEvaluationContext context, String propertyName)
    throws ConfigGenException
  {
    Preconditions.checkNotNull(context.getService(), "Context has no service: " + context);

    KmsConnector kmsConnector = (KmsConnector)ConfigEvaluatorHelpers.getDependencyConnector(context.getSdp().getServiceHandlerRegistry(), context.getService(), KmsConnector.CONNECTOR_TYPE);

    if (null != kmsConnector) {
      URI uri = kmsConnector.getUri();
      if (null != uri) {
        return ImmutableList.of(new EvaluatedConfig(propertyName, uri.toString()));
      }
    }
    return ImmutableList.of();
  }
}