xmlhelpers.cc 7.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2008 University of Utah and the Flux Group.
 * All rights reserved.
 */

#ifdef WITH_XML

#include "xmlhelpers.h"
#include "xstr.h"
#include <string>
#include <vector>

const XMLCh* getChildValue(const DOMElement *tag, const char *name) {
    DOMNodeList *list = tag->getElementsByTagName(XStr(name).x());
    if (list->getLength() != 1) {
        throw "Incorrect number of child elements in getChildValue()";
    } else {
	return dynamic_cast<DOMElement*>(list->item(0))->getFirstChild()->getNodeValue();
    }
}

/* This will only work if there is only one element with that tag within the root
 * It is the callers responsibility to ensure that this is the case before calling this function
 */ 
void setChildValue(const DOMElement* parent, const char* tag, const char* value)
{
	DOMElement* ele = getElementByTagName (parent, tag);
	if (ele == NULL)
		throw "Error finding tag in parent in setChildValue ()";
	else
		ele->getFirstChild()->setNodeValue (XStr(value).x());	
}

DOMElement* getNodeByName (const DOMElement *root, const char* name)
{
	DOMNodeList *list = root -> getElementsByTagName(XStr("node").x());
	for (int i = 0; i < list -> getLength() ; ++i)
	{	
		DOMElement *ele = dynamic_cast<DOMElement*>(list->item(i)); 
		if (strcmp(XStr(ele->getAttribute(XStr("name").x())).c(),name) == 0)
			return ele;
	}
	return NULL;
}

DOMElement* getElementByAttributeValue (const DOMElement* root, const char* tag, const char* attribute_name, const char* attribute_value)
{
	DOMNodeList *list = root -> getElementsByTagName(XStr(tag).x());
	for (int i = 0; i < list -> getLength() ; ++i)
	{	
		DOMElement *ele = dynamic_cast<DOMElement*>(list->item(i)); 
		if (strcmp(XStr(ele->getAttribute(XStr(attribute_name).x())).c(),attribute_value) == 0)
			return ele;
	}
	return NULL;
}

DOMElement* getElementByAttributeValue (vector<const DOMElement*> roots, const char* tag, const char* attribute_name, const char* attribute_value)
{
	for (vector<const DOMElement*>::iterator it = roots.begin(); it != roots.end(); ++it)
	{	
		DOMNodeList *list = (*it)->getElementsByTagName(XStr(tag).x());
		for (int i = 0; i < list -> getLength() ; ++i)
		{
			DOMElement *ele = dynamic_cast<DOMElement*>(list->item(i)); 
			if (strcmp(XStr(ele->getAttribute(XStr(attribute_name).x())).c(),attribute_value) == 0)
				return ele;
		}
	}
	return NULL;
}

vector<const DOMElement*> getElementsByAttributeValue (const DOMElement* root, const char* tag, const char* attribute_name, const char* attribute_value)
{
	DOMNodeList *list = root -> getElementsByTagName(XStr(tag).x());
	vector<const DOMElement*> elements;
	for (int i = 0; i < list -> getLength() ; ++i)
	{	
		DOMElement *ele = dynamic_cast<DOMElement*>(list->item(i)); 
		if (strcmp(XStr(ele->getAttribute(XStr(attribute_name).x())).c(),attribute_value) == 0)
			elements.push_back(ele);
	}
	return elements;
}


/* This will only work if there is only one element with that tag within the root
 * It is the callers responsibility to ensure that this is the case before calling this function
 */ 
DOMElement* getElementByTagName (const DOMElement* root, const char* tag)
{
	DOMNodeList *list = root -> getElementsByTagName(XStr(tag).x());
	if (list->getLength() != 1) {
		throw "More than one child with this tag in getElementByTagName()";
	} else {
		return dynamic_cast<DOMElement*>(list->item(0));
	}
}

/* Returns the nth interface in a link (can be used for a node as well only if n is 0 */
DOMElement* getNthInterface (const DOMElement* root, int n)
{
	DOMNodeList* interfaces = root->getElementsByTagName(XStr("interface").x());
	if (interfaces->getLength() <= n) {
		throw "Too few interfaces found";
	}
	return (dynamic_cast<DOMElement*>(interfaces->item(n)));	
}

/*
 * TODO: Better error handling
 */
bool hasChildTag(const DOMElement *tag, const char *name) {
    return (tag->getElementsByTagName(XStr(name).x())->getLength() > 0);
}

