From 386d9cfff3f9c51c141c2f73638d28f192ece728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 8 Apr 2014 13:11:38 +0200 Subject: [PATCH] Feature #2562: Java classes for zone pool --- .../src/org/opennebula/client/zone/Zone.java | 187 ++++++++++++++++++ .../org/opennebula/client/zone/ZonePool.java | 103 ++++++++++ 2 files changed, 290 insertions(+) create mode 100644 src/oca/java/src/org/opennebula/client/zone/Zone.java create mode 100644 src/oca/java/src/org/opennebula/client/zone/ZonePool.java diff --git a/src/oca/java/src/org/opennebula/client/zone/Zone.java b/src/oca/java/src/org/opennebula/client/zone/Zone.java new file mode 100644 index 0000000000..91a31a2b90 --- /dev/null +++ b/src/oca/java/src/org/opennebula/client/zone/Zone.java @@ -0,0 +1,187 @@ +/******************************************************************************* + * Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +package org.opennebula.client.zone; + + +import org.opennebula.client.Client; +import org.opennebula.client.OneResponse; +import org.opennebula.client.PoolElement; +import org.w3c.dom.Node; + +/** + * This class represents an OpenNebula zone. + * It also offers static XML-RPC call wrappers. + */ +public class Zone extends PoolElement{ + + private static final String METHOD_PREFIX = "zone."; + private static final String INFO = METHOD_PREFIX + "info"; + private static final String ALLOCATE = METHOD_PREFIX + "allocate"; + private static final String UPDATE = METHOD_PREFIX + "update"; + private static final String RENAME = METHOD_PREFIX + "rename"; + private static final String DELETE = METHOD_PREFIX + "delete"; + + /** + * Creates a new Zone representation. + * + * @param id The zone id. + * @param client XML-RPC Client. + */ + public Zone(int id, Client client) + { + super(id, client); + } + + /** + * @see PoolElement + */ + protected Zone(Node xmlElement, Client client) + { + super(xmlElement, client); + } + + + // ================================= + // Static XML-RPC methods + // ================================= + + /** + * Allocates a new Zone in OpenNebula. + * + * @param client XML-RPC Client. + * @param description A string containing the template of the zone. + * @return If successful the message contains the associated + * id generated for this Zone. + */ + public static OneResponse allocate(Client client, String description) + { + return client.call(ALLOCATE, description); + } + + /** + * Retrieves the information of the given zone. + * + * @param client XML-RPC Client. + * @param id The zone id. + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public static OneResponse info(Client client, int id) + { + return client.call(INFO, id); + } + + /** + * Deletes a zone from OpenNebula. + * + * @param client XML-RPC Client. + * @param id The zone id. + * @return A encapsulated response. + */ + public static OneResponse delete(Client client, int id) + { + return client.call(DELETE, id); + } + + /** + * Replaces the template contents. + * + * @param client XML-RPC Client. + * @param id The zone id of the target zone we want to modify. + * @param new_template New template contents + * @param append True to append new attributes instead of replace the whole template + * @return If successful the message contains the zone id. + */ + public static OneResponse update(Client client, int id, String new_template, + boolean append) + { + return client.call(UPDATE, id, new_template, append ? 1 : 0); + } + + /** + * Renames this Zone + * + * @param client XML-RPC Client. + * @param id The Zone id of the target Zone. + * @param name New name for the Zone. + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse rename(Client client, int id, String name) + { + return client.call(RENAME, id, name); + } + + // ================================= + // Instanced object XML-RPC methods + // ================================= + + /** + * Loads the xml representation of the zone. + * The info is also stored internally. + * + * @see Zone#info(Client, int) + */ + public OneResponse info() + { + OneResponse response = info(client, id); + super.processInfo(response); + return response; + } + + /** + * Deletes the zone from OpenNebula. + * + * @see Zone#delete(Client, int) + */ + public OneResponse delete() + { + return delete(client, id); + } + + /** + * Renames this Zone + * + * @param name New name for the Zone. + * @return If an error occurs the error message contains the reason. + */ + public OneResponse rename(String name) + { + return rename(client, id, name); + } + + /** + * Replaces the template contents. + * + * @param new_template New template contents + * @return If successful the message contains the zone id. + */ + public OneResponse update(String new_template) + { + return update(new_template, false); + } + + /** + * Replaces the template contents. + * + * @param new_template New template contents + * @param append True to append new attributes instead of replace the whole template + * @return If successful the message contains the zone id. + */ + public OneResponse update(String new_template, boolean append) + { + return update(client, id, new_template, append); + } +} diff --git a/src/oca/java/src/org/opennebula/client/zone/ZonePool.java b/src/oca/java/src/org/opennebula/client/zone/ZonePool.java new file mode 100644 index 0000000000..4f781c815b --- /dev/null +++ b/src/oca/java/src/org/opennebula/client/zone/ZonePool.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +package org.opennebula.client.zone; + +import java.util.AbstractList; +import java.util.Iterator; + + +import org.opennebula.client.Client; +import org.opennebula.client.OneResponse; +import org.opennebula.client.Pool; +import org.opennebula.client.PoolElement; +import org.w3c.dom.Node; + +/** + * This class represents an OpenNebula zone pool. + * It also offers static XML-RPC call wrappers. + */ +public class ZonePool extends Pool implements Iterable{ + + private static final String ELEMENT_NAME = "ZONE"; + private static final String INFO_METHOD = "zonepool.info"; + + /** + * Creates a new zone pool + * @param client XML-RPC Client. + */ + public ZonePool(Client client) + { + super(ELEMENT_NAME, client, INFO_METHOD); + } + + @Override + public PoolElement factory(Node node) + { + return new Zone(node, client); + } + + /** + * Retrieves all the zones in the pool. + * + * @param client XML-RPC Client. + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public static OneResponse info(Client client) + { + return Pool.info(client, INFO_METHOD); + } + + /** + * Loads the xml representation of the zone pool. + * + * @see ZonePool#info(Client) + */ + public OneResponse info() + { + return super.info(); + } + + public Iterator iterator() + { + AbstractList ab = new AbstractList() + { + public int size() + { + return getLength(); + } + + public Zone get(int index) + { + return (Zone) item(index); + } + }; + + return ab.iterator(); + } + + /** + * Returns the Zone with the given Id from the pool. If it is not found, + * then returns null. The method {@link #info()} must be called before. + * + * @param id of the Zone to retrieve + * @return The Image with the given Id, or null if it was not found. + */ + public Zone getById(int id) + { + return (Zone) super.getById(id); + } +}