/*
 * 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.orchestration.yaml;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.shardingsphere.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.config.DataSourceConfiguration;
import io.shardingsphere.core.rule.Authentication;
import io.shardingsphere.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration;
import io.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.yaml.snakeyaml.Yaml;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Configuration yaml loader.
 *
 * @author panjuan
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ConfigurationYamlHandler {
    
    /**
     * Load config map.
     *
     * @param data data
     * @return config map
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> loadConfigMap(final String data) {
        return Strings.isNullOrEmpty(data) ? new LinkedHashMap<String, Object>() : (Map) new Yaml().load(data);
    }
    
    /**
     * Load properties configuration.
     *
     * @param data data
     * @return properties
     */
    public static Properties loadProperties(final String data) {
        return Strings.isNullOrEmpty(data) ? new Properties() : new Yaml().loadAs(data, Properties.class);
    }
    
    /**
     * Load authentication.
     *
     * @param data data
     * @return authentication
     */
    public static Authentication loadAuthentication(final String data) {
        Authentication result = new Yaml().loadAs(data, Authentication.class);
        Preconditions.checkState(!Strings.isNullOrEmpty(result.getUsername()), "Authority configuration is invalid.");
        return result;
    }
    
    /**
     * Load master-slave rule configuration.
     *
     * @param data data
     * @return master-slave rule configuration
     */
    public static MasterSlaveRuleConfiguration loadMasterSlaveRuleConfiguration(final String data) {
        return new Yaml().loadAs(data, YamlMasterSlaveRuleConfiguration.class).getMasterSlaveRuleConfiguration();
    }
    
    /**
     * Load sharding rule configuration.
     *
     * @param data data
     * @return sharding rule configuration
     */
    public static ShardingRuleConfiguration loadShardingRuleConfiguration(final String data) {
        return new Yaml().loadAs(data, YamlShardingRuleConfiguration.class).getShardingRuleConfiguration();
    }
    
    /**
     * Load data sources.
     *
     * @param data data
     * @return data sources map
     */
    @SuppressWarnings("unchecked")
    public static Map<String, DataSourceConfiguration> loadDataSourceConfigurations(final String data) {
        Map<String, YamlDataSourceConfiguration> result = (Map) new Yaml().load(data);
        Preconditions.checkState(null != result && !result.isEmpty(), "No available data sources to load for orchestration.");
        return Maps.transformValues(result, new Function<YamlDataSourceConfiguration, DataSourceConfiguration>() {
            
            @Override
            public DataSourceConfiguration apply(final YamlDataSourceConfiguration input) {
                DataSourceConfiguration result = new DataSourceConfiguration(input.getDataSourceClassName());
                result.getProperties().putAll(input.getProperties());
                return result;
            }
        });
    }
    
    /**
     * Dump config map.
     *
     * @param configMap config map
     * @return data
     */
    public static String dumpConfigMap(final Map<String, Object> configMap) {
        return new Yaml(new DefaultYamlRepresenter()).dumpAsMap(configMap);
    }
    
    /**
     * Dump properties configuration.
     *
     * @param props props
     * @return data
     */
    public static String dumpProperties(final Properties props) {
        return new Yaml(new DefaultYamlRepresenter()).dumpAsMap(props);
    }
}

@@ -4761,10 +4761,2046 @@
 ;%0A    %7D%0A
+    %0A    /**%0A     * Dump authentication.%0A     *%0A     * @param authentication authentication%0A     * @return data%0A     */%0A    public static String dumpAuthentication(final Authentication authentication) %7B%0A       return new Yaml(new DefaultYamlRepresenter()).dumpAsMap(authentication);%0A    %7D%0A    %0A    /**%0A     * Dump master-slave rule configuration.%0A     *%0A     * @param masterSlaveRuleConfig master-slave rule configuration%0A     * @return data%0A     */%0A    public static String dumpMasterSlaveRuleConfiguration(final MasterSlaveRuleConfiguration masterSlaveRuleConfig) %7B%0A        return new Yaml(new DefaultYamlRepresenter()).dumpAsMap(new YamlMasterSlaveRuleConfiguration(masterSlaveRuleConfig));%0A    %7D%0A    %0A    /**%0A     * Dump sharding rule configuration.%0A     *%0A     * @param shardingRuleConfig sharding rule configuration%0A     * @return data%0A     */%0A    public static String dumpShardingRuleConfiguration(final ShardingRuleConfiguration shardingRuleConfig) %7B%0A        return new Yaml(new DefaultYamlRepresenter()).dumpAsMap(new YamlShardingRuleConfiguration(shardingRuleConfig));%0A    %7D%0A    %0A    /**%0A     * Dump data sources.%0A     *%0A     * @param dataSourceConfigs data sources map%0A     * @return data%0A     */%0A    @SuppressWarnings(%22unchecked%22)%0A    public static String dumpDataSourceConfigurations(final Map%3CString, DataSourceConfiguration%3E dataSourceConfigs) %7B%0A        return new Yaml(new DefaultYamlRepresenter()).dumpAsMap(%0A                Maps.transformValues(dataSourceConfigs, new Function%3CDataSourceConfiguration, YamlDataSourceConfiguration%3E() %7B%0A            %0A                    @Override%0A                    public YamlDataSourceConfiguration apply(final DataSourceConfiguration input) %7B%0A                        YamlDataSourceConfiguration result = new YamlDataSourceConfiguration();%0A                        result.setDataSourceClassName(input.getDataSourceClassName());%0A                        result.setProperties(input.getProperties());%0A                        return result;%0A                    %7D%0A                %7D));%0A    %7D%0A
 %7D%0A

add some functions of dump