<?xml version="1.0" encoding="UTF-8"?>

<!-- Specify the namespace prefix used for dpbuddy tasks here -->
<project name="dpbuddy.example" basedir="." xmlns:dp="antlib:com.myarch.dpbuddy" default="run.all">

    <description>
        Ant file demonstrating the usage of dpbuddy tasks for DataPower management
    </description>
    

    <!-- Load dpbuddy task definitions. You only need to do it if you don't want to put
    dpbuddy-single.jar into $ANT_HOME/lib -->

    <!-- Specify where dpbuddy is installed, update this value according to your environment -->
    <property name="dpbuddy.home" location="../target/dpbuddy-2.3-dist/dpbuddy-2.3" />
    <!-- uri has to match the uri of the dp namespace prefix -->
    <taskdef uri="antlib:com.myarch.dpbuddy"
            resource="com/myarch/dpbuddy/antlib.xml">
        <classpath>
            <!-- just put all jars from the lib directory on the classpath -->
            <fileset dir="${dpbuddy.home}/lib" includes="*.jar"/>
        </classpath>
    </taskdef>

    <!-- Environment prefix refers to a group of DataPower properties defined with this prefix
    See dp.properties for an example. This value will be used by all tasks in this build file unless it's 
    overridden at a task level using envPrefix attribute -->
    <property name="dp.env.prefix" value="dev" />

    <!-- File with DataPower connection properties, such as URL, username/password -->
    <loadProperties srcFile="dp.properties" />
    
    <!-- Each target demonstrates usage of a dpbuddy task.
    Each dpbuddy task utilizes one or more XML management commands (SOMA).
    You can find out about SOMA at http://www.redbooks.ibm.com/abstracts/redp4446.html -->

    <!-- Targets below export a configuration and then import it -->    
    
    <!-- Export DataPower configuration. We have a simple WS proxy (testServiceProxy) defined in our domain -->
    <property name="wsproxy.name" value="testServiceProxy" />
    <!-- Where to save the exported file -->
    <property name="wsproxy.file" value="dpconfigs/WSProxy.zip" />
    <target name="export" description="Export configuration">
        <!-- Use exportObject nested element to define what to export.
        Both class and name values are regexps
         -->
        <!-- Export all WS proxies (to figure out the name of the class, first export manually and then look at element names in export.xml)-->
        <dp:export file="${wsproxy.file}"  >
            <exportObject class="WSGateway" />
        </dp:export>
    
        <!-- Export all objects with names that begin with testService -->
        <dp:export file="${wsproxy.file}" allFiles="false" >
            <exportObject name="testService.*" />
        </dp:export>

        <!-- Export all WS proxies with name beginning with testService.
        Additional attributes, such as includeDebug, will apply to all matching objects.
        See DPbuddy User Guide for the list of supported attributes -->
        <dp:export file="${wsproxy.file}" allFiles="false" >
            <exportObject class="WSGateway" name="testService.*" includeDebug="true"/>
        </dp:export>

    </target>
    
    <!-- Before we import, we want to create a checkpoint we can revert to-->
    <target name="checkpoint" description="Create a checkpoint">
        <!-- note that if the checkpoint with the same name already exists, it will be deleted -->
        <dp:checkpoint name="beforeTestServiceImport" />
    </target>

        
    <!-- Import our test proxy configuration -->
    <target name="import" 
            description="Import configuration and apply deployment policy to it">
        <!-- We can use Ant variables in the deployment policy; our policy contains ${ws.port} in the "value" element for the port -->
        <property name="ws.port" value="8094" />
    	<dp:import envPrefix="dev" file="${wsproxy.file}"  overwriteFiles="true" overwriteObjects="true" 
	       	deploymentPolicyFile="dpconfigs/testServiceDeplPolicy.xml"/>                                

    </target>
    
    <!-- Save domain configuration. Alternatively, you can set dp.auto.save to true, in which case 
    changes will be saved automatically by import and setConfig tasks -->
    <target name="save" description="Save running domain configuration">
        <dp:save/>
    </target>

    <!-- Upload local files to the device outside of import command - this is convenient for development -->
    <target name="copy" description="Upload files to the device">
        <!-- root directory for out wsdl files on the device -->
        <property name="dp.wsdl.dir" value="apps/services" />
        <!-- cleanDirectories="true" will automatically delete all files from the directory
        on the device where we're uploading the files to -->
        <dp:copy cleanDirectories="false"  >
            <!-- 
                dpfileset has the same attributes and nested elements as Ant fileset. 
                "prefix" defines the root directory on the device.

                The task automatically replicates entire local directory tree (including
                all subdirectories that have matching files) on the device.
                'local:' filesystem is the default.
            -->
<!--        <dpFileset prefix="${dp.wsdl.dir}"            -->
<!--            dir="wsdl" includes="**/*.wsdl **/*.xsd"/>-->
            <dpFileset dir="wsdl" includes="**/*.wsdl **/*.xsd"/>

            <!-- 
                We can have multiple dpfilesets. We can also explicitly provide DataPower filesystem as part of the prefix
            -->
<!--        <dpFileset prefix="local://service-wsdl" dir="wsdl" includes="**/*"/>-->

            <!-- if the prefix is not provided, 'local://wsdl' directory will be used as the root on the device -->
