// Path: monica-configuration/src/main/java/monica/configuration/context/ConfigurationContext.java
// public class ConfigurationContext {
// 	private static String yamlBaseFilePath = "yaml";
// 	
// 	public static ConcurrentHashMap<String, Object> propMap = new ConcurrentHashMap<String, Object>();
// 	
// 	/**
// 	 * need to adapt to different registry framework.  below just be customized to zookeeper
// 	 * @throws FileNotFoundException
// 	 * @throws IOException
// 	 */
// 	public static void loadYamlServerConfig() throws FileNotFoundException, IOException{		
// 		Yaml yaml = new Yaml();
// 		File path = Utils.getPropertiesPath("yaml\\monica.yaml");
// 		//YamlMonica yamlMonica = yaml.loadAs(new FileInputStream(path), YamlMonica.class);		
// 		YamlMonica yamlMonica = yaml.loadAs(Utils.loadResources("yaml/monica.yaml").openStream(), YamlMonica.class);
// 		propMap.putIfAbsent("yamlMonica", yamlMonica);
// 		propMap.putIfAbsent("serverString", yamlMonica.toServerString());
// 		propMap.putIfAbsent("storage", yamlMonica.getStorage());
// 	}
// 	
// 	
// 	public static void loadYamlClientConfig() throws FileNotFoundException, IOException{
// 		Yaml yaml = new Yaml();
// 		File path = Utils.getPropertiesPath("\\yaml\\client.yaml");
// 		//YamlClient yamlClient = yaml.loadAs(new FileInputStream(path), YamlClient.class);		
// 		YamlClient yamlClient = yaml.loadAs(Utils.loadResources("yaml/client.yaml").openStream(), YamlClient.class);
// 		propMap.putIfAbsent("client", yamlClient);
// 		propMap.putIfAbsent("serverString", yamlClient.toServerString());
// 	}
// 	
// 	public static List<String> loadYamlRouterConfig() throws FileNotFoundException, IOException{
// 		Yaml yaml = new Yaml();
// 		File path = Utils.getPropertiesPath("\\yaml\\router.yaml");
// 		//YamlRouter yamlRouter = yaml.loadAs(new FileInputStream(path), YamlRouter.class);	
// 		YamlRouter yamlRouter = yaml.loadAs(Utils.loadResources("yaml/router.yaml").openStream(), YamlRouter.class);
// 		return yamlRouter.toStringList();
// 	}
// 	
// 	
// 	private static ClassLoader getClassLoader(){
// 		return ConfigurationContext.class.getClassLoader();
// 	}
// 	
// 	public static void main(String args[]){	
// 		try {
// 			//loadYamlServerConfig();
// 			//YamlMonica monica = (YamlMonica)propMap.get("yamlMonica");
// 			List<String> rules = loadYamlRouterConfig();
// 			System.out.println("zk --------  "+rules.get(0));
// 		} catch (FileNotFoundException e) {
// 			e.printStackTrace();
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		}
// 		
// 	}
// 
// }
// 
// Path: monica-registry/src/main/java/monica/registry/context/RegistryContext.java
// public class RegistryContext {
// 	// caheKey: curatorClient providers server usefulProviders
// 	public static ConcurrentHashMap<String, Object> clientCache = new ConcurrentHashMap<String, Object>();
// }
// 
// Path: monica-registry/src/main/java/monica/registry/service/ZookeeperMonicaClient.java
// public class ZookeeperMonicaClient {
// 	private final String ZK_NAMESPACE = "monica";
// 	private CuratorFramework zkClient;
// 
// 	public void start() {
// 		CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
// 		zkClient = builder.connectString(getServerConnectionString()).namespace(ZK_NAMESPACE)
// 				.retryPolicy(new RetryOneTime(1)).connectionTimeoutMs(1).sessionTimeoutMs(140).build();
// 		zkClient.start();
// 		RegistryContext.clientCache.putIfAbsent("curatorClient", zkClient);
// 	}
// 
// 	private String getServerConnectionString() {
// 		return String.valueOf(ConfigurationContext.propMap.get("serverString"));
// 	}
// 
// 	public CuratorFramework getZookeeperClient() throws KeeperException.ConnectionLossException {
// 		if (zkClient.getZookeeperClient().isConnected()) {
// 			return this.zkClient;
// 		} else {
// 			throw new KeeperException.ConnectionLossException();
// 		}
// 
// 	}
// 
// }


import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.curator.framework.CuratorFramework;
import monica.configuration.context.ConfigurationContext;
import monica.registry.context.RegistryContext;
import monica.registry.service.ZookeeperMonicaClient;

package monica.cluster.router.main;




/**
 * 
 * @author lucy@polarcoral.com
 *
 *         2017-08-29
 */
public class UpdateRulesMain {
	private final CuratorFramework zkClient = (CuratorFramework) RegistryContext.clientCache.get("curatorClient");
	private final String RULE_PATH = "/routers";

