rspec_parser_v2.cc 13.1 KB
Newer Older
1 2
/*
 * Copyright (c) 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
 */

/*
 * XML parser for RSPEC version 2.0
 */

#ifdef WITH_XML

30
#include "common.h"
31 32 33
#include "rspec_parser_v2.h"
#include "rspec_parser.h"

34
#include <cstdlib>
35 36 37 38 39 40 41 42 43
#include <string>
#include <vector>
#include <xercesc/dom/DOM.hpp>

#include "xstr.h"

XERCES_CPP_NAMESPACE_USE

using namespace std;
44
using namespace rspec_emulab_extension;
45

46 47
vector<struct link_interface> 
rspec_parser_v2::readLinkInterface (const DOMElement* link, int& ifaceCount)
48
{
49
  vector<struct link_interface> rv;
50 51 52 53 54 55 56
  // We can't use getElementsByTagName here because
  // that returns all interface_refs in all descendants of link
  // which would include any component hops
  // as would be present in a partially-mapped rspec.
  vector<DOMElement*> interfaceRefs 
    = this->getChildrenByName(link, "interface_ref");
  ifaceCount = interfaceRefs.size();
57
  
58
  for (int i = 0; i < ifaceCount; i += 2) {
59
    DOMElement* ref = interfaceRefs[i];
60 61 62 63
    string sourceId = this->getAttribute(ref, "component_id");
    if (this->rspecType == RSPEC_TYPE_REQ) {
      sourceId = this->getAttribute(ref, "client_id");
    }
64
    if ((this->ifacesSeen).find(sourceId) == (this->ifacesSeen).end()) {
65
      cerr << "*** Could not find source interface " << sourceId << endl;
66
      exit(EXIT_FATAL);
67
    }
68 69 70

    string destId = "";
    if ((i+1) < ifaceCount) {
71
      DOMElement* nextRef = interfaceRefs[i+1];
72 73 74 75 76
      destId = this->getAttribute(nextRef, "component_id");
      if (this->rspecType == RSPEC_TYPE_REQ) {
	destId = this->getAttribute(nextRef, "client_id");
      }
      if ((this->ifacesSeen).find(destId) == (this->ifacesSeen).end()) {
77
	cerr << "*** Could not find destination interface " << destId << endl;
78
	exit(EXIT_FATAL);
79
      }
80
    }
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    struct link_interface srcIface, dstIface;
    if (this->rspecType == RSPEC_TYPE_ADVT) {
      srcIface.physicalNodeId = this->ifacesSeen[sourceId];
      srcIface.physicalIfaceId = sourceId;
      dstIface.physicalNodeId = this->ifacesSeen[destId];
      dstIface.physicalIfaceId = destId;
    }
    else { // if (this->rspecType == RSPEC_TYPE_REQ)
      srcIface.virtualNodeId = this->ifacesSeen[sourceId];
      srcIface.virtualIfaceId = sourceId;
      dstIface.virtualNodeId = this->ifacesSeen[destId];
      dstIface.virtualIfaceId = destId;
    }
    
    rv.push_back(srcIface);
    rv.push_back(dstIface);
  }
  return rv;
100 101
}

102 103 104 105
// Since this parser is meant to be used by assign,
// and assign doesn't allow for asymmetric properties on a single link,
// this only returns the specified values when there is only one link property
// 
106
// Returns a link_characteristics element
107 108 109 110 111
struct link_characteristics 
rspec_parser_v2 :: readLinkCharacteristics (const DOMElement* link,
					    int& count,
					    int defaultBandwidth,
					    int unlimitedBandwidth)
