// Path: src/main/java/org/trustedanalytics/servicebroker/hdfs/path/HdfsBrokerInstancePath.java
// public final class HdfsBrokerInstancePath {
//   private static final String UUID_REGEX = "[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}";
//   private static final String INSTANCE = "instance";
//   private static final String NAMESPACE = "namespace";
//   private static final String ORG = "org";
//   private static final String HDFS_URI_REGEX = String.format(
//       "^hdfs://(?<%s>\\w+)/org/(?<%s>%s)/brokers/userspace/(?<%s>%s)/", NAMESPACE, ORG, UUID_REGEX,
//       INSTANCE, UUID_REGEX);
// 
//   @Getter
//   private final String hdfsUri;
//   @Getter
//   private final UUID orgId;
//   @Getter
//   private final UUID instanceId;
//   @Getter
//   private final String namespace;
// 
//   private HdfsBrokerInstancePath(String hdfsUri, String namespace, UUID orgId, UUID instanceId) {
//     this.hdfsUri = hdfsUri;
//     this.orgId = orgId;
//     this.instanceId = instanceId;
//     this.namespace = namespace;
//   }
// 
//   public static HdfsBrokerInstancePath createInstance(String hdfsUri) throws ServiceBrokerException {
//     Matcher matcher = Pattern.compile(HDFS_URI_REGEX).matcher(hdfsUri);
//     if (!matcher.find()) {
//       throw new ServiceBrokerException(
//           "Invalid hdfs path, doesn't match template: hdfs://<namespace>/org/<uuid>/brokers/userspace/<uuid>/ - "
//               + hdfsUri);
//     }
//     return new HdfsBrokerInstancePath(hdfsUri, matcher.group(NAMESPACE), UUID.fromString(matcher
//         .group(ORG)), UUID.fromString(matcher.group(INSTANCE)));
//   }
// 
// }
// 
// Path: src/main/java/org/trustedanalytics/servicebroker/hdfs/plans/binding/HdfsSpecificOrgBindingOperations.java
// public interface HdfsSpecificOrgBindingOperations {
//   Map<String, Object> createCredentialsMap(UUID instanceId, UUID orgId);
// }


