/*
 * 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.shardingproxy.runtime;

import io.shardingsphere.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.api.config.RuleConfiguration;
import io.shardingsphere.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.executor.ShardingExecuteEngine;
import io.shardingsphere.core.metadata.ShardingMetaData;
import io.shardingsphere.core.rule.DataSourceParameter;
import io.shardingsphere.core.rule.MasterSlaveRule;
import io.shardingsphere.core.rule.ShardingRule;
import io.shardingsphere.orchestration.internal.rule.OrchestrationMasterSlaveRule;
import io.shardingsphere.orchestration.internal.rule.OrchestrationShardingRule;
import io.shardingsphere.shardingproxy.backend.BackendExecutorContext;
import io.shardingsphere.shardingproxy.backend.jdbc.datasource.JDBCBackendDataSource;
import io.shardingsphere.shardingproxy.runtime.metadata.ProxyTableMetaDataConnectionManager;
import lombok.Getter;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Master-slave schema.
 *
 * @author panjuan
 */
@Getter
public final class MasterSlaveSchema extends LogicSchema {
    
    private final MasterSlaveRule masterSlaveRule;
    
    public MasterSlaveSchema(final String name, final Map<String, DataSourceParameter> dataSources, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final boolean isUsingRegistry) {
        super(name, dataSources);
        masterSlaveRule = getMasterSlaveRule(masterSlaveRuleConfig, isUsingRegistry);
    }
    
    private ShardingRule getShardingRule(final ShardingRuleConfiguration shardingRule, final boolean isUsingRegistry) {
        return isUsingRegistry ? new OrchestrationShardingRule(shardingRule, dataSources.keySet()) : new ShardingRule(shardingRule, dataSources.keySet());
    }
    
    private MasterSlaveRule getMasterSlaveRule(final MasterSlaveRuleConfiguration masterSlaveRule, final boolean isUsingRegistry) {
        return isUsingRegistry ? new OrchestrationMasterSlaveRule(masterSlaveRule) : new MasterSlaveRule(masterSlaveRule);
    }
    
    private ShardingMetaData getShardingMetaData(final ShardingExecuteEngine executeEngine) {
        return new ShardingMetaData(getDataSourceURLs(dataSources), shardingRule,
                DatabaseType.MySQL, executeEngine, new ProxyTableMetaDataConnectionManager(backendDataSource), GlobalRegistry.getInstance().getMaxConnectionsSizePerQuery());
    }
    
    private Map<String, String> getDataSourceURLs(final Map<String, DataSourceParameter> dataSourceParameters) {
        Map<String, String> result = new LinkedHashMap<>(dataSourceParameters.size(), 1);
        for (Entry<String, DataSourceParameter> entry : dataSourceParameters.entrySet()) {
            result.put(entry.getKey(), entry.getValue().getUrl());
        }
        return result;
    }
    
    /**
     * Judge is master slave only.
     *
     * @return is master slave only
     */
    public boolean isMasterSlaveOnly() {
        return (null == shardingRule || shardingRule.getTableRules().isEmpty()) && null != masterSlaveRule;
    }
}

@@ -733,298 +733,8 @@
 on;%0A
-import io.shardingsphere.api.config.RuleConfiguration;%0Aimport io.shardingsphere.api.config.ShardingRuleConfiguration;%0Aimport io.shardingsphere.core.constant.DatabaseType;%0Aimport io.shardingsphere.core.executor.ShardingExecuteEngine;%0Aimport io.shardingsphere.core.metadata.ShardingMetaData;%0A
 impo
@@ -841,57 +841,8 @@
 le;%0A
-import io.shardingsphere.core.rule.ShardingRule;%0A
 impo
@@ -931,434 +931,44 @@
 ort 
-io.shardingsphere.orchestration.internal.rule.OrchestrationShardingRule;%0Aimport io.shardingsphere.shardingproxy.backend.BackendExecutorContext;%0Aimport io.shardingsphere.shardingproxy.backend.jdbc.datasource.JDBCBackendDataSource;%0Aimport io.shardingsphere.shardingproxy.runtime.metadata.ProxyTableMetaDataConnectionManager;%0Aimport lombok.Getter;%0A%0Aimport java.util.LinkedHashMap;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry
+lombok.Getter;%0A%0Aimport java.util.Map
 ;%0A%0A/
@@ -1486,1555 +1486,249 @@
 ate 
-ShardingRule getShardingRule(final ShardingRuleConfiguration shardingRule, final boolean isUsingRegistry) %7B%0A        return isUsingRegistry ? new OrchestrationShardingRule(shardingRule, dataSources.keySet()) : new ShardingRule(shardingRule, dataSources.keySet());%0A    %7D%0A    %0A    private MasterSlaveRule getMasterSlaveRule(final MasterSlaveRuleConfiguration masterSlaveRule, final boolean isUsingRegistry) %7B%0A        return isUsingRegistry ? new OrchestrationMasterSlaveRule(masterSlaveRule) : new MasterSlaveRule(masterSlaveRule);%0A    %7D%0A    %0A    private ShardingMetaData getShardingMetaData(final ShardingExecuteEngine executeEngine) %7B%0A        return new ShardingMetaData(getDataSourceURLs(dataSources), shardingRule,%0A                DatabaseType.MySQL, executeEngine, new ProxyTableMetaDataConnectionManager(backendDataSource), GlobalRegistry.getInstance().getMaxConnectionsSizePerQuery());%0A    %7D%0A    %0A    private Map%3CString, String%3E getDataSourceURLs(final Map%3CString, DataSourceParameter%3E dataSourceParameters) %7B%0A        Map%3CString, String%3E result = new LinkedHashMap%3C%3E(dataSourceParameters.size(), 1);%0A        for (Entry%3CString, DataSourceParameter%3E entry : dataSourceParameters.entrySet()) %7B%0A            result.put(entry.getKey(), entry.getValue().getUrl());%0A        %7D%0A        return result;%0A    %7D%0A    %0A    /**%0A     * Judge is master slave only.%0A     *%0A     * @return is master slave only%0A     */%0A    public boolean isMasterSlaveOnly() %7B%0A        return (null == shardingRule %7C%7C shardingRule.getTableRules().isEmpty()) && null != masterSlaveRule
+MasterSlaveRule getMasterSlaveRule(final MasterSlaveRuleConfiguration masterSlaveRule, final boolean isUsingRegistry) %7B%0A        return isUsingRegistry ? new OrchestrationMasterSlaveRule(masterSlaveRule) : new MasterSlaveRule(masterSlaveRule)
 ;%0A  

modify MasterSlaveSchema