int parse_fds_xml(const DOMElement *tag, node_fd_set *fd_set) {
    DOMNodeList *fds = tag->getElementsByTagName(XStr("fd").x());
    for (int i = 0; i < fds->getLength(); i++) 
	{
		DOMElement *elt = dynamic_cast<DOMElement*>(fds->item(i));
	
		XStr fd_name (elt->getAttribute(XStr("fd_name").x()));
		XStr fd_weight (elt->getAttribute(XStr("fd_weight").x()));
	
		bool violatable = elt->hasAttribute(XStr("violatable").x());
		featuredesire::fd_type fd_type;
		if (elt->hasAttribute(XStr("local_operator").x())) 
		{
			/*
			* Right now, there is only one type of local feature
			*/
			fd_type = featuredesire::FD_TYPE_LOCAL_ADDITIVE;
		} 
		else if (elt->hasAttribute(XStr("global_operator").x())) 
		{	
			XStr fd_operator(elt->getAttribute(XStr("global_operator").x()));
			if (fd_operator == "OnceOnly") {
				fd_type = featuredesire::FD_TYPE_GLOBAL_ONE_IS_OKAY;
			} else {
				fd_type = featuredesire::FD_TYPE_GLOBAL_MORE_THAN_ONE;
			}
		} 
		else {
			fd_type = featuredesire::FD_TYPE_NORMAL;
		}
	
		fd_set->push_front(tb_node_featuredesire(fd_name.c(), fd_weight.d(),
						 violatable, fd_type));
	
    }
    return fds->getLength();
}

/*
 * TODO: Better error handling
 */
node_interface_pair parse_interface_xml(const DOMElement *tag) {
	const XMLCh* node_name = tag->getAttribute(XStr("node_name").x());
	const XMLCh* interface_name = tag->getAttribute(XStr("interface_name").x());
	
	node_interface_pair rv(node_name,interface_name);
	return rv;
}

/* Parse the features and desires on a virtual node */
int parse_fds_vnode_xml (const DOMElement *tag, node_fd_set *fd_set)
{
	DOMNodeList *fds = tag->getElementsByTagName(XStr("fd").x());
    for (int i = 0; i < fds->getLength(); i++) 
    {
		DOMElement *elt = dynamic_cast<DOMElement*>(fds->item(i));
		XStr fd_name (elt->getAttribute(XStr("fd_name").x()));
		XStr fd_weight (elt->getAttribute(XStr("fd_weight").x()));
	
		bool violatable = elt->hasAttribute(XStr("violatable").x());
		featuredesire::fd_type fd_type;
		if (elt->hasAttribute(XStr("local_operator").x())) {
			/*
			* Right now, there is only one type of local feature
			*/
			fd_type = featuredesire::FD_TYPE_LOCAL_ADDITIVE;
		} else if (elt->hasAttribute(XStr("global_operator").x())) {	
			XStr fd_operator(elt->getAttribute(XStr("global_operator").x()));
			if (fd_operator == "OnceOnly") {
				fd_type = featuredesire::FD_TYPE_GLOBAL_ONE_IS_OKAY;
			} else {
				fd_type = featuredesire::FD_TYPE_GLOBAL_MORE_THAN_ONE;
			}
		} else {
			fd_type = featuredesire::FD_TYPE_NORMAL;
		}
		
		tb_node_featuredesire node_fd (fd_name.c(), fd_weight.d(), violatable, fd_type);
		node_fd.add_desire_user(fd_weight.d());
		
		fd_set->push_front(node_fd);
    }
    return fds->getLength();
}

interface_spec parse_interface_rspec_xml(const DOMElement *tag) 
{
	interface_spec rv =
		{	
			string(XStr(tag->getAttribute(XStr("virtual_node_id").x())).c()),
208
			string(XStr(tag->getAttribute(XStr("virtual_interface_id").x())).c()),
209
			string(XStr(tag->getAttribute(XStr("component_node_uuid").x())).c()),
210
			string(XStr(tag->getAttribute(XStr("component_interface_id").x())).c())		
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
		};
    return rv;
}

component_spec parse_component_spec (const DOMElement *elt) {
	component_spec rv = {string(""), string(""), string(""), string("")};
	rv.component_manager_uuid = string (XStr(elt->getAttribute(XStr("component_manager_uuid").x())).c());
	if (elt->hasAttribute(XStr("component_name").x()))
		rv.component_name = string (XStr(elt->getAttribute(XStr("component_name").x())).c());
	rv.component_uuid = string (XStr(elt->getAttribute(XStr("component_uuid").x())).c());
	if (elt->hasAttribute(XStr("sliver_uuid").x()))
		rv.sliver_uuid = string (XStr(elt->getAttribute(XStr("sliver_uuid").x())).c());
	return rv;
}

// Check if the component spec is present. We check if the aggregate UUID and the component UUID are both present
bool hasComponentSpec (DOMElement* elt)
{
	if (elt->hasAttribute(XStr("component_manager_uuid").x()) && elt->hasAttribute(XStr("component_uuid").x()))
		return true;
	return false;
}

#endif