/************************************************
* Columbia University CS Department
* Network Security Lab
*
* Open and Survivable Embedded Systems (OASES)
*
* Structured Policy Command Language (SPCL)
*
*
* This software is provided as is without any
* warranty or guarantee of merchantibility
* or fitness for a particular purpose. This
* software is released under the terms of the
* GNU General Public License (GPL).
*
* Last updated: May 8, 2003
*
***********************************************/

package spcl.data;

/**
 * This class is a place holder for testing.
 *  defaultrule.setAction( spcl.data.Rule.DENY, spcl.data.Rule.STAR );
 * @author locasto@cs.columbia.edu
 *
*/
public class Test1PolicyInstaller extends PolicyInstaller{

  public Test1PolicyInstaller(){}

  /**
   * a default test policy
   *
  */
  public void doInstall() throws PolicyException{

    if( null==policyLoader )
       throw new PolicyException( "No policy loader defined.", PolicyException.NULL_LOADER );

    /*ZONE declaration*/

    Zone zone = new Zone( "zone:blah" );

    /*POLICY declaration*/

    Policy policy = new Policy( "policy:blah.foo" );

    /*PRINCIPAL declaration*/

    Principal p1 = new Principal( "principal:blah.foo.Michael.Michael" );
    Principal p2 = new Principal( "principal:blah.foo.John.John" );

    /*RULE declaration*/

    final Rule rule1 = new Rule("rule:blah.foo.Michael.Michael.1");
    final Rule rule2 = new Rule("rule:blah.foo.John.John.1");
    final Rule rule3 = new Rule("rule:blah.foo.John.John.2");

    /*GROUP declaration*/

    Group defaultGroup = new Group( "group:blah.foo.default" );
    Group g1 = new Group( "group:blah.foo.parents" );

    /*OBJECT declaration*/

    final SystemObject so1 = new SystemObject( "object:blah.foo.webserver" );
	final Variable v1 = new Variable( "object:blah.foo.webserver.num_clients",
							   Variable.NUMBER, "4" );
	final Variable v2 = new Variable( "object:blah.foo.webserver.ip_addr",
							   Variable.STRING, "128.59.12.3" );


    Action action1 = new Action( "action:blah.foo.webserver.a" );
	action1.setActionPattern( "GET /index.* HTTP/1.[0|1]" );
	so1.addAction( action1 );
	so1.addVariable( v1 );


	final SystemObject so2 = new SystemObject( "object:blah.foo.socket" );
	so2.addVariable( v2 );
    Action action2 = new Action( "action:blah.foo.socket.open" );
    action2.setActionPattern( "open([1..9])" );
	action2.setObject( so2 );
	Action action3 = new Action( "action:blah.foo.socket.close" );
	action3.setActionPattern( "close[1..9]" );
	action3.setObject( so2 );

    /*RULE configuration*/

    rule1.setOwner( p1 );
    rule1.setObject( so1 );
    rule1.setActive( true );
    rule1.setAccess( spcl.data.Rule.ALLOW );
    rule1.setAction( action1 );

    rule2.setOwner( p2 );
	rule2.setObject( so2 );
    rule2.setAccess( spcl.data.Rule.DENY );
    rule2.setActive( false );
	rule2.setAction( action2 );

    rule3.setOwner( p2 );
	rule3.setObject( so2 );
	rule3.setAccess( spcl.data.Rule.ALLOW );
    rule3.setAction( action3 );
    rule3.setActive( true );

    rule1.addFiring(
       new Firing(){
         public void doFire(){
			if( policyLoader.resolveCondition( v2, "=", "128.59.16.2" ) ){
            	rule2.setActive( true );
			}
         }
       }
    );
    rule3.addFiring(
       new Firing(){
         public void doFire(){
		   System.out.println( ">>>Rule 3 fired." );
         }
       }
    );

    /*PRINCIPAL configuration*/

    p1.addRule( rule1 );
    p2.addRule( rule2 );
    p2.addRule( rule3 );

    /*GROUP configuration*/

    g1.addMember( p1 );
    g1.addMember( p2 );

    /*POLICY configuration*/

    policy.addGroup( g1 );
    policy.addGroup( defaultGroup );

    policy.addPrincipal( p1 );
    policy.addPrincipal( p2 );

    /*ZONE resolution and loading*/

    zone.attach( policy );
    policyLoader.load( zone );

    System.out.println( "<Test1PolicyInstaller install=\"yes\" />" );

    return;
  }


}
