Changeset b9c0090


Ignore:
Timestamp:
Apr 8, 2011 2:32:11 PM (14 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master
Children:
a218fe2
Parents:
e2d324a
Message:

Parses topdl to the point where it will successfully create.

Location:
axis
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • axis/Create.java

    re2d324a rb9c0090  
    55import edu.isi.www.fedd_types.*;
    66import edu.isi.www.fedd_wsdl.*;
     7
     8// Topdl classes
     9import edu.isi.www.topdl.*;
    710
    811// The fault thrown by failed commands
     
    4144
    4245    /**
     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    /**
    4362     * Create an ABAC credential indicating the the given destination acts for
    4463     * 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.
    4566     * @param id the Identity delegating authority
    4667     * @param dest the destination
     
    7091        // Parse out the args
    7192        String exptName = "test";
    72         String tclFile = "./deter-only.tcl";
     93        String topoFileName = "./deter-only.tcl";
    7394        String certFile = "./emulab.pem";
    7495        String urlString = "https://users.isi.deterlab.net:23235";
    7596
    7697        if (args.length > 0) exptName = args[0];
    77         if (args.length > 1) tclFile = args[1];
     98        if (args.length > 1) topoFileName = args[1];
    7899        if (args.length > 2) certFile = args[2];
    79100        if (args.length > 3) urlString = args[3];
     
    83104         */
    84105        FeddPortType port = getPort(urlString);
     106        File topoFile = new File(topoFileName);
    85107        Identity AbacID = null;
    86108        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 {
    89123            AbacID = new Identity(new File(certFile));
    90124        }
     
    96130        }
    97131        catch (IOException e) {
    98             System.err.println("Cannot load file " + e);
     132            System.err.println("Cannot load ABAC id from " +
     133                    certFile + ": " + e);
    99134            System.exit(20);
    100135        }
     
    122157
    123158        CreateRequestType createReq = new CreateRequestType(null,
    124                 new ExperimentDescriptionType(nsContents, null),
     159                new ExperimentDescriptionType(nsContents, topo),
    125160                null,
    126161                new IDType(null, null, null, newLabels.getLocalname(), null),
  • axis/ParseTopdl.java

    re2d324a rb9c0090  
    1212import org.xml.sax.helpers.*;
    1313
    14 class ParseTopdl {
     14public class ParseTopdl {
    1515    /** Parser instance */
    1616    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 (&lt;element&gt;) when
     24     * one ends (&lt;element/&gt;) 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     */
    1829    protected class TopdlHandler extends DefaultHandler {
     30        /** The name of the outermost element, "experiment" by default */
    1931        protected String topName;
    2032
    2133        // Topology parameters
     34        /** The current topology being built */
    2235        protected TopologyType topo;
     36        /** The current element being built */
    2337        protected ElementType element;
     38        /** The current computer being built */
    2439        protected ComputerType comp;
     40        /** The current testbed being built */
    2541        protected TestbedType tb;
     42        /** The current segment being built */
    2643        protected SegmentType seg;
     44        /** The current other element being built */
    2745        protected OtherType other;
    2846
    2947        // Many elements have a name
     48        /** the most recent name element parsed */
    3049        protected String name;
    3150
    3251        // CPU parameters
     52        /** current cpu type */
    3353        protected String type;
     54        /** current number of cpus */
    3455        protected int ncpus;
    3556
    3657        // Operatingsystem parameters
     58        /** Current OS version */
    3759        protected String version;
     60        /** Current OS distro */
    3861        protected String distribution;
     62        /** Current OS distro version */
    3963        protected String distributionversion;
    4064
    4165        // Software parameters
     66        /** Current software installation point */
    4267        protected String install;
     68        /** Current software location */
    4369        protected String location;
    4470
    4571        // Storage parameters
     72        /** Current storage amount */
    4673        protected double amount;
     74        /** Current storage persistence */
    4775        protected PersistenceType persistence;
    4876
    4977        // Interface Parameters
     78        /** Vector of substrate names the current interface is attached to */
    5079        protected Vector<String> ifsubs;
     80        /** Current interface capacity */
    5181        protected edu.isi.www.topdl.CapacityType cap;
     82        /** Current interface latency */
    5283        protected LatencyType lat;
    5384
    5485        // Capacity parameters
     86        /** Current capacity rate (bandwidth) */
    5587        protected double rate;
     88        /** Current capacity kind (max, peak)*/
    5689        protected edu.isi.www.topdl.KindType kind;
    5790
    5891        // Latency parameters (shares kind)
     92        /** Current latency time */
    5993        protected double time;
    6094
    6195        // Testbed parameters (shares interfaces, type)
     96        /** Current URI */
    6297        protected String uri;
    6398
    6499        // Segment parameters (shares interfaces, type, uri)
     100        /** Current segment ID */
    65101        protected IDType id;
    66102        // IDType parameters
     103        /** Current ID uuid (if any) */
    67104        protected byte[] uuid;
     105        /** Current ID fedid (if any) */
    68106        protected byte[] fedid;
     107        /** Current ID uri (if any) */
    69108        protected String id_uri;
     109        /** Current ID localname (if any) */
    70110        protected String localname;
     111        /** Current ID kerberosUsername (if any) */
    71112        protected String kerberosUsername;
     113        /** True when we are parsing an ID (so URIs are stored in id_uri) */
    72114        protected boolean inID;
    73115
    74116        // Attribute parameters
     117        /** Current attribute name */
    75118        protected String aname;
     119        /** Current attribute value */
    76120        protected String aval;
    77121
     122        /** Elements seen so far */
    78123        protected Vector<ElementType> elements;
     124        /** Substrates seen so far */
    79125        protected Vector<SubstrateType> subs;
     126        /** Attributes seen so far */
    80127        protected Vector<AttributeType> attrs;
     128        /** CPUs seen so far */
    81129        protected Vector<CpuType> cpus;
     130        /** Operating Systems seen so far */
    82131        protected Vector<OperatingsystemType> oses;
     132        /** Software seen so far */
    83133        protected Vector<SoftwareType> software;
     134        /** Storage seen so far */
    84135        protected Vector<StorageType> storage;
     136        /** Interfaces seen so far */
    85137        protected Vector<InterfaceType> interfaces;
    86138
     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         */
    87146        protected Set<String> attrElements;
     147        /**
     148         * The stack of attribute contexts.
     149         */
    88150        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         */
    89162        protected char[] c;
    90163
    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;
    92177            topName = top;
    93178            topo = null;
     
    129214
    130215            elements = new Vector<ElementType>();
     216            cpus = new Vector<CpuType>();
    131217            subs = new Vector<SubstrateType>();
    132             attrs = new Vector<AttributeType>();
    133218            oses = new Vector<OperatingsystemType>();
    134219            software = new Vector<SoftwareType>();
    135220            storage = new Vector<StorageType>();
    136221            interfaces = new Vector<InterfaceType>();
     222            attrs = new Vector<AttributeType>();
    137223
    138224            attrElements = new TreeSet<String>();
     
    143229
    144230            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>();
    145237            c = new char[0];
    146238        }
    147239
     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         */
    148255        public void startElement(String u, String l, String qn, Attributes a)
    149256                throws SAXException {
    150257
     258            if (debug) System.err.println("<" + qn + ">");
    151259            c = new char[0];
    152260            if ( attrElements.contains(qn) ) {
    153261                attrStack.push(attrs);
    154262                attrs = new Vector<AttributeType>();
     263            }
     264            if ( nameElements.contains(qn) ) {
     265                nameStack.push(name);
     266                name = null;
    155267            }
    156268            if (qn.equals("id")) inID = true;
     
    162274        }
    163275
     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         */
    164287        public void endElement(String u, String l, String qn)
    165288                throws SAXException {
    166289
     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.
    167299            if (qn.equals(topName)) {
    168300                topo = new TopologyType("1.0",
     
    174306                attrs = attrStack.pop();
    175307            }
    176             else if (qn.equals("element")) {
     308            else if (qn.equals("elements")) {
    177309                elements.add(new ElementType(comp, tb, seg, other));
    178310                comp = null;
     
    190322                            new InterfaceType[interfaces.size()]),
    191323                        attrs.toArray(new AttributeType[attrs.size()]));
    192                 name = null;
     324                name = nameStack.pop();
    193325                cpus = new Vector<CpuType>();
    194326                oses = new Vector<OperatingsystemType>();
     
    210342                        distribution, distributionversion,
    211343                        attrs.toArray(new AttributeType[attrs.size()])));
    212                 name = version = distribution = distributionversion = null;
     344                name = nameStack.pop();
     345                version = distribution = distributionversion = null;
    213346                attrs = attrStack.pop();
    214347            }
     
    250383                            attrs.toArray(new AttributeType[attrs.size()])));
    251384                ifsubs = new Vector<String>();
     385                name = nameStack.pop();
    252386                cap = null;
    253387                lat = null;
     
    332466                subs.add(new SubstrateType(name, cap, lat,
    333467                        attrs.toArray(new AttributeType[attrs.size()])));
    334                 name = null;
     468                name = nameStack.pop();
    335469                cap = null;
    336470                lat = null;
     
    347481            else if ( qn.equals("name")) { name = new String(c).trim(); }
    348482
     483            // Always clear any accumulated characters
    349484            c = new char[0];
    350485        }
     
    359494            c = nc;
    360495        }
     496        /**
     497         * Return the parsed topology
     498         * @return the parsed topology
     499         */
     500        public TopologyType getTopology() { return topo; }
    361501    }
    362502
    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)
    364516        throws IOException, SAXException, ParserConfigurationException {
     517        h = new TopdlHandler(topName != null ? topName: "experiment", debug);
     518
    365519        xr = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
    366 
     520        xr.setContentHandler(h);
    367521        xr.parse(new InputSource(s));
    368522    }
    369523
    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    }
    371562}
Note: See TracChangeset for help on using the changeset viewer.