emulab_extensions_parser.cc 10 KB
Newer Older
1
/*
Robert Ricci's avatar
Robert Ricci committed
2
 * Copyright (c) 2008-2010 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file 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 Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
22 23 24 25 26 27 28 29 30 31 32 33 34
 */

/*
 * XML Parser for emulab extensions for rspec
 */

#ifdef WITH_XML

#include "emulab_extensions_parser.h"

XERCES_CPP_NAMESPACE_USE

using namespace std;
35 36
using namespace rspec_emulab_extension;

37
emulab_operator emulab_extensions_parser::readOperator(const DOMElement* tag)
38
{
39 40 41 42 43 44 45 46 47 48
  struct emulab_operator op = { "", NORMAL_OPERATOR };
  if (this->hasAttribute(tag, "global_operator")) {
    op.op = this->getAttribute(tag, "global_operator");
    op.type = GLOBAL_OPERATOR;
  }
  else if (this->hasAttribute(tag, "local_operator")) {
    op.op = this->getAttribute(tag, "local_operator");
    op.type = LOCAL_OPERATOR;
  }
  return op;
49 50
}

51
vector<struct fd> 
52
emulab_extensions_parser::readFeaturesDesires (const DOMElement* ele, int& count)
53
{
54
  vector<struct fd> fds;
55 56
  DOMNodeList* fdNodes = ele->getElementsByTagName(XStr("emulab:fd").x());
  count = fdNodes->getLength();
57
  for (unsigned int i = 0; i < fdNodes->getLength(); i++) 	{
58 59 60 61
    fds.push_back(this->readFeatureDesire
		  (dynamic_cast<DOMElement*>(fdNodes->item(i))));
  }
  return fds;
62
}
63

64
struct fd emulab_extensions_parser::readFeatureDesire (const DOMElement* tag)
65
{
66 67 68 69 70 71 72 73
  struct fd fdObject = {
    this->getAttribute(tag, "name"),
    rspec_parser_helper::stringToNum
    (this->getAttribute(tag, "weight")),
    this->hasAttribute(tag, "violatable"),
    this->readOperator(tag)
  };
  return fdObject;
74 75
}

76 77
vector<struct property> emulab_extensions_parser::readProperties
(const DOMElement* elem)
78
{
79 80
  DOMNodeList* propNodes = elem->getElementsByTagName(XStr("property").x());
  vector<struct property> properties;
81
  for (unsigned int i = 0; i < propNodes->getLength(); i++) {
82 83 84 85
    properties.push_back(this->readProperty
			 (dynamic_cast<DOMElement*>(propNodes->item(i))));
  }
  return properties;
86 87
}

88
struct property emulab_extensions_parser::readProperty (const DOMElement* tag)
89
{
90 91 92 93 94 95 96 97 98
  struct property propertyObject = {
    this->getAttribute(tag, "property_name"),
    this->getAttribute(tag, "property_value"),
    rspec_parser_helper::stringToNum 
    (this->getAttribute(tag, "property_penalty")),
    this->hasAttribute(tag, "violatable"),
    this->readOperator(tag)
  };
  return propertyObject;
99 100
}

101
struct hardness emulab_extensions_parser::readHardness (const DOMElement* tag)
102
{
103
  struct hardness hardnessObject;
104 105 106
  string strWeight = this->getAttribute(tag, "weight");

  if (strWeight == "hard") {
107 108
    hardnessObject.type = HARD_VCLASS;
  }
109
  else {
110
    hardnessObject.type = SOFT_VCLASS;
111
    hardnessObject.weight = rspec_parser_helper::stringToNum(strWeight);
112 113
  }
  return hardnessObject;
114 115
}

116
vector<struct vclass> 
117
emulab_extensions_parser::readVClasses (const DOMElement* elem)
118
{
119 120 121
  DOMNodeList* vclassNodes 
    = elem->getElementsByTagName(XStr("emulab:vclass").x());
  vector<struct vclass> vclasses;
122
  for (unsigned int i = 0; i < vclassNodes->getLength(); i++) {
123 124 125 126 127
    vclasses.push_back(this->readVClass
		       (dynamic_cast<DOMElement*>
			(vclassNodes->item(i))));
  }
  return vclasses;
128 129
}