<!--        <dpFileset dir="wsdl" includes="**/*"/>-->

        </dp:copy>

        <!-- We can also 'flatten' the directory structure. No directories under wsdl-flat will be created -->
        <dp:copy flatten="true" >
            <dpFileset prefix="wsdl-flat" dir="wsdl" includes="**/*.wsdl **/*.xsd"/>
        </dp:copy>

    </target>
    
    <!-- SetConfig provides an alternative to import. 
    You can only use xml files (no zip). To create the initial file, use export. 
    Instead of deployment policies we can use xpath expressions to override values in config files 
    See http://myarch.com/datapower-deployment-policies-and-xpath for more details.
    setConfig enforces schema validation. Unfortunately, currently the DP schema misses some attributes 
    and the validation fails.
    You need to comment out the following elements in the file created from export.xml: Memoization DebugMode, DebuggerType.
    If dpbuddy's schema validation is disabled and the file doesn't validate, the request will fail in the device and 
    you'll get 'Internal error'
    -->
    <target name="config" >
        <property name="endpoint.port" value="8880" />

        <dp:setConfig>
            <configFile file="dpconfigs/WSproxy.xml">
                <!-- override this property in the config file -->
                <override xpath="//RemoteEndpointPort" value="${endpoint.port}" />
            </configFile>
        </dp:setConfig>

        <!-- You can combine multiple config files using fileset, however, in this case,
        you can't use 'override' -->
        <dp:setConfig>
            <fileset dir="dpconfigs" includes="WSProxy.xml" />
        </dp:setConfig>

        <!-- modifyConfig is similar to setConfig, but it does not create new objects if they don't exist.-->
        <dp:modifyConfig file="dpconfigs/WSproxy.xml"/>

    </target>
            

    <!-- Backup a domain or multiple domains 
     All objects and files are backed up, but not users and certs -->
    <target name="backup" description="Backup one or multiple domains">
        <!-- Backup domains that match 'domainPatterns'. To backup all domains, simply use '.*' 
        Here we're backing up all domains that end with 'a' -->
        <!-- if appendTimestamp is set to true, the timestamp will be automatically appended to the file name-->
        <dp:backup file="backups/backup.zip" domainPatterns=".*a" appendTimestamp="true" />
    </target>


    <!-- Download an arbitrary file from the device.
    This is useful when you need to copy files from one device to another.
    You can download to a folder or you can specify the file name.
    Note that you cannot download files from cert:// -->
    <target name="download" description="Download a file from the device">
        <dp:download file="pubcert://American-Express-Global-CA.pem" to="dpconfigs" />
    </target>

    <!-- Working with directories -->
    <target name="mkdir" description="Create a directory on the device">
        <dp:mkdir dir="/dir1/dir2/dir3" />
    </target>

    <target name="rmdir" description="Delete a remote directory">
        <!-- Set failOnError to false if you don't want to fail if the directory doesn't exist-->
        <dp:rmdir dir="/dir1/dir2" failOnError="true" />
    </target>
    

    <!-- DPBuddy provides extensive support for various SOMA 'actions', such as flushing caches and resetting domains. 
    Full list of SOMA actions can be found in xml-mgmt.xsd file, under the type "AnyActionElement". -->

    <!-- 'ping' action -->
    <property name="ping.host" value="localhost" />
    <target name="ping" description="Ping action">
        <dp:action>
            <!-- Simply specify action's XML within the task. 
            Note that you don't need to worry about SOAP envelope, it will be added automatically -->
            <Ping>
                <!-- you can use Ant properties anywhere in the request -->
                <RemoteHost>${ping.host}</RemoteHost>
            </Ping>
        </dp:action>
    </target>

    <!-- Action to reset the domain.
    Note that reset does not affect files, only the domain's configuration. -->
    <target name="reset" description="Reset the DataPower domain">
        <dp:action name="ResetThisDomain"/>
    </target>

    <!-- An action (or any other SOMA command) can also be stored in an external XML file
    You can specify Ant variables anywhere in the request's XML file -->	
    <target name="exec.soma.file" description="Run a SOMA command defined in an external XML file">
        <dp:fileRequest file="soma/ping-remote-host.xml" />
    </target>

    <target name="migrate" depends="export, backup, checkpoint, import, save" 
        description="Example of using DataPower Buddy tasks to copy configuration between domains"  />


    <!-- Tailing DataPower logs -->	
    <target name="tail" description="Tail device logs">
        <!-- If domainPatterns matches multiple domains, tailLog will consolidate logs from these domains -->
        <dp:tailLog domainPatterns=".*a, dev.*"  lines="100" />

        <!-- You can also change the log format. The following template will add transaction ID to the log. See User Guide for more details  -->
        <!-- You can specify any logTarget configured on the device -->
        <dp:tailLog logTarget="default-log" format="{1,date,yyyy-MM-dd HH:mm:ss} |{2}| {4,number,#} | {0}{3}"/>
    
        <!-- You can also filter out all log entries that do not match the class and object names
        specified in the 'where' elements.
        Both class and object are regexps -->
        <dp:tailLog failOnError="false" >
            <where class="wsgw" object="testService.*" />
            <where class="xmlfire.*" />
        </dp:tailLog>

    </target>

    <!-- You can continuously tail logs using 'follow=true'. 
    If any of the failPatterns matches, tailLog will fail the build. -->
    <target name="tail.follow" description="Continuously monitor logs">
        <!-- If follow is set to true, tailLog will be querying the device every 3 seconds 
        (can be changed using autoInterval attribute). This is similar to tail -f Unix command -->
        <dp:tailLog  lines="48" failPatterns="\|[E|W]\|" follow="true"/>
    </target>

    <!-- Status task gets the status from the device (get-status SOMA command) -->
    <target name="status" description="Get device status" >
        <!-- 'class' is any valid value from StatusEnum defined in xml-mgmt.xsd -->
        <dp:status class="ObjectStatus" />
        <dp:status class="MemoryStatus" />
        <dp:status class="FilesystemStatus" />
    </target>


    <target name="run.all" depends="migrate, config, copy, download, mkdir, rmdir, tail, status, ping, exec.soma.file" 
        description="Run all DataPower Buddy tasks"/>

</project>

