This is the fifth article in the “Java iControl Objects” series in which I define a set of objects that implement the iControl methods in various iControl interfaces.  The previous articles covered the LocalLB Rule, Pool and PoolMember as well as the System Service interfaces.

For this article, I’m going to move to a different interface and look at Networking SelfIPs.  Self IP addresses are a way to assign additional local addresses to the LTM.  These can be used for various purposes such as providing alternate management interfaces for configuration and reporting purposes.

Prerequisites

The code in this article, relies on the iControl library for Java that can be downloaded in it’s DevCentral labs project.  If you haven’t watched it already, check out my previous article on Configuring Eclipse for iControl development with Java.

The Class

The Self IP is defined by it’s network address, netmask, floating state, unit id, and VLAN.  I’ve included these attributes in the class definition along with accessor methods to get and set them.  Once a Self IP is created, you cannot modify the address so that attribute does not have a set method associated with it.

   1: package iControl.Objects.Networking;
   2:  
   3: import iControl.Objects.System.Service;
   4:  
   5: public class SelfIP {
   6:   private iControl.Interfaces _interfaces = null;
   7:   private String _address = null;
   8:   private String _netmask = null;
   9:   private iControl.CommonEnabledState _floating_state = null;
  10:   private long _unit_id = -1;
  11:   private String _vlan = null;
  12:  
  13:   //-----------------------------------------------------------------------
  14:   // Member Accessors
  15:   //-----------------------------------------------------------------------
  16:   public iControl.Interfaces getInterfaces() { return _interfaces; }
  17:   public void setInterfaces(iControl.Interfaces interfaces) { _interfaces = interfaces; }
  18:  
  19:   public String getAddress() { return _address; }
  20:   public void setAddress(String address) { _address = address; }
  21:   
  22:   //-----------------------------------------------------------------------
  23:   // Constructors
  24:   //-----------------------------------------------------------------------
  25:   public SelfIP(iControl.Interfaces interfaces, String address)
  26:   {
  27:     _interfaces = interfaces; 
  28:     _address = address;
  29:   }

Member Accessors

In this class, I’ve added local caching of the attribute accessor methods.  This way, once you query the attribute, it will only have to poll the BIG-IP once.  An example of this is with the following netmask accessor.  The internal _netmask holds the value and is updated only if the value has not been set yet.

   1: public String getNetmask() throws Exception
   2: {
   3:   if ( null == _netmask )
   4:   {
   5:     validateMembers();
   6:     String [] self_ips = { _address };
   7:     String [] netmasks = _interfaces.getNetworkingSelfIP().get_netmask(self_ips);
   8:     _netmask = netmasks[0];
   9:   }
  10:   return _netmask;
  11: }
  12: public void setNetmask(String netmask) throws Exception
  13: {
  14:   validateMembers();
  15:   String [] self_ips = { _address };
  16:   String [] netmasks = { netmask};
  17:   _interfaces.getNetworkingSelfIP().set_netmask(self_ips, netmasks);
  18:   _netmask = netmask;
  19: }

Public Methods

I’ve included 3 public methods in this class. 

  • create – this method will create the Self IP if it doesn’t already exist.
  • remove – This method will remove the Self IP from the configuration on the BIG-IP.
  • syncProperties – This method queries all the object attributes and loads them into the local cache.
   1: public void create(
   2:   String vlan,
   3:   String netmask,
   4:   long unit_id,
   5:   iControl.CommonEnabledState state
   6: ) throws Exception
   7: {
   8:   validateMembers();
   9:   
  10:   String [] self_ips = { _address };
  11:   String [] vlan_names = { vlan };
  12:   String [] netmasks = { netmask };
  13:   long [] unit_ids = { unit_id };
  14:   iControl.CommonEnabledState [] states = { state };
  15:   
  16:   _interfaces.getNetworkingSelfIP().create(self_ips, vlan_names, netmasks, unit_ids, states);
  17:   
  18:   _unit_id = unit_id;
  19:   _netmask = netmask;
  20:   _floating_state = state;
  21:   _vlan = vlan;
  22: }
  23:  
  24: public void remove() throws Exception
  25: {
  26:   validateMembers();
  27:   String [] self_ips = { _address };
  28:   _interfaces.getNetworkingSelfIP().delete_self_ip(self_ips);
  29: }
  30:  
  31: public void syncProperties() throws Exception
  32: {
  33:   validateMembers();
  34:   getFloatingState();
  35:   getNetmask();
  36:   getUnitId();
  37:   getVLAN();
  38: }

Static Methods

As with the classes in my previous articles, I’ve included the getList() static method.  In here, I’ve added ones for create() and remove_all() as well.  The getList() method will return an array of SelfIP objects loaded with all their attributes.  The create() method does just what it’s name implies.  The remove_all() method should be used with care as it removes ALL Self IP objects on the BIG-IP.  There’s no undo, so use at your own risk.

   1: public static SelfIP [] getList(iControl.Interfaces interfaces) throws Exception
   2: {
   3:   SelfIP [] selfips = null;
   4:   if ( null != interfaces )
   5:   {
   6:     String [] selfip_list = interfaces.getNetworkingSelfIP().get_list();
   7:  
   8:     selfips = new SelfIP[selfip_list.length];
   9:     for(int i=0; i<selfip_list.length; i++)
  10:     {
  11:       selfips[i] = new SelfIP(interfaces, selfip_list[i]);
  12:     }
  13:   }
  14:   return selfips;
  15: }
  16:  
  17: public static SelfIP create
  18: (
  19:   iControl.Interfaces interfaces, 
  20:   String address, 
  21:   String vlan, 
  22:   String netmask, 
  23:   long unit_id, 
  24:   iControl.CommonEnabledState state
  25: ) throws Exception
  26: {
  27:   SelfIP selfIP = null;
  28:   if ( (null != interfaces) )
  29:   {
  30:     selfIP = new SelfIP(interfaces, address);
  31:     selfIP.create(vlan, netmask, unit_id, state);
  32:   }
  33:   return selfIP;
  34: }
  35:  
  36: public static void remove_all(iControl.Interfaces interfaces) throws Exception
  37: {
  38:   if ( (null != interfaces) )
  39:   {
  40:     interfaces.getNetworkingSelfIP().delete_all_self_ips();
  41:   }
  42: }

Example Code

The following example function will first query the list of Self IPs and print to the console their settings.  It will then create a new Self IP Address.  It then queries the list again to show that it was created.  After that, the SelfIP object is deleted and finally the list is queried again to show that the task completed successfully.

   1: public void testSelfIp(String[] args)
   2: {
   3:   try
   4:   {
   5:     iControl.Interfaces interfaces = new iControl.Interfaces();
   6:     interfaces.initialize(args[0], args[1], args[2]);
   7:     
   8:     // Get list of existing SelfIPs
   9:     iControl.Objects.Networking.SelfIP [] selfips =
  10:       iControl.Objects.Networking.SelfIP.getList(interfaces);
  11:     System.out.println("Existing SelfIP List");
  12:     System.out.println("--------------------");
  13:     for(int i=0; i<selfips.length; i++)
  14:     {
  15:       selfips[i].syncProperties();
  16:       
  17:       System.out.println("[" + selfips[i].getUnitId() + "] " +
  18:         selfips[i].getAddress() + "/" + selfips[i].getNetmask() + 
  19:         ", VLAN='" + selfips[i].getVLAN() +
  20:         "', FSTATE='" + selfips[i].getFloatingState() + "'"  );
  21:     }
  22:     
  23:     // Create SelfIP
  24:     System.out.println("Creating SelfIP 20.20.20.1...");
  25:     iControl.Objects.Networking.SelfIP.create(interfaces, "20.20.20.1", 
  26:       "external1", "255.255.255.0", 0, iControl.CommonEnabledState.STATE_DISABLED);
  27:     System.out.println("SelfIP 20.20.20.1 successfully created...");
  28:     
  29:     iControl.Objects.Networking.SelfIP selfip2 =
  30:       new iControl.Objects.Networking.SelfIP(interfaces, "20.20.20.1");
  31:     
  32:     selfip2.syncProperties();
  33:     System.out.println("[" + selfip2.getUnitId() + "] " +
  34:         selfip2.getAddress() + "/" + selfip2.getNetmask() + 
  35:         ", VLAN='" + selfip2.getVLAN() +
  36:         "', FSTATE='" + selfip2.getFloatingState() + "'"  );
  37:  
  38:     // Get list of existing SelfIPs
  39:     selfips = iControl.Objects.Networking.SelfIP.getList(interfaces);
  40:     System.out.println("Existing SelfIP List");
  41:     System.out.println("--------------------");
  42:     for(int i=0; i<selfips.length; i++)
  43:     {
  44:       selfips[i].syncProperties();
  45:       
  46:       System.out.println("[" + selfips[i].getUnitId() + "] " +
  47:         selfips[i].getAddress() + "/" + selfips[i].getNetmask() + 
  48:         ", VLAN='" + selfips[i].getVLAN() +
  49:         "', FSTATE='" + selfips[i].getFloatingState() + "'"  );
  50:     }
  51:     
  52:     // Remove SelfIP
  53:     System.out.println("Removing SelfIP 20.20.20.1...");
  54:     selfip2.remove();
  55:     System.out.println("SelfIP 20.20.20.1 successfully removed...");
  56:  
  57:     // Get list of existing SelfIPs
  58:     selfips = iControl.Objects.Networking.SelfIP.getList(interfaces);
  59:     System.out.println("Existing SelfIP List");
  60:     System.out.println("--------------------");
  61:     for(int i=0; i<selfips.length; i++)
  62:     {
  63:       selfips[i].syncProperties();
  64:       
  65:       System.out.println("[" + selfips[i].getUnitId() + "] " +
  66:         selfips[i].getAddress() + "/" + selfips[i].getNetmask() + 
  67:         ", VLAN='" + selfips[i].getVLAN() +
  68:         "', FSTATE='" + selfips[i].getFloatingState() + "'"  );
  69:     }
  70:     
  71:   }
  72:   catch(Exception ex)
  73:   {
  74:     ex.printStackTrace(System.out);
  75:   }
  76: }

View The Source

The source code can be found in the iControl CodeShare under JavaObjectNetworkingSelfip.

Related Articles on DevCentral

 
Technorati Tags: selfip, networking, iControl, Joe Pruitt