Navigation
Guide That Contains This Content
[+] Expand All
[-] Collapse All

    NETCONF Java Toolkit Class: XML

    A net.juniper.netconf.XML object represents XML-encoded data and provides methods to modify and parse the XML. The XML object internally maintains an org.w3c.dom.Document object, corresponding to the XML data it represents.

    It is recommended that you work with the XML object to create new configurations, remote procedure calls (RPCs), or any XML-based data. Using an XML object, you can easily add, delete, or modify elements and attributes. To facilitate modification of XML content, the XML object maintains an ‘active’ element, which represents the hierarchy level exposed for modification.

    To create an XML object, you first create an XMLBuilder object and construct the initial XML hierarchy. The XMLBuilder methods return an XML object on which you can then build. This makes it convenient to create XML-based configurations and RPCs and also parse the XML-based replies received from the NETCONF server.

    Example: Creating a Configuration Hierarchy

    This example creates the following sample XML configuration hierarchy. The steps used to create the configuration hierarchy are outlined in Table 1.

    <configuration>
        <security>
            <policies>
                <policy>
                    <from-zone-name>trust</from-zone-name>
                    <to-zone-name>untrust</to-zone-name>
                    <policy>
                        <name>my-sec-policy</name>
                        <match>
                            <source-address>any</source-address>
                            <destination-address>any</destinationaddress>
                            <application>junos-ftp</application>
                            <application>junos-ntp</application>
                            <application>junos-ssh</application>
                        </match>
                        <then>
                            <permit>
                            </permit>
                        </then>
                    </policy>
                </policy>
            </policies>
        </security>
    </configuration>

    Table 1: Creating a Configuration Hierarchy with XMLBuilder and XML Objects

    Java Code

    Resulting Hierarchy

    // Create an XMLBuilder object and a 3-level hierarchy
     
    XMLBuilder builder = new XMLBuilder();
    XML policy = builder.createNewConfig("security","policies","policy");

    <configuration>
        <security>
            <policies>
                <policy>
                </policy>
            </policies>
        </security>
    </configuration>

    // Append nodes at the 'policy' level
     
    policy.append("from-zone-name","trust");
    policy.append("to-zone-name","untrust");

    <configuration>
        <security>
            <policies>
                <policy>
     
    <from-zone-name>trust</from-zone-name> <to-zone-name>untrust</to-zone-name>  
    </policy> </policies> </security> </configuration>

    // Create a new hierarchy level for the first policy
     
    XML policyOne = policy.append("policy");
    policyOne.append("name","my-sec-policy");

    <configuration>
        <security>
            <policies>
                <policy>
                    <from-zone-name>trust</from-zone-name>
                    <to-zone-name>untrust</to-zone-name>
     
    <policy> <name>my-sec-policy</name> </policy>  
    </policy> </policies> </security> </configuration>

    // Create the ’match’ hierarchy
     
    XML match = policyOne.append("match");
     
    // Create and append an applications array
    // to make three nodes with the same node name
     
    String[] applications =
           {"junos-ftp","junos-ntp","junos-ssh"};

    match.append("application", applications);

    <configuration>
        <security>
            <policies>
                <policy>
                    <from-zone-name>trust</from-zone-name>
                    <to-zone-name>untrust</to-zone-name>
                    <policy>
                        <name>my-sec-policy</name>
     
    <match> <application>junos-ftp</application> <application>junos-ntp</application> <application>junos-ssh</application> </match>  
    </policy> </policy> </policies> </security> </configuration>

    // Add elements under 'match'
     
    match.append("source-address","any");
    match.append("destination-address","any");

    <configuration>
        <security>
            <policies>
                <policy>
                    <from-zone-name>trust</from-zone-name>
                    <to-zone-name>untrust</to-zone-name>
                    <policy>
                        <name>my-sec-policy</name>
                        <match>
                            <application>junos-ftp</application>
                            <application>junos-ntp</application>
                            <application>junos-ssh</application>
     
    <source-address>any</source-address> <destination-address>any</destination-address>  
    </match> </policy> </policy> </policies> </security> </configuration>

    // Add the 'then' hierarchy with a child 'permit' element
     
    policyOne.append("then").append("permit");

    <configuration>
        <security>
            <policies>
                <policy>
                    <from-zone-name>trust</from-zone-name>
                    <to-zone-name>untrust</to-zone-name>
                <policy>
                    <name>my-sec-policy</name>
                    <match>
                        <application>junos-ftp</application>
                        <application>junos-ntp</application>
                        <application>junos-ssh</application>
                        <source-address>any</source-address>
                        <destination-address>any</destination-address>
                    </match>
     
    <then> <permit/> </then>  
    </policy> </policy> </policies> </security> </configuration>

    // Complete code and final configuration

    XMLBuilder builder = new XMLBuilder();
    XML policy = builder.createNewConfig("security","policies","policy");
    policy.append("from-zone-name","trust");
    policy.append("to-zone-name","untrust");
    XML policyOne = policy.append("policy");
    policyOne.append("name","my-sec-policy");
    XML match = policyOne.append("match");
    String[] applications = {"junos-ftp","junos-ntp","junos-ssh"};
    match.append("application", applications);
    match.append("source-address","any");
    match.append("destination-address","any");
    policyOne.append("then").append("permit");
    
    <configuration>
        <security>
            <policies>
                <policy>
                    <from-zone-name>trust</from-zone-name>
                    <to-zone-name>untrust</to-zone-name>
                    <policy>
                        <name>my-sec-policy</name>
                        <match>
                            <application>junos-ftp</application>
                            <application>junos-ntp</application>
                            <application>junos-ssh</application>
                            <source-address>any</source-address>
                            <destination-address>any</destination-address>
                        </match>
                        <then>
                            <permit/>
                        </then>
                    </policy>
                </policy>
            </policies>
        </security>
    </configuration>

    Published: 2013-07-26