Changeset b9c0090
- Timestamp:
- Apr 8, 2011 2:32:11 PM (14 years ago)
- Branches:
- axis_example, compt_changes, info-ops, master
- Children:
- a218fe2
- Parents:
- e2d324a
- Location:
- axis
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
axis/Create.java
re2d324a rb9c0090 5 5 import edu.isi.www.fedd_types.*; 6 6 import edu.isi.www.fedd_wsdl.*; 7 8 // Topdl classes 9 import edu.isi.www.topdl.*; 7 10 8 11 // The fault thrown by failed commands … … 41 44 42 45 /** 46 * Reads a topology file as topdl. If this fails, just return null as 47 * later code will read the file as tcl. 48 * @param f The file to read 49 * @return the TopologyType encoded, or null of unparsable/unreadable 50 * @throws IOException if the file cannot be read 51 */ 52 static public TopologyType readTopdl(File f) throws IOException { 53 try { 54 ParseTopdl p = new ParseTopdl(new FileInputStream(f), "experiment"); 55 return p.getTopology(); 56 } 57 catch (IOException e) { throw e; } 58 catch (Exception e) { return null; } 59 } 60 61 /** 43 62 * Create an ABAC credential indicating the the given destination acts for 44 63 * the given Identity, and attach a certificate to it. 64 * For some reason, the parse doesn't fail silently - something in the 65 * bowels of the XML parser prints an error. Sigh. 45 66 * @param id the Identity delegating authority 46 67 * @param dest the destination … … 70 91 // Parse out the args 71 92 String exptName = "test"; 72 String t clFile = "./deter-only.tcl";93 String topoFileName = "./deter-only.tcl"; 73 94 String certFile = "./emulab.pem"; 74 95 String urlString = "https://users.isi.deterlab.net:23235"; 75 96 76 97 if (args.length > 0) exptName = args[0]; 77 if (args.length > 1) t clFile = args[1];98 if (args.length > 1) topoFileName = args[1]; 78 99 if (args.length > 2) certFile = args[2]; 79 100 if (args.length > 3) urlString = args[3]; … … 83 104 */ 84 105 FeddPortType port = getPort(urlString); 106 File topoFile = new File(topoFileName); 85 107 Identity AbacID = null; 86 108 byte[] nsContents = null; 87 try { 88 nsContents = readNsFile(new File(tclFile)); 109 TopologyType topo = null; 110 111 try { 112 113 if ( (topo = readTopdl(topoFile)) == null) 114 nsContents = readNsFile(topoFile); 115 116 } 117 catch (IOException e) { 118 System.err.println("Cannot load topology file " + e); 119 System.exit(20); 120 } 121 122 try { 89 123 AbacID = new Identity(new File(certFile)); 90 124 } … … 96 130 } 97 131 catch (IOException e) { 98 System.err.println("Cannot load file " + e); 132 System.err.println("Cannot load ABAC id from " + 133 certFile + ": " + e); 99 134 System.exit(20); 100 135 } … … 122 157 123 158 CreateRequestType createReq = new CreateRequestType(null, 124 new ExperimentDescriptionType(nsContents, null),159 new ExperimentDescriptionType(nsContents, topo), 125 160 null, 126 161 new IDType(null, null, null, newLabels.getLocalname(), null), -
axis/ParseTopdl.java
re2d324a rb9c0090 12 12 import org.xml.sax.helpers.*; 13 13 14 class ParseTopdl {14 public class ParseTopdl { 15 15 /** Parser instance */ 16 16 protected XMLReader xr; 17 17 /** Handler that parses topdl file */ 18 protected TopdlHandler h; 19 20 /** 21 * This class encapsulates the state of an XML parse of a topdl file. It 22 * contains abunch of parsing state and is called from the standard SAX 23 * parser at 3 places: when an XML element starts (<element>) when 24 * one ends (<element/>) and whenever characters inside an element 25 * are encountered. It mostly initializes fileds as elements are exited, 26 * collecting other parsed fields into objects that are finally attached to 27 * the topology itself. 28 */ 18 29 protected class TopdlHandler extends DefaultHandler { 30 /** The name of the outermost element, "experiment" by default */ 19 31 protected String topName; 20 32 21 33 // Topology parameters 34 /** The current topology being built */ 22 35 protected TopologyType topo; 36 /** The current element being built */ 23 37 protected ElementType element; 38 /** The current computer being built */ 24 39 protected ComputerType comp; 40 /** The current testbed being built */ 25 41 protected TestbedType tb; 42 /** The current segment being built */ 26 43 protected SegmentType seg; 44 /** The current other element being built */ 27 45 protected OtherType other; 28 46 29 47 // Many elements have a name 48 /** the most recent name element parsed */ 30 49 protected String name; 31 50 32 51 // CPU parameters 52 /** current cpu type */ 33 53 protected String type; 54 /** current number of cpus */ 34 55 protected int ncpus; 35 56 36 57 // Operatingsystem parameters 58 /** Current OS version */ 37 59 protected String version; 60 /** Current OS distro */ 38 61 protected String distribution; 62 /** Current OS distro version */ 39 63 protected String distributionversion; 40 64 41 65 // Software parameters 66 /** Current software installation point */ 42 67 protected String install; 68 /** Current software location */ 43 69 protected String location; 44 70 45 71 // Storage parameters 72 /** Current storage amount */ 46 73 protected double amount; 74 /** Current storage persistence */ 47 75 protected PersistenceType persistence; 48 76 49 77 // Interface Parameters 78 /** Vector of substrate names the current interface is attached to */ 50 79 protected Vector<String> ifsubs; 80 /** Current interface capacity */ 51 81 protected edu.isi.www.topdl.CapacityType cap; 82 /** Current interface latency */ 52 83 protected LatencyType lat; 53 84 54 85 // Capacity parameters 86 /** Current capacity rate (bandwidth) */ 55 87 protected double rate; 88 /** Current capacity kind (max, peak)*/ 56 89 protected edu.isi.www.topdl.KindType kind; 57 90 58 91 // Latency parameters (shares kind) 92 /** Current latency time */ 59 93 protected double time; 60 94 61 95 // Testbed parameters (shares interfaces, type) 96 /** Current URI */ 62 97 protected String uri; 63 98 64 99 // Segment parameters (shares interfaces, type, uri) 100 /** Current segment ID */ 65 101 protected IDType id; 66 102 // IDType parameters 103 /** Current ID uuid (if any) */ 67 104 protected byte[] uuid; 105 /** Current ID fedid (if any) */ 68 106 protected byte[] fedid; 107 /** Current ID uri (if any) */ 69 108 protected String id_uri; 109 /** Current ID localname (if any) */ 70 110 protected String localname; 111 /** Current ID kerberosUsername (if any) */ 71 112 protected String kerberosUsername; 113 /** True when we are parsing an ID (so URIs are stored in id_uri) */ 72 114 protected boolean inID; 73 115 74 116 // Attribute parameters 117 /** Current attribute name */ 75 118 protected String aname; 119 /** Current attribute value */ 76 120 protected String aval; 77 121 122 /** Elements seen so far */ 78 123 protected Vector<ElementType> elements; 124 /** Substrates seen so far */ 79 125 protected Vector<SubstrateType> subs; 126 /** Attributes seen so far */ 80 127 protected Vector<AttributeType> attrs; 128 /** CPUs seen so far */ 81 129 protected Vector<CpuType> cpus; 130 /** Operating Systems seen so far */ 82 131 protected Vector<OperatingsystemType> oses; 132 /** Software seen so far */ 83 133 protected Vector<SoftwareType> software; 134 /** Storage seen so far */ 84 135 protected Vector<StorageType> storage; 136 /** Interfaces seen so far */ 85 137 protected Vector<InterfaceType> interfaces; 86 138 139 /** 140 * Attributes appear in many elements, some of which can appear inside 141 * the definition of others. We keep a stack of live attributes so 142 * that a sub-element does not overwrite a super-element's attributes. 143 * attrElements is a set of element names that should start a new set 144 * of recorded attributes, and attrStack keeps the contexts stacked. 145 */ 87 146 protected Set<String> attrElements; 147 /** 148 * The stack of attribute contexts. 149 */ 88 150 protected Stack<Vector<AttributeType>> attrStack; 151 /** 152 * Analogous to attrElements for names. 153 */ 154 protected Set<String> nameElements; 155 /** 156 * Analogous to attrStack for names. 157 */ 158 protected Stack<String> nameStack; 159 /** 160 * The current buffer of characters collected. 161 */ 89 162 protected char[] c; 90 163 91 TopdlHandler(String top) { 164 /** 165 * Print parsing info if true 166 */ 167 protected boolean debug; 168 169 /** 170 * Initialize the internal data structures. Top is the outer element 171 * name. If d is true, debugging info is printed. 172 * @param top a String containing the outer element name 173 * @param d a boolean, if true pring debugging info 174 */ 175 TopdlHandler(String top, boolean d) { 176 debug = d; 92 177 topName = top; 93 178 topo = null; … … 129 214 130 215 elements = new Vector<ElementType>(); 216 cpus = new Vector<CpuType>(); 131 217 subs = new Vector<SubstrateType>(); 132 attrs = new Vector<AttributeType>();133 218 oses = new Vector<OperatingsystemType>(); 134 219 software = new Vector<SoftwareType>(); 135 220 storage = new Vector<StorageType>(); 136 221 interfaces = new Vector<InterfaceType>(); 222 attrs = new Vector<AttributeType>(); 137 223 138 224 attrElements = new TreeSet<String>(); … … 143 229 144 230 attrStack = new Stack<Vector<AttributeType>>(); 231 232 nameElements = new TreeSet<String>(); 233 for (String e : new String[] { 234 "computer", "os", "interface", "substrates" 235 }) nameElements.add(e); 236 nameStack = new Stack<String>(); 145 237 c = new char[0]; 146 238 } 147 239 240 /** 241 * Called when an element begins. qn is the element and a has 242 * attributes assigned in the element. This starts new name or 243 * attribute contexts, if necessary as well as noting the start of an 244 * id (which is a lightweight uri context). The parameter definitions 245 * below are from org.xml.sax.helpers.DefaultHandler 246 * @param u - The Namespace URI, or the empty string if the element has 247 * no Namespace URI or if Namespace processing is not being performed. 248 * @param l - The local name (without prefix), or the empty string if 249 * Namespace processing is not being performed. 250 * @param qn - The qualified name (with prefix), or the empty string if 251 * qualified names are not available. 252 * @param a - The attributes attached to the element. If there are no 253 * attributes, it shall be an empty Attributes object. 254 */ 148 255 public void startElement(String u, String l, String qn, Attributes a) 149 256 throws SAXException { 150 257 258 if (debug) System.err.println("<" + qn + ">"); 151 259 c = new char[0]; 152 260 if ( attrElements.contains(qn) ) { 153 261 attrStack.push(attrs); 154 262 attrs = new Vector<AttributeType>(); 263 } 264 if ( nameElements.contains(qn) ) { 265 nameStack.push(name); 266 name = null; 155 267 } 156 268 if (qn.equals("id")) inID = true; … … 162 274 } 163 275 276 /** 277 * Collect the data from each element and stash it where any containing 278 * elent can find it when it is time to construct that thing. Clear 279 * any fields used. This is long but straightforward parsing. 280 * @param u - The Namespace URI, or the empty string if the element has 281 * no Namespace URI or if Namespace processing is not being performed. 282 * @param l - The local name (without prefix), or the empty string if 283 * Namespace processing is not being performed. 284 * @param qn - The qualified name (with prefix), or the empty string if 285 * qualified names are not available. 286 */ 164 287 public void endElement(String u, String l, String qn) 165 288 throws SAXException { 166 289 290 if (debug) System.err.println("<" + qn + "/>"); 291 292 // Each branch parses the data from the given element, e.g, 293 // "computer" parses the fields collected for </computer>. In that 294 // case the data stashed by trips through here for 295 // <operatingsystem>, <name>, etc. 296 // 297 // The vector.toArray(new Type[vector.size()], idiom just returns 298 // the contents of the vector as a java array of the vector's type. 167 299 if (qn.equals(topName)) { 168 300 topo = new TopologyType("1.0", … … 174 306 attrs = attrStack.pop(); 175 307 } 176 else if (qn.equals("element ")) {308 else if (qn.equals("elements")) { 177 309 elements.add(new ElementType(comp, tb, seg, other)); 178 310 comp = null; … … 190 322 new InterfaceType[interfaces.size()]), 191 323 attrs.toArray(new AttributeType[attrs.size()])); 192 name = n ull;324 name = nameStack.pop(); 193 325 cpus = new Vector<CpuType>(); 194 326 oses = new Vector<OperatingsystemType>(); … … 210 342 distribution, distributionversion, 211 343 attrs.toArray(new AttributeType[attrs.size()]))); 212 name = version = distribution = distributionversion = null; 344 name = nameStack.pop(); 345 version = distribution = distributionversion = null; 213 346 attrs = attrStack.pop(); 214 347 } … … 250 383 attrs.toArray(new AttributeType[attrs.size()]))); 251 384 ifsubs = new Vector<String>(); 385 name = nameStack.pop(); 252 386 cap = null; 253 387 lat = null; … … 332 466 subs.add(new SubstrateType(name, cap, lat, 333 467 attrs.toArray(new AttributeType[attrs.size()]))); 334 name = n ull;468 name = nameStack.pop(); 335 469 cap = null; 336 470 lat = null; … … 347 481 else if ( qn.equals("name")) { name = new String(c).trim(); } 348 482 483 // Always clear any accumulated characters 349 484 c = new char[0]; 350 485 } … … 359 494 c = nc; 360 495 } 496 /** 497 * Return the parsed topology 498 * @return the parsed topology 499 */ 500 public TopologyType getTopology() { return topo; } 361 501 } 362 502 363 public ParseTopdl(InputStream s) 503 /** 504 * Main constructor. Parse the given stream assuming that the outermost 505 * element is in topName (defaults to "experiment" if topName is null). If 506 * debug is true, print extra debugging information to the standard error 507 * stream. 508 * @param s the InputStream to parse 509 * @param topName a String with the outer element name 510 * @param debug a boolean, true for extra debugging output 511 * @throws IOException if the stream cannot be read 512 * @throws SAXException if the parsing fails 513 * @throws ParserConfigurationException if the parser creation fails 514 */ 515 public ParseTopdl(InputStream s, String topName, boolean debug) 364 516 throws IOException, SAXException, ParserConfigurationException { 517 h = new TopdlHandler(topName != null ? topName: "experiment", debug); 518 365 519 xr = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 366 520 xr.setContentHandler(h); 367 521 xr.parse(new InputSource(s)); 368 522 } 369 523 370 524 /** 525 * Equivalent to ParseTopdl(s, topName, false) 526 * @param s the InputStream to parse 527 * @param topName a String with the outer element name 528 * @throws IOException if the stream cannot be read 529 * @throws SAXException if the parsing fails 530 * @throws ParserConfigurationException if the parser creation fails 531 */ 532 public ParseTopdl(InputStream s, String topName) 533 throws IOException, SAXException, ParserConfigurationException { 534 this(s, topName, false); 535 } 536 537 /** 538 * Return the parsed topology 539 * @return the parsed topology 540 */ 541 public TopologyType getTopology() { return h.getTopology(); } 542 543 /** 544 * Test code. Shouldn't really be called. 545 */ 546 static public void main(String args[]) { 547 ParseTopdl p = null; 548 try { 549 p = new ParseTopdl(new FileInputStream(new File(args[0])), 550 "experiment", true); 551 } 552 catch (ParserConfigurationException e) { 553 System.err.println(e); 554 } 555 catch (SAXException e) { 556 System.err.println(e); 557 } 558 catch (IOException e) { 559 System.err.println(e); 560 } 561 } 371 562 }
Note: See TracChangeset
for help on using the changeset viewer.