130 131
struct vclass 
emulab_extensions_parser::readVClass (const DOMElement* tag)
132
{
133 134 135
  vector<string> physTypes;
  DOMNodeList* physNodes
    = tag->getElementsByTagName(XStr("emulab:physical_type").x());
136
  for (unsigned int i = 0; i < physNodes->getLength(); i++) {
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    DOMElement* physNode = dynamic_cast<DOMElement*>(physNodes->item(i));
    // XXX: This is nasty because the type name that we give assign
    // has to be the concatation of the hardware type and sliver type
    // It's not clear which is the best place to do this
    // i.e. whether to generate this concatenated type here in the parser
    // (in which case any time the formula to convert the single type to 
    // a hardware type and sliver type changes, it will have to be updated
    // both here and in libvtop) or to do it just once in libvtop in which
    // case the reverse holds true i.e. when we change the way we concatenate
    // the types and present it to assign, we will have to change the code 
    // in libvtop
    string physNodeName  = this->getAttribute(physNode, "name");
    physTypes.push_back(rspec_parser_helper::convertType(physNodeName));
  }
    
152
  struct vclass vclassObject = {
153
    rspec_parser_helper::convertType(this->getAttribute(tag, "name")),
154
    this->readHardness(tag),
155
    physTypes
156 157
  };
  return vclassObject;
158 159
}

160
string emulab_extensions_parser::readTypeSlots (const DOMElement* tag)
161
{
162 163 164 165 166 167 168
  DOMNodeList* typeSlotsNodes 
    = tag->getElementsByTagName(XStr("emulab:node_type").x());
  if (typeSlotsNodes->getLength() == 0) {
    return "";
  }
  DOMElement* typeSlotsNode = dynamic_cast<DOMElement*>(typeSlotsNodes->item(0));
  return (this->getAttribute(typeSlotsNode, "type_slots"));
169
}
170

171
bool emulab_extensions_parser::readStaticType (const DOMElement* tag)
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
  DOMNodeList* typeSlotsNodes
    = tag->getElementsByTagName(XStr("emulab:node_type").x());
  if (typeSlotsNodes->getLength() == 0) {
    return false;
  }
  DOMElement* typeSlotsNode = dynamic_cast<DOMElement*>(typeSlotsNodes->item(0));
  return (typeSlotsNode->hasAttribute(XStr("static").x()));
}

vector<struct type_limit> 
emulab_extensions_parser::readTypeLimits (const DOMElement* tag, int& count)
{
  vector<struct type_limit> rv;
  DOMNodeList* typeLimitNodes
    = tag->getElementsByTagName(XStr("emulab:set_type_limit").x());
  count = typeLimitNodes->getLength();
  for (int i = 0; i < count; i++) {
    DOMElement* limitNode = dynamic_cast<DOMElement*>(typeLimitNodes->item(i));
    string typeClass = this->getAttribute(limitNode, "typeclass");
    string typeCount = this->getAttribute(limitNode, "count");
    struct type_limit limit = { 
      typeClass , 
      (int)rspec_parser_helper::stringToNum(typeCount) 
    };
    rv.push_back(limit);
  }
  return rv;
200 201
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
string 
emulab_extensions_parser::readFixedInterface(const DOMElement* tag,bool& fixed)
{
  string rv = "";
  DOMNodeList* fixIfaces 
    = tag->getElementsByTagName(XStr("emulab:fixedinterface").x());
  fixed = (fixIfaces->getLength() == 1);
  if (fixed) {
    DOMElement* fixedIface = dynamic_cast<DOMElement*>(fixIfaces->item(0));
    rv = this->getAttribute(fixedIface, "name");
  }
  return rv;
}

string
emulab_extensions_parser::readShortInterfaceName (const DOMElement* tag,
						  bool& hasShortName)
{
  string rv = "";
  DOMNodeList* interfaces 
    = tag->getElementsByTagName(XStr("emulab:interface").x());
  hasShortName = (interfaces->getLength() == 1);
  if (hasShortName) {
    DOMElement* interface = dynamic_cast<DOMElement*>(interfaces->item(0));
    rv = this->getAttribute(interface, "name");
  }
  return rv;
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
string emulab_extensions_parser::readSubnodeOf (const DOMElement* tag, 
						bool& isSubnode)
{
  string rv = "";
  DOMNodeList* subnodes
    = tag->getElementsByTagName(XStr("emulab:subnode_of").x());
  isSubnode = (subnodes->getLength() > 0);
  if (isSubnode) {
    DOMElement* subnode = dynamic_cast<DOMElement*>(subnodes->item(0));
    rv = this->getAttribute(subnode, "parent");
  }
  return rv;
}

bool emulab_extensions_parser::readDisallowTrivialMix (const DOMElement* tag) 
{
  DOMNodeList* trivialMix 
    = tag->getElementsByTagName(XStr("emulab:disallow_trivial_mix").x());
  return (trivialMix->getLength() > 0);
}

bool emulab_extensions_parser::readUnique (const DOMElement* tag) 
{
  DOMNodeList* uniques = tag->getElementsByTagName(XStr("emulab:unique").x());
  return (uniques->getLength() > 0);
}

int emulab_extensions_parser::readTrivialBandwidth(const DOMElement* tag,
						   bool& hasTrivialBw) 
{
  int trivialBw = 0;
  DOMNodeList* bws 
    = tag->getElementsByTagName(XStr("emulab:trivial_bandwidth").x());
  hasTrivialBw = (bws->getLength() > 0);
  if (hasTrivialBw) {
    trivialBw =(int)this->stringToNum(this->getAttribute
				      (dynamic_cast<DOMElement*>(bws->item(0)),
				       "value"));
  }
  return trivialBw;
}

string emulab_extensions_parser::readHintTo (const DOMElement* tag, 
					     bool& hasHint)
{
  string hint = "";
  DOMNodeList* hints = tag->getElementsByTagName(XStr("emulab:hint_to").x());
  hasHint = (hints->getLength() > 0);
  if (hasHint) {
    hint = this->getAttribute(dynamic_cast<DOMElement*>(hints->item(0)), 
			      "value");
  }
  return hint;
}

bool emulab_extensions_parser::readNoDelay (const DOMElement* tag)
{
  DOMNodeList* nodelays= tag->getElementsByTagName(XStr("emulab:nodelay").x());
  return (nodelays->getLength() > 0);
}

bool emulab_extensions_parser::readTrivialOk (const DOMElement* tag)
{
  DOMNodeList* trivial_oks
    = tag->getElementsByTagName(XStr("emulab:trivial_ok").x());
  return (trivial_oks->getLength() > 0);
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
bool emulab_extensions_parser::readMultiplexOk (const DOMElement* tag)
{
  DOMNodeList* multiplexOks
    = tag->getElementsByTagName(XStr("emulab:multiplex_ok").x());
  return (multiplexOks->getLength() > 0);
}

struct policy emulab_extensions_parser::readPolicy(const DOMElement* tag) 
{
  struct policy policy = {
    this->getAttribute(tag, "type"),
    this->getAttribute(tag, "name"),
    this->getAttribute(tag, "limit")
  };
  return policy;
}

vector<struct policy> 
emulab_extensions_parser::readPolicies (const DOMElement* tag, int& count)
{
  vector<struct policy> rv;
  DOMNodeList* policies = tag->getElementsByTagName(XStr("emulab:policy").x());
  count = policies->getLength();
  for (int i = 0; i < count; i++) {
    rv.push_back
      (this->readPolicy(dynamic_cast<DOMElement*>(policies->item(0))));
  }
  return rv;
}

329
#endif // WITH_XML