/*
 * This event script activates/deactivates the qualified next-hop of the default
 * route based on the success or failure of a RPM test. When the test is successful 
 * the route will be activated.  When the test fails the route will be deactivated.
 *
 * The qualified next-hop must be passed as the next-hop argument.
 *
 */

version 1.0;

ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";

import "../import/junos.xsl";

param $next-hop = "10.0.0.1";

match / {
    <event-script-results> {    
        /* Learn the event type, either a PING_TEST_FAILED or PING_TEST_COMPLETED */
        var $event-type = event-script-input/trigger-event/id;

        /* Retrieve the current configuration for the static route */
        var $configuration-rpc = {
            <get-configuration database="committed"> {
                <configuration> {
                    <routing-options>;
                }
            }
        }
        var $current = jcs:invoke( $configuration-rpc );
        
        /* Grab the routing-options static node to make further location paths shorter */
        var $static = $current/routing-options/static;

        /* Is the route currently inactive? */
        var $inactive = $static/route[name == "0.0.0.0/0"]/qualified-next-hop[name == $next-hop]/@inactive;
        
        /* 
         * Compare the event type vs the current value of $inactive.  If they
         * do not match then a configuration change must be performed.
         */
         
        /* RPM test failed but the route is currently active */
        if( $event-type == "PING_TEST_FAILED" && jcs:empty( $inactive ) ) {
            
            /* Needed configuration change */
            var $configuration = {
                <configuration> {
                    <routing-options> {
                        <static> {
                            <route> {
                                <name> "0.0.0.0/0";
                                <qualified-next-hop inactive="inactive"> {
                                    <name> $next-hop;
                                }
                            }
                        }
                    }
                }
            }
            
            /* Open connection, load and commit the change, and close connection  */
            var $connection = jcs:open();
            var $results := { 
                call jcs:load-configuration( $connection, $configuration );
                copy-of jcs:close( $connection );
            }
            
            /* If any errors occurred during the commit process then report them to the syslog */
            if( $results//xnm:error ) {
                for-each( $results//xnm:error ) {
                    expr jcs:syslog( "external.error", "Error deactivating ", $next-hop, " next-hop: ", message );
                }   
            }
            /* Otherwise, report success */
            else {
                expr jcs:syslog( "external.notice", "0/0 next-hop ", $next-hop, " disabled." );
            }
        }
        /* RPM test succeeded but the route is currently inactive */
        else if( $event-type == "PING_TEST_COMPLETED" && $inactive ) {
            
            /* Needed configuration change */
            var $configuration = {
                <configuration> {
                    <routing-options> {
                        <static> {
                            <route> {
                                <name> "0.0.0.0/0";
                                <qualified-next-hop active="active"> {
                                    <name> $next-hop;
                                }
                            }
                        }
                    }
                }
            }
            
            /* Open connection, load and commit the change, and close connection  */
            var $connection = jcs:open();
            var $results := { 
                call jcs:load-configuration( $connection, $configuration );
                copy-of jcs:close( $connection );
            }
            
            /* If any errors occurred during the commit process then report them to the syslog */
            if( $results//xnm:error ) {
                for-each( $results//xnm:error ) {
                    expr jcs:syslog( "external.error", "Error activating ", $next-hop, " next-hop: ", message );
                }   
            }
            /* Otherwise, report success */
            else {
                expr jcs:syslog( "external.notice", "0/0 next-hop ", $next-hop, " activated." );
            }
        }
    }
}


