class CommonTopologyParser extends TopologyParser {
@Override
Topology parseElement(Element rootElement) throws TopologyParserException, UnsupportedEncodingException {
Topology topology = new Topology();
// Finding keypair to use
NodeList keyNodes = rootElement.getElementsByTagName("Key");
if(logger.isDebugEnabled()) {
logger.debug("Found " + keyNodes.getLength() + " SSH keys");
}
for(int i = 0; i < keyNodes.getLength(); i++)
{
Element keyElement = (Element) keyNodes.item(i);
if(keyElement == null) {
continue;
}
String strPrivateKey = "", strPublicKey = "";
String strKeyName = keyElement.getAttribute("name");
String privateKeyPath = keyElement.getAttribute("privatekeypath");
if (privateKeyPath != null && !privateKeyPath.isEmpty())
{
String strKeyFormat = keyElement.getAttribute("format");
privateKeyPath += File.separatorChar + strKeyName + "." + strKeyFormat;
try {
StringBuffer fileContent = new StringBuffer(1000);
BufferedReader br = new BufferedReader(new FileReader(privateKeyPath));
char[] buf = new char[1024];
int nRead=0;
while((nRead=br.read(buf)) != -1){
fileContent.append(buf, 0, nRead);
}
br.close();
strPrivateKey = fileContent.toString();
} catch (Exception e) {
if(logger.isErrorEnabled()) {
logger.error(e.getMessage());
}
throw new TopologyParserException(e.getMessage());
}
}
else
{
NodeList privateKeyNode = keyElement.getElementsByTagName("PrivateKey");
strPublicKey = keyElement.getElementsByTagName("PublicKey").item(0).getTextContent();
strPrivateKey = (privateKeyNode == null || privateKeyNode.getLength() == 0) ? null : privateKeyNode.item(0).getTextContent();
}
SSHKey key = new SSHKey(strKeyName, strPrivateKey == null ? null : strPrivateKey.getBytes("UTF-8"), strPublicKey.getBytes("UTF-8"));
topology.addKey(key);
if(logger.isDebugEnabled()) {
logger.debug(key.toString());
}
}
NodeList labNodes = rootElement.getElementsByTagName("Node");
if(logger.isDebugEnabled()) {
logger.debug("Found " + labNodes.getLength() + " nodes in this topology");
}
HashSet<String> instancesNameAlreadyAdded = new HashSet<String>();
for(int i = 0; i < labNodes.getLength(); i++)
{
Instance instance = new Instance();
Node node = labNodes.item(i);
instance.setName(node.getAttributes().getNamedItem("name").getTextContent());
if (instancesNameAlreadyAdded.contains(instance.getName()))
{
throw new TopologyParserException("Each node name in the topology configuration file must be unique. The name " + instance.getName() + "is set to more than one node");
}
for(int j = 0; j < node.getChildNodes().getLength(); j++)
{
Node child = node.getChildNodes().item(j);
if(child.getNodeName().equals("Type"))
{
instance.setType(child.getAttributes().getNamedItem("type").getTextContent());
}
else if(child.getNodeName().equals("ExternalDNS"))
{
instance.setExternalDNS(child.getAttributes().getNamedItem("url").getTextContent());
}
else if(child.getNodeName().equals("ExternalIP"))
{
instance.setExternalIP(child.getAttributes().getNamedItem("ip").getTextContent());
}
else if(child.getNodeName().equals("InternalIP"))
{
instance.setInternalIP(child.getAttributes().getNamedItem("ip").getTextContent());
}
else if(child.getNodeName().equals("Port"))
{
instance.setPort(child.getAttributes().getNamedItem("port").getTextContent());
}
else if(child.getNodeName().equals("Area"))
{
instance.setArea(child.getAttributes().getNamedItem("name").getTextContent());
topology.getAreas().add(child.getAttributes().getNamedItem("name").getTextContent());
}
else if(child.getNodeName().equals("Credentials"))
{
if (child.getAttributes().getNamedItem("key") != null) {
instance.setSSHKeyName(child.getAttributes().getNamedItem("key").getTextContent());
instance.setSSHKey(topology.getKeys().get(instance.getSSHKeyName()));
}
if (child.getAttributes().getNamedItem("user") != null) {
instance.setUser(child.getAttributes().getNamedItem("user").getTextContent());
}
if (child.getAttributes().getNamedItem("password") != null) {
instance.setPassword(child.getAttributes().getNamedItem("password").getTextContent());
}
}
else if(child.getNodeName().equals("KeyUsed"))
{
if (child.getAttributes().getNamedItem("key") != null) {
instance.setSSHKeyName(child.getAttributes().getNamedItem("key").getTextContent());
instance.setSSHKey(topology.getKeys().get(instance.getSSHKeyName()));
}
if (child.getAttributes().getNamedItem("user") != null) {
instance.setUser(child.getAttributes().getNamedItem("user").getTextContent());
}
if (child.getAttributes().getNamedItem("password") != null) {
instance.setPassword(child.getAttributes().getNamedItem("password").getTextContent());
}
}
}
logger.debug(instance.toString());
topology.addInstance(instance);
}
return topology;
}