	/**
	 * The only entrance for update routing rules step 1) delete all rule nodes
	 * from zk step 2) create all rule nodes on zk
	 */
	public static void main(String args[]) {
		try {

// Path: monica-configuration/src/main/java/monica/configuration/context/ConfigurationContext.java
// public class ConfigurationContext {
// 	private static String yamlBaseFilePath = "yaml";
// 	
// 	public static ConcurrentHashMap<String, Object> propMap = new ConcurrentHashMap<String, Object>();
// 	
// 	/**
// 	 * need to adapt to different registry framework.  below just be customized to zookeeper
// 	 * @throws FileNotFoundException
// 	 * @throws IOException
// 	 */
// 	public static void loadYamlServerConfig() throws FileNotFoundException, IOException{		
// 		Yaml yaml = new Yaml();
// 		File path = Utils.getPropertiesPath("yaml\\monica.yaml");
// 		//YamlMonica yamlMonica = yaml.loadAs(new FileInputStream(path), YamlMonica.class);		
// 		YamlMonica yamlMonica = yaml.loadAs(Utils.loadResources("yaml/monica.yaml").openStream(), YamlMonica.class);
// 		propMap.putIfAbsent("yamlMonica", yamlMonica);
// 		propMap.putIfAbsent("serverString", yamlMonica.toServerString());
// 		propMap.putIfAbsent("storage", yamlMonica.getStorage());
// 	}
// 	
// 	
// 	public static void loadYamlClientConfig() throws FileNotFoundException, IOException{
// 		Yaml yaml = new Yaml();
// 		File path = Utils.getPropertiesPath("\\yaml\\client.yaml");
// 		//YamlClient yamlClient = yaml.loadAs(new FileInputStream(path), YamlClient.class);		
// 		YamlClient yamlClient = yaml.loadAs(Utils.loadResources("yaml/client.yaml").openStream(), YamlClient.class);
// 		propMap.putIfAbsent("client", yamlClient);
// 		propMap.putIfAbsent("serverString", yamlClient.toServerString());
// 	}
// 	
// 	public static List<String> loadYamlRouterConfig() throws FileNotFoundException, IOException{
// 		Yaml yaml = new Yaml();
// 		File path = Utils.getPropertiesPath("\\yaml\\router.yaml");
// 		//YamlRouter yamlRouter = yaml.loadAs(new FileInputStream(path), YamlRouter.class);	
// 		YamlRouter yamlRouter = yaml.loadAs(Utils.loadResources("yaml/router.yaml").openStream(), YamlRouter.class);
// 		return yamlRouter.toStringList();
// 	}
// 	
// 	
// 	private static ClassLoader getClassLoader(){
// 		return ConfigurationContext.class.getClassLoader();
// 	}
// 	
// 	public static void main(String args[]){	
// 		try {
// 			//loadYamlServerConfig();
// 			//YamlMonica monica = (YamlMonica)propMap.get("yamlMonica");
// 			List<String> rules = loadYamlRouterConfig();
// 			System.out.println("zk --------  "+rules.get(0));
// 		} catch (FileNotFoundException e) {
// 			e.printStackTrace();
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		}
// 		
// 	}
// 
// }
// 
// Path: monica-registry/src/main/java/monica/registry/context/RegistryContext.java
// public class RegistryContext {
// 	// caheKey: curatorClient providers server usefulProviders
// 	public static ConcurrentHashMap<String, Object> clientCache = new ConcurrentHashMap<String, Object>();
// }
// 
// Path: monica-registry/src/main/java/monica/registry/service/ZookeeperMonicaClient.java
// public class ZookeeperMonicaClient {
// 	private final String ZK_NAMESPACE = "monica";
// 	private CuratorFramework zkClient;
// 
// 	public void start() {
// 		CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
// 		zkClient = builder.connectString(getServerConnectionString()).namespace(ZK_NAMESPACE)
// 				.retryPolicy(new RetryOneTime(1)).connectionTimeoutMs(1).sessionTimeoutMs(140).build();
// 		zkClient.start();
// 		RegistryContext.clientCache.putIfAbsent("curatorClient", zkClient);
// 	}
// 
// 	private String getServerConnectionString() {
// 		return String.valueOf(ConfigurationContext.propMap.get("serverString"));
// 	}
// 
// 	public CuratorFramework getZookeeperClient() throws KeeperException.ConnectionLossException {
// 		if (zkClient.getZookeeperClient().isConnected()) {
// 			return this.zkClient;
// 		} else {
// 			throw new KeeperException.ConnectionLossException();
// 		}
// 
// 	}
// 
// }


// Path: monica-cluster/src/main/java/monica/cluster/router/main/UpdateRulesMain.java
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.curator.framework.CuratorFramework;
import monica.configuration.context.ConfigurationContext;
import monica.registry.context.RegistryContext;
import monica.registry.service.ZookeeperMonicaClient;

package monica.cluster.router.main;




/**
 * 
 * @author lucy@polarcoral.com
 *
 *         2017-08-29
 */
public class UpdateRulesMain {
	private final CuratorFramework zkClient = (CuratorFramework) RegistryContext.clientCache.get("curatorClient");
	private final String RULE_PATH = "/routers";

	/**
	 * The only entrance for update routing rules step 1) delete all rule nodes
	 * from zk step 2) create all rule nodes on zk
	 */
	public static void main(String args[]) {
		try {

			ConfigurationContext.loadYamlClientConfig();