112
{
113
  DOMNodeList* properties = link->getElementsByTagName(XStr("property").x());
114
  
115
  string strBw = "", strLat = "", strLoss = "";
116
  int bandwidth = defaultBandwidth, latency = 0;
117 118 119
  float packetLoss = 0.0;

  bool isOk = false;
120
  bool hasBandwidth, hasLatency, hasPacketLoss;
121 122
  count = properties->getLength();

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
  if (count > 0) {
    // Read only from the first property and ignore the rest
    DOMElement* property = dynamic_cast<DOMElement*>(properties->item(0));
    strBw = this->getAttribute(property, "capacity", hasBandwidth);
    strLat = this->getAttribute(property, "latency", hasLatency);
    strLoss = this->getAttribute(property, "packet_loss", hasPacketLoss);
    
    if (!hasBandwidth) {
      bandwidth = defaultBandwidth;
    }
    else if(strBw == "unlimited") {
      bandwidth = unlimitedBandwidth;
    }
    else {
      bandwidth = atoi(strBw.c_str());
    }
    
    latency = hasLatency ? atoi(strLat.c_str()) : 0 ;
    packetLoss = hasPacketLoss ? atof(strLoss.c_str()) : 0.0;

    if (count > 1) {
      int revBandwidth = 0, revLatency = 0;
      float revPacketLoss = 0.0;
      
      DOMElement* property = dynamic_cast<DOMElement*>(properties->item(1));
      strBw = this->getAttribute(property, "capacity", hasBandwidth);
      strLat = this->getAttribute(property, "latency", hasLatency);
      strLoss = this->getAttribute(property, "packet_loss", hasPacketLoss);
      
      if (!hasBandwidth) {
        revBandwidth = defaultBandwidth;
      }
      else if(strBw == "unlimited") {
        revBandwidth = unlimitedBandwidth;
      }
      else {
        revBandwidth = atoi(strBw.c_str());
      }
      
      revLatency = hasLatency ? atoi(strLat.c_str()) : 0 ;
      revPacketLoss = hasPacketLoss ? atof(strLoss.c_str()) : 0.0;
      
      if (bandwidth == revBandwidth 
          && latency == revLatency 
          && packetLoss == revPacketLoss) {
        isOk = true;
      }
    }
  }
    
  if (!isOk) {
    count = RSPEC_ASYMMETRIC_LINK;
  }
176 177
  struct link_characteristics rv = {bandwidth, latency, packetLoss};
  return rv;
178 179
}

180 181 182 183
vector<struct node_type> 
rspec_parser_v2::readNodeTypes (const DOMElement* node,
				int& typeCount,
				int unlimitedSlots)
184
{
185 186 187 188 189
  string defHw = "pc";
  string defSl = "raw-pc";
  bool isSwitch = false;

  vector<struct node_type> types;
190 191 192 193 194 195 196 197 198 199 200
  DOMNodeList* sliverTypes 
    = node->getElementsByTagName(XStr("sliver_type").x());
  DOMNodeList* hardwareTypes 
    = node->getElementsByTagName(XStr("hardware_type").x());

  if (hardwareTypes->getLength() == 0) {
    // If there are no sliver types, return an empty vector
    if (sliverTypes->getLength() == 0) {
      typeCount = 0;
      return vector<struct node_type>();
    }
201

202
    for (unsigned int j = 0; j < sliverTypes->getLength(); j++) {
203
      DOMElement* slNode = dynamic_cast<DOMElement*>(sliverTypes->item(j));
204
      string slTypeName = this->getAttribute(slNode, "name");
205
      string typeName = this->convertType(defHw, slTypeName);
206 207 208 209 210
      struct node_type type = {typeName, 1, false};
      types.push_back(type);
    }
  }
  
211
  for (unsigned int i = 0; i < hardwareTypes->getLength(); i++) {
212 213
    DOMElement* hardwareNode 
      = dynamic_cast<DOMElement*>(hardwareTypes->item(i));
214 215
    
    string hwTypeName = this->getAttribute (hardwareNode, "name");
216

217 218 219 220 221
    string slot = (this->emulabExtensions)->readTypeSlots(hardwareNode);
    // Default number of slots
    if (slot == "") {
      slot = "unlimited";
    }
222 223
    int typeSlots 
      = (slot == "unlimited") ? unlimitedSlots : (int)stringToNum(slot);
224
    bool isStatic = (this->emulabExtensions)->readStaticType(hardwareNode);
225 226 227 228 229 230 231 232 233 234 235 236 237

    // XXX: If the node is a switch, add it to the list of switches
    // Since switches are treated specially in assign 
    // and it's such a hardcoded &#$*% mess, if a hardware type is "switch"
    // only switch is returned and the sliver types are ignored
    // If someone wants a switch with sliver type openvz or something like that
    // they are out of luck
    if (hwTypeName == "switch") {
      isSwitch = true;
      struct node_type type = {"switch", typeSlots, isStatic};
      types.push_back(type);
      continue;
    }
238 239
    
    if (sliverTypes->getLength() == 0) {
240
      string typeName = this->convertType(hwTypeName, defSl);
241 242 243 244
      struct node_type type = {typeName, typeSlots, isStatic};
      types.push_back(type);
    }

245
    for (unsigned int j = 0; j < sliverTypes->getLength(); j++) {
246 247
      DOMElement* slNode = dynamic_cast<DOMElement*>(sliverTypes->item(j));
      string slTypeName = this->getAttribute(slNode, "name");
248
      string typeName = this->convertType(hwTypeName, slTypeName);
249 250 251 252 253
      
      struct node_type type = {typeName, typeSlots, isStatic};
      types.push_back(type);
    }
  }
254 255 256 257 258

  if (isSwitch) {
    this->addSwitch(node);
  }

259 260
  typeCount = types.size();
  return types;
261 262 263
}