import org.trustedanalytics.servicebroker.hdfs.plans.binding.HdfsSpecificOrgBindingOperations;
import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.cloudfoundry.community.servicebroker.exception.ServiceBrokerException;
import org.cloudfoundry.community.servicebroker.exception.ServiceInstanceExistsException;
import org.cloudfoundry.community.servicebroker.model.ServiceInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.trustedanalytics.servicebroker.framework.service.ServicePlanDefinition;
import org.trustedanalytics.servicebroker.framework.store.CredentialsStore;
import org.trustedanalytics.servicebroker.hdfs.path.HdfsBrokerInstancePath;
/**
 * Copyright (c) 2015 Intel Corporation
 *
 * Licensed 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.trustedanalytics.servicebroker.hdfs.plans;




@Component("get-user-directory")
class HdfsPlanGetUserDirectory implements ServicePlanDefinition {

  private static final Logger LOGGER = LoggerFactory.getLogger(HdfsPlanCreateUserDirectory.class);
  private static final String URI_KEY = "uri";

  private final HdfsSpecificOrgBindingOperations bindingOperations;
  private final CredentialsStore credentialsStore;

  @Autowired
  public HdfsPlanGetUserDirectory(HdfsSpecificOrgBindingOperations bindingOperations,
      CredentialsStore zookeeperCredentialsStore) {
    this.bindingOperations = bindingOperations;
    this.credentialsStore = zookeeperCredentialsStore;
  }

  @Override
  public void provision(ServiceInstance serviceInstance, Optional<Map<String, Object>> parameters)
      throws ServiceInstanceExistsException, ServiceBrokerException {
    if (!isMapNotNullAndNotEmpty(parameters)) {
      throw new ServiceBrokerException("This plan require parametere uri");
    }
    String uri =
        getParameterUri(parameters.get(), URI_KEY).orElseThrow(
            () -> new ServiceBrokerException("No required parameter uri")).toString();
    LOGGER.info("Detected parameter path: " + uri);

// Path: src/main/java/org/trustedanalytics/servicebroker/hdfs/path/HdfsBrokerInstancePath.java
// public final class HdfsBrokerInstancePath {
//   private static final String UUID_REGEX = "[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}";
//   private static final String INSTANCE = "instance";
//   private static final String NAMESPACE = "namespace";
//   private static final String ORG = "org";
//   private static final String HDFS_URI_REGEX = String.format(
//       "^hdfs://(?<%s>\\w+)/org/(?<%s>%s)/brokers/userspace/(?<%s>%s)/", NAMESPACE, ORG, UUID_REGEX,
//       INSTANCE, UUID_REGEX);
// 
//   @Getter
//   private final String hdfsUri;
//   @Getter
//   private final UUID orgId;
//   @Getter
//   private final UUID instanceId;
//   @Getter
//   private final String namespace;
// 
//   private HdfsBrokerInstancePath(String hdfsUri, String namespace, UUID orgId, UUID instanceId) {
//     this.hdfsUri = hdfsUri;
//     this.orgId = orgId;
//     this.instanceId = instanceId;
//     this.namespace = namespace;
//   }
// 
//   public static HdfsBrokerInstancePath createInstance(String hdfsUri) throws ServiceBrokerException {
//     Matcher matcher = Pattern.compile(HDFS_URI_REGEX).matcher(hdfsUri);
//     if (!matcher.find()) {
//       throw new ServiceBrokerException(
//           "Invalid hdfs path, doesn't match template: hdfs://<namespace>/org/<uuid>/brokers/userspace/<uuid>/ - "
//               + hdfsUri);
//     }
//     return new HdfsBrokerInstancePath(hdfsUri, matcher.group(NAMESPACE), UUID.fromString(matcher
//         .group(ORG)), UUID.fromString(matcher.group(INSTANCE)));
//   }
// 
// }
// 
// Path: src/main/java/org/trustedanalytics/servicebroker/hdfs/plans/binding/HdfsSpecificOrgBindingOperations.java
// public interface HdfsSpecificOrgBindingOperations {
//   Map<String, Object> createCredentialsMap(UUID instanceId, UUID orgId);
// }


// Path: src/main/java/org/trustedanalytics/servicebroker/hdfs/plans/HdfsPlanGetUserDirectory.java
import org.trustedanalytics.servicebroker.hdfs.plans.binding.HdfsSpecificOrgBindingOperations;
import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.cloudfoundry.community.servicebroker.exception.ServiceBrokerException;
import org.cloudfoundry.community.servicebroker.exception.ServiceInstanceExistsException;
import org.cloudfoundry.community.servicebroker.model.ServiceInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.trustedanalytics.servicebroker.framework.service.ServicePlanDefinition;
import org.trustedanalytics.servicebroker.framework.store.CredentialsStore;
import org.trustedanalytics.servicebroker.hdfs.path.HdfsBrokerInstancePath;

/**
 * Copyright (c) 2015 Intel Corporation
 *
 * Licensed 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.trustedanalytics.servicebroker.hdfs.plans;




@Component("get-user-directory")
class HdfsPlanGetUserDirectory implements ServicePlanDefinition {

  private static final Logger LOGGER = LoggerFactory.getLogger(HdfsPlanCreateUserDirectory.class);
  private static final String URI_KEY = "uri";

  private final HdfsSpecificOrgBindingOperations bindingOperations;
  private final CredentialsStore credentialsStore;

  @Autowired
  public HdfsPlanGetUserDirectory(HdfsSpecificOrgBindingOperations bindingOperations,
      CredentialsStore zookeeperCredentialsStore) {
    this.bindingOperations = bindingOperations;
    this.credentialsStore = zookeeperCredentialsStore;
  }

  @Override
  public void provision(ServiceInstance serviceInstance, Optional<Map<String, Object>> parameters)
      throws ServiceInstanceExistsException, ServiceBrokerException {
    if (!isMapNotNullAndNotEmpty(parameters)) {
      throw new ServiceBrokerException("This plan require parametere uri");
    }
    String uri =
        getParameterUri(parameters.get(), URI_KEY).orElseThrow(
            () -> new ServiceBrokerException("No required parameter uri")).toString();
    LOGGER.info("Detected parameter path: " + uri);

    HdfsBrokerInstancePath instance = HdfsBrokerInstancePath.createInstance(uri);