From 2d246ee02b095e9879542a01e04b71266a4d28a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 2 Feb 2011 16:46:17 +0100 Subject: [PATCH] Feature #474: Add/remove leases feature for Java OCA --- src/oca/java/build.sh | 1 + .../client/vnet/VirtualNetwork.java | 78 +++++++++++++++++++ src/oca/java/test/VirtualNetworkTest.java | 67 ++++++++++++++++ 3 files changed, 146 insertions(+) diff --git a/src/oca/java/build.sh b/src/oca/java/build.sh index f9a15d9a1f..c643abbf80 100755 --- a/src/oca/java/build.sh +++ b/src/oca/java/build.sh @@ -118,6 +118,7 @@ do_tests() do_clean() { + echo "Cleaning .class files..." rm -rf $BIN_DIR > /dev/null 2>&1 mkdir -p $BIN_DIR diff --git a/src/oca/java/src/org/opennebula/client/vnet/VirtualNetwork.java b/src/oca/java/src/org/opennebula/client/vnet/VirtualNetwork.java index b01c781015..dfdad55ed6 100644 --- a/src/oca/java/src/org/opennebula/client/vnet/VirtualNetwork.java +++ b/src/oca/java/src/org/opennebula/client/vnet/VirtualNetwork.java @@ -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 // ================================= diff --git a/src/oca/java/test/VirtualNetworkTest.java b/src/oca/java/test/VirtualNetworkTest.java index cbc232b2eb..6f32d2a697 100644 --- a/src/oca/java/test/VirtualNetworkTest.java +++ b/src/oca/java/test/VirtualNetworkTest.java @@ -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(); + } }