map< pair<string, string>, pair<string, string> >
264 265 266 267 268 269
rspec_parser_v2::readInterfacesOnNode  (const DOMElement* node, 
					bool& allUnique)
{
  DOMNodeList* ifaces = node->getElementsByTagName(XStr("interface").x());
  map< pair<string, string>, pair<string, string> > fixedInterfaces;
  allUnique = true;
270
  for (unsigned int i = 0; i < ifaces->getLength(); i++) {
271 272 273 274 275 276 277
    DOMElement* iface = dynamic_cast<DOMElement*>(ifaces->item(i));
    bool hasAttr;
    string nodeId = "";
    string ifaceId = "";
    if (this->rspecType == RSPEC_TYPE_ADVT) {
      nodeId = this->readPhysicalId (node, hasAttr);
      ifaceId = XStr(iface->getAttribute(XStr("component_id").x())).c();
278 279 280 281 282 283
      bool hasShortName = false;
      string shortName 
	= (this->emulabExtensions)->readShortInterfaceName(iface,hasShortName);
      if (hasShortName) {
	(this->shortNames).insert(pair<string,string>(ifaceId, shortName));
      }
284 285 286 287
    }
    else { //(this->rspecType == RSPEC_TYPE_REQ)
      nodeId = this->readVirtualId (node, hasAttr);
      ifaceId = XStr(iface->getAttribute(XStr("client_id").x())).c();
288 289 290 291
      bool isFixed = false;
      string fixedTo
	= (this->emulabExtensions)->readFixedInterface(iface, isFixed);
      if (isFixed) {
292 293
	fixedInterfaces.insert (make_pair 
				(make_pair(nodeId,ifaceId),
294
				 make_pair("", fixedTo)));
295 296 297 298 299 300 301 302 303 304
      }
    }
    allUnique &= ((this->ifacesSeen).insert	
		  (pair<string, string> (ifaceId, nodeId))).second;
  }
  return (fixedInterfaces);
}

// Reads the available tag. In v2, it is mandatory
string rspec_parser_v2::readAvailable (const DOMElement* node, bool& hasTag) 
305
{
306 307 308 309 310 311 312
  DOMNodeList* tags = node->getElementsByTagName(XStr("available").x());
  hasTag = (tags->getLength() > 0);
  if (hasTag) {
    DOMElement* availableNode = dynamic_cast<DOMElement*>(tags->item(0));
    return (this->getAttribute(availableNode, "now"));
  }
  return "";
313 314
}

