diff --git a/src/oca/java/src/org/opennebula/client/Pool.java b/src/oca/java/src/org/opennebula/client/Pool.java index 69d694bcfd..6afc200198 100644 --- a/src/oca/java/src/org/opennebula/client/Pool.java +++ b/src/oca/java/src/org/opennebula/client/Pool.java @@ -108,7 +108,7 @@ public abstract class Pool{ return xmlrpcInfo(client, infoMethod, MINE_GROUP, -1, -1); } - private static OneResponse xmlrpcInfo(Client client, String infoMethod, Object...args) + protected static OneResponse xmlrpcInfo(Client client, String infoMethod, Object...args) { return client.call(infoMethod, args); } @@ -153,7 +153,7 @@ public abstract class Pool{ * representation of the pool. * @param info The XML-RPC *pool.info response */ - public void processInfo(OneResponse info) + protected void processInfo(OneResponse info) { if (info.isError()) { diff --git a/src/oca/java/src/org/opennebula/client/document/Document.java b/src/oca/java/src/org/opennebula/client/document/Document.java new file mode 100644 index 0000000000..0b92c1a051 --- /dev/null +++ b/src/oca/java/src/org/opennebula/client/document/Document.java @@ -0,0 +1,270 @@ +/******************************************************************************* + * Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) + * + * 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.document; + +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 document. + * Documents are generic objects. You can dynamically create new Pools in + * OpenNebula, creating subclasses with different TYPE values. + *
+ * TYPE must be the same for the corresponding pool, see {@link DocumentPool} + *
+ * For example: + *
+ * 
+ * public class GenericObjA extends Document
+ * {
+ *     private static final int TYPE = 200;
+ *
+ *     protected int type()
+ *     {
+ *         return TYPE;
+ *     }
+ *
+ *     public GenericObjA(int id, Client client)
+ *     {
+ *         super(id, client);
+ *     }
+ *
+ *     public GenericObjA(Node xmlElement, Client client)
+ *     {
+ *         super(xmlElement, client);
+ *     }
+ *
+ *     public static OneResponse allocate(Client client, String description)
+ *     {
+ *         return Document.allocate(client, description, TYPE);
+ *     }
+ * }
+ * 
+ * 
+ * + */ +public abstract class Document extends PoolElement +{ + + private static final String METHOD_PREFIX = "document."; + private static final String ALLOCATE = METHOD_PREFIX + "allocate"; + private static final String DELETE = METHOD_PREFIX + "delete"; + private static final String INFO = METHOD_PREFIX + "info"; + + private static final String UPDATE = METHOD_PREFIX + "update"; + private static final String CHOWN = METHOD_PREFIX + "chown"; + private static final String CHMOD = METHOD_PREFIX + "chmod"; + private static final String CLONE = METHOD_PREFIX + "clone"; + + /** + * Creates a new Document representation. + * @param id The document id. + * @param client XML-RPC Client. + */ + public Document(int id, Client client) + { + super(id, client); + } + + /** + * @see PoolElement + */ + public Document(Node xmlElement, Client client) + { + super(xmlElement, client); + } + + protected abstract int type(); + + // ================================= + // Static XML-RPC methods + // ================================= + + /** + * Allocates a new Document in OpenNebula. + * + * @param client XML-RPC Client. + * @param description A string containing the template of the document. + * @return If successful the message contains the associated + * id generated for this Document. + */ + protected static OneResponse allocate(Client client, String description, int type) + { + return client.call(ALLOCATE, description, type); + } + + /** + * Retrieves the information of the given Document. + * + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public OneResponse info() + { + return client.call(INFO, id, type()); + } + + /** + * Deletes a document from OpenNebula. + * + * @return A encapsulated response. + */ + public OneResponse delete() + { + return client.call(DELETE, id, type()); + } + + /** + * Replaces the document template contents. + * + * @param new_document New template contents. + * @return If successful the message contains the document id. + */ + public OneResponse update(String new_document) + { + return client.call(UPDATE, id, new_document, type()); + } + + /** + * Publishes or unpublishes a document. + * + * @param publish True for publishing, false for unpublishing. + * @return If successful the message contains the document id. + */ + public OneResponse publish(boolean publish) + { + int group_u = publish ? 1 : 0; + + return chmod(-1, -1, -1, group_u, -1, -1, -1, -1, -1); + } + + /** + * Changes the owner/group + * + * @param uid The new owner user ID. Set it to -1 to leave the current one. + * @param gid The new group ID. Set it to -1 to leave the current one. + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chown(int uid, int gid) + { + return client.call(CHOWN, id, uid, gid, type()); + } + + /** + * Changes the document permissions + * + * @param owner_u 1 to allow, 0 deny, -1 do not change + * @param owner_m 1 to allow, 0 deny, -1 do not change + * @param owner_a 1 to allow, 0 deny, -1 do not change + * @param group_u 1 to allow, 0 deny, -1 do not change + * @param group_m 1 to allow, 0 deny, -1 do not change + * @param group_a 1 to allow, 0 deny, -1 do not change + * @param other_u 1 to allow, 0 deny, -1 do not change + * @param other_m 1 to allow, 0 deny, -1 do not change + * @param other_a 1 to allow, 0 deny, -1 do not change + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chmod( int owner_u, int owner_m, int owner_a, + int group_u, int group_m, int group_a, + int other_u, int other_m, int other_a) + { + return chmod(client, CHMOD, id, + owner_u, owner_m, owner_a, + group_u, group_m, group_a, + other_u, other_m, other_a); + } + + /** + * Changes the permissions + * + * @param octet Permissions octed , e.g. 640 + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chmod(String octet) + { + return chmod(client, CHMOD, id, octet); + } + + /** + * Changes the permissions + * + * @param octet Permissions octed , e.g. 640 + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chmod(int octet) + { + return chmod(client, CHMOD, id, octet); + } + + /** + * Clones this document into a new one + * + * @param name Name for the new document. + * @return If successful the message contains the new document ID. + */ + public OneResponse clone(String name) + { + return client.call(CLONE, id, name, type()); + } + + // ================================= + // Helpers + // ================================= + + /** + * Publishes the document. + * + * @return If successful the message contains the document id. + */ + public OneResponse publish() + { + return publish(true); + } + + /** + * Unpublishes the document. + * + * @return If successful the message contains the document id. + */ + public OneResponse unpublish() + { + return publish(false); + } + + /** + * Changes the owner + * + * @param uid The new owner user ID. + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chown(int uid) + { + return chown(uid, -1); + } + + /** + * Changes the group + * + * @param gid The new group ID. + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chgrp(int gid) + { + return chown(-1, gid); + } +} diff --git a/src/oca/java/src/org/opennebula/client/document/DocumentPool.java b/src/oca/java/src/org/opennebula/client/document/DocumentPool.java new file mode 100644 index 0000000000..80e588627f --- /dev/null +++ b/src/oca/java/src/org/opennebula/client/document/DocumentPool.java @@ -0,0 +1,220 @@ +/******************************************************************************* + * Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) + * + * 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.document; + +import org.opennebula.client.Client; +import org.opennebula.client.OneResponse; +import org.opennebula.client.Pool; + +/** + *

+ * This class represents an OpenNebula Document pool. + *

+ * Documents are generic objects. You can dynamically create new Pools in + * OpenNebula, creating subclasses with different TYPE values. + *
+ * TYPE must be the same for the corresponding pool element, see {@link Document} + *
+ * For example: + *
+ * 
+ * public class GenericObjAPool extends DocumentPool implements Iterable{@code  }
+ * {
+ *     private static final int TYPE = 200;
+ *
+ *     protected int type()
+ *     {
+ *         return TYPE;
+ *     }
+ *
+ *     public GenericObjAPool(Client client)
+ *     {
+ *         super(client);
+ *     }
+ *
+ *     public GenericObjAPool(Client client, int filter)
+ *     {
+ *         super(client, filter);
+ *     }
+ *
+ *     public GenericObjA factory(Node node)
+ *     {
+ *         return new GenericObjA(node, client);
+ *     }
+ *
+ *     {@code public Iterator iterator()}
+ *     {
+ *         {@code AbstractList ab = new AbstractList()}
+ *         {
+ *             public int size()
+ *             {
+ *                 return getLength();
+ *             }
+ *
+ *             public GenericObjA get(int index)
+ *             {
+ *                 return (GenericObjA) item(index);
+ *             }
+ *         };
+ *
+ *         return ab.iterator();
+ *     }
+ * }
+ * 
+ * 
+ */ +public abstract class DocumentPool extends Pool +{ + private static final String ELEMENT_NAME = "DOCUMENT"; + private static final String INFO_METHOD = "documentpool.info"; + + private final int filter; + + protected abstract int type(); + + /** + * Creates a new Document pool with the default filter flag value + * set to {@link Pool#MINE_GROUP} (Document belonging to the connected user, + * and the ones in his group) + * + * @param client XML-RPC Client. + * + * @see DocumentPool#DocumentPool(Client, int) + */ + public DocumentPool(Client client) + { + super(ELEMENT_NAME, client, INFO_METHOD); + this.filter = MINE_GROUP; + } + + /** + * Creates a new Document pool. + * + * @param client XML-RPC Client. + * @param filter Filter flag to use by default in the method + * {@link DocumentPool#info()}. Possible values: + * + */ + public DocumentPool(Client client, int filter) + { + super(ELEMENT_NAME, client, INFO_METHOD); + this.filter = filter; + } + + /** + * Loads the xml representation of all or part of the + * Documents in the pool. The filter used is the one set in + * the constructor. + * + * @see DocumentPool#info + * + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public OneResponse info() + { + OneResponse response = + xmlrpcInfo(client, infoMethod, filter, -1, -1, type()); + processInfo(response); + return response; + } + + /** + * Loads the xml representation of all the Documents in the pool. + * + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public OneResponse infoAll() + { + OneResponse response = + xmlrpcInfo(client, infoMethod, ALL, -1, -1, type()); + processInfo(response); + return response; + } + + /** + * Loads the xml representation of all the connected user's Documents. + * + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public OneResponse infoMine() + { + OneResponse response = + xmlrpcInfo(client, infoMethod, MINE, -1, -1, type()); + processInfo(response); + return response; + } + + /** + * Loads the xml representation of all the connected user's Documents and + * the ones in his group. + * + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public OneResponse infoGroup() + { + OneResponse response = + xmlrpcInfo(client, infoMethod, MINE_GROUP, -1, -1, type()); + processInfo(response); + return response; + } + + /** + * Retrieves all or part of the Documents in the pool. The Documents to retrieve + * can be also filtered by Id, specifying the first and last Id to include. + * + * @param filter Filter flag to use. Possible values: + * + * @param startId Lowest Id to retrieve + * @param endId Biggest Id to retrieve + * @return If successful the message contains the string + * with the information returned by OpenNebula. + */ + public OneResponse info(int filter, int startId, int endId) + { + OneResponse response = + xmlrpcInfo(client, infoMethod, filter, startId, endId, type()); + processInfo(response); + return response; + } + + /** + * Returns the Document 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 ACl rule to retrieve + * @return The Document with the given Id, or null if it was not found. + */ + public Document getById(int id) + { + return (Document) super.getById(id); + } +} diff --git a/src/oca/java/test/DocumentTest.java b/src/oca/java/test/DocumentTest.java new file mode 100644 index 0000000000..31456e1963 --- /dev/null +++ b/src/oca/java/test/DocumentTest.java @@ -0,0 +1,148 @@ +/******************************************************************************* + * Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) + * + * 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. + ******************************************************************************/ +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opennebula.client.Client; +import org.opennebula.client.OneResponse; + + +public class DocumentTest +{ + private static GenericObjA objA; + private static GenericObjB objB; + private static GenericObjAPool objAPool; + private static GenericObjBPool objBPool; + + private static Client client; + + private static OneResponse res; + + private static String nameA = "obj_one"; + private static String nameB = "obj_two"; + + private static String template_one = + "NAME = \"" + nameA + "\"\n" + + "ATT1 = \"VAL1\"\n" + + "ATT2 = \"VAL2\""; + + private static String template_two = + "NAME = \"" + nameB + "\"\n" + + "ATT1 = \"VAL1\"\n" + + "ATT2 = \"VAL2\""; + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception + { + client = new Client(); + objAPool = new GenericObjAPool(client); + objBPool = new GenericObjBPool(client); + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception + { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception + { + res = GenericObjA.allocate(client, template_one); + assertTrue( res.getErrorMessage(), !res.isError() ); + + int oidA = Integer.parseInt(res.getMessage()); + objA = new GenericObjA(oidA, client); + + res = GenericObjB.allocate(client, template_two); + assertTrue( res.getErrorMessage(), !res.isError() ); + + int oidB = Integer.parseInt(res.getMessage()); + objB = new GenericObjB(oidB, client); + + res = objA.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + res = objB.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception + { + objA.delete(); + objB.delete(); + } + + @Test + public void sameOidSpace() + { + assertTrue( + "objA id : " + objA.getId() + "; objB id : " + objB.getId(), + objB.id() == objA.id()+1 ); + } + + @Test + public void differentPools() + { + res = objAPool.infoAll(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + boolean foundA = false; + boolean foundB = false; + + for(GenericObjA ob : objAPool) + { + foundA = foundA || ob.getName().equals(nameA); + foundB = foundB || ob.getName().equals(nameB); + } + + assertTrue( foundA ); + assertTrue( !foundB ); + + + + res = objBPool.infoMine(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + foundA = false; + foundB = false; + + for(GenericObjB ob : objBPool) + { + foundA = foundA || ob.getName().equals(nameA); + foundB = foundB || ob.getName().equals(nameB); + } + + assertTrue( !foundA ); + assertTrue( foundB ); + } +} diff --git a/src/oca/java/test/GenericObjA.java b/src/oca/java/test/GenericObjA.java new file mode 100644 index 0000000000..ec8245f455 --- /dev/null +++ b/src/oca/java/test/GenericObjA.java @@ -0,0 +1,31 @@ +import org.opennebula.client.Client; +import org.opennebula.client.OneResponse; +import org.opennebula.client.document.Document; +import org.w3c.dom.Node; + +public class GenericObjA extends Document +{ + private static final int TYPE = 200; + + @Override + protected int type() + { + return TYPE; + } + + public GenericObjA(int id, Client client) + { + super(id, client); + } + + public GenericObjA(Node xmlElement, Client client) + { + super(xmlElement, client); + } + + + public static OneResponse allocate(Client client, String description) + { + return Document.allocate(client, description, TYPE); + } +} \ No newline at end of file diff --git a/src/oca/java/test/GenericObjAPool.java b/src/oca/java/test/GenericObjAPool.java new file mode 100644 index 0000000000..fe63f198a5 --- /dev/null +++ b/src/oca/java/test/GenericObjAPool.java @@ -0,0 +1,53 @@ +import java.util.AbstractList; +import java.util.Iterator; + +import org.opennebula.client.Client; +import org.opennebula.client.document.DocumentPool; +import org.w3c.dom.Node; + + +public class GenericObjAPool extends DocumentPool implements Iterable +{ + private static final int TYPE = 200; + + @Override + protected int type() + { + return TYPE; + } + + public GenericObjAPool(Client client) + { + super(client); + } + + public GenericObjAPool(Client client, int filter) + { + super(client, filter); + } + + @Override + public GenericObjA factory(Node node) + { + return new GenericObjA(node, client); + } + + @Override + public Iterator iterator() + { + AbstractList ab = new AbstractList() + { + public int size() + { + return getLength(); + } + + public GenericObjA get(int index) + { + return (GenericObjA) item(index); + } + }; + + return ab.iterator(); + } +} diff --git a/src/oca/java/test/GenericObjB.java b/src/oca/java/test/GenericObjB.java new file mode 100644 index 0000000000..a8caf0aaf5 --- /dev/null +++ b/src/oca/java/test/GenericObjB.java @@ -0,0 +1,31 @@ +import org.opennebula.client.Client; +import org.opennebula.client.OneResponse; +import org.opennebula.client.document.Document; +import org.w3c.dom.Node; + +public class GenericObjB extends Document +{ + private static final int TYPE = 201; + + @Override + protected int type() + { + return TYPE; + } + + public GenericObjB(int id, Client client) + { + super(id, client); + } + + public GenericObjB(Node xmlElement, Client client) + { + super(xmlElement, client); + } + + + public static OneResponse allocate(Client client, String description) + { + return Document.allocate(client, description, TYPE); + } +} \ No newline at end of file diff --git a/src/oca/java/test/GenericObjBPool.java b/src/oca/java/test/GenericObjBPool.java new file mode 100644 index 0000000000..04025070e3 --- /dev/null +++ b/src/oca/java/test/GenericObjBPool.java @@ -0,0 +1,53 @@ +import java.util.AbstractList; +import java.util.Iterator; + +import org.opennebula.client.Client; +import org.opennebula.client.document.DocumentPool; +import org.w3c.dom.Node; + + +public class GenericObjBPool extends DocumentPool implements Iterable +{ + private static final int TYPE = 201; + + @Override + protected int type() + { + return TYPE; + } + + public GenericObjBPool(Client client) + { + super(client); + } + + public GenericObjBPool(Client client, int filter) + { + super(client, filter); + } + + @Override + public GenericObjB factory(Node node) + { + return new GenericObjB(node, client); + } + + @Override + public Iterator iterator() + { + AbstractList ab = new AbstractList() + { + public int size() + { + return getLength(); + } + + public GenericObjB get(int index) + { + return (GenericObjB) item(index); + } + }; + + return ab.iterator(); + } +} diff --git a/src/oca/java/test/all_tests.sh b/src/oca/java/test/all_tests.sh index f6932e2b40..cffeb16dff 100755 --- a/src/oca/java/test/all_tests.sh +++ b/src/oca/java/test/all_tests.sh @@ -17,18 +17,21 @@ #--------------------------------------------------------------------------- # if [ -z $ONE_LOCATION ]; then - echo "ONE_LOCATION not defined." - exit -1 + ONEDCONF_LOCATION="/etc/one/oned.conf" +else + ONEDCONF_LOCATION="$ONE_LOCATION/etc/oned.conf" fi -ONEDCONF_LOCATION="$ONE_LOCATION/etc/oned.conf" - if [ -f $ONEDCONF_LOCATION ]; then echo "$ONEDCONF_LOCATION has to be overwritten, move it to a safe place." exit -1 fi -cp oned.conf $ONEDCONF_LOCATION +if [ -z $ONE_LOCATION ]; then + sudo cp oned.conf $ONEDCONF_LOCATION +else + cp oned.conf $ONEDCONF_LOCATION +fi export ONE_XMLRPC=http://localhost:2666/RPC2 @@ -61,4 +64,7 @@ let RC=RC+$? ./test.sh AclTest let RC=RC+$? +./test.sh DocumentTest +let RC=RC+$? + exit $RC \ No newline at end of file diff --git a/src/oca/java/test/test.sh b/src/oca/java/test/test.sh index 4da8d19706..d021c7fa8c 100755 --- a/src/oca/java/test/test.sh +++ b/src/oca/java/test/test.sh @@ -22,14 +22,13 @@ JUNIT_JAR="/usr/share/java/junit4.jar" if [ -z $ONE_LOCATION ]; then - echo "ONE_LOCATION not defined." - exit -1 + DB_LOCATION="/var/lib/one/one.db" +else + DB_LOCATION="$ONE_LOCATION/var/one.db" fi -VAR_LOCATION="$ONE_LOCATION/var" - -if [ -f $VAR_LOCATION/one.db ]; then - echo "$VAR_LOCATION/one.db has to be overwritten, move it to a safe place." +if [ -f $DB_LOCATION ]; then + echo "$DB_LOCATION has to be overwritten, move it to a safe place." exit -1 fi @@ -50,6 +49,6 @@ CODE=$? pkill -P $PID oned sleep 4s; pkill -9 -P $PID oned -rm -f $VAR_LOCATION/one.db +rm -f $DB_LOCATION exit $CODE \ No newline at end of file