1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

Feature #474: Add/remove leases feature for Java OCA

This commit is contained in:
Carlos Martín 2011-02-02 16:46:17 +01:00
parent 6850348eba
commit 2d246ee02b
3 changed files with 146 additions and 0 deletions

View File

@ -118,6 +118,7 @@ do_tests()
do_clean()
{
echo "Cleaning .class files..."
rm -rf $BIN_DIR > /dev/null 2>&1
mkdir -p $BIN_DIR

View File

@ -32,6 +32,8 @@ public class VirtualNetwork extends PoolElement{
private static final String INFO = METHOD_PREFIX + "info";
private static final String DELETE = METHOD_PREFIX + "delete";
private static final String PUBLISH = METHOD_PREFIX + "publish";
private static final String ADDLEASES = METHOD_PREFIX + "addleases";
private static final String RMLEASES = METHOD_PREFIX + "rmleases";
/**
@ -110,6 +112,46 @@ public class VirtualNetwork extends PoolElement{
return client.call(PUBLISH, id, publish);
}
/**
* Adds a lease to the VirtualNetwork
*
* @param client XML-RPC Client.
* @param id The virtual network id (nid) of the target network.
* @param ip IP to add, e.g. "192.168.0.5"
* @param mac MAC address associated to the IP. Can be null, in which case
* OpenNebula will generate it using the following rule:
* MAC = MAC_PREFFIX:IP
* @return A encapsulated response.
*/
public static OneResponse addLeases(Client client, int id, String ip,
String mac)
{
String lease_template = "LEASES = [ IP = " + ip;
if( mac != null )
{
lease_template += ", MAC = " + mac;
}
lease_template += " ]";
return client.call(ADDLEASES, id, lease_template);
}
/**
* Removes a lease from the VirtualNetwork
*
* @param client XML-RPC Client.
* @param id The virtual network id (nid) of the target network.
* @param ip IP to remove, e.g. "192.168.0.5"
* @return A encapsulated response.
*/
public static OneResponse rmLeases(Client client, int id, String ip)
{
String lease_template = "LEASES = [ IP = " + ip + " ]";
return client.call(RMLEASES, id, lease_template);
}
// =================================
// Instanced object XML-RPC methods
// =================================
@ -168,6 +210,42 @@ public class VirtualNetwork extends PoolElement{
return publish(false);
}
/**
* Adds a lease to the VirtualNetwork
*
* @param ip IP to add, e.g. "192.168.0.5"
* @return A encapsulated response.
*/
public OneResponse addLeases(String ip)
{
return addLeases(ip, null);
}
/**
* Adds a lease to the VirtualNetwork
*
* @param ip IP to add, e.g. "192.168.0.5"
* @param mac MAC address associated to the IP. Can be null, in which case
* OpenNebula will generate it using the following rule:
* MAC = MAC_PREFFIX:IP
* @return A encapsulated response.
*/
public OneResponse addLeases(String ip, String mac)
{
return addLeases(client, id, ip, mac);
}
/**
* Removes a lease from the VirtualNetwork
*
* @param ip IP to remove, e.g. "192.168.0.5"
* @return A encapsulated response.
*/
public OneResponse rmLeases(String ip)
{
return rmLeases(client, id, ip);
}
// =================================
// Helpers
// =================================

View File

@ -45,6 +45,12 @@ public class VirtualNetworkTest
"NETWORK_SIZE = C\n" +
"NETWORK_ADDRESS = 192.168.0.0\n";
private static String fixed_template =
"NAME = \"Net number one\"\n" +
"TYPE = FIXED\n" +
"BRIDGE = br1\n" +
"LEASES = [IP=130.10.0.1]";
/**
* @throws java.lang.Exception
*/
@ -143,4 +149,65 @@ public class VirtualNetworkTest
res = vnet.info();
assertTrue( res.isError() );
}
@Test
public void publish()
{
res = vnet.info();
assertTrue( !res.isError() );
assertTrue( !vnet.isPublic() );
// Publish it
res = vnet.publish();
assertTrue( !res.isError() );
res = vnet.info();
assertTrue( vnet.isPublic() );
// Unpublish it
res = vnet.unpublish();
assertTrue( !res.isError() );
res = vnet.info();
assertTrue( !vnet.isPublic() );
}
@Test
public void addLeases()
{
res = VirtualNetwork.allocate(client, fixed_template);
assertTrue( !res.isError() );
VirtualNetwork fixed_vnet =
new VirtualNetwork(Integer.parseInt(res.getMessage()), client);
res = fixed_vnet.addLeases("130.10.0.5");
assertTrue( !res.isError() );
res = fixed_vnet.addLeases("130.10.0.6", "50:20:20:20:20:20");
assertTrue( !res.isError() );
res = fixed_vnet.addLeases("130.10.0.6");
assertTrue( res.isError() );
fixed_vnet.delete();
}
@Test
public void rmLeases()
{
res = VirtualNetwork.allocate(client, fixed_template);
assertTrue( !res.isError() );
VirtualNetwork fixed_vnet =
new VirtualNetwork(Integer.parseInt(res.getMessage()), client);
res = fixed_vnet.rmLeases("130.10.0.1");
assertTrue( !res.isError() );
res = fixed_vnet.rmLeases("130.10.0.5");
assertTrue( res.isError() );
fixed_vnet.delete();
}
}