315 316 317 318 319
vector<struct link_type> 
rspec_parser_v2::readLinkTypes (const DOMElement* link, int& typeCount)
{
  DOMNodeList* linkTypes = link->getElementsByTagName(XStr("link_type").x());
  vector<struct link_type> types;
320
  for (unsigned int i = 0; i < linkTypes->getLength(); i++) {
321 322 323 324 325 326 327 328 329 330
    DOMElement* tag = dynamic_cast<DOMElement*>(linkTypes->item(i));
    string name = this->getAttribute(tag, "name");

    struct link_type type = {name, name};
    types.push_back(type);
  }
  typeCount = types.size();
  return types;
}

331 332 333 334 335 336 337
string rspec_parser_v2::readSubnodeOf (const DOMElement* node,
				       bool& isSubnode,
				       int& count)
{
  int cnt = 0;
  string parent = "";
  DOMNodeList* relations = node->getElementsByTagName(XStr("relations").x());
338
  for (unsigned int i = 0, cnt = 0; i < relations->getLength(); i++) {
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    DOMElement* relation = dynamic_cast<DOMElement*>(relations->item(0));
    string type = this->getAttribute(node, "type");
    if (type == "parent") {
      bool hasSubnode = false;
      parent = (this->emulabExtensions)->readSubnodeOf(relation, hasSubnode);
      if (hasSubnode) {
	isSubnode = true;
	++cnt;
      }
    }
  }
  count = cnt;
  if (count == 1) {
    return parent;
  }
  return "";
}

vector<struct vclass>
rspec_parser_v2::readVClasses(const DOMElement* tag) 
{
  return ((this->emulabExtensions)->readVClasses(tag));
}

363 364 365 366 367
vector<struct type_limit>
rspec_parser_v2::readTypeLimits (const DOMElement* tag, int& count)
{
  return ((this->emulabExtensions)->readTypeLimits(tag, count));
}
368

369 370 371 372 373 374
vector<struct fd>
rspec_parser_v2::readFeaturesDesires (const DOMElement* tag, int& count)
{
  return ((this->emulabExtensions)->readFeaturesDesires(tag, count));
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
bool rspec_parser_v2::readDisallowTrivialMix (const DOMElement* tag)
{
  return ((this->emulabExtensions)->readDisallowTrivialMix(tag));
}

bool rspec_parser_v2::readUnique (const DOMElement* tag)
{
  return ((this->emulabExtensions)->readUnique(tag));
}

int rspec_parser_v2::readTrivialBandwidth (const DOMElement* tag,
					   bool& hasTrivialBw)
{
  return ((this->emulabExtensions)->readTrivialBandwidth(tag, hasTrivialBw));
}

string rspec_parser_v2::readHintTo (const DOMElement* tag, bool& hasHintTo)
{
  return ((this->emulabExtensions)->readHintTo(tag, hasHintTo));
}

bool rspec_parser_v2::readNoDelay (const DOMElement* tag)
{
  return ((this->emulabExtensions)->readNoDelay(tag));
}

bool rspec_parser_v2::readTrivialOk (const DOMElement* tag)
{
  return ((this->emulabExtensions)->readTrivialOk(tag));
}

406 407 408 409 410 411 412 413 414 415 416
bool rspec_parser_v2::readMultiplexOk (const DOMElement* tag)
{
  return ((this->emulabExtensions)->readMultiplexOk(tag));
}

vector<struct policy> 
rspec_parser_v2::readPolicies (const DOMElement* tag, int& count) 
{
  return ((this->emulabExtensions)->readPolicies(tag, count));
}

417 418 419 420 421 422 423 424 425 426 427
string rspec_parser_v2::convertType (const string hwType, const string slType) 
{
  return rspec_parser_helper::convertType(hwType, slType);
}

string rspec_parser_v2::convertType (const string type)
{
  return rspec_parser_helper::convertType(type);
}


428
#endif // #ifdef WITH_XML