/*
* Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software;Designed and Developed mainly by many Chinese
* opensource volunteers. you can redistribute it and/or modify it under the
* terms of the GNU General Public License version 2 only, as published by the
* Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Any questions about this component can be directed to it's project Web address
* https://code.google.com/p/opencloudb/.
*
*/
package org.opencloudb.config.model;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.opencloudb.config.util.ConfigException;
import org.opencloudb.config.util.ConfigUtil;
import org.opencloudb.util.SplitUtil;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* @author mycat
*/
public class ClusterConfig {
private final Map<String, MycatNodeConfig> nodes;
private final Map<String, List<String>> groups;
public ClusterConfig(Element root, int port) {
nodes = Collections.unmodifiableMap(loadNode(root, port));
groups = Collections.unmodifiableMap(loadGroup(root, nodes));
}
public Map<String, MycatNodeConfig> getNodes() {
return nodes;
}
public Map<String, List<String>> getGroups() {
return groups;
}
private static Map<String, MycatNodeConfig> loadNode(Element root, int port) {
Map<String, MycatNodeConfig> nodes = new HashMap<String, MycatNodeConfig>();
NodeList list = root.getElementsByTagName("node");
Set<String> hostSet = new HashSet<String>();
for (int i = 0, n = list.getLength(); i < n; i++) {
Node node = list.item(i);
if (node instanceof Element) {
Element element = (Element) node;
String name = element.getAttribute("name").trim();
if (nodes.containsKey(name)) {
throw new ConfigException("node name duplicated :" + name);
}
Map<String, Object> props = ConfigUtil.loadElements(element);
String host = (String) props.get("host");
if (null == host || "".equals(host)) {
throw new ConfigException("host empty in node: " + name);
}
if (hostSet.contains(host)) {
throw new ConfigException("node host duplicated :" + host);
}
String wei = (String) props.get("weight");
if (null == wei || "".equals(wei)) {
throw new ConfigException("weight should not be null in host:" + host);
}
int weight = Integer.valueOf(wei);
if (weight <= 0) {
throw new ConfigException("weight should be > 0 in host:" + host + " weight:" + weight);
}
MycatNodeConfig conf = new MycatNodeConfig(name, host, port, weight);
nodes.put(name, conf);
hostSet.add(host);
}
}
return nodes;
}
private static Map<String, List<String>> loadGroup(Element root, Map<String, MycatNodeConfig> nodes) {
Map<String, List<String>> groups = new HashMap<String, List<String>>();
NodeList list = root.getElementsByTagName("group");
for (int i = 0, n = list.getLength(); i < n; i++) {
Node node = list.item(i);
if (node instanceof Element) {
Element e = (Element) node;
String groupName = e.getAttribute("name").trim();
if (groups.containsKey(groupName)) {
throw new ConfigException("group duplicated : " + groupName);
}
Map<String, Object> props = ConfigUtil.loadElements(e);
String value = (String) props.get("nodeList");
if (null == value || "".equals(value)) {
throw new ConfigException("group should contain 'nodeList'");
}
String[] sList = SplitUtil.split(value, ',', true);
if (null == sList || sList.length == 0) {
throw new ConfigException("group should contain 'nodeList'");
}
for (String s : sList) {
if (!nodes.containsKey(s)) {
throw new ConfigException("[ node :" + s + "] in [ group:" + groupName + "] doesn't exist!");
}
}
List<String> nodeList = Arrays.asList(sList);
groups.put(groupName, nodeList);
}
}
if (!groups.containsKey("default")) {
List<String> nodeList = new ArrayList<String>(nodes.keySet());
groups.put("default", nodeList);
}
return groups;
}
}