PolicyAction and PolicyCondition - Example



Feature Overview

The word 'policy', in general, refers to a plan of action. Here, in Web NMS context, it refers to executing a task or set of tasks at a specified time based on a set of specified conditions. Policies are tasks that are executed by Web NMS at a system level, at a specified point of time. In Network Management, policies are used to control a variety of network activities like automated backups, routing & prioritization of network traffic, bandwidth allocation, cleaning up database tables, deleting failed nodes, etc.

This policy framework enables scalable administration of the Web NMS server and the network elements managed by it. The primary goal of the policy engine is to enable administration of complex functions easily. In Web NMS, policies can be used to customize the behaviour of the NMS and to provide a framework for adding policies for different network elements.

Web NMS policies can be broadly classified into two categories :

For more details, refer to the following section of our documentation : Developer Guide --> Policy Framework

Example Overview

Example name : PolicyAction and PolicyCondition
Related files : LogsArchivePolicy.java, LogsArchiveCondition.java, LogsArchiveAction.java, ExecuteLogsArchivePolicy.java
Location : <Web NMS Home>/examples/policy

This example policy relates to moving the Log files that are getting created in Web NMS to the Archives based on a condition.

Policy
A policy named LogsArchivePolicy has been written which is for moving the Log files into the archives when the number of Log files reach the maximum specified count.
Condition and Action

The above policy is required to be executed based on a condition. The condition in this case is that at the scheduled time of execution of this policy, a check has to be made on the number of Log files. If the file count had reached the maximum specified limit, the policy will be executed and the Log files will be moved to the archives as a zip file. If not, the policy will not be executed. Here, the check to ascertain whether the maximum file count is reached, is the condition. The moving of the Log files into the archives when the condition is satisfied, is the action.

In this example, the policy belongs to the periodic category, and it is scheduled to be executed every five hours. The maximum file count is specified as 50.

Code snippet

(i) Code snippet for the PolicyObject
 
public class LogsArchivePolicy extends PeriodicPolicyObject

{

    /**

This policy runs every 5 hours to move the older Log files to archive.

     **/

    public LogsArchivePolicy()

    {

super();

period = 1000*60*60*5;  // check whether to execute every 5 hours.

    }

    /**

     Sets the properties for this PolicyObject

     */

    public void setProperties(Properties p)

    {
        super.setBaseProperties(p);

    }

    /**

     Returns the properties of this PolicyObject

     */

    public Properties getProperties()

    {

Properties p = super.getBaseProperties();

return p;

    }

    /**

Returns the help url of this policy object.

     */

    public String getHelpURL()

    {

return "User_Guide/Java_UI/Policies/policyviewer.html";

    }

  /**

     Returns the properties of this PolicyObject

     */

    public String getPolicyObjectCustomizer() {

return policyObjectCustomizer;

    }

  /**

   Executes the action of this PolicyObject. In this example, this executeAction is just a dummy implementation because PolicyActionImpl is merged with this policy.

    */

    public  void executeAction(PolicyEvent policyEvt)

    {

    }
}

(ii) Code snippet for PolicyCondition
 
 
public class PolicyConditionImpl  implements PolicyCondition

{

/**

This method checks the total number of files under logs directory to ascertain whether the count has reached the maximum limit 50. This method is invoked every 5 hours.

*/

    public boolean isConditionSatisfied(PolicyEvent pe)

    {

        boolean isSatisfied=false;

        Vector logsDirVect=NmsLogMgr.getRelativelogDirectories();

        File[] logsDirs=new File[logsDirVect.size()];

        int count=0;

        for(Enumeration en=logsDirVect.elements();en.hasMoreElements();)

        {

            logsDirs[count++]=new File(PureUtils.rootDir+File.separator+(String)en.nextElement());

        }

        count=0;

        for(int i=0;i<logsDirs.length;i++)

        {

            count+=(logsDirs[i].list()).length;

        }

        if(count < 50)

        {

            isSatisfied=false;

        }

        else

        {

            isSatisfied=true;

        }

        return isSatisfied;

    }

/**

The condition and its corresponding 'action' share a common, unique key. Based on that key, the condition is correlated with its corresponding action. This method correlates the  PolicyActions with their Policyconditions. Returns the unique string key.

*/

    public String getKey()

    {

        return "MoveToArchives";

    }

}

(iii) Code snippet for PolicyAction
 
 
public class PolicyActionImpl  implements PolicyAction

{

public String getKey()

{

return "MoveToArchives";

}

public int getPriority()

{

return 5;

}

public void executeAction(PolicyEvent pe)

{
 

The required action (i.e. moving the Log files to the archives is performed here. The log files are moved as a zip file with the name as Logs_BackUp_month_day_year_hour_minute under LogsArchiveDir.

}
 

(iv) Code snippet for integrating PolicyObject, PolicyCondition and Action

You have written separate codes for PolicyObject, Condition and Action. They need to be integrated to execute the policy based on this condition. The following code snippet explains how this can be done.
 
NmsPolicyAPI api = (NmsPolicyAPI)java.rmi.Naming.lookup("//"+host+portOption+"/NmsPolicyAPI");

                    PeriodicPolicyObject archObj = new LogsArchivePolicy();

                    PolicyAction policyAct=new PolicyActionImpl();

                    PolicyCondition policyCond=new PolicyConditionImpl();

                    archObj.setName("ArchivePolicy");

                    archObj.setGroupName("TestArchiveGroup");

                    // for adding a policy

                    api.addPolicy(archObj);

                    Thread.sleep(2000);

                    api.addPolicyActionAndCondition("ArchivePolicy",policyAct,policyCond);

                    Thread.sleep(2000);

                    PolicyEvent pe = new PolicyEvent(this);

                    pe.addPolicyNamesToTrigger("ArchivePolicy");

                    Thread.sleep(2000);

Using the Example

The steps given below will help you in integrating this example with Web NMS. If any of the steps given below are not followed properly, then the example might not function in a desired manner.
Classpath Settings

Set the CLASSPATH as <WebNMS  Home>\classes; <Web NMS Home>\ classes\AdventNetFramework.jar;

Compiling and Bundling with Web NMS

Compile it over the destination directory using the following syntax in the command prompt.

javac -d <Web NMS Home>/classes LogsArchivePolicy.java.

javac -d <Web NMS Home>/classes LogsArchiveCondition.java.

javac -d <Web NMS Home>/classes LogsArchiveAction.java.

javac -d <Web NMS Home>/classes ExecuteLogsArchivePolicy.java.

Make an entry for this policy in relationalclasses.conffile as shown below.

test.LogsArchivePolicy  policydb RelationalPolicyObject PolicyObject


Invoking the Example

If Web NMS is running in other hosts execute the following command  :

java test.ExecuteLogsArchivePolicy -h <hostname> -p <RMI Port>

If Web NMS is running in localhost and the RMI registry is running in the default port 1099, no need for passing the arguments. The command for this case will be,

java test.ExecuteLogsArchivePolicy

Sample Output

On successful execution of this example, a new policy named 'ArchivePolicy' will get added (which can be seen in the Policy UI). Also, the added policy will be executed and the following line will be displayed on the console : "Policy executed successfully".


Copyright © 1996-2002, AdventNet Inc. All Rights Reserved.