From 74f72f488d1f16cbbd7dbb98b236b39cf24bdd75 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Wed, 19 Dec 2012 12:00:57 +0000 Subject: [PATCH] Initial import. git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/admin/rmp-common-library@64966 82a268e6-3cf1-43bd-a215-b396298e98cf --- .checkstyle | 8 + .classpath | 7 + .project | 29 + .settings/org.eclipse.core.resources.prefs | 4 + .settings/org.eclipse.jdt.core.prefs | 13 + .settings/org.eclipse.m2e.core.prefs | 5 + distro/INSTALL | 1 + distro/LICENSE | 8 + distro/MAINTAINERS | 2 + distro/README | 45 + distro/changelog.xml | 10 + distro/descriptor.xml | 48 ++ distro/profile.xml | 25 + distro/svnpath.txt | 1 + etc/build.properties | 4 + etc/profile.xml | 22 + pom.xml | 152 ++++ .../exceptions/AbstractResourceException.java | 48 ++ .../exceptions/ResourceAccessException.java | 43 + .../ResourceOperationException.java | 55 ++ .../ResourceParameterException.java | 42 + .../managers/report/ReportBuilder.java | 56 ++ .../support/managers/report/ReportEntry.java | 100 +++ .../managers/report/ReportOperation.java | 33 + .../resources/AbstractResourceManager.java | 798 ++++++++++++++++++ .../managers/resources/CollectionManager.java | 84 ++ .../managers/resources/GHNManager.java | 210 +++++ .../resources/GenericResourceManager.java | 203 +++++ .../managers/resources/ManagementUtils.java | 340 ++++++++ .../managers/resources/ResourceFactory.java | 71 ++ .../resources/RunningInstanceManager.java | 215 +++++ .../resources/RuntimeResourceManager.java | 109 +++ .../managers/resources/ServiceManager.java | 83 ++ .../managers/resources/ViewManager.java | 86 ++ .../support/managers/scope/ScopeManager.java | 236 ++++++ .../services/SWRepositoryManager.java | 187 ++++ .../services/SWRepositoryUpgrader.java | 571 +++++++++++++ .../managers/services/UpgradeListener.java | 102 +++ .../support/tests/GenericTest.java | 187 ++++ .../support/tests/ScopeTest.java | 45 + .../support/types/AllowedResourceTypes.java | 31 + .../support/utils/Assertion.java | 65 ++ .../support/utils/ServerConsole.java | 60 ++ .../utils/persistence/PersistenceHandler.java | 62 ++ .../utils/persistence/PersistentItem.java | 152 ++++ .../utils/persistence/TestPersistence.java | 46 + test-suite/scopes/scopedata.xml | 57 ++ test-suite/scopes/scopedata_admin.xml | 56 ++ 48 files changed, 4817 insertions(+) create mode 100644 .checkstyle create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 distro/INSTALL create mode 100644 distro/LICENSE create mode 100644 distro/MAINTAINERS create mode 100644 distro/README create mode 100644 distro/changelog.xml create mode 100644 distro/descriptor.xml create mode 100644 distro/profile.xml create mode 100644 distro/svnpath.txt create mode 100644 etc/build.properties create mode 100644 etc/profile.xml create mode 100644 pom.xml create mode 100644 src/main/java/org/gcube/resourcemanagement/support/exceptions/AbstractResourceException.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/exceptions/ResourceAccessException.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/exceptions/ResourceOperationException.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/exceptions/ResourceParameterException.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/report/ReportBuilder.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/report/ReportEntry.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/report/ReportOperation.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/resources/AbstractResourceManager.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/resources/CollectionManager.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/resources/GHNManager.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/resources/GenericResourceManager.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/resources/ManagementUtils.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/resources/ResourceFactory.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/resources/RunningInstanceManager.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/resources/RuntimeResourceManager.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/resources/ServiceManager.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/resources/ViewManager.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/scope/ScopeManager.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/services/SWRepositoryManager.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/services/SWRepositoryUpgrader.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/managers/services/UpgradeListener.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/tests/GenericTest.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/tests/ScopeTest.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/types/AllowedResourceTypes.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/utils/Assertion.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/utils/ServerConsole.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/utils/persistence/PersistenceHandler.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/utils/persistence/PersistentItem.java create mode 100644 src/main/java/org/gcube/resourcemanagement/support/utils/persistence/TestPersistence.java create mode 100644 test-suite/scopes/scopedata.xml create mode 100644 test-suite/scopes/scopedata_admin.xml diff --git a/.checkstyle b/.checkstyle new file mode 100644 index 0000000..7f7eb5f --- /dev/null +++ b/.checkstyle @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..813cba9 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..d90d747 --- /dev/null +++ b/.project @@ -0,0 +1,29 @@ + + + ResourceManagementSupport + + + + + + org.eclipse.jdt.core.javabuilder + + + + + net.sf.eclipsecs.core.CheckstyleBuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + net.sf.eclipsecs.core.CheckstyleNature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..eb22c70 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +#Wed Dec 19 12:49:25 CET 2012 +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..50394fa --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,13 @@ +#Wed Dec 19 12:49:25 CET 2012 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..a2b441e --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,5 @@ +#Wed Dec 19 12:41:58 CET 2012 +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/distro/INSTALL b/distro/INSTALL new file mode 100644 index 0000000..dfeae98 --- /dev/null +++ b/distro/INSTALL @@ -0,0 +1 @@ +Used as a Web service in the gCube Framework \ No newline at end of file diff --git a/distro/LICENSE b/distro/LICENSE new file mode 100644 index 0000000..bd0c2b4 --- /dev/null +++ b/distro/LICENSE @@ -0,0 +1,8 @@ +gCube System - License +------------------------------------------------------------ + +The gCube/gCore software is licensed as Free Open Source software conveying to +the EUPL (http://ec.europa.eu/idabc/eupl). +The software and documentation is provided by its authors/distributors "as is" +and no expressed or implied warranty is given for its use, quality or fitness +for a particular case. diff --git a/distro/MAINTAINERS b/distro/MAINTAINERS new file mode 100644 index 0000000..08bb1f2 --- /dev/null +++ b/distro/MAINTAINERS @@ -0,0 +1,2 @@ +Daniele Strollo (daniele.strollo@isti.cnr.it), CNR Pisa, +Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" \ No newline at end of file diff --git a/distro/README b/distro/README new file mode 100644 index 0000000..ad9a69c --- /dev/null +++ b/distro/README @@ -0,0 +1,45 @@ +The gCube System +------------------------------------------------------------ + +This work is partially funded by the European Commission in the +context of the D4Science project (www.d4science.eu), under the 1st +call of FP7 IST priority. + + +Authors +------- + +* Daniele Strollo (daniele.strollo@isti.cnr.it), CNR Pisa, + Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" + +Version and Release Date +------------------------ + +version 1.0.0 (20-05-2010) + +Description +-------------------- + +Consists of a gcube service able to properly manage the scheduling and +the deployment planning of services in a gcube infrastructure. + + +Download information +-------------------- + +Source code is available from SVN: +http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/vre-management/ResourceBroker/org.gcube.vremanagement.resourcebroker/ + +Binaries can be downloaded from: +http://software.d4science.research-infrastructures.eu/ + +Documentation +------------- +VREManager documentation is available on-line from the Projects Documentation Wiki: +https://gcube.wiki.gcube-system.org/gcube/index.php/Resource_Broker + +Licensing +--------- + +This software is licensed under the terms you may find in the file named "LICENSE" in this directory. + diff --git a/distro/changelog.xml b/distro/changelog.xml new file mode 100644 index 0000000..1178bad --- /dev/null +++ b/distro/changelog.xml @@ -0,0 +1,10 @@ + + + First release + + + For 1.2.0 of resource management + + \ No newline at end of file diff --git a/distro/descriptor.xml b/distro/descriptor.xml new file mode 100644 index 0000000..c468f13 --- /dev/null +++ b/distro/descriptor.xml @@ -0,0 +1,48 @@ + + servicearchive + + tar.gz + + / + + + ${distroDirectory} + / + true + + README + LICENSE + INSTALL + MAINTAINERS + changelog.xml + + 755 + true + + + target/apidocs + /${artifactId}/doc/api + true + 755 + + + + + ${distroDirectory}/profile.xml + ./ + true + + + target/${build.finalName}.jar + /${artifactId} + + + ${distroDirectory}/svnpath.txt + /${artifactId} + true + + + \ No newline at end of file diff --git a/distro/profile.xml b/distro/profile.xml new file mode 100644 index 0000000..da53c66 --- /dev/null +++ b/distro/profile.xml @@ -0,0 +1,25 @@ + + + + Library + + Resource Management Portlet Common IS Operations Support Library + PortletsAdmin + ${artifactId} + 1.0.0 + + + ${artifactId} + ${version} + + ${groupId} + ${artifactId} + ${version} + + + ${build.finalName}.jar + + + + + diff --git a/distro/svnpath.txt b/distro/svnpath.txt new file mode 100644 index 0000000..edacb04 --- /dev/null +++ b/distro/svnpath.txt @@ -0,0 +1 @@ +${scm.url} \ No newline at end of file diff --git a/etc/build.properties b/etc/build.properties new file mode 100644 index 0000000..8a2056a --- /dev/null +++ b/etc/build.properties @@ -0,0 +1,4 @@ +name = ResourceManagerSupport +package = org.gcube.resourcemanagement.support +package.dir = org/gcube/resourcemanagement/support +lib.dir = ResourceManagementPortlet/war/WEB-INF/lib diff --git a/etc/profile.xml b/etc/profile.xml new file mode 100644 index 0000000..df58715 --- /dev/null +++ b/etc/profile.xml @@ -0,0 +1,22 @@ + + + + Library + + ResourceManagement Support Library + portlet + ResourceManagementSupport + 1.0.0 + + + ResourceManagement Support Library + ResourceManagement-support + 1.0.1 + + org.gcube.resourcemanagement.support.jar + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..43e4f1f --- /dev/null +++ b/pom.xml @@ -0,0 +1,152 @@ + + + 4.0.0 + + maven-parent + org.gcube.tools + 1.0.0 + + + + org.gcube.portlets.admin + rmp-common-library + 1.0.0-SNAPSHOT + Resource Management Portlet Common IS Operations Support Library + + + distro + 1.6 + 1.6 + + UTF-8 + UTF-8 + + + + + src/main/java + + **/*.* + + + + + + maven-compiler-plugin + 3.0 + + 1.6 + 1.6 + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.2 + + + + test-jar + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12 + + true + + + + org.apache.maven.plugins + maven-resources-plugin + 2.5 + + + copy-profile + install + + copy-resources + + + target + + + ${distroDirectory} + true + + profile.xml + + + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 2.2 + + + ${distroDirectory}/descriptor.xml + + + + + servicearchive + install + + single + + + + + + + + + junit + junit + 3.8.1 + test + + + org.gcube.core + gcf + [1.4.0,1.5.0] + provided + + + org.gcube.applicationsupportlayer + aslcore + 3.2.0-SNAPSHOT + + + com.thoughtworks.xstream + xstream + 1.3.1 + + + org.gcube.resourcemanagement + softwarerepository-stubs + 1.2.0-SNAPSHOT + + + org.gcube.resourcemanagement + resource-manager-stubs + 2.0.1-SNAPSHOT + + + org.gcube.resourcemanagement + ghnmanager-stubs + 1.5.0-SNAPSHOT + + + diff --git a/src/main/java/org/gcube/resourcemanagement/support/exceptions/AbstractResourceException.java b/src/main/java/org/gcube/resourcemanagement/support/exceptions/AbstractResourceException.java new file mode 100644 index 0000000..ca8cee3 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/exceptions/AbstractResourceException.java @@ -0,0 +1,48 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: AbstractResourceException.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.exceptions; + +/** + * Represents the basic type of exception thrown by functionalities exposed + * in this library. + * + * The reason is to provide an access point for serialization issues + * (e.g. for GWT based portlets that require additional serialization + * annotations). + * + * @author Daniele Strollo (ISTI-CNR) + */ +public class AbstractResourceException extends Exception { + private static final long serialVersionUID = 4851998460190622583L; + + public AbstractResourceException() { + super(); + } + + public AbstractResourceException(final String message, final Throwable cause) { + super(message, cause); + } + + public AbstractResourceException(final String message) { + super(message); + } + + public AbstractResourceException(final Throwable cause) { + super(cause); + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/exceptions/ResourceAccessException.java b/src/main/java/org/gcube/resourcemanagement/support/exceptions/ResourceAccessException.java new file mode 100644 index 0000000..41df7b8 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/exceptions/ResourceAccessException.java @@ -0,0 +1,43 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ResourceAccessException.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.exceptions; + +/** + * Thrown when is required an operation the user is not allowed to execute. + * + * @author Daniele Strollo (ISTI-CNR) + */ +public class ResourceAccessException extends AbstractResourceException { + private static final long serialVersionUID = -4491094127600507185L; + + public ResourceAccessException() { + super(); + } + + public ResourceAccessException(final String message, final Throwable cause) { + super(message, cause); + } + + public ResourceAccessException(final String message) { + super(message); + } + + public ResourceAccessException(final Throwable cause) { + super(cause); + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/exceptions/ResourceOperationException.java b/src/main/java/org/gcube/resourcemanagement/support/exceptions/ResourceOperationException.java new file mode 100644 index 0000000..67b01e3 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/exceptions/ResourceOperationException.java @@ -0,0 +1,55 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ResourceOperationException.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.exceptions; + +/** + * If an operation of the library internally fails. + * @author Daniele Strollo (ISTI-CNR) + */ +public class ResourceOperationException extends AbstractResourceException { + private static final long serialVersionUID = -8748539948441128210L; + + /** + * + */ + public ResourceOperationException() { + super(); + } + + /** + * @param message + */ + public ResourceOperationException(final String message) { + super(message); + } + + /** + * @param cause + */ + public ResourceOperationException(final Throwable cause) { + super(cause); + } + + /** + * @param message + * @param cause + */ + public ResourceOperationException(final String message, final Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/exceptions/ResourceParameterException.java b/src/main/java/org/gcube/resourcemanagement/support/exceptions/ResourceParameterException.java new file mode 100644 index 0000000..46205b2 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/exceptions/ResourceParameterException.java @@ -0,0 +1,42 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ResourceParameterException.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.exceptions; + +/** + * Wrong parameters provided by the user. + * @author Daniele Strollo (ISTI-CNR) + */ +public class ResourceParameterException extends AbstractResourceException { + private static final long serialVersionUID = -1117521050663690780L; + + public ResourceParameterException() { + super(); + } + + public ResourceParameterException(final String message, final Throwable cause) { + super(message, cause); + } + + public ResourceParameterException(final String message) { + super(message); + } + + public ResourceParameterException(final Throwable cause) { + super(cause); + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/report/ReportBuilder.java b/src/main/java/org/gcube/resourcemanagement/support/managers/report/ReportBuilder.java new file mode 100644 index 0000000..c58319c --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/report/ReportBuilder.java @@ -0,0 +1,56 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ReportBuilder.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.report; + +import java.util.List; +import java.util.Vector; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class ReportBuilder { + private List entries = null; + public ReportBuilder() { + entries = new Vector(); + } + + public final void addEntry(final ReportEntry entry) { + this.entries.add(entry); + } + + public final int size() { + return this.entries.size(); + } + + public final String getXML() { + StringBuilder builder = new StringBuilder(); + + builder.append("\n"); + for (ReportEntry entry : this.entries) { + builder.append(entry.toXML()); + } + builder.append("\n"); + return builder.toString(); + } + + @Override + public final String toString() { + return this.getXML(); + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/report/ReportEntry.java b/src/main/java/org/gcube/resourcemanagement/support/managers/report/ReportEntry.java new file mode 100644 index 0000000..545774d --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/report/ReportEntry.java @@ -0,0 +1,100 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ReportEntry.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.report; + +import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException; +import org.gcube.resourcemanagement.support.managers.resources.AbstractResourceManager; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class ReportEntry { + private ReportOperation operation = null; + private AbstractResourceManager resource = null; + private boolean success = false; + private String message = null; + + public ReportEntry( + final ReportOperation operation, + final AbstractResourceManager resource + ) throws AbstractResourceException { + this(operation, resource, null, false); + } + + public ReportEntry( + final ReportOperation operation, + final AbstractResourceManager resource, + final boolean success + ) throws AbstractResourceException { + this(operation, resource, null, success); + } + + public ReportEntry( + final ReportOperation operation, + final AbstractResourceManager resource, + final String message, + final boolean success + ) throws AbstractResourceException { + this.operation = operation; + this.resource = resource; + this.success = success; + this.message = message; + } + public final ReportOperation getOperation() { + return operation; + } + public final AbstractResourceManager getResource() { + return resource; + } + public final boolean isSuccess() { + return success; + } + public final void setSuccess(final boolean success) { + this.success = success; + } + public final void setMessage(final String message) { + this.message = message; + } + public final void setMessage(final Throwable ex) { + this.message = ex.getMessage(); + } + + @Override + public final String toString() { + return this.toXML(); + } + + public final String toXML() { + return + "\t\n" + + "\t\t" + (this.isSuccess() ? "SUCCESS" : "FAILURE") + "\n" + + "\t\t\n" + + "\t\t\t" + this.getResource().getID() + "\n" + + // If the resource has a name + (this.getResource().getName() != null ? + "\t\t\t" + this.getResource().getName() + "\n" : "") + + + "\t\t\t" + this.getResource().getType() + "\n" + + // If a message is present + (this.message != null ? + "\t\t\t\n\t\t\t\t" + this.message.trim() + "\n\t\t\t\n" : "") + + + "\t\t\n" + + "\t\n"; + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/report/ReportOperation.java b/src/main/java/org/gcube/resourcemanagement/support/managers/report/ReportOperation.java new file mode 100644 index 0000000..f144e66 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/report/ReportOperation.java @@ -0,0 +1,33 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ReportOperation.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.report; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public enum ReportOperation { + AddToScope("Add To Scope"); + + private String label = null; + private ReportOperation(final String label) { + this.label = label; + } + public final String getLabel() { + return this.label; + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/resources/AbstractResourceManager.java b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/AbstractResourceManager.java new file mode 100644 index 0000000..857ed87 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/AbstractResourceManager.java @@ -0,0 +1,798 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ResourceManager.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.resources; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Vector; +import org.apache.axis.message.addressing.EndpointReferenceType; +import org.gcube.common.core.contexts.GCUBERemotePortTypeContext; +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.informationsystem.client.AtomicCondition; +import org.gcube.common.core.informationsystem.client.ISClient; +import org.gcube.common.core.informationsystem.client.XMLResult; +import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery; +import org.gcube.common.core.informationsystem.client.queries.GCUBERIQuery; +import org.gcube.common.core.informationsystem.publisher.ISPublisher; +import org.gcube.common.core.resources.GCUBEResource; +import org.gcube.common.core.resources.GCUBERunningInstance; +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.common.core.scope.GCUBEScope.Type; +import org.gcube.common.core.security.GCUBESecurityManagerImpl; +import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException; +import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException; +import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException; +import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException; +import org.gcube.resourcemanagement.support.managers.report.ReportBuilder; +import org.gcube.resourcemanagement.support.managers.report.ReportEntry; +import org.gcube.resourcemanagement.support.managers.report.ReportOperation; +import org.gcube.resourcemanagement.support.managers.scope.ScopeManager; +import org.gcube.resourcemanagement.support.types.AllowedResourceTypes; +import org.gcube.resourcemanagement.support.utils.Assertion; +import org.gcube.resourcemanagement.support.utils.ServerConsole; + +import org.gcube.vremanagement.resourcemanager.stubs.binder.AddResourcesParameters; +import org.gcube.vremanagement.resourcemanager.stubs.binder.RemoveResourcesParameters; +import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceBinderPortType; +import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceItem; +import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceList; +import org.gcube.vremanagement.resourcemanager.stubs.binder.service.ResourceBinderServiceAddressingLocator; +import org.gcube.vremanagement.resourcemanager.stubs.reporting.ReportingPortType; +import org.gcube.vremanagement.resourcemanager.stubs.reporting.service.ReportingServiceAddressingLocator; + + +/** + * The minimal interface all the resource managers must implement. + * Here is implemented the greatest part of the operations exposed in the + * + * official wiki. + * @author Daniele Strollo (ISTI-CNR) + */ +public abstract class AbstractResourceManager { + private String id = null; + private String name = null; + private AllowedResourceTypes type = null; + private String subType = null; + private ISClient client = null; + private GCUBESecurityManagerImpl managerSec = null; + private ISPublisher publisher = null; + private static final String LOG_PREFIX = "[AbstractResMgr]"; + + /** + * @deprecated discouraged use. With no ID some operations cannot be accessed. For internal use only. + */ + public AbstractResourceManager(final AllowedResourceTypes type) + throws ResourceParameterException, ResourceAccessException { + Assertion checker = new Assertion(); + checker.validate(type != null, new ResourceParameterException("Invalid Parameter type")); + + this.type = type; + + /** + * Initially the security management is disabled. + */ + this.managerSec = new GCUBESecurityManagerImpl() { + public boolean isSecurityEnabled() { + return false; + } + }; + + try { + client = GHNContext.getImplementation(ISClient.class); + } catch (Exception e) { + throw new ResourceAccessException("Cannot instantiate the ISClient"); + } + + try { + this.publisher = GHNContext.getImplementation(ISPublisher.class); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + + /** + * Instantiate the handler of a resource given its identifier and its type. + * Notice that this constructor is implicitly invoked when instantiating the + * concrete resource manager (e.g. new GHNManager(id)). + * @param id the identifier of the resource the manage + * @param type the type (GHN, RI, ...). + * @throws ResourceParameterException + * @throws ResourceAccessException + */ + public AbstractResourceManager( + final String id, + final AllowedResourceTypes type) + throws ResourceParameterException, ResourceAccessException { + this(type); + + Assertion checker = new Assertion(); + checker.validate(id != null && id.trim().length() > 0, new ResourceParameterException("Invalid Parameter id")); + + this.id = id.trim(); + } + + public AbstractResourceManager( + final String id, + final String name, + final AllowedResourceTypes type) + throws ResourceParameterException, ResourceAccessException { + this(id, type); + Assertion checker = new Assertion(); + checker.validate(name != null && name.trim().length() > 0, new ResourceParameterException("Invalid Parameter name")); + this.name = name; + } + + public AbstractResourceManager( + final String id, + final String name, + final AllowedResourceTypes type, + final String subtype) + throws ResourceParameterException, ResourceAccessException { + this(id, name, type); + if (subtype != null) { + this.subType = subtype.trim(); + } + } + + /** + * The singleton ISClient instance is preferred. + * All resource managers can internally access this instance + * to make queries to the IS. + * @return + */ + protected final ISClient getISClient() { + return this.client; + } + + /** + * The singleton ISPublisher instance is preferred. + * All resource managers can internally access this instance + * to interact with ISPublisher to handle resources. + * @return + */ + public final ISPublisher getISPublisher() { + return publisher; + } + + public final void setSecurityManager(final GCUBESecurityManagerImpl securityManager) { + this.managerSec = securityManager; + } + + /** + * The security manager is initially instantiated with empty permissions. + * The {@link AbstractResourceManager#setSecurityManager} can be used to + * change it. + * @return + */ + public final GCUBESecurityManagerImpl getSecurityManager() { + return this.managerSec; + } + + /** + * All resources must be identifiable through an unique ID. + *
+ * This field is mandatory + * @return + */ + public final String getID() { + return this.id; + } + + public final void setID(final String id) { + this.id = id; + } + + /** + * All resources must have a valid name. + *
+ * This field is mandatory + * @return + */ + public final String getName() { + return this.name; + } + + /** + * All resources have a type. + *
+ * This field is mandatory + * @return + */ + public final AllowedResourceTypes getType() { + return this.type; + } + + /** + * Resources can have a subtype (e.g. for GHNs is the domain). + *
+ * This field is optional + * @return + */ + public final String getSubType() { + return this.subType; + } + + /** + * Internally used by {@link AbstractResourceManager#getResourceManager(GCUBEScope)}. + * @param scope + * @return a raw list of resource manager descriptors. + * @throws Exception + */ + private List getResourceManagerFromScope(final GCUBEScope scope) + throws Exception { + GCUBERIQuery query = this.client.getQuery(GCUBERIQuery.class); + query.addAtomicConditions(new AtomicCondition("//Profile/ServiceClass", "VREManagement")); + query.addAtomicConditions(new AtomicCondition("//Profile/ServiceName", "ResourceManager")); + + List result = client.execute(query, scope); + List toReturn = new ArrayList(); + for (GCUBERunningInstance ri : result) { + if (ri.getScopes().containsValue(scope)) { + toReturn.add(ri); + } + } + return toReturn; + } + + /** + * The the list of resource managers able to handle the resource in a given scope. + * @param scope the scope in which to operate + * @return all the available managers + * @throws ResourceAccessException if no manager can be instantiated + * @throws ResourceParameterException if the parameters are invalid + */ + public final List getResourceManagers(final GCUBEScope scope) throws ResourceAccessException, ResourceParameterException { + Assertion checker = new Assertion(); + checker.validate(scope != null, new ResourceParameterException("Invalid scope")); + + List resourceManagerList = null; + try { + resourceManagerList = this.getResourceManagerFromScope(scope); + } catch (Exception e) { + e.printStackTrace(); + throw new ResourceAccessException("Cannot retrieve the managers for resource: " + this.getID()); + } + List retval = new Vector(); + + if (resourceManagerList.isEmpty()) { + throw new ResourceAccessException("Unable to find ResourceManagers for resource " + this.getType() + " in scope: " + scope.toString()); + } + + EndpointReferenceType endpoint = null; + ResourceBinderPortType pt = null; + + for (GCUBERunningInstance resourceManager : resourceManagerList) { + try { + endpoint = resourceManager.getAccessPoint().getEndpoint("gcube/vremanagement/resourcemanager/binder"); + + pt = GCUBERemotePortTypeContext.getProxy( + new ResourceBinderServiceAddressingLocator() + .getResourceBinderPortTypePort(endpoint), + scope, + this.managerSec); + if (pt != null) { + retval.add(pt); + } + } catch (Throwable e) { + // trying on next entry + ServerConsole.error(LOG_PREFIX, e); + } + } + if (retval != null && retval.size() > 0) { + // Return a random manager from the available ones + return retval; + } + // no managers found + throw new ResourceAccessException("Unable to find ResourceManagers for resource " + this.getID() + "in scope: " + scope.toString()); + } + + /** + * The the list of resource report managers able to handle the resource in a given scope. + * @param scope the scope in which to operate + * @return all the available managers + * @throws ResourceAccessException if no manager can be instantiated + * @throws ResourceParameterException if the parameters are invalid + */ + public final List getResourceReportManagers(final GCUBEScope scope) throws ResourceAccessException, ResourceParameterException { + Assertion checker = new Assertion(); + checker.validate(scope != null, new ResourceParameterException("Invalid scope")); + + List resourceManagerList = null; + try { + resourceManagerList = this.getResourceManagerFromScope(scope); + } catch (Exception e) { + e.printStackTrace(); + throw new ResourceAccessException("Cannot retrieve the managers for resource: " + this.getID()); + } + List retval = new Vector(); + + if (resourceManagerList.isEmpty()) { + throw new ResourceAccessException("Unable to find ResourceManagers for resource " + this.getType() + " in scope: " + scope.toString()); + } + + EndpointReferenceType endpoint = null; + ReportingPortType pt = null; + + for (GCUBERunningInstance resourceManager : resourceManagerList) { + try { + endpoint = resourceManager.getAccessPoint().getEndpoint("gcube/vremanagement/resourcemanager/reporting"); + + pt = GCUBERemotePortTypeContext.getProxy( + new ReportingServiceAddressingLocator().getReportingPortTypePort(endpoint), + scope, + this.managerSec); + if (pt != null) { + retval.add(pt); + } + } catch (Throwable e) { + // trying on next entry + ServerConsole.error(LOG_PREFIX, e); + } + } + if (retval != null && retval.size() > 0) { + // Return a random report manager from the available ones + return retval; + } + // no managers found + throw new ResourceAccessException("Unable to find ReportResourceManagers for resource " + this.getID() + "in scope: " + scope.toString()); + } + + /** + * The resource manager needed to handle the resource in a given scope. + * @param scope the scope in which to operate + * @return a random chosen manager from the avaiable ones + * @throws ResourceAccessException if no manager can be instantiated + * @throws ResourceParameterException if the parameters are invalid + */ + public final ResourceBinderPortType getResourceManager(final GCUBEScope scope) + throws AbstractResourceException { + ServerConsole.info(LOG_PREFIX, "Getting Resource Manager in scope [" + scope.toString() + "]"); + + List retval = this.getResourceManagers(scope); + if (retval != null && retval.size() > 0) { + Random generator = new Random(); + // Return a random manager from the available ones + ResourceBinderPortType manager = retval.get(generator.nextInt(retval.size())); + return manager; + } + // no managers found + throw new ResourceAccessException("Unable to find ResourceManagers for resource " + this.getType() + " in scope: " + scope.toString()); + } + + /** + * The report resource manager needed to handle the resource in a given scope. + * @param scope the scope in which to operate + * @return a random chosen manager from the avaiable ones + * @throws ResourceAccessException if no manager can be instantiated + * @throws ResourceParameterException if the parameters are invalid + */ + public final ReportingPortType getReportResourceManager(final GCUBEScope scope) + throws AbstractResourceException { + ServerConsole.info(LOG_PREFIX, "Getting Resource Manager in scope [" + scope.toString() + "]"); + + List retval = this.getResourceReportManagers(scope); + if (retval != null && retval.size() > 0) { + Random generator = new Random(); + // Return a random manager from the available ones + ReportingPortType manager = retval.get(generator.nextInt(retval.size())); + return manager; + } + // no managers found + throw new ResourceAccessException("Unable to find ResourceManagers for resource " + this.getType() + " in scope: " + scope.toString()); + } + + /** + * Once the conditions for binding a resource to a target scope are satisfied + * (see the + * + * official wiki + * for scope binding rules) this method is internally called. + * @param targetScope + * @return + * @throws ResourceOperationException + */ + private String bindToScope(final GCUBEScope targetScope) throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(targetScope != null, new ResourceParameterException("Invalid parameter targetScope. null not allowed.")); + checker.validate(this.getID() != null, new ResourceOperationException("Invalid resource ID. null not allowed.")); + + ServerConsole.trace( + LOG_PREFIX, + "[BIND-SCOPE-ENTER] Adding " + this.getType() + " " + this.getID() + " to scope [" + + targetScope.toString() + "]"); + + AddResourcesParameters addParam = new AddResourcesParameters(); + ResourceItem toAdd = new ResourceItem(); + toAdd.setID(this.getID()); + toAdd.setType(this.getType().name()); + ResourceList r = new ResourceList(); + + + r.setResource(new ResourceItem[]{toAdd}); + addParam.setResources(r); + addParam.setTargetScope(targetScope.toString()); + + ResourceBinderPortType manager = this.getResourceManager(targetScope); + + try { + String reportID = manager.addResources(addParam); + + ServerConsole.trace( + LOG_PREFIX, + "[BIND-SCOPE-EXIT] Applyed Adding " + this.getType() + " " + this.getID() + " to scope [" + + targetScope.toString() + "]... reportID: " + reportID); + + ReportingPortType pt = this.getReportResourceManager(targetScope); + return pt.getReport(reportID); + } catch (Exception e) { + e.printStackTrace(); + ServerConsole.trace( + LOG_PREFIX, + "[BIND-SCOPE-EXIT] [FAILURE]"); + throw new ResourceOperationException("During resource::addToScope: " + e.getMessage()); + } + } + + + + /** + * Add a scope to a gHN and the related Service Map is already available on the gHN. + * @param nestingPublication true for resources different from gHN and RI. + * @return the reportID generated + */ + public final String addToExistingScope( + final GCUBEScope sourceScope, final GCUBEScope targetScope) + throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(sourceScope != null, new ResourceParameterException("Invalid parameter sourceScope. null not allowed.")); + checker.validate(targetScope != null, new ResourceParameterException("Invalid parameter targetScope. null not allowed.")); + checker.validate(this.getID() != null, new ResourceOperationException("Invalid resource ID. null not allowed.")); + + ReportBuilder report = new ReportBuilder(); + + ServerConsole.trace( + LOG_PREFIX, + "[ADD-ToExistingScope] Adding from scope [" + + sourceScope.toString() + + "] to existing scope [" + + targetScope.toString() + + "] " + this.getType() + " " + this.getID()); + + // If not RI or GHN and the scopes are sibling and VO copyFromToVO + if (!(this.getType() == AllowedResourceTypes.GHN) && + !(this.getType() == AllowedResourceTypes.RunningInstance) && + sourceScope.getType() == Type.VO && targetScope.getType() == Type.VO) { + return copyFromToVO(sourceScope, targetScope); + } + + // Add a gCube Resource to + // (i) a VRE scope from the parent VO or + // (ii) a VO scope from the infrastructure scope + if (!targetScope.isEnclosedIn(sourceScope)) { + throw new ResourceOperationException( + "You are not allowed to apply to this scope. Target scope is not enclosed in the source one."); + } + + report.addEntry(new ReportEntry(ReportOperation.AddToScope, this, + "Added " + this.getType() + " " + this.getID() + " to parent scope " + + targetScope.toString() + " the remote report ID is: " + + this.bindToScope(targetScope), true)); + + return report.getXML(); + } + + /** + * Similar to the {@link AbstractResourceManager#addToExistingScope} method but involves + * two scopes of type VO. + * Notice that this operation in reserved for resources different from gHN and RI. + * See + * + * here for further details. + * @param sourceScope + * @param targetScope + * @return + * @throws AbstractResourceException + */ + public final String copyFromToVO(final GCUBEScope sourceScope, final GCUBEScope targetScope) + throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate( + sourceScope != null && sourceScope.getType() == Type.VO, + new ResourceParameterException("The sourceScope is invalid or not of type VO.")); + checker.validate( + targetScope != null && targetScope.getType() == Type.VO, + new ResourceParameterException("The targetScope is invalid or not of type VO.")); + checker.validate( + sourceScope.getEnclosingScope() == targetScope.getEnclosingScope(), + new ResourceParameterException("The sourceScope and targetScope must be children of the same root VO.")); + checker.validate(this.getType() != AllowedResourceTypes.GHN && this.getType() != AllowedResourceTypes.RunningInstance, + new ResourceAccessException("Operation not allowed for RI and GHNs.")); + checker.validate(this.getID() != null, + new ResourceAccessException("Operation not allowed on resources with no ID.")); + + // Phase 1. retrieve the resource to copy + GCUBEResource resStub = this.getGCUBEResource(sourceScope); + + // Phase 2. Before to register the resource, the scope must be + // bound to the local GCUBEResource + this.bindToScope(targetScope); + + // Phase 3. Register to the new VO through the ISPublisher + try { + return this.getISPublisher().registerGCUBEResource(resStub, targetScope, this.getSecurityManager()); + } catch (Exception e) { + throw new ResourceAccessException(e.getMessage()); + } + } + + /** + * Given the XML profile representation of a gcube resource, its GCUBEResource is built. + * Since it depends on the type of the resource, each concrete implementation of resource + * managers must implement it. + * @param xmlRepresentation + * @return + * @throws AbstractResourceException + */ + protected abstract GCUBEResource buildGCUBEResource(final String xmlRepresentation) throws AbstractResourceException; + + /** + * From a resource retrieves its GCUBEResource depending on the scope in which it is + * published. + * @param scope + * @return + * @throws AbstractResourceException + */ + public final GCUBEResource getGCUBEResource(final GCUBEScope scope) throws AbstractResourceException { + return this.buildGCUBEResource(this.getXMLDescription(scope)); + } + + /** + * Returns the XML profile of a resource (given its ID that is encapsulated inside the resource + * manager). + * @param scope + * @return + * @throws AbstractResourceException + */ + protected final String getXMLDescription(final GCUBEScope scope) throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(this.getID() != null, new ResourceAccessException("Cannot execute on resources with no ID.")); + + // Phase 1. retrieve the resource to copy + GCUBEGenericQuery query = null; + try { + query = this.getISClient().getQuery(GCUBEGenericQuery.class); + query.setExpression( + "for $resource in collection('/db/Profiles/" + this.getType().name() + "')//Resource " + + "where ( $resource/ID/string() eq '" + + this.getID() + + "') " + + "return $resource" + ); + } catch (Exception e) { + throw new ResourceAccessException(e); + } + + XMLResult resDescription = null; + try { + resDescription = this.getISClient().execute(query, scope).get(0); + return resDescription.toString(); + } catch (Exception e) { + throw new ResourceAccessException("Cannot retrieve the IS profile for resource: " + this.getID() + + " in scope: " + scope.toString()); + } + } + + /** + * The first phase of remove form scope. + * This is common to all the resources (RI and GHNs). + * @param scope + * @return + * @throws AbstractResourceException + */ + private String basicRemoveFromScope(final GCUBEScope scope) + throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(scope != null, new ResourceParameterException("Invalid parameter scope. null not allowed.")); + checker.validate(this.getID() != null, new ResourceOperationException("Invalid ID. null not allowed.")); + + ServerConsole.trace(LOG_PREFIX, "[REMOVE-FROM-SCOPE] Removing from scope [" + scope.toString() + "] " + this.getType() + " " + this.getID()); + + String retval = null; + + RemoveResourcesParameters params = new RemoveResourcesParameters(); + ResourceItem toRemove = new ResourceItem(); + toRemove.setID(this.getID()); + toRemove.setType(this.getType().name()); + + ResourceList resourcesToRemove = new ResourceList(); + resourcesToRemove.setResource(new ResourceItem[]{toRemove}); + params.setResources(resourcesToRemove); + params.setTargetScope(scope.toString()); + ServerConsole.trace(LOG_PREFIX, "[REMOVE-FROM-SCOPE] Sending the Remove Resource request...."); + try { + ResourceBinderPortType manager = this.getResourceManager(scope); + retval = manager.removeResources(params); + } catch (Exception e) { + throw new ResourceOperationException("During removeFrom scope of " + + this.getType() + + " " + this.getID() + ": " + e.getMessage()); + } + return retval; + } + + /** + * Removes the current resource from the scope. + * @param nestingRemoval true for resources different from gHN and RI + */ + public final String removeFromScope(final GCUBEScope scope) + throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(scope != null, new ResourceParameterException("Invalid parameter scope. null not allowed.")); + checker.validate(this.getID() != null, new ResourceOperationException("Invalid ID. null not allowed.")); + + String retval = this.basicRemoveFromScope(scope); + + // -- PHASE 2 - optional + + // Builds the stub without the removed scope + GCUBEResource resStub = this.getGCUBEResource(scope); + // Refreshes the local profile + resStub.removeScope(scope); + + /* + * These steps are for resources different from GHN and RI + */ + if (!this.getType().equals(AllowedResourceTypes.GHN.name()) && + !this.getType().equals(AllowedResourceTypes.RunningInstance.name())) { + // Phase 1 + // if the resource joins only the current scope, after the removal it has also to be deleted + try { + List boundedScopes = this.validateScopes(resStub.getScopes().values().toArray(new GCUBEScope[]{})); + if (boundedScopes == null) { + ServerConsole.warn(LOG_PREFIX, "[REMOVE-FROM-SCOPE] The resource " + this.getType() + " has no bound scopes"); + return retval; + } + + if (boundedScopes.size() == 0) { + ServerConsole.trace(LOG_PREFIX, "[REMOVE-FROM-SCOPE] [DELETE-BRANCH] deleting resource since no more scopes remained"); + this.getISPublisher().removeGCUBEResource( + this.getID(), this.getType().name(), scope, this.getSecurityManager()); + return retval; + } else { + // requires the update of the resources in all other scopes + for (GCUBEScope _scope : boundedScopes) { + if (!scope.equals(_scope)) { + try { + ServerConsole.trace(LOG_PREFIX, "[REMOVE-FROM-SCOPE] [UPDATE-BRANCH] Updating profile in scope [" + _scope.toString() + "]"); + this.getISPublisher().updateGCUBEResource(resStub, _scope, getSecurityManager()); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + } + } + + } catch (Exception e) { + throw new ResourceOperationException(e); + } + } + + return retval; + } + + /** + * Adds to scopes the required maps (if needed). + * Internally used to ensure that the scopes at Infrastructure or VO level + * have correctly setup the maps. + * @param scopes + * @return + */ + protected List validateScopes(final GCUBEScope[] scopes) { + List retval = new Vector(); + for (GCUBEScope scope : scopes) { + try { + retval.add(ScopeManager.getScope(scope.toString())); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + return retval; + } + + + /** + * @deprecated you must be sure before requiring this operation... take care + * @param scope where the resource is bound + * @throws AbstractResourceException + */ + public final void forceDelete(final GCUBEScope scope) throws AbstractResourceException { + ServerConsole.trace(LOG_PREFIX, "[DELETE] [DELETE-BRANCH] deleting resource since no more scopes remained"); + try { + this.getISPublisher().removeGCUBEResource( + this.getID(), this.getType().name(), scope, this.getSecurityManager()); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + + /** + * Removes a resource. + * According to the + * + * official wiki the resource is deleted only if is not bound in other scopes. Otherwise this + * operation simply corresponds to the in deep remove from scope. + * @param scope + * @throws ResourceOperationException + */ + public final void delete(final GCUBEScope scope) throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(scope != null, new ResourceParameterException("Invalid parameter scope. null not allowed.")); + checker.validate(this.getID() != null, new ResourceOperationException("Invalid ID. null not allowed.")); + + System.out.println("DELETING TYPE: "+ this.getType()); + + GCUBEResource resStub = this.getGCUBEResource(scope); + List boundedScopes = this.validateScopes(resStub.getScopes().values().toArray(new GCUBEScope[]{})); + + ServerConsole.trace(LOG_PREFIX, "[DELETE] " + this.getType() + " " + this.getID() + " in scope [" + scope + "]"); + + ServerConsole.trace(LOG_PREFIX, "[DELETE] " + this.getType() + " " + this.getID() + " is bound to (" + boundedScopes.size() + ") scopes"); + + /* + * Removefromscope accetta un boolean piu la resource gia ottenuta. + * boolean per decidere se saltare fase due di update che la ripeto piu volte + * rispetto a qui che lo faccio una volta sola. + */ + for (GCUBEScope _scope : boundedScopes) { + // Removing from the children scopes + if (_scope.isEnclosedIn(scope) || scope.equals(_scope)) { + this.basicRemoveFromScope(_scope); + resStub.removeScope(_scope); + } + } + + // Phase 2 + // after the removal, the resource must be also updated in the other scopes it belongs to, + // otherwise it will wrongly report there a scope where actually it is not registered anymore. + // In order to do so, the just removed scope must be removed from the local + // GCUBEResource object by invoking the method GCUBEResource.removeScope() + // and then the resource must be updated in all the remaining scopes. + + + if (resStub.getScopes().isEmpty()) { + ServerConsole.trace(LOG_PREFIX, "[DELETE] [DELETE-BRANCH] deleting resource since no more scopes remained"); + try { + this.getISPublisher().removeGCUBEResource( + this.getID(), this.getType().name(), scope, this.getSecurityManager()); + } catch (Exception e) { + + } + } else { + boundedScopes = this.validateScopes(resStub.getScopes().values().toArray(new GCUBEScope[]{})); + for (GCUBEScope _scope : boundedScopes) { + try { + ServerConsole.trace(LOG_PREFIX, "[DELETE] [UPDATE-BRANCH]" + this.getType() + " " + this.getID() + " in scope [" + _scope + "]"); + this.getISPublisher().updateGCUBEResource(resStub, _scope, getSecurityManager()); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + } + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/resources/CollectionManager.java b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/CollectionManager.java new file mode 100644 index 0000000..9da9f56 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/CollectionManager.java @@ -0,0 +1,84 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: CollectionManager.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.resources; + +import java.io.StringReader; +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.resources.GCUBECollection; +import org.gcube.common.core.resources.GCUBEResource; +import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException; +import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException; +import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException; +import org.gcube.resourcemanagement.support.types.AllowedResourceTypes; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class CollectionManager extends AbstractResourceManager { + /** + * @deprecated discouraged use. With no ID some operations cannot be accessed. + */ + public CollectionManager() + throws ResourceParameterException, ResourceAccessException { + super(AllowedResourceTypes.Collection); + } + + public CollectionManager(final String id) + throws ResourceParameterException, ResourceAccessException { + super(id, AllowedResourceTypes.Collection); + } + + /** + * @param id + * @param name + * @throws ResourceParameterException + * @throws ResourceAccessException + */ + public CollectionManager(final String id, final String name) + throws ResourceParameterException, ResourceAccessException { + super(id, name, AllowedResourceTypes.Collection); + } + + /** + * @param id + * @param name + * @param subtype + * @throws ResourceParameterException + * @throws ResourceAccessException + */ + public CollectionManager(final String id, final String name, final String subtype) + throws ResourceParameterException, ResourceAccessException { + super(id, name, AllowedResourceTypes.Collection, subtype); + } + + /** + * {@inheritDoc} + */ + @Override + protected final GCUBEResource buildGCUBEResource(final String xmlRepresentation) + throws AbstractResourceException { + try { + GCUBECollection impl = GHNContext.getImplementation(GCUBECollection.class); + impl.load(new StringReader(xmlRepresentation)); + return impl; + } catch (Exception e) { + throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e); + } + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/resources/GHNManager.java b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/GHNManager.java new file mode 100644 index 0000000..193e69f --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/GHNManager.java @@ -0,0 +1,210 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: GHNManager.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.resources; + +import java.io.StringReader; + +import org.apache.axis.message.addressing.Address; +import org.apache.axis.message.addressing.EndpointReferenceType; +import org.gcube.common.core.contexts.GCUBERemotePortTypeContext; +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.resources.GCUBEHostingNode; +import org.gcube.common.core.resources.GCUBEResource; +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.common.vremanagement.ghnmanager.stubs.AddScopeInputParams; +import org.gcube.common.vremanagement.ghnmanager.stubs.GHNManagerPortType; +import org.gcube.common.vremanagement.ghnmanager.stubs.ShutdownOptions; +import org.gcube.common.vremanagement.ghnmanager.stubs.service.GHNManagerServiceAddressingLocator; +import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException; +import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException; +import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException; +import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException; +import org.gcube.resourcemanagement.support.types.AllowedResourceTypes; +import org.gcube.resourcemanagement.support.utils.Assertion; +import org.gcube.resourcemanagement.support.utils.ServerConsole; + + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class GHNManager extends AbstractResourceManager { + + private static final String LOG_PREFIX = "[GHN-MGR]"; + + + /** + * @deprecated discouraged use. With no ID some operations cannot be accessed. + */ + public GHNManager() + throws ResourceParameterException, ResourceAccessException { + super(AllowedResourceTypes.GHN); + } + + /** + * @param id the identifier of wrapper resource. + * @throws ResourceParameterException + * @throws ResourceAccessException + */ + public GHNManager(final String id) + throws ResourceParameterException, ResourceAccessException { + super(id, AllowedResourceTypes.GHN); + } + + /** + * @param id + * @param name + * @throws ResourceParameterException + * @throws ResourceAccessException + */ + public GHNManager(final String id, final String name) + throws ResourceParameterException, ResourceAccessException { + super(id, name, AllowedResourceTypes.GHN); + } + + /** + * @param id + * @param name + * @param subtype + * @throws ResourceParameterException + * @throws ResourceAccessException + */ + public GHNManager(final String id, final String name, final String subtype) + throws ResourceParameterException, ResourceAccessException { + super(id, name, AllowedResourceTypes.GHN, subtype); + } + + /** + * Add a scope to a gHN and the related Service Map that is not available on the gHN. + *

+ * Required information: + *
+ * The ID of the resource must be specified and valid.

+ * @param scope + * @param scopeMap + * @return the generated reportID + * @throws ResourceParameterException + * @throws ResourceOperationException + */ + public final String addToNewScope( + final GCUBEScope sourceScope, + final GCUBEScope targetScope, + final String scopeMap) + throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(sourceScope != null, new ResourceParameterException("Parameter sourceScope null not allowed.")); + checker.validate(targetScope != null, new ResourceParameterException("Parameter targetScope null not allowed.")); + checker.validate(scopeMap != null && scopeMap.trim().length() > 0, new ResourceParameterException("Invalid scopeMap parameter.")); + checker.validate(this.getID() != null, new ResourceOperationException("This operation cannot be applied to resources with no ID.")); + + if (!targetScope.isEnclosedIn(sourceScope)) { + throw new ResourceOperationException( + "You are not allowed to apply to this scope. Target scope is not enclosed in the source one."); + } + ServerConsole.trace( + LOG_PREFIX, + "Adding from scope " + + sourceScope.toString() + + "Adding to existing scope " + + targetScope.toString() + + " " + this.getType() + " " + this.getID()); + + AddScopeInputParams params = new AddScopeInputParams(); + params.setScope(sourceScope.toString()); + params.setMap(scopeMap.trim()); //eventually, set here the new Service Map + try { + this.getGHNManager(sourceScope).addScope(params); + } catch (Exception e) { + throw new ResourceOperationException( + "Failed to add the new scope to the gHN " + this.getID() + ": " + e.getMessage()); + } + return this.addToExistingScope(sourceScope, targetScope); + } + + /** + *

+ * Required information: + *
+ * The name of the resource must be specified and valid. It is used to retrieve the GHN manager URL.

+ * @param scope the scope in which the manager is bound. + * @return + * @throws AbstractResourceException + */ + public final GHNManagerPortType getGHNManager(final GCUBEScope scope) + throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(scope != null, new ResourceParameterException("Invalid scope")); + checker.validate(this.getName() != null, new ResourceOperationException("This operation cannot be applied to resources with no name.")); + + EndpointReferenceType endpoint = new EndpointReferenceType(); + try { + endpoint.setAddress(new Address("http://" + this.getName() + "/wsrf/services/gcube/common/vremanagement/GHNManager")); + GHNManagerServiceAddressingLocator locator = new GHNManagerServiceAddressingLocator(); + GHNManagerPortType pt = locator.getGHNManagerPortTypePort(endpoint); + pt = GCUBERemotePortTypeContext.getProxy(pt, scope, this.getSecurityManager()); + return pt; + } catch (Exception e) { + throw new ResourceAccessException(e.getMessage()); + } + } + + /** + * Implements all the three possible shutdown policies according to + * restart and clean parameters. + *

+ * Required information: + *
+ * The ID of the resource must be specified and valid.

+ * @param scope + * @param restart + * @param clean + * @throws ResourceOperationException + * @throws ResourceAccessException + * @throws ResourceParameterException + */ + public final void shutDown(final GCUBEScope scope, final boolean restart, final boolean clean) + throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(this.getID() != null, new ResourceAccessException("This operation cannot be applied to resources with no ID.")); + + GHNManagerPortType ghnManager = this.getGHNManager(scope); + + ServerConsole.trace(LOG_PREFIX, "Shutting down " + scope.toString() + " " + this.getType() + " " + this.getID()); + + ShutdownOptions options = new ShutdownOptions(); + options.setRestart(restart); + options.setClean(clean); + try { + ghnManager.shutdown(options); + } catch (Exception e) { + throw new ResourceOperationException("Cannot shutdown ghn: " + this.getID()); + } + } + + @Override + protected final GCUBEResource buildGCUBEResource(final String xmlRepresentation) + throws AbstractResourceException { + try { + GCUBEHostingNode impl = GHNContext.getImplementation(GCUBEHostingNode.class); + impl.load(new StringReader(xmlRepresentation)); + return impl; + } catch (Exception e) { + throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e); + } + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/resources/GenericResourceManager.java b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/GenericResourceManager.java new file mode 100644 index 0000000..faf04bd --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/GenericResourceManager.java @@ -0,0 +1,203 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: GenericResourceManager.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.resources; + +import java.io.StringReader; +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.informationsystem.publisher.ISPublisher; +import org.gcube.common.core.resources.GCUBEGenericResource; +import org.gcube.common.core.resources.GCUBEResource; +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException; +import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException; +import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException; +import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException; +import org.gcube.resourcemanagement.support.managers.scope.ScopeManager; +import org.gcube.resourcemanagement.support.types.AllowedResourceTypes; +import org.gcube.resourcemanagement.support.utils.Assertion; +import org.gcube.resourcemanagement.support.utils.ServerConsole; +import org.w3c.dom.Document; + + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class GenericResourceManager extends AbstractResourceManager { + // Used internally to require static functionalities (e.g. deploy). + private static GenericResourceManager singleton = null; + private static final String LOG_PREFIX = "[GenericResource-MGR]"; + + static { + if (GenericResourceManager.singleton == null) { + try { + GenericResourceManager.singleton = new GenericResourceManager("dummyservice", "dummyservice"); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + } + + /** + * @deprecated discouraged use. With no ID some operations cannot be accessed. + */ + public GenericResourceManager() + throws ResourceParameterException, + ResourceAccessException { + super(AllowedResourceTypes.GenericResource); + } + + public GenericResourceManager(final String id) + throws ResourceParameterException, + ResourceAccessException { + super(id, AllowedResourceTypes.GenericResource); + } + + /** + * @param id + * @param name + * @param type + * @throws ResourceParameterException + * @throws ResourceAccessException + */ + public GenericResourceManager(final String id, final String name) + throws ResourceParameterException, + ResourceAccessException { + super(id, name, AllowedResourceTypes.GenericResource); + } + + /** + * @param id + * @param name + * @param type + * @param subtype + * @throws ResourceParameterException + * @throws ResourceAccessException + */ + public GenericResourceManager(final String id, final String name, final String subtype) + throws ResourceParameterException, ResourceAccessException { + super(id, name, AllowedResourceTypes.GenericResource, subtype); + } + + /** + * Updates the resource. + * @param name (Mandatory) the name to assign to the resource + * @param description (optional) if null it will not be changed + * @param body (optional) if null it will not be changed + * @param subType (optional) if null it will not be changed + * @param scope (optional) if null it will not be changed + * @throws AbstractResourceException + */ + public final void update( + final String name, + final String description, + final String body, + final String subType, + final GCUBEScope scope) + throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(name != null && name.trim().length() != 0, new ResourceParameterException("Invalid field name. Null or empty value not allowed")); + + ServerConsole.trace(LOG_PREFIX, "[RES-UPDATE] updating resource " + this.getType() + " " + this.getID()); + + GCUBEGenericResource res = (GCUBEGenericResource) this.getGCUBEResource(scope); + res.setName(name.trim()); + if (description != null) { + res.setDescription(description.trim()); + } + if (body != null) { + res.setBody(body.trim()); + } + if (subType != null) { + res.setSecondaryType(subType.trim()); + } + try { + this.getISPublisher().updateGCUBEResource(res, scope, this.getSecurityManager()); + + /* FIXME old release + * List boundedScopes = this.validateScopes(res.getScopes().values().toArray(new GCUBEScope[]{})); + + + for (GCUBEScope _scope : boundedScopes) { + ServerConsole.trace(LOG_PREFIX, "[RES-UPDATE] ISPublisher updating resource " + this.getType() + " " + this.getID() + " in scope: [" + _scope + "]"); + this.getISPublisher().updateGCUBEResource(res, _scope, this.getSecurityManager()); + } + */ + } catch (Exception e) { + throw new ResourceOperationException(e.getMessage()); + } + } + + /** + * Creates a Generic Resource and returns the ID given by the + * resource manager at creation phase. + * @return the id assigned to the newly created resource + */ + public static final synchronized String create( + final String ID, + final GCUBEScope scope, + final String name, + final String description, + final String body, + final String subType) + throws Exception { + Assertion checker = new Assertion(); + checker.validate(name != null && name.trim().length() != 0, new ResourceParameterException("Invalid field name. Null or empty value not allowed")); + checker.validate(subType != null && subType.trim().length() != 0, new ResourceParameterException("Invalid field subType. Null or empty value not allowed")); + + GCUBEGenericResource resource = GHNContext.getImplementation(GCUBEGenericResource.class); + if (ID != null) { + resource.setID(ID); + } + resource.setName(name.trim()); + if (description != null) { + resource.setDescription(description.trim()); + } + if (body != null) { + resource.setBody(body.trim()); + } + resource.setSecondaryType(subType.trim()); + resource.addScope(scope); + + GenericResourceManager gm = new GenericResourceManager(); + ISPublisher publisher = gm.getISPublisher(); + String retval = publisher.registerGCUBEResource(resource, scope, gm.getSecurityManager()); + + if (retval == null || retval.length() == 0) { + throw new Exception("The GenericResource has not been created"); + } + + Document doc = ScopeManager.getDocumentGivenXML(retval); + String id = doc.getElementsByTagName("ID").item(0).getFirstChild().getNodeValue(); + + ServerConsole.info(LOG_PREFIX, "Resource Created with ID: " + id); + return id; + } + + @Override + protected final GCUBEResource buildGCUBEResource( + final String xmlRepresentation) throws AbstractResourceException { + try { + GCUBEGenericResource impl = GHNContext.getImplementation(GCUBEGenericResource.class); + impl.load(new StringReader(xmlRepresentation)); + return impl; + } catch (Exception e) { + throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e); + } + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/resources/ManagementUtils.java b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/ManagementUtils.java new file mode 100644 index 0000000..2f17b88 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/ManagementUtils.java @@ -0,0 +1,340 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ManagementUtils.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.resources; + +import java.io.File; +import java.rmi.RemoteException; +import java.util.List; +import java.util.Vector; + +import org.gcube.common.core.faults.GCUBEFault; +import org.gcube.common.core.informationsystem.client.AtomicCondition; +import org.gcube.common.core.informationsystem.client.queries.GCUBEServiceQuery; +import org.gcube.common.core.resources.GCUBEResource; +import org.gcube.common.core.resources.GCUBEService; +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.common.core.scope.GCUBEScope.Type; +import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException; +import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException; +import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException; +import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException; +import org.gcube.resourcemanagement.support.managers.scope.ScopeManager; +import org.gcube.resourcemanagement.support.types.AllowedResourceTypes; +import org.gcube.resourcemanagement.support.utils.Assertion; +import org.gcube.resourcemanagement.support.utils.ServerConsole; +import org.gcube.vremanagement.resourcemanager.stubs.binder.AddResourcesParameters; +import org.gcube.vremanagement.resourcemanager.stubs.binder.PackageItem; +import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceBinderPortType; +import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceItem; +import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceList; +import org.gcube.vremanagement.resourcemanager.stubs.binder.SoftwareList; + +/** + * A support class containing operations to manage multiple resources. + * Here are provided the functionalities to delete/addToScope/deploy + * groups of homogeneous resources. + * @author Daniele Strollo (ISTI-CNR) + */ +public class ManagementUtils { + private static final String LOG_PREFIX = "[MANAGEMENT-UTILS]"; + + /** + * Applies the add to scope to multiple resources having the same type. + * @param type + * @param resources + * @param sourceScope + * @param targetScope + * @return the generated report ID + */ + public static final synchronized String addToExistingScope( + final AllowedResourceTypes type, + final String[] resourceIDs, + final GCUBEScope sourceScope, + final GCUBEScope targetScope) + throws Exception { + ServerConsole.trace( + LOG_PREFIX, + "[ADD-ToExistingScope] Adding from scope [" + + sourceScope.toString() + + "] to existing scope [" + + targetScope.toString() + + "] resources having type " + type.name()); + + // 1 - If not RI or GHN and the scopes are sibling and VO copyFromToVO + if (!(type == AllowedResourceTypes.GHN) && + !(type == AllowedResourceTypes.RunningInstance) && + sourceScope.getType() == Type.VO && targetScope.getType() == Type.VO) { + // Phase 1. retrieve the resource to copy + //GCUBEResource resStub = this.getGCUBEResource(sourceScope); + + // Phase 2. Before to register the resource, the scope must be + // bound to the local GCUBEResource + String retval = bindToScope(type, resourceIDs, targetScope); + + // Phase 3. Register to the new VO through the ISPublisher + // applies a copy of old descriptors bound in other scopes. + try { + for (String id : resourceIDs) { + AbstractResourceManager res = ResourceFactory.createResourceManager(type, id); + GCUBEResource resStub = res.getGCUBEResource(sourceScope); + res.getISPublisher().registerGCUBEResource(resStub, targetScope, res.getSecurityManager()); + } + } catch (Exception e) { + throw new ResourceAccessException(e.getMessage()); + } + return retval; + } + + // Add a gCube Resource to + // (i) a VRE scope from the parent VO or + // (ii) a VO scope from the infrastructure scope + if (!targetScope.isEnclosedIn(sourceScope)) { + throw new ResourceOperationException( + "You are not allowed to apply to this scope. Target scope is not enclosed in the source one."); + } + + // 2 - Applies the normal scope binding + return bindToScope(type, resourceIDs, targetScope); + } + + /** + * Applies the add to scope to multiple resources having the same type. + * @param type + * @param resources + * @param sourceScope + * @param targetScope + * @return the generated report ID + */ + public static final synchronized String removeFromExistingScope(final AllowedResourceTypes type, final String[] resourceIDs, + final GCUBEScope sourceScope, final GCUBEScope targetScope) throws Exception { + + ServerConsole.trace( + LOG_PREFIX, + "[REMOVE-FromExistingScope] Removing scope [" + + sourceScope.toString() + + "] to existing scope [" + + targetScope.toString() + + "] resources having type " + type.name()); + // cannot remove a Scope if its the same + if (targetScope.toString().compareTo(sourceScope.toString()) == 0) { + return "You are not allowed to remove this scope. Current and Target scope are the same."; + } + AbstractResourceManager resource = ResourceFactory.createResourceManager(type); + for (String id : resourceIDs) { + try { + resource.setID(id); + resource.delete(targetScope); + } catch (AbstractResourceException e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + + return "ACK"; + } + + /** + * + * @param type + * @param resourceIDs + * @param targetScope + * @return the ID of generated report + * @throws AbstractResourceException + */ + private static synchronized String bindToScope( + final AllowedResourceTypes type, + final String[] resourceIDs, + final GCUBEScope targetScope) + throws AbstractResourceException { + AddResourcesParameters addParam = new AddResourcesParameters(); + ResourceBinderPortType manager = ResourceFactory.createResourceManager(type).getResourceManager(targetScope); + List resToBind = new Vector(); + + for (String id : resourceIDs) { + ResourceItem toAdd = new ResourceItem(); + toAdd.setID(id); + toAdd.setType(type.name()); + resToBind.add(toAdd); + } + ResourceList r = new ResourceList(); + r.setResource(resToBind.toArray(new ResourceItem[]{})); + addParam.setResources(r); + addParam.setTargetScope(targetScope.toString()); + + try { + String reportID = manager.addResources(addParam); + + ServerConsole.trace( + LOG_PREFIX, + "[BIND-SCOPE-EXIT] Applyed Adding of resources " + type.name() + " to scope [" + + targetScope.toString() + "]... reportID: " + reportID); + + return reportID; + } catch (Exception e) { + ServerConsole.trace( + LOG_PREFIX, + "[BIND-SCOPE-EXIT] [FAILURE]"); + throw new ResourceOperationException("During resource::addToScope: " + e.getMessage()); + } + } + + public static synchronized void delete( + final AllowedResourceTypes type, + final String[] resourceIDs, + final GCUBEScope scope) + throws AbstractResourceException { + AbstractResourceManager resource = ResourceFactory.createResourceManager(type); + for (String id : resourceIDs) { + try { + resource.setID(id); + resource.delete(scope); + } catch (AbstractResourceException e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + } + + /** + * Makes the deployment of software on a list of ghns. + * @param ghnsID + * @param servicesID + * @return the generated report ID + * @throws Exception + */ + public static final synchronized String deploy( + final GCUBEScope scope, + final String[] ghnsID, + final String[] servicesID) + throws Exception { + Assertion checker = new Assertion(); + checker.validate(ghnsID != null && ghnsID.length != 0, new ResourceParameterException("Invalid ghnsID parameter. It cannot be null or empty.")); + checker.validate(servicesID != null && servicesID.length != 0, new ResourceParameterException("Invalid servicesID parameter. It cannot be null or empty.")); + checker.validate(scope != null, new Exception("Cannot retrieve the scope.")); + + AbstractResourceManager resource = ResourceFactory.createResourceManager(AllowedResourceTypes.Service); + ResourceBinderPortType manager = ResourceFactory.createResourceManager(AllowedResourceTypes.Service).getResourceManager(scope); + + System.out.println("\n\n**** These are the service ids to deploy on SCOPE " + scope); + for (String sid : servicesID) { + System.out.println(sid); + } + System.out.println("\n\n**** These are the gHNs ids to deploy on SCOPE " + scope); + for (String ghn : ghnsID) { + System.out.println(ghn); + } + + List serviceProfiles = new Vector(); + // Retrieves the profiles of services + final GCUBEServiceQuery query = resource.getISClient().getQuery(GCUBEServiceQuery.class); + prepareServices: for (String serviceID : servicesID) { + System.out.println("\n\n**** Query the IsClient to get the profile"); + query.clearConditions(); + query.addAtomicConditions(new AtomicCondition("/ID", serviceID)); + + System.out.println("**** Query : " + query.getExpression()); + + List results = resource.getISClient().execute(query, scope); + + System.out.println("**** results received : " + results.size()); + + GCUBEService ret = null; + if (results != null && results.size() > 0) { + ret = results.get(0); + } else { + continue prepareServices; + } + + if (ret == null || + ret.getServiceClass() == null || + ret.getServiceName() == null || + ret.getVersion() == null) { + ServerConsole.error(LOG_PREFIX, "found an invalid service profile"); + continue; + } + + PackageItem toAdd = new PackageItem(); + + toAdd.setServiceClass(ret.getServiceClass()); + toAdd.setServiceName(ret.getServiceName()); + toAdd.setServiceVersion(ret.getVersion()); + if (ret.getPackages().size() == 1) { + toAdd.setPackageName(ret.getPackages().get(0).getName()); + toAdd.setPackageVersion(ret.getPackages().get(0).getVersion()); + } else { + for (org.gcube.common.core.resources.service.Package p : ret.getPackages()) { + if (p.getClass().isAssignableFrom(org.gcube.common.core.resources.service.MainPackage.class)) { + toAdd.setPackageName(p.getName()); + toAdd.setPackageVersion(p.getVersion()); + break; + } + } + } + + serviceProfiles.add(toAdd); + } + + SoftwareList serviceList = new SoftwareList(); + serviceList.setSuggestedTargetGHNNames(ghnsID); + serviceList.setSoftware(serviceProfiles.toArray(new PackageItem[0])); + + AddResourcesParameters addResourcesParameters = new AddResourcesParameters(); + addResourcesParameters.setSoftware(serviceList); + addResourcesParameters.setTargetScope(scope.toString()); + + System.out.println("\n\n**** These is the ServiceList i pass to ResourceManagerPortType: "); + for (int i = 0; i < serviceList.getSoftware().length; i++) { + System.out.println(serviceList.getSoftware()[i]); + } + + String id = ""; + try { + id = manager.addResources(addResourcesParameters); + ServerConsole.debug(LOG_PREFIX, "Report ID = " + id); + } catch (GCUBEFault e) { + ServerConsole.error(LOG_PREFIX, "during deployment.", e); + throw e; + } catch (RemoteException e) { + ServerConsole.error(LOG_PREFIX, "during deployment.", e); + throw e; + } + System.out.println("Returning.... no exceptions"); + return id; + } + + public static final void main(final String[] args) { + List ids = new Vector(); + ids.add(null); + ids.add("id2"); + ids.add(null); + + ScopeManager.setScopeConfigFile("test-suite" + File.separator + "scopes" + File.separator + "scopedata.xml"); + + try { + + + ManagementUtils.delete(AllowedResourceTypes.GenericResource, + new String[]{ + "3f7384a0-d51c-11df-80cc-ece35605c26c", + "975419b0-d2e1-11df-b0ed-f8e6e669b8ad", + null, + "test" + }, + ScopeManager.getScope("/gcube/devsec/devVRE")); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/resources/ResourceFactory.java b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/ResourceFactory.java new file mode 100644 index 0000000..b5716e8 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/ResourceFactory.java @@ -0,0 +1,71 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ResourceFactory.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.resources; + +import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException; +import org.gcube.resourcemanagement.support.types.AllowedResourceTypes; +import org.gcube.resourcemanagement.support.utils.ServerConsole; + +/** + * For lazy developers here given the facilities to instantiate the proper + * resource manager of a resource given its identifier and type. + * @author Daniele Strollo (ISTI-CNR) + */ +public class ResourceFactory { + private static final String LOG_PREFIX = "[RES-MGR-FACTORY]"; + + public static final AbstractResourceManager createResourceManager(final AllowedResourceTypes type) + throws ResourceOperationException { + return createResourceManager(type, null); + } + + /** + * + * @param type + * @param id if null the default constructor (empty params) will be used + * @return + * @throws ResourceOperationException + */ + public static final AbstractResourceManager createResourceManager(final AllowedResourceTypes type, final String id) + throws ResourceOperationException { + ServerConsole.info(LOG_PREFIX, "ResourceFactory building the " + type.name() + "Manager"); + String classToLoad = + // It is supposed that the other classes are in this package + ResourceFactory.class.getPackage().getName() + "." + + type.name() + "Manager"; + try { + if (id == null) { + return (AbstractResourceManager) Class.forName(classToLoad).newInstance(); + } + return (AbstractResourceManager) Class.forName(classToLoad).getConstructor(String.class).newInstance(id); + } catch (Exception e) { + throw new ResourceOperationException(e); + } + } + + public static final void main(final String[] args) throws Exception { + for (AllowedResourceTypes res : AllowedResourceTypes.values()) { + try { + ServerConsole.info(LOG_PREFIX, + "Loaded " + res.name() + " with ID: " + + ResourceFactory.createResourceManager(res, "HelloID").getID()); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, "[ERR] Failed to load: " + res.name()); + } + } + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/resources/RunningInstanceManager.java b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/RunningInstanceManager.java new file mode 100644 index 0000000..b3407df --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/RunningInstanceManager.java @@ -0,0 +1,215 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: RunningInstanceManager.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.resources; + +import java.io.StringReader; +import java.util.List; +import java.util.Vector; + +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.informationsystem.client.AtomicCondition; +import org.gcube.common.core.informationsystem.client.queries.GCUBEServiceQuery; +import org.gcube.common.core.resources.GCUBEResource; +import org.gcube.common.core.resources.GCUBERunningInstance; +import org.gcube.common.core.resources.GCUBEService; +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException; +import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException; +import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException; +import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException; +import org.gcube.resourcemanagement.support.types.AllowedResourceTypes; +import org.gcube.resourcemanagement.support.utils.Assertion; +import org.gcube.resourcemanagement.support.utils.ServerConsole; +import org.gcube.vremanagement.resourcemanager.stubs.binder.AddResourcesParameters; +import org.gcube.vremanagement.resourcemanager.stubs.binder.PackageItem; +import org.gcube.vremanagement.resourcemanager.stubs.binder.RemoveResourcesParameters; +import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceBinderPortType; +import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceItem; +import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceList; +import org.gcube.vremanagement.resourcemanager.stubs.binder.SoftwareList; +import org.gcube.vremanagement.resourcemanager.stubs.reporting.ReportingPortType; + + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class RunningInstanceManager extends AbstractResourceManager { + // Used internally to require static functionalities (e.g. deploy). + private static final String LOG_PREFIX = "[RI-MGR]"; + + /** + * @deprecated discouraged use. With no ID some operations cannot be accessed. + */ + public RunningInstanceManager() + throws ResourceParameterException, ResourceAccessException { + super(AllowedResourceTypes.RunningInstance); + } + + public RunningInstanceManager(final String id) + throws ResourceParameterException, ResourceAccessException { + super(id, AllowedResourceTypes.RunningInstance); + } + + public RunningInstanceManager(final String id, final String name) + throws ResourceParameterException, ResourceAccessException { + super(id, name, AllowedResourceTypes.RunningInstance); + } + + public RunningInstanceManager(final String id, final String name, final String subType) + throws ResourceParameterException, ResourceAccessException { + super(id, name, AllowedResourceTypes.RunningInstance, subType); + } + + public final String deploy( + final GCUBEScope scope, final String[] ghnsID, final String[] servicesID) + throws ResourceParameterException, ResourceOperationException { + Assertion checker = new Assertion(); + checker.validate(servicesID != null && servicesID.length != 0, + new ResourceParameterException("Invalid servicesID parameter. It cannot be null or empty.")); + checker.validate(scope != null, + new ResourceParameterException("Cannot retrieve the scope.")); + + List serviceProfiles = new Vector(); + + try { + // Retrieves the profiles of services + final GCUBEServiceQuery query = this.getISClient().getQuery(GCUBEServiceQuery.class); + prepareServices: for (String serviceID : servicesID) { + query.addAtomicConditions(new AtomicCondition("/ID", serviceID)); + List results = this.getISClient().execute(query, scope); + GCUBEService ret = null; + if (results != null && results.size() > 0) { + ret = results.get(0); + } else { + continue prepareServices; + } + + if (ret == null || + ret.getServiceClass() == null || + ret.getServiceName() == null || + ret.getVersion() == null) { + ServerConsole.error(LOG_PREFIX, "found an invalid service profile"); + continue; + } + + PackageItem toAdd = new PackageItem(); + + toAdd.setServiceClass(ret.getServiceClass()); + toAdd.setServiceName(ret.getServiceName()); + toAdd.setServiceVersion(ret.getVersion()); + if (ret.getPackages().size() == 1) { + toAdd.setPackageName(ret.getPackages().get(0).getName()); + toAdd.setPackageVersion(ret.getPackages().get(0).getVersion()); + } else { + for (org.gcube.common.core.resources.service.Package p : ret.getPackages()) { + if (p.getClass().isAssignableFrom(org.gcube.common.core.resources.service.MainPackage.class)) { + toAdd.setPackageName(p.getName()); + toAdd.setPackageVersion(p.getVersion()); + break; + } + } + } + + serviceProfiles.add(toAdd); + } + + SoftwareList serviceList = new SoftwareList(); + + // The GHNs are optional, suggested gHNs to use. + if (ghnsID != null && ghnsID.length > 0) { + serviceList.setSuggestedTargetGHNNames(ghnsID); + } + + serviceList.setSoftware(serviceProfiles.toArray(new PackageItem[0])); + + AddResourcesParameters addResourcesParameters = new AddResourcesParameters(); + addResourcesParameters.setSoftware(serviceList); + addResourcesParameters.setTargetScope(scope.toString()); + + String reportID = this.getResourceManager(scope).addResources(addResourcesParameters); + ServerConsole.debug(LOG_PREFIX, "Report ID = " + reportID); + return reportID; + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, "Error during deployment.", e); + throw new ResourceOperationException("Software deployment failure: " + e.getMessage()); + } + } + + public final String undeploy( + final GCUBEScope scope) + throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(scope != null, + new ResourceParameterException("Cannot retrieve the scope.")); + checker.validate(this.getID() != null, + new ResourceOperationException("Invalid ID.")); + + try { + ResourceBinderPortType rm = this.getResourceManager(scope); + //prepare the parameters + RemoveResourcesParameters params = new RemoveResourcesParameters(); + ResourceItem[] resourcelist = new ResourceItem[1]; + resourcelist[0] = new ResourceItem(); + resourcelist[0].setID(this.getID()); + resourcelist[0].setType(this.getType().name()); + ResourceList r = new ResourceList(); + r.setResource(resourcelist); + params.setResources(r); + params.setTargetScope(scope.toString()); + + //sending the request + ServerConsole.info(LOG_PREFIX, "Sending the Remove Resource request...."); + String reportID = rm.removeResources(params); + ServerConsole.info(LOG_PREFIX, "Returned report ID: " + reportID); + return reportID; + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, "Error during deployment.", e); + throw new ResourceOperationException("Software deployment failure: " + e.getMessage()); + } + } + + public final String checkDeployStatus(final GCUBEScope scope, final String deployID) + throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(scope != null, + new ResourceParameterException("Invalid scope passed")); + checker.validate(deployID != null && deployID.trim().length() > 0, + new ResourceParameterException("Invalid reportID passed")); + + ReportingPortType vreManagerPortType = this.getReportResourceManager(scope); + + try { + return vreManagerPortType.getReport(deployID); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + throw new ResourceOperationException("Cannot retrieve the report: " + deployID + " " + e.getMessage()); + } + } + + @Override + protected final GCUBEResource buildGCUBEResource(final String xmlRepresentation) + throws AbstractResourceException { + try { + GCUBERunningInstance impl = GHNContext.getImplementation(GCUBERunningInstance.class); + impl.load(new StringReader(xmlRepresentation)); + return impl; + } catch (Exception e) { + throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e); + } + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/resources/RuntimeResourceManager.java b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/RuntimeResourceManager.java new file mode 100644 index 0000000..5c1a32c --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/RuntimeResourceManager.java @@ -0,0 +1,109 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: RuntimeResourceManager.java + **************************************************************************** + * @author Massimiliano Assante + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.resources; + +import java.io.StringReader; + +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.resources.GCUBEResource; +import org.gcube.common.core.resources.GCUBERuntimeResource; +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException; +import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException; +import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException; +import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException; +import org.gcube.resourcemanagement.support.types.AllowedResourceTypes; +import org.gcube.resourcemanagement.support.utils.Assertion; +import org.gcube.resourcemanagement.support.utils.ServerConsole; +import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceBinderPortType; +import org.gcube.vremanagement.resourcemanager.stubs.reporting.ReportingPortType; + + + +/** + * @author Massimiliano Assante (ISTI-CNR) + * + */ +public class RuntimeResourceManager extends AbstractResourceManager { + // Used internally to RuntimeResourceManager static functionalities (e.g. deploy). + private static RuntimeResourceManager singleton = null; + private static final String LOG_PREFIX = "[RR-MGR]"; + + static { + if (RuntimeResourceManager.singleton == null) { + try { + RuntimeResourceManager.singleton = new RuntimeResourceManager("dummyservice", "dummyservice"); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + } + + /** + * @deprecated discouraged use. With no ID some operations cannot be accessed. + */ + public RuntimeResourceManager() + throws ResourceParameterException, ResourceAccessException { + super(AllowedResourceTypes.RuntimeResource); + } + + public RuntimeResourceManager(final String id) + throws ResourceParameterException, ResourceAccessException { + super(id, AllowedResourceTypes.RuntimeResource); + } + + public RuntimeResourceManager(final String id, final String name) + throws ResourceParameterException, ResourceAccessException { + super(id, name, AllowedResourceTypes.RuntimeResource); + } + + public RuntimeResourceManager(final String id, final String name, final String subType) + throws ResourceParameterException, ResourceAccessException { + super(id, name, AllowedResourceTypes.RuntimeResource, subType); + } + + + public final String checkDeployStatus(final GCUBEScope scope, final String deployID) + throws AbstractResourceException { + Assertion checker = new Assertion(); + checker.validate(scope != null, + new ResourceParameterException("Invalid scope passed")); + checker.validate(deployID != null && deployID.trim().length() > 0, + new ResourceParameterException("Invalid reportID passed")); + + ReportingPortType vreManagerPortType = this.getReportResourceManager(scope); + + try { + return vreManagerPortType.getReport(deployID); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + throw new ResourceOperationException("Cannot retrieve the report: " + deployID + " " + e.getMessage()); + } + } + + @Override + protected final GCUBEResource buildGCUBEResource(final String xmlRepresentation) + throws AbstractResourceException { + try { + GCUBERuntimeResource impl = GHNContext.getImplementation(GCUBERuntimeResource.class); + impl.load(new StringReader(xmlRepresentation)); + return impl; + } catch (Exception e) { + throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e); + } + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/resources/ServiceManager.java b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/ServiceManager.java new file mode 100644 index 0000000..fd6bc4b --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/ServiceManager.java @@ -0,0 +1,83 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ServiceManager.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.resources; + +import java.io.StringReader; +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.resources.GCUBEResource; +import org.gcube.common.core.resources.GCUBEService; +import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException; +import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException; +import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException; +import org.gcube.resourcemanagement.support.types.AllowedResourceTypes; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class ServiceManager extends AbstractResourceManager { + + /** + * @deprecated discouraged use. With no ID some operations cannot be accessed. + */ + public ServiceManager() throws ResourceParameterException, ResourceAccessException { + super(AllowedResourceTypes.Service); + } + + public ServiceManager(final String id) + throws ResourceParameterException, ResourceAccessException { + super(id, AllowedResourceTypes.Service); + } + + /** + * @param id + * @param name + * @param type + * @throws ResourceParameterException + * @throws ResourceAccessException + */ + public ServiceManager(final String id, final String name) + throws ResourceParameterException, ResourceAccessException { + super(id, name, AllowedResourceTypes.Service); + } + + /** + * @param id + * @param name + * @param type + * @param subtype + * @throws ResourceParameterException + * @throws ResourceAccessException + */ + public ServiceManager(final String id, final String name, final String subtype) + throws ResourceParameterException, + ResourceAccessException { + super(id, name, AllowedResourceTypes.Service, subtype); + } + + @Override + protected final GCUBEResource buildGCUBEResource(final String xmlRepresentation) + throws AbstractResourceException { + try { + GCUBEService impl = GHNContext.getImplementation(GCUBEService.class); + impl.load(new StringReader(xmlRepresentation)); + return impl; + } catch (Exception e) { + throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e); + } + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/resources/ViewManager.java b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/ViewManager.java new file mode 100644 index 0000000..b90d183 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/resources/ViewManager.java @@ -0,0 +1,86 @@ +/**************************************************************************** + * This software is part of the gCube System. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: MetadataCollectionManager.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.resources; + +import java.io.StringReader; +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.resources.GCUBEMCollection; +import org.gcube.common.core.resources.GCUBEResource; +import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException; +import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException; +import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException; +import org.gcube.resourcemanagement.support.types.AllowedResourceTypes; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class ViewManager extends AbstractResourceManager { + + /** + * @deprecated discouraged use. With no ID some operations cannot be accessed. + */ + public ViewManager() + throws ResourceParameterException, + ResourceAccessException { + super(AllowedResourceTypes.VIEW); + } + + public ViewManager(final String id) + throws ResourceParameterException, + ResourceAccessException { + super(id, AllowedResourceTypes.VIEW); + } + + /** + * @param id + * @param name + * @param type + * @throws ResourceParameterException + * @throws ResourceAccessException + */ + public ViewManager(final String id, final String name) + throws ResourceParameterException, + ResourceAccessException { + super(id, name, AllowedResourceTypes.VIEW); + } + + /** + * @param id + * @param name + * @param type + * @param subtype + * @throws ResourceParameterException + * @throws ResourceAccessException + */ + public ViewManager(final String id, final String name, final String subtype) + throws ResourceParameterException, ResourceAccessException { + super(id, name, AllowedResourceTypes.VIEW, subtype); + } + + @Override + protected final GCUBEResource buildGCUBEResource(final String xmlRepresentation) + throws AbstractResourceException { + try { + GCUBEMCollection impl = GHNContext.getImplementation(GCUBEMCollection.class); + impl.load(new StringReader(xmlRepresentation)); + return impl; + } catch (Exception e) { + throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e); + } + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/scope/ScopeManager.java b/src/main/java/org/gcube/resourcemanagement/support/managers/scope/ScopeManager.java new file mode 100644 index 0000000..9bd087e --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/scope/ScopeManager.java @@ -0,0 +1,236 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ScopeManager.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.scope; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.informationsystem.ISException; +import org.gcube.common.core.informationsystem.client.AtomicCondition; +import org.gcube.common.core.informationsystem.client.ISClient; +import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericResourceQuery; +import org.gcube.common.core.resources.GCUBEGenericResource; +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.common.core.scope.ServiceMap; +import org.gcube.common.core.scope.VO; +import org.gcube.common.core.scope.VRE; +import org.gcube.resourcemanagement.support.utils.ServerConsole; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +/** + * Scopes and their maps are persisted in this structure. + * + * @author Massimiliano Assante (ISTI-CNR) + * @author Daniele Strollo + * + */ +public class ScopeManager { + private static final Map SCOPES = new LinkedHashMap(); + private static String confFile = null; + private static final String LOG_PREFIX = "[SCOPE-MGR]"; + + public static GCUBEScope getScope(final String scope) throws Exception { + synchronized (SCOPES) { + if (getAvailableScopes().containsKey(scope)) { + return getAvailableScopes().get(scope); + } + } + ServerConsole.warn(LOG_PREFIX, "Using DEFAULT scope manager"); + return GCUBEScope.getScope(scope); + } + + public static void setScopeConfigFile(final String file) { + confFile = file; + } + + /** + * Refreshes the list of scopes and associated maps. + */ + + public static Map getAvailableScopes() + throws Exception { + if (SCOPES.size() == 0) { + update(); + } + return SCOPES; + } + + public static void clear() { + SCOPES.clear(); + } + + public static void update() + throws Exception { + if (confFile == null) { + throw new NullPointerException("the scope file has not been defined"); + } + String scopeXML = fileToString(confFile); + ServerConsole.info(LOG_PREFIX, "Starting retrieving scopes.."); + Document scopeDocument = getDocumentGivenXML(scopeXML); + NodeList voElements = scopeDocument.getElementsByTagName("vo"); + for (int i = 0; i < voElements.getLength(); i++) { + NodeList voDetails = voElements.item(i).getChildNodes(); + String voString = voDetails.item(5).getFirstChild().getNodeValue(); + String fileName = voDetails.item(3).getFirstChild().getNodeValue(); + // String voName = voDetails.item(1).getFirstChild().getNodeValue(); + GCUBEScope vo = GCUBEScope.getScope(voString); + + try { + vo.setServiceMap(loadServiceMap((VO) vo, fileName)); + SCOPES.put(vo.toString(), vo); + + ServerConsole.info(LOG_PREFIX, " Scopes in VO " + vo.toString()); + + try { + for (VRE vre : getVREFromVO((VO) vo)) { + // This operation overrides the vo map + vre.getEnclosingScope().setServiceMap(vo.getServiceMap()); + SCOPES.put(vre.toString(), vre); + } + } catch (ISException e) { + ServerConsole.error(LOG_PREFIX, "Exception raised while loading VREs for VO : " + vo, e); + } + } + catch (FileNotFoundException e) { + ServerConsole.warn(LOG_PREFIX, "skipping... map not found for VO : " + vo, e); + } + + + } + ServerConsole.info(LOG_PREFIX, "*** found scopes : " + SCOPES.keySet()); + } + + protected static List getVREFromVO(final VO vo) + throws Exception { + ServerConsole.info(LOG_PREFIX, "*******************\n\n********************\nStarting Retrieving VREs for VO : " + vo); + List toReturn = new ArrayList(); + + ISClient client = GHNContext.getImplementation(ISClient.class); + + // FIXME query to get VREs + try { + GCUBEGenericResourceQuery query = client.getQuery(GCUBEGenericResourceQuery.class); + query.addAtomicConditions(new AtomicCondition("/Profile/SecondaryType", "VRE")); + //query.addGenericCondition("not($result/Scopes/Scope/string() eq '" + vo.toString() + "')"); + System.out.println( "************** \n\n\n *****************\nready to query : " + query.getExpression()); + + for (GCUBEGenericResource resource : client.execute(query, vo)) { + ServerConsole.info(LOG_PREFIX, "Found: " + resource.getName()); + for (String vreName : resource.getScopes().keySet()) { + GCUBEScope vre = resource.getScopes().get(vreName); + if (vre.getType().equals(GCUBEScope.Type.VRE)) { + toReturn.add((VRE) vre); + } + } + + } + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + + return toReturn; + + } + + /** + * Given a scope, if it is a VO or an infrastructure, the corresponding + * map file is retrieved from the globus location and the contained xml + * representation is returned. + * @param searchvo + * @return + * @throws Exception + */ + public static String getMapXML(final GCUBEScope searchvo) throws Exception { + if (confFile == null) { + throw new NullPointerException("the scope file has not been defined"); + } + String scopeXML = fileToString(confFile); + ServerConsole.info(LOG_PREFIX, "Starting retrieving scopes.."); + Document scopeDocument = getDocumentGivenXML(scopeXML); + NodeList voElements = scopeDocument.getElementsByTagName("vo"); + for (int i = 0; i < voElements.getLength(); i++) { + NodeList voDetails = voElements.item(i).getChildNodes(); + String voString = voDetails.item(5).getFirstChild().getNodeValue(); + String fileName = voDetails.item(3).getFirstChild().getNodeValue(); + // String voName = voDetails.item(1).getFirstChild().getNodeValue(); + GCUBEScope vo = GCUBEScope.getScope(voString); + + if (vo.equals(searchvo)) { + return fileToString(System.getenv("GLOBUS_LOCATION") + File.separator + "config" + File.separator + fileName); + } + } + ServerConsole.error(LOG_PREFIX, "*** maps for " + searchvo + " not found"); + return null; + } + + private static ServiceMap loadServiceMap(final VO vo, final String fileName) throws Exception { + ServiceMap map = new ServiceMap(); + String filePath = System.getenv("GLOBUS_LOCATION") + File.separator + "config" + File.separator + fileName; + ServerConsole.info(LOG_PREFIX, "--- Loading " + vo.getName() + " from: " + filePath); + map.load(new FileReader(filePath)); + return map; + } + + public static String fileToString(final String path) throws IOException { + BufferedReader filebuf = null; + String nextStr = null; + StringBuilder ret = new StringBuilder(); + + filebuf = new BufferedReader(new FileReader(path)); + nextStr = filebuf.readLine(); // legge una riga dal file + while (nextStr != null) { + ret.append(nextStr); + nextStr = filebuf.readLine(); // legge la prossima riga + } + filebuf.close(); // chiude il file + + return ret.toString(); + } + + public static Document getDocumentGivenXML(final String result) { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setValidating(false); + DocumentBuilder db; + Document document = null; + try { + db = dbf.newDocumentBuilder(); + document = db.parse(new ByteArrayInputStream(result.getBytes())); + } catch (ParserConfigurationException e1) { + e1.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return document; + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/services/SWRepositoryManager.java b/src/main/java/org/gcube/resourcemanagement/support/managers/services/SWRepositoryManager.java new file mode 100644 index 0000000..9a4ea7d --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/services/SWRepositoryManager.java @@ -0,0 +1,187 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: SWRepositoryManager.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.services; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Vector; +import org.apache.axis.message.addressing.EndpointReferenceType; +import org.gcube.common.core.contexts.GCUBERemotePortTypeContext; +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.informationsystem.client.AtomicCondition; +import org.gcube.common.core.informationsystem.client.ISClient; +import org.gcube.common.core.informationsystem.client.queries.GCUBERIQuery; +import org.gcube.common.core.resources.GCUBERunningInstance; +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.common.core.security.GCUBESecurityManagerImpl; +import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException; +import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException; +import org.gcube.resourcemanagement.support.utils.Assertion; +import org.gcube.resourcemanagement.support.utils.ServerConsole; +import org.gcube.vremanagement.softwarerepository.stubs.SoftwareRepositoryPortType; +import org.gcube.vremanagement.softwarerepository.stubs.service.SoftwareRepositoryServiceAddressingLocator; + +/** + * A support library to handle and retrieve remote software + * repository manager. + *
+ * Useful to handle the proxy for repository manager. + *
Usage: + *
+ *  SWRepositoryManager repMgr = new SWRepositoryManager();
+ *  // if needed sets the time out for socket connection
+ *  // -- repMgr.setCallTimeOut(240000);
+ *  SoftwareRepositoryPortType swrep = repMgr.getSoftwareRepository(scope);
+ *  // Applies a deploy
+ *  swrep.store(...);
+ * 
+ * @author Daniele Strollo (ISTI-CNR) + */ +public class SWRepositoryManager { + private GCUBESecurityManagerImpl managerSec = null; + private ISClient client = null; + private int callTimeOut = 240000; + private static final String LOG_PREFIX = "[SW-UPDT-MGR]"; + + public SWRepositoryManager() throws ResourceAccessException { + /** + * Initially the security management is disabled. + */ + this.managerSec = new GCUBESecurityManagerImpl() { + public boolean isSecurityEnabled() { + return false; + } + }; + + try { + client = GHNContext.getImplementation(ISClient.class); + } catch (Exception e) { + throw new ResourceAccessException("Cannot instantiate the ISClient"); + } + } + + /** + * Internally used by {@link AbstractResourceManager#getResourceManager(GCUBEScope)}. + * @param scope + * @return a raw list of resource manager descriptors. + * @throws Exception + */ + private List getSoftwareRepositoryFromScope(final GCUBEScope scope) + throws Exception { + + GCUBERIQuery query = this.client.getQuery(GCUBERIQuery.class); + query.addAtomicConditions(new AtomicCondition("//Profile/ServiceClass", "VREManagement")); + query.addAtomicConditions(new AtomicCondition("//Profile/ServiceName", "SoftwareRepository")); + + List result = client.execute(query, scope); + List toReturn = new ArrayList(); + for (GCUBERunningInstance ri : result) { + if (ri.getScopes().containsValue(scope)) { + toReturn.add(ri); + } + } + return toReturn; + } + + /** + * The the list of resource managers able to handle the resource in a given scope. + * @param scope the scope in which to operate + * @return all the available managers + * @throws ResourceAccessException if no manager can be instantiated + * @throws ResourceParameterException if the parameters are invalid + */ + private List getSoftwareRepositories(final GCUBEScope scope) + throws ResourceAccessException, ResourceParameterException { + Assertion checker = new Assertion(); + checker.validate(scope != null, new ResourceParameterException("Invalid scope")); + + List resourceManagerList = null; + try { + resourceManagerList = this.getSoftwareRepositoryFromScope(scope); + } catch (Exception e) { + throw new ResourceAccessException("Cannot retrieve the software repository in scope: " + scope.toString()); + } + + List retval = new Vector(); + + if (resourceManagerList.isEmpty()) { + throw new ResourceAccessException("Unable to find the software repository in scope: " + scope.toString()); + } + + EndpointReferenceType endpoint = null; + SoftwareRepositoryPortType pt = null; + + for (GCUBERunningInstance resourceManager : resourceManagerList) { + try { + endpoint = resourceManager.getAccessPoint().getEndpoint("gcube/vremanagement/softwarerepository/SoftwareRepository"); + + pt = GCUBERemotePortTypeContext.getProxy( + new SoftwareRepositoryServiceAddressingLocator() + .getSoftwareRepositoryPortTypePort(endpoint), + scope, + this.callTimeOut, + this.managerSec); + if (pt != null) { + retval.add(pt); + } + } catch (Throwable e) { + // trying on next entry + ServerConsole.error(LOG_PREFIX, e); + } + } + if (retval != null && retval.size() > 0) { + // Return a random manager from the available ones + return retval; + } + // no managers found + throw new ResourceAccessException("Unable to find the software repository in scope: " + scope.toString()); + } + + /** + * Retrieves form the current scope the list of registered software managers + * and returns one of them (random choice). + * @param scope the scope in which search the sw repository + * @return one randomly chosen repository manager (if many available). + * @throws ResourceAccessException if no sw manager found + * @throws ResourceParameterException if wrong parameters passed + */ + public final SoftwareRepositoryPortType getSoftwareRepository(final GCUBEScope scope) + throws ResourceAccessException, ResourceParameterException { + List retval = this.getSoftwareRepositories(scope); + if (retval != null && retval.size() > 0) { + Random generator = new Random(); + // Return a random software repository manager from the available ones + return retval.get(generator.nextInt(retval.size())); + } + // no managers found + throw new ResourceAccessException("Unable to find SoftwareRepository in scope: " + scope.toString()); + } + + /** + * Used to set the time out for socket connection with the software repository + * to update. + * @param callTimeOut + */ + public final void setCallTimeOut(final int callTimeOut) { + this.callTimeOut = callTimeOut; + } + + public final int getCallTimeOut() { + return callTimeOut; + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/services/SWRepositoryUpgrader.java b/src/main/java/org/gcube/resourcemanagement/support/managers/services/SWRepositoryUpgrader.java new file mode 100644 index 0000000..bdb8217 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/services/SWRepositoryUpgrader.java @@ -0,0 +1,571 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: SWRepositoryUpgrader.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.services; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.net.HttpURLConnection; +import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Vector; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.gcube.common.core.types.StringArray; +import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException; +import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException; +import org.gcube.resourcemanagement.support.managers.scope.ScopeManager; +import org.gcube.resourcemanagement.support.utils.Assertion; +import org.gcube.resourcemanagement.support.utils.ServerConsole; +import org.gcube.vremanagement.softwarerepository.stubs.ListServicePackagesMessage; +import org.gcube.vremanagement.softwarerepository.stubs.SoftwareRepositoryPortType; +import org.gcube.vremanagement.softwarerepository.stubs.Store; +import org.gcube.vremanagement.softwarerepository.stubs.StoreItem; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +class UpgradeStatus implements Serializable { + private static final long serialVersionUID = 2884364307629521252L; + private double currentPackage = 0; + private double totalPackages = 0; + + /** + * @deprecated for serialization only + */ + public UpgradeStatus() { + super(); + } + + public UpgradeStatus(final double currentPackage, final double totalPackages) { + this.currentPackage = currentPackage; + this.totalPackages = totalPackages; + } + + public double getCurrentPackage() { + return this.currentPackage; + } + + public double getTotalPackages() { + return this.totalPackages; + } +} + +/** + *

+ * Responsible to interact with the repository manager to require + * an update of the infrastructure. + *

+ *

To facilitate the monitoring of main steps involved the + * {@link UpgradeListener} class has been introduced. + *

+ *

+ * Notice that one single update per time can be required. + * If a pending update is running the next request will be blocked + * and an exception will be raised. + *

+ *

+ * Three files will be created at update request (the folder in which they + * will be created depends on the parameter reportsDir). + *

    + *
  • swupdt-rept-<timestamp>.txt containing + * the final report of the sw repository update procedure
  • + *
  • swupdt-dist-<timestamp>.txt that stores the list of packages to update
  • + *
  • swreport.lock represents the lock file and has a double purpose + *
    + * i) avoids multiple requests of update + *
    + * ii) contains information about the progress status. + *
  • + *
+ *

+ * Usage: + *
+ * // [optional]
+ * // to ensure at least a repository manager is registered in the scope
+ * new SWRepositoryManager().getSoftwareRepository(scope);
+ * ServerConsole.info(LOG_PREFIX, "Software repository [FOUND]");
+ *
+ * new SWRepositoryUpgrader(
+ *  // The URL containing the list of packages to update
+ *  "http://acme.org/builds/build.txt",
+ *  // The directory to store report files
+ *  "temp",
+ *  // The scope
+ *  "/gcube/devsec")
+ *  // requires the upgrade
+ *  .doUpgrade();
+ * 
+ * @author Daniele Strollo (ISTI-CNR) + * + */ +public class SWRepositoryUpgrader implements Serializable { + private static final long serialVersionUID = 733460314188332825L; + private static final String LOG_PREFIX = "[SW-UPGR]"; + private String todeployTxtURL = null; + private String reportFileName = null; + private String distributionFileName = null; + private String scope = null; + private String reportsDir = "temp"; + private String lockFile = null; + private List listeners = new Vector(); + + + /** + * @deprecated for internal use only. Required for serialization. + */ + private SWRepositoryUpgrader() { + super(); + } + + /** + * Builds a new instance of repository update manager. + * + * @param todeployTxtURL the URL to use to retrieve the list of software to update + * @param reportsDir where the reports must be stored + * @throws Exception + */ + public SWRepositoryUpgrader( + final String todeployTxtURL, + final String reportsDir, + final String scope) throws Exception { + this(); + // Checks parameters + Assertion checker = new Assertion(); + checker.validate(todeployTxtURL != null && todeployTxtURL.trim().length() > 0, + new ResourceParameterException("Invalid parameter todeployTxtURL")); + checker.validate(reportsDir != null && reportsDir.trim().length() > 0, + new ResourceParameterException("Invalid parameter reportsDir")); + checker.validate(scope != null && scope.trim().length() > 0 && ScopeManager.getScope(scope) != null, + new ResourceParameterException("Invalid parameter scope")); + + this.reportsDir = reportsDir; + + // Create the folder if it does not exist + boolean success = (new File(this.reportsDir)).mkdirs(); + if (success) { + ServerConsole.trace(LOG_PREFIX, "Directory " + this.reportsDir + " [CREATED]"); + } + + this.todeployTxtURL = todeployTxtURL; + + SimpleDateFormat dateFormatter = new SimpleDateFormat("yyMMdd-hhmm"); + Date date = new Date(); + String curDate = dateFormatter.format(date); + + + this.reportFileName = this.reportsDir + File.separator + "swupdt-rept-" + curDate + ".txt"; + this.distributionFileName = this.reportsDir + File.separator + "swupdt-dist-" + curDate + ".txt"; + this.lockFile = this.reportsDir + File.separator + "swreport.lock"; + + ServerConsole.trace(LOG_PREFIX, "Setting report file: " + this.reportFileName); + ServerConsole.trace(LOG_PREFIX, "Setting distribution file: " + this.distributionFileName); + + this.scope = scope; + } + + public final String getScope() { + return this.scope; + } + + /** + * Upgrades a software repository. + * @param softwareRepositoryURL the url to contact the service repository + * @param todeployTxtURL the txt file containing software descriptions + * @param reportFileName the filename where report must be stored + * @param distributionFileName the filename where distribution must be stored + * @param scope + */ + public final void doUpgrade() throws Exception { + try { + this.reserve(); + } catch (Exception e) { + this.release(); + throw new Exception("The required report file already exists. It cannot be replaced."); + } + + File reportFile = new File(reportFileName); + if (reportFile.exists()) { + this.release(); + throw new Exception("The required report file already exists. It cannot be replaced."); + } + + File distributionFile = new File(distributionFileName); + if (distributionFile.exists()) { + this.release(); + throw new Exception("The required distribution file already exists. It cannot be replaced."); + } + + SoftwareRepositoryPortType swManager = null; + try { + swManager = new SWRepositoryManager().getSoftwareRepository(ScopeManager.getScope(this.scope)); + } catch (Exception e) { + this.release(); + throw new Exception("The swManager cannot be instantiated. " + e.getMessage()); + } + + ArrayList listIds = new ArrayList(); + List listServicePackagesMessages = new ArrayList(); + List storeItemsList = null; + + try { + storeItemsList = parseTxtFile(this.todeployTxtURL); + } catch (Exception e) { + this.release(); + throw new Exception("The passed update URL is not valid. " + e.getMessage()); + } + + try { + stringToFile("\n", reportFile); + } catch (IOException e) { + ServerConsole.error(LOG_PREFIX, "doUpgrade", e); + } + + for (int i = 0; i < storeItemsList.size(); i++) { + StoreItem[] storeItems = new StoreItem[1]; + storeItems[0] = storeItemsList.get(i); + + Store store = new Store(); + store.setStoreMessage(storeItems); + + try { + String ret = swManager.store(store); + ret = ret.replace("\n", ""); + ret = ret.replace("\n", ""); + listIds.add(parseXMLStoreSoftwareArchive(ret, listServicePackagesMessages)); + stringToFile(ret, reportFile); + + // -- PROGRESS UPDATE PROCEDURE + // updates the status so that the client can retrieve + // the current update progress (0..1). + UpgradeStatus status = new UpgradeStatus(i + 1, storeItemsList.size()); + this.storeStatus(status); + double progress = 1.0d * (status.getCurrentPackage() / status.getTotalPackages()); + // Informs the listeners of update progress + for (UpgradeListener listener : this.listeners) { + listener.onProgress(progress); + } + ServerConsole.trace(LOG_PREFIX, "*** UPDATED [" + (i + 1) + "/" + storeItemsList.size() + "]"); + // -- ENDOF PROGRESS UPDATE PROCEDURE + + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + + try { + stringToFile("\n", reportFile); + } catch (IOException e) { + e.printStackTrace(); + } + + StringBuilder sb = new StringBuilder(); + sb.append("\n"); + + for (int k = 0; k < listIds.size(); k++) { + try { + StringArray stringArray = swManager.listUniquesServicePackages(listServicePackagesMessages.get(k)); + if (stringArray != null) { + String[] aux = stringArray.getItems(); + for (int i = 0; i < aux.length; i++) { + sb.append(aux[i]); + } + } else { + ServerConsole.info(LOG_PREFIX, "listServicePackages return null"); + continue; + } + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, "Error getting package list Service with Class=" + + listServicePackagesMessages.get(k).getServiceClass() + + " Name=" + listServicePackagesMessages.get(k).getServiceName() + + " Version=" + listServicePackagesMessages.get(k).getServiceVersion() + "\n\n"); + // Informs the listeners of update exception + for (UpgradeListener listener : this.listeners) { + listener.onError( + // provides the package raising update failure + "Error getting package list Service with Class=" + + listServicePackagesMessages.get(k).getServiceClass() + + " Name=" + listServicePackagesMessages.get(k).getServiceName() + + " Version=" + listServicePackagesMessages.get(k).getServiceVersion(), + e); + } + // Continues to next package + continue; + } + } + + sb.append(""); + + + try { + stringToFile(sb.toString(), distributionFile); + } catch (IOException e) { + ServerConsole.info(LOG_PREFIX, "Unable to write on output file"); + ServerConsole.error(LOG_PREFIX, e); + } + + this.release(); + + + ServerConsole.debug(LOG_PREFIX, "Infrastructure Update [DONE]"); + // Informs the release has been done + for (UpgradeListener listener : this.listeners) { + listener.onFinish(this.todeployTxtURL, this.reportFileName, this.distributionFileName); + } + } + + /** + * Each time a single update can be required. + * For a such reason the repository upgrader must be reserved and + * released at the end of the process. + */ + private void reserve() throws Exception { + if (isReserved()) { + throw new ResourceAccessException("The Infrastracture update cannot be executed. It is already locked by another process. Try later."); + } + // Informs the release has been done + for (UpgradeListener listener : this.listeners) { + listener.onReserve(); + } + stringToFile("Reserved by: " + this.reportFileName, new File(this.lockFile)); + this.storeStatus(new UpgradeStatus(0, 1)); + } + + private void storeStatus(final UpgradeStatus status) { + ServerConsole.info(LOG_PREFIX, "Storing status"); + + try { + File file = new File(this.lockFile); + ObjectOutputStream mine = new ObjectOutputStream(new FileOutputStream(file)); + mine.reset(); + mine.writeObject(status); + mine.flush(); + mine.close(); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + + /** + * Gives the completed upgrade percentage (0...1). + * @return + */ + public final double getProgressStatus() { + ServerConsole.info(LOG_PREFIX, "Getting status"); + + if (!this.isReserved()) { + return 1.0; + } + + try { + File file = new File(this.lockFile); + ObjectInputStream mine = new ObjectInputStream(new FileInputStream(file)); + + UpgradeStatus status = (UpgradeStatus) mine.readObject(); + mine.close(); + + double retval = 1.0d * (status.getCurrentPackage() / status.getTotalPackages()); + ServerConsole.info(LOG_PREFIX, "Current Status: " + retval); + return retval; + } catch (Exception e) { + return 1; + } + } + + /** + * Checks if the software update is locked by another process. + * @return + */ + private boolean isReserved() { + return new File(this.lockFile).exists(); + } + + /** + * Releases the reservation (removes the lock file). + */ + private void release() { + // Informs the release has been done + for (UpgradeListener listener : this.listeners) { + listener.onRelease(); + } + + ServerConsole.debug(LOG_PREFIX, "Releasing lock for " + this.reportFileName); + new File(this.lockFile).delete(); + } + + private List parseTxtFile(final String txtURL) throws Exception { + List ret = new ArrayList(); + + ServerConsole.info(LOG_PREFIX, "Opening URL connection to " + txtURL); + + final URL url = new URL(txtURL); + final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); + + httpURLConnection.setRequestMethod("GET"); + httpURLConnection.setDoInput(true); + httpURLConnection.setDoOutput(true); + httpURLConnection.setUseCaches(false); + + + ServerConsole.info(LOG_PREFIX, "Getting data from given URL " + txtURL); + InputStream is = httpURLConnection.getInputStream(); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); + ServerConsole.info(LOG_PREFIX, "Connection return code: " + httpURLConnection.getResponseCode()); + ServerConsole.info(LOG_PREFIX, "Bytes available: " + is.available() + "\n\n\n"); + + String nextStr = bufferedReader.readLine(); // read a row + + while (nextStr != null) { + + String row = nextStr.toLowerCase(); + if (!row.contains("-servicearchive-")) { + nextStr = bufferedReader.readLine(); // read the next row before continuing + continue; + } + String serviceClass = "Class"; + ServerConsole.info(LOG_PREFIX, "Service Class = " + serviceClass); + String serviceName = "Name"; + ServerConsole.info(LOG_PREFIX, "Service Name = " + serviceName); + //String version = nextStr.split("-servicearchive-")[1]; + String version = "1.0.0"; + // version = version.split("-")[0]; + ServerConsole.info(LOG_PREFIX, "Version = " + version); + String softwareArchiveURL = nextStr; + ServerConsole.info(LOG_PREFIX, "URL = " + nextStr); + String description = "ETICS Client programmed Description"; + ServerConsole.info(LOG_PREFIX, "Description = " + description + "\n\n\n"); + String[] scopes = new String[] {scope}; + + StoreItem storeItem = new StoreItem(); + storeItem.setServiceClass(serviceClass); + storeItem.setServiceName(serviceName); + storeItem.setServiceVersion(version); + storeItem.setURL(softwareArchiveURL); + storeItem.setDescription(description); + storeItem.setScopes(scopes); + + ret.add(storeItem); + + nextStr = bufferedReader.readLine(); // read the next row + } + + bufferedReader.close(); + httpURLConnection.disconnect(); + + + return ret; + } + + /** + * + * @param xml + * @return + */ + private String parseXMLStoreSoftwareArchive( + final String xml, + final List listServicePackagesMessages) { + + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = null; + try { + db = dbf.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + + + Document document = null; + try { + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xml.getBytes()); + document = db.parse(byteArrayInputStream); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + + Element root = document.getDocumentElement(); + NodeList nl = root.getChildNodes(); + String id = ""; + ListServicePackagesMessage listServicePackagesMessage = new ListServicePackagesMessage(); + for (int i = 0; i < nl.getLength(); i++) { + if (!(nl.item(i) instanceof Element)) { + continue; + } + Element el = (Element) nl.item(i); + + if (el == null) { + continue; + } else if (el.getTagName().compareTo("ServiceClass") == 0) { + listServicePackagesMessage.setServiceClass(el.getTextContent()); + } else if (el.getTagName().compareTo("ID") == 0) { + id = el.getTextContent(); + } else if (el.getTagName().compareTo("ServiceName") == 0) { + listServicePackagesMessage.setServiceName(el.getTextContent()); + } else if (el.getTagName().compareTo("ServiceVersion") == 0) { + listServicePackagesMessage.setServiceVersion(el.getTextContent()); + } + + } + listServicePackagesMessages.add(listServicePackagesMessage); + ServerConsole.info(LOG_PREFIX, "required store for SA ID: " + id); + return id; + + } + + /** + * @param str string + * @param targetFile Target File + * @throws IOException if fails + */ + private void stringToFile( + final String str, + final File targetFile) throws IOException { + try { + FileWriter fw = new FileWriter(targetFile, true); + fw.append(str); + fw.flush(); + fw.close(); + } catch (IOException e) { + throw e; + } + } + + public final void addListener(final UpgradeListener listener) { + if (listener == null) { + return; + } + listener.setUpgrader(this); + this.listeners.add(listener); + } + +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/managers/services/UpgradeListener.java b/src/main/java/org/gcube/resourcemanagement/support/managers/services/UpgradeListener.java new file mode 100644 index 0000000..088eccd --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/managers/services/UpgradeListener.java @@ -0,0 +1,102 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: UpgradeListener.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.managers.services; + +import org.gcube.resourcemanagement.support.utils.ServerConsole; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ + +/** + * To an upgrade process can be attached an handler to perform + * additional operations at the happening of events. + * @author Daniele Strollo (ISTI-CNR) + * + */ +public abstract class UpgradeListener { + protected static final String LOG_PREFIX = "[SW-UPGR-LISTENER]"; + private SWRepositoryUpgrader upgrader = null; + + public UpgradeListener() { + this.onInit(); + } + + /** + * Method invoked at construction of listener. + */ + public abstract void onInit(); + + /** + * Automatically invoked when the listener is attached to an upgrader. + * + * @param upgrader + */ + public final void setUpgrader(final SWRepositoryUpgrader upgrader) { + this.upgrader = upgrader; + } + + public final SWRepositoryUpgrader getUpgrader() { + return this.upgrader; + } + + /** + * Invoked each time the update progresses. + * @param progress + */ + public abstract void onProgress(final double progress); + /** + * Invoked in case of failure during update operation. + * @param failure + */ + public final void onError(final Exception failure) { + ServerConsole.error(LOG_PREFIX, failure); + } + /** + * Invoked in case of failure during update operation. + * @param failure + */ + public final void onError(final String message, final Exception failure) { + ServerConsole.error(LOG_PREFIX, message, failure); + } + + /** + * In successful update, the resulting report files will be + * provided in input. + */ + public abstract void onFinish( + final String deployURL, + final String reportFileName, + final String distributionFileName); + + /** + * Invoked once the lock has been acquired. + */ + public final void onReserve() { + ServerConsole.info(LOG_PREFIX, "Software repository lock request [DONE]"); + } + + /** + * Once finished the lock is released and this event raised. + * The release is called both if the process successfully finish + * or an abort is required. + */ + public void onRelease() { + ServerConsole.info(LOG_PREFIX, "Software repository lock release [DONE]"); + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/tests/GenericTest.java b/src/main/java/org/gcube/resourcemanagement/support/tests/GenericTest.java new file mode 100644 index 0000000..a0a6363 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/tests/GenericTest.java @@ -0,0 +1,187 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: GenericTest.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.tests; + +import java.io.File; +import java.util.Map; + +import org.gcube.common.core.contexts.GHNContext.Status; +import org.gcube.common.core.resources.GCUBEHostingNode; +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.common.core.scope.GCUBEScope.Type; +import org.gcube.resourcemanagement.support.managers.resources.GHNManager; +import org.gcube.resourcemanagement.support.managers.resources.GenericResourceManager; +import org.gcube.resourcemanagement.support.managers.resources.ResourceFactory; +import org.gcube.resourcemanagement.support.managers.scope.ScopeManager; +import org.gcube.resourcemanagement.support.types.AllowedResourceTypes; +import org.gcube.resourcemanagement.support.utils.ServerConsole; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class GenericTest { + private static final String LOG_PREFIX = "[SW-SUPPORT-TEST]"; + + public static void testScope() { + + try { + Map scopes = ScopeManager.getAvailableScopes(); + + System.out.println("\n\n\n******************** TEST SCOPE ***************\n"); + for (GCUBEScope scope : scopes.values()) { + if ((scope.getType().compareTo(Type.INFRASTRUCTURE) == 0) + || (scope.getType().compareTo(Type.VO) == 0)) { + if (scope.getServiceMap() != null) { + ServerConsole.trace(null, "*** Map loaded for " + scope.getType() + " " + scope.toString() + " [OK]"); + } else { + ServerConsole.trace(null, "*** Map loaded for " + scope.getType() + " " + scope.toString() + " [ERR]"); + } + } else { + try { + if (scope.getEnclosingScope().getServiceMap() != null) { + ServerConsole.trace(null, "*** Map loaded for " + scope.getType() + scope.toString() + " [OK]"); + } + } catch (Exception e) { + ServerConsole.trace(null, "*** Map loaded for " + scope.getType() + scope.toString() + " [ERR]"); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + System.out.println("\n******************** TEST SCOPE END ***************\n"); + } + } + + public static String testCreation() { + System.out.println("\n\n\n******************** TEST CREATION ***************"); + + String resID = null; + + try { + resID = GenericResourceManager.create( + null, + ScopeManager.getScope("/gcube/devsec"), + "GR Test", + "GR Test Description", + "Hello", + "XXX"); + ServerConsole.trace(null, "Generic Resource Created with ID: " + resID); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } finally { + System.out.println("\n******************** TEST CREATION END ***************\n"); + } + return resID; + } + + public static void testScopeCopy(final String resID, final String fromScope, final String toScope) { + System.out.println("\n\n\n******************** TEST SCOPE COPY ***************"); + try { + GenericResourceManager res = new GenericResourceManager(resID); + ServerConsole.trace(null, + res.addToExistingScope(ScopeManager.getScope(fromScope), ScopeManager.getScope(toScope)) + ); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } finally { + System.out.println("\n******************** TEST SCOPE COPY END ***************\n"); + } + } + + public static void testResourceEdit(final String resID, final GCUBEScope scope) { + System.out.println("\n\n\n******************** TEST RESEDIT COPY ***************"); + try { + GenericResourceManager res = new GenericResourceManager(resID); + res.update("New Name", "updated description", null, null, scope); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } finally { + System.out.println("\n******************** TEST RESEDIT END ***************\n"); + } + } + + public static void testModeGHN(final String resID, final String scope) + throws Exception { + GCUBEScope queryScope = ScopeManager.getScope(scope); + GHNManager ghnManager = new GHNManager(resID); + GCUBEHostingNode res = (GCUBEHostingNode) ghnManager.getGCUBEResource(queryScope); + res.getNodeDescription().setStatus(Status.UNREACHABLE); + ghnManager.getISPublisher().updateGCUBEResource(res, queryScope, ghnManager.getSecurityManager()); + } + + public static void testGHN() { + System.out.println("\n\n\n******************** TEST GHN ***************"); + try { + GHNManager ghn1 = new GHNManager("20ddb210-b779-11df-96c9-a66904b26e27", "pc-strollo"); + ghn1.addToExistingScope(ScopeManager.getScope("/gcube"), ScopeManager.getScope("/gcube/devsec")); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } finally { + System.out.println("\n******************** TEST GHN END ***************\n"); + } + } + + public static void testRemoveFromScope(final String resID, final GCUBEScope scope) { + System.out.println("\n\n\n******************** TEST RESOURCE REMOVEFROMSCOPE ***************"); + try { + GenericResourceManager res = new GenericResourceManager(resID); + res.removeFromScope(scope); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } finally { + System.out.println("\n******************** TEST RESOURCE REMOVEFROMSCOPE END ***************\n"); + } + } + + public static void testDelete(final String resID, final GCUBEScope scope) { + System.out.println("\n\n\n******************** TEST RESOURCE DELETE ***************"); + try { + GenericResourceManager res = new GenericResourceManager(resID); + res.delete(scope); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } finally { + System.out.println("\n******************** TEST RESOURCE DELETE END ***************\n"); + } + } + + @SuppressWarnings("deprecation") + public static void main(final String[] args) throws Exception { + // The scopes must be initialized + ScopeManager.setScopeConfigFile("test-suite" + File.separator + "scopes" + File.separator + "scopedata_admin.xml"); + + boolean deepTest = false; + // testScope(); + // testGHN(); + if (deepTest) { + String resID = testCreation(); + testScopeCopy(resID, "/gcube/devsec", "/gcube/devsec/devVRE"); + + System.out.println("\n\nWaiting for resource refresh 60secs.\n\n\n"); + Thread.sleep(60000); + + //testResourceEdit(resID, ScopeManager.getScope("/gcube/devsec")); + testDelete(resID, ScopeManager.getScope("/gcube/devsec/devVRE")); + + // testRemoveFromScope(resID, ScopeManager.getScope("/gcube/devsec")); + } else { + testModeGHN("f5cb0640-f1a7-11df-93d0-fc409084cf46", "/gcube/devsec/devVRE"); + } + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/tests/ScopeTest.java b/src/main/java/org/gcube/resourcemanagement/support/tests/ScopeTest.java new file mode 100644 index 0000000..a6b97e8 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/tests/ScopeTest.java @@ -0,0 +1,45 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ScopeTest.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.tests; + +import java.io.File; + +import org.gcube.resourcemanagement.support.managers.scope.ScopeManager; + +/** + * Makes tests on ScopeManager. + * @author Daniele Strollo (ISTI-CNR) + */ +public class ScopeTest { + + public static final void testLoadScopes() { + ScopeManager.setScopeConfigFile("test-suite" + File.separator + "scopes" + File.separator + "scopedata_admin.xml"); + try { + ScopeManager.update(); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Scopes retrievial [ERR]"); + return; + } + System.out.println("Scopes retrievial [DONE]"); + } + + + public static void main(final String[] args) { + testLoadScopes(); + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/types/AllowedResourceTypes.java b/src/main/java/org/gcube/resourcemanagement/support/types/AllowedResourceTypes.java new file mode 100644 index 0000000..17ff102 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/types/AllowedResourceTypes.java @@ -0,0 +1,31 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: AllowedResourceTypes.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.types; + +/** + * @author Massimiliano Assante (ISTI-CNR) + * + */ +public enum AllowedResourceTypes { + GHN(), + RunningInstance(), + Service(), + VIEW(), + GenericResource(), + RuntimeResource(), + Collection(); +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/utils/Assertion.java b/src/main/java/org/gcube/resourcemanagement/support/utils/Assertion.java new file mode 100644 index 0000000..4fb2088 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/utils/Assertion.java @@ -0,0 +1,65 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: Assertion.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.utils; + +import java.io.Serializable; + +/** + * General purpose assertion handler. + * Assertion can be generalized to check a boolean expression and + * to raise an exception in correspondence to a failure happening + * during checking. + *
+ * Example:
+ *
+ *     Assertion<TheExceptionType> assertion = new Assertion<ParamException> ();
+ *     assertion.validate (param != null, new TheExceptionType("invalid parameter null"));
+ *
+ * or, in a more compact form:
+ *    // The exception to throw in case of failure
+ *    // during the evaluation of the expected condition
+ *    new Assertion<TheExceptionType>().validate(
+ *    	i>5,                                                     // The expected boolean condition
+ *    	new TheExceptionType("Parameter must be greater than 5")); //The error message
+ *
+ * 
+ * + * @author Daniele Strollo (ISTI-CNR) + */ +public class Assertion implements Serializable { + private static final long serialVersionUID = -2007903339251667541L; + + /** + * Makes an assertion and if the expression evaluation fails, throws an + * exception of type T. + *
+	 * Example:
+	 * 	new Assertion<MyException>().validate(whatExpected, new MyException("guard failed"));
+	 * 
+ * @param assertion the boolean expression to evaluate + * @param exc the exception to throw if the condition does not hold + * @throws T the exception extending {@link java.lang.Throwable} + */ + public final void validate(final boolean assertion, final T exc) + throws T { + // TOCHECK junit.framework.Assert.assertTrue(assertion); + if (!assertion) { + throw exc; + } + } +} + diff --git a/src/main/java/org/gcube/resourcemanagement/support/utils/ServerConsole.java b/src/main/java/org/gcube/resourcemanagement/support/utils/ServerConsole.java new file mode 100644 index 0000000..cebfb70 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/utils/ServerConsole.java @@ -0,0 +1,60 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: ServerConsole.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.utils; + +import org.gcube.common.core.utils.logging.GCUBEClientLog; +import org.gcube.resourcemanagement.support.managers.resources.AbstractResourceManager; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public class ServerConsole { + private static final GCUBEClientLog LOGGER = new GCUBEClientLog(AbstractResourceManager.class); + private static final String LOG_PREFIX = "*** [RMP] "; + + public static void error(final String prefix, final String msg) { + LOGGER.error(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg); + } + public static void error(final String prefix, final Throwable exc) { + LOGGER.error(LOG_PREFIX + ((prefix != null) ? prefix : ""), exc); + } + public static void error(final String prefix, final String msg, final Throwable exc) { + LOGGER.error(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg, exc); + } + public static void warn(final String prefix, final String msg) { + LOGGER.warn(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg); + } + public static void warn(final String prefix, final String msg, final Throwable exc) { + LOGGER.warn(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg, exc); + } + public static void info(final String prefix, final String msg) { + LOGGER.info(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg); + } + public static void trace(final String prefix, final String msg) { + LOGGER.trace(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg); + } + public static void debug(final String prefix, final String msg) { + LOGGER.debug(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg); + } + public static void fatal(final String prefix, final String msg) { + LOGGER.fatal(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg); + } + public static void fatal(final String prefix, final String msg, final Throwable exc) { + LOGGER.fatal(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg, exc); + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/utils/persistence/PersistenceHandler.java b/src/main/java/org/gcube/resourcemanagement/support/utils/persistence/PersistenceHandler.java new file mode 100644 index 0000000..119ff65 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/utils/persistence/PersistenceHandler.java @@ -0,0 +1,62 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: PersistenceHandler.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.utils.persistence; + +/** + * @author Daniele Strollo (ISTI-CNR) + * + */ +public interface PersistenceHandler { + /** + * The code to execute once the persistent object is refreshed. + */ + void onRefresh(); + + /** + * Automatically invoked by the library at instantiation + * of persistent items. + * Used to make initializations before starting the persistence + * manager. + */ + void onLoad(); + + /** + * When the item need no more to be persisted + * the refresh loop is closed and this method is + * invoked. + */ + void onDestroy(); + + /** + * Requires the destroy of the persistent resource. + * The persistent file will be deleted and the onDestroy + * event will be raised. + */ + void destroy(); + + /** + * The implementation to retrieve data from the persistence manager. + * @return + */ + T getData(); + + /** + * Sets the new data to persist. + * @param data + */ + void setData(final T data); +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/utils/persistence/PersistentItem.java b/src/main/java/org/gcube/resourcemanagement/support/utils/persistence/PersistentItem.java new file mode 100644 index 0000000..2e57372 --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/utils/persistence/PersistentItem.java @@ -0,0 +1,152 @@ +/**************************************************************************** + * This software is part of the gCube Project. + * Site: http://www.gcube-system.org/ + **************************************************************************** + * The gCube/gCore software is licensed as Free Open Source software + * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). + * The software and documentation is provided by its authors/distributors + * "as is" and no expressed or + * implied warranty is given for its use, quality or fitness for a + * particular case. + **************************************************************************** + * Filename: PersistentItem.java + **************************************************************************** + * @author Daniele Strollo + ***************************************************************************/ + +package org.gcube.resourcemanagement.support.utils.persistence; + +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.Serializable; +import java.util.List; +import java.util.Vector; +import org.gcube.resourcemanagement.support.utils.ServerConsole; +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.io.xml.StaxDriver; + +/** + * Wrapping for data that can be persisted on the filesystem. + *
+ * new PersistentItem<DataTypeToPersist>(fileToStore, refreshDelay) {
+ *  public void onLoad() {
+ *	 //...
+ *  }
+ *  
+ *  // The other methods to overload
+ *  // ...
+ *  
+ * }
+ * 
+ * @author Daniele Strollo (ISTI-CNR) + */ +public abstract class PersistentItem +implements PersistenceHandler { + protected static final String LOG_PREFIX = "[PERSISTENCE]"; + private long refreshDelay = -1; + private boolean processClosed = false; + private String persistenceFileName = null; + private XStream serializer = null; + + /** + * @deprecated for internal use only. + */ + public PersistentItem() { + } + + public PersistentItem(final String persistenceFileName) { + this(persistenceFileName, -1); + } + + public PersistentItem(final String persistenceFileName, final long refreshDelay) { + this.setRefreshDelay(refreshDelay); + this.setPersistenceFileName(persistenceFileName); + + this.onLoad(); + + // loops to retrieve the data to persist + if (this.refreshDelay > 0) { + new Thread() { + public void run() { + while (!processClosed) { + try { + sleep(refreshDelay); + } catch (final InterruptedException e) { + ServerConsole.error(LOG_PREFIX, e); + } + // time elapsed... do refresh + onRefresh(); + } + } + } .start(); + } + } + + public PersistentItem(final T data, final String persistenceFileName, final long refreshDelay) { + this(persistenceFileName, refreshDelay); + this.setData(data); + } + + protected final void setPersistenceFileName(final String persistenceFileName) { + this.persistenceFileName = persistenceFileName; + } + + protected final void setRefreshDelay(final long refreshDelay) { + this.refreshDelay = refreshDelay; + } + + private synchronized XStream getSerializer() { + if (this.serializer == null) { + StaxDriver driver = new StaxDriver(); + driver.setRepairingNamespace(false); + this.serializer = new XStream(driver); + this.serializer.addDefaultImplementation(Vector.class, List.class); + } + return this.serializer; + } + + public final void destroy() { + this.processClosed = true; + // Thread has finished... invoking destroy + onDestroy(); + } + + /** + * Returns the corresponding persistent data. + * @return + */ + @SuppressWarnings("unchecked") + public final T getData() { + try { + StringBuilder xml = new StringBuilder(); + BufferedReader reader = new BufferedReader(new FileReader(persistenceFileName)); + String currLine = null; + + while ((currLine = reader.readLine()) != null) { + xml.append(currLine); + } + + return (T) this.getSerializer().fromXML(xml.toString()); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + return null; + } + } + + public final void setData(final T data) { + String xmlData = this.getSerializer().toXML(data); + + BufferedOutputStream mine = null; + try { + mine = new BufferedOutputStream(new FileOutputStream(persistenceFileName)); + mine.write(xmlData.getBytes()); + //mine.flush(); + mine.close(); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + return; + } + } +} diff --git a/src/main/java/org/gcube/resourcemanagement/support/utils/persistence/TestPersistence.java b/src/main/java/org/gcube/resourcemanagement/support/utils/persistence/TestPersistence.java new file mode 100644 index 0000000..53a450a --- /dev/null +++ b/src/main/java/org/gcube/resourcemanagement/support/utils/persistence/TestPersistence.java @@ -0,0 +1,46 @@ +package org.gcube.resourcemanagement.support.utils.persistence; + +import java.io.File; + +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.resourcemanagement.support.managers.scope.ScopeManager; +import org.gcube.resourcemanagement.support.utils.ServerConsole; + +public class TestPersistence { + + /** + * @param args + */ + public static void main(final String[] args) { + PersistentItem persistentScopes = new PersistentItem("data.xml", 10000) { + // Builds the data to persist + private void refreshData() { + try { + GCUBEScope[] toStore = + ScopeManager.getAvailableScopes().values().toArray(new GCUBEScope[]{}); + this.setData(toStore); + } catch (Exception e) { + ServerConsole.error(LOG_PREFIX, e); + } + } + + public void onLoad() { + ScopeManager.setScopeConfigFile("test-suite" + File.separator + "scopes" + File.separator + "scopedata_admin.xml"); + + this.refreshData(); + } + public void onRefresh() { + this.refreshData(); + + GCUBEScope[] scopes = this.getData(); + System.out.println(scopes); + } + public void onDestroy() { + this.setData(null); + } + }; + + + } + +} diff --git a/test-suite/scopes/scopedata.xml b/test-suite/scopes/scopedata.xml new file mode 100644 index 0000000..035aafa --- /dev/null +++ b/test-suite/scopes/scopedata.xml @@ -0,0 +1,57 @@ + + + + + gcube + + + gcube + ServiceMap_gcube.xml + /gcube + + + gcube/devsec + ServiceMap_devsec.xml + /gcube/devsec + + + + + diff --git a/test-suite/scopes/scopedata_admin.xml b/test-suite/scopes/scopedata_admin.xml new file mode 100644 index 0000000..2fad2ce --- /dev/null +++ b/test-suite/scopes/scopedata_admin.xml @@ -0,0 +1,56 @@ + + + + + d4science + + + d4science + ServiceMap_d4science.research-infrastructures.eu.xml + /d4science.research-infrastructures.eu + + + d4science/FARM + ServiceMap_FARM.xml + /d4science.research-infrastructures.eu/FARM + + + d4science/Ecosystem + ServiceMap_Ecosystem.xml + /d4science.research-infrastructures.eu/Ecosystem + + + d4science/INSPIRE + ServiceMap_INSPIRE.xml + /d4science.research-infrastructures.eu/INSPIRE + + + d4science/Arts-Humanities + ServiceMap_Arts-Humanities.xml + /d4science.research-infrastructures.eu/Arts-Humanities + + + + + + gcube + + + gcube + ServiceMap_gcube.xml + /gcube + + + gcube/devsec + ServiceMap_devsec.xml + /gcube/devsec + + + gcube/devNext + ServiceMap_devNext.xml + /gcube/devNext + + + + +