/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * 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.
 * </p>
 */

package io.shardingsphere.jdbc.orchestration.api.yaml;

import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingsphere.core.rule.ShardingRule;
import io.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration;
import io.shardingsphere.jdbc.orchestration.config.OrchestrationConfiguration;
import io.shardingsphere.jdbc.orchestration.internal.OrchestrationFacade;
import io.shardingsphere.jdbc.orchestration.api.datasource.OrchestrationShardingDataSource;
import io.shardingsphere.jdbc.orchestration.internal.yaml.YamlOrchestrationShardingRuleConfiguration;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import javax.sql.DataSource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.Map;

/**
 * Orchestration sharding data source factory for YAML.
 *
 * @author zhangliang
 * @author caohao
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class YamlOrchestrationShardingDataSourceFactory {
    
    /**
     * Create sharding data source.
     *
     * @param yamlFile yaml file for rule configuration of databases and tables sharding with data sources
     * @return sharding data source
     * @throws SQLException SQL exception
     * @throws IOException IO exception
     */
    public static DataSource createDataSource(final File yamlFile) throws SQLException, IOException {
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlFile);
        return createDataSource(config.getDataSources(), config.getShardingRule(), config.getOrchestration().getOrchestrationConfiguration());
    }
    
    /**
     * Create sharding data source.
     *
     * @param dataSourceMap data source map
     * @param yamlFile yaml file for rule configuration of databases and tables sharding without data sources
     * @return sharding data source
     * @throws SQLException SQL exception
     * @throws IOException IO exception
     */
    public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final File yamlFile) throws SQLException, IOException {
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlFile);
        return createDataSource(dataSourceMap, config.getShardingRule(), config.getOrchestration().getOrchestrationConfiguration());
    }
    
    /**
     * Create sharding data source.
     *
     * @param yamlByteArray yaml byte array for rule configuration of databases and tables sharding with data sources
     * @return sharding data source
     * @throws SQLException SQL exception
     */
    public static DataSource createDataSource(final byte[] yamlByteArray) throws SQLException {
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlByteArray);
        return createDataSource(config.getDataSources(), config.getShardingRule(), config.getOrchestration().getOrchestrationConfiguration());
    }
    
    /**
     * Create sharding data source.
     *
     * @param dataSourceMap data source map
     * @param yamlByteArray yaml byte array for rule configuration of databases and tables sharding without data sources
     * @return sharding data source
     * @throws SQLException SQL exception
     */
    public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlByteArray) throws SQLException {
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlByteArray);
        return createDataSource(dataSourceMap, config.getShardingRule(), config.getOrchestration().getOrchestrationConfiguration());
    }
    
    private static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, 
                                               final YamlShardingRuleConfiguration yamlConfig, final OrchestrationConfiguration orchestrationConfig) throws SQLException {
        if (null == yamlConfig) {
            return new OrchestrationShardingDataSource(new OrchestrationFacade(orchestrationConfig));
        } else {
            ShardingDataSource shardingDataSource = new ShardingDataSource(
                    dataSourceMap, new ShardingRule(yamlConfig.getShardingRuleConfiguration(), dataSourceMap.keySet()), yamlConfig.getConfigMap(), yamlConfig.getProps());
            return new OrchestrationShardingDataSource(shardingDataSource, new OrchestrationFacade(orchestrationConfig));
        }
    }
    
    private static YamlOrchestrationShardingRuleConfiguration unmarshal(final File yamlFile) throws IOException {
        try (
                FileInputStream fileInputStream = new FileInputStream(yamlFile);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8")
        ) {
            return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(inputStreamReader, YamlOrchestrationShardingRuleConfiguration.class);
        }
    }
    
    private static YamlOrchestrationShardingRuleConfiguration unmarshal(final byte[] yamlByteArray) {
        return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlOrchestrationShardingRuleConfiguration.class);
    }
}

@@ -912,114 +912,53 @@
 ion.
-config.OrchestrationConfiguration;%0Aimport io.shardingsphere.jdbc.orchestration.internal.OrchestrationFacad
+api.datasource.OrchestrationShardingDataSourc
 e;%0Ai
@@ -1004,54 +1004,41 @@
 ion.
-api.datasource.OrchestrationShardingDataSource
+config.OrchestrationConfiguration
 ;%0Aim
@@ -4685,32 +4685,8 @@
 rce(
-new OrchestrationFacade(
 orch
@@ -4693,33 +4693,32 @@
 estrationConfig)
-)
 ;%0A        %7D else
@@ -5046,32 +5046,8 @@
 ce, 
-new OrchestrationFacade(
 orch
@@ -5062,17 +5062,16 @@
 nConfig)
-)
 ;%0A      

modify YamlOrchestrationShardingDataSourceFactory