Java Object Ltm Rule

Problem this snippet solves:

This class is part of the "Java iControl Objects" series of Articles on DevCentral. This class creates an iRule object and allows a developer to interact with the iControl LTM Rule API methods in the context of using an object.

Code :

package iControl.Objects.LTM;

public class iRule {
  private iControl.Interfaces _interfaces = null;
  private String _name = null;
  private String _definition = null;
  private Exception _lastException = null;
  
  //-----------------------------------------------------------------------
  // Member Accessors
  //-----------------------------------------------------------------------
  public iControl.Interfaces getInterfaces() { return _interfaces; }
  public void setInterfaces(iControl.Interfaces interfaces) { _interfaces = interfaces; }

  public String getName() { return _name;  }
  public void setName(String value) { _name = value; }
  
  public String getDefinition() { return _definition;  }
  public void setDefinition(String value) { _definition = value; }
  
  public Exception getLastException() { return _lastException; }
  
  //-----------------------------------------------------------------------
  // Constructors
  //-----------------------------------------------------------------------
  public iRule(iControl.Interfaces interfaces, String name, String definition)
  {
    _interfaces = interfaces; 
    _name = name;
    _definition = definition;
  }
  public iRule(iControl.Interfaces interfaces, iControl.LocalLBRuleRuleDefinition rule_def)
  {
    _interfaces = interfaces;
    _name = rule_def.getRule_name();
    _definition = rule_def.getRule_definition();
  }

  protected void validateMembers() throws Exception
  {
    if ( (null == _interfaces) || (null == _name) || (null == _definition) )
    {
      throw new Exception("Invalid Rule Parameters");
    }
  }
  
  protected void clearException()
  {
    _lastException = null;
  }

  //-----------------------------------------------------------------------
  // Attribute Accessors
  //-----------------------------------------------------------------------
  
  //-----------------------------------------------------------------------
  // Public Methods
  //-----------------------------------------------------------------------
  public boolean save() throws Exception
  {
    validateMembers();
    clearException();
    
    boolean bSaved = false;
    
    iControl.LocalLBRuleRuleDefinition ruleDef =
      new iControl.LocalLBRuleRuleDefinition();
    ruleDef.setRule_name(_name);
    ruleDef.setRule_definition(_definition);
    
    iControl.LocalLBRuleRuleDefinition [] ruleDefA = { ruleDef };

    // First attempt to create the iRule.
    try
    {
      _interfaces.getLocalLBRule().create(ruleDefA);
      bSaved = true;
    }
    catch(Exception ex)
    {
      if ( ex.getMessage().contains("already") )
      {
        // If create fails due to it already existing,
        // then try to modify the existing one.
        try
        {
          _interfaces.getLocalLBRule().modify_rule(ruleDefA);
          bSaved = true;
        }
        catch(Exception ex2)
        {
          _lastException = ex2;
        }
      }
      else
      {
        _lastException = ex;
      }
    }
    return bSaved;
  }
  
  public boolean load() throws Exception
  {
    boolean bLoaded = false;
    
    validateMembers();
    
    String [] rule_list = { _name };
    
    try
    {
      iControl.LocalLBRuleRuleDefinition []ruleDef = 
        _interfaces.getLocalLBRule().query_rule(rule_list);
      _definition = ruleDef[0].getRule_definition();
      
      bLoaded = true;
    }
    catch(Exception ex)
    {
      _lastException = ex;
    }
    
    return bLoaded;
  }
  
  public boolean checkSyntax() throws Exception
  {
    clearException();
    validateMembers();
    
    boolean bValid = false;
    
    String tmpName = "icoTempRuleForStatusCheck";
    
    iRule tmpiRule = new iRule(_interfaces, tmpName, _definition);
    try
    {
      if ( tmpiRule.save() )
      {
        tmpiRule.delete();
        bValid = true;
      }
    }
    catch(Exception ex)
    {
      _lastException = ex;
      bValid = false;
    }
    
    return bValid;
  }
  
  public void delete() throws Exception
  {
    validateMembers();
    
    String [] rule_list = { _name };
    _interfaces.getLocalLBRule().delete_rule(rule_list);
  }
  
  public iControl.LocalLBRuleRuleStatisticEntry getStatistics() throws Exception
  {
    validateMembers();
    
    String [] rule_list = { _name };
    iControl.LocalLBRuleRuleStatistics ruleStats =
      _interfaces.getLocalLBRule().get_statistics(rule_list);
    
    iControl.LocalLBRuleRuleStatisticEntry statsEntry =
      ruleStats.getStatistics()[0];
    
    return statsEntry;
  }
  
  public void resetStatistics() throws Exception
  {
    validateMembers();
    
    String [] rule_list = { _name };
    _interfaces.getLocalLBRule().reset_statistics(rule_list);
  }

  //-----------------------------------------------------------------------
  // Public Static Methods
  //-----------------------------------------------------------------------
  public static iRule [] getList(iControl.Interfaces interfaces) throws Exception
  {
    iRule [] irule_list = null;
    
    iControl.LocalLBRuleRuleDefinition [] rule_defs = 
      interfaces.getLocalLBRule().query_all_rules();
    irule_list = new iRule[rule_defs.length];
    for(int i=0; i
Published Mar 08, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment