@ -16,24 +16,20 @@
package org.gcube.resourcemanagement.support.server.managers.resources ;
import static org.gcube.resources.discovery.icclient.ICFactory.client ;
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.common.resources.gcore.Resource ;
import org.gcube.common.resources.gcore.ScopeGroup ;
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.common.scope.impl.ScopeBean.Type ;
import org.gcube.informationsystem.publisher.RegistryPublisher ;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory ;
import org.gcube.resourcemanagement.support.server.exceptions.AbstractResourceException ;
import org.gcube.resourcemanagement.support.server.exceptions.ResourceAccessException ;
import org.gcube.resourcemanagement.support.server.exceptions.ResourceOperationException ;
@ -41,20 +37,19 @@ import org.gcube.resourcemanagement.support.server.exceptions.ResourceParameterE
import org.gcube.resourcemanagement.support.server.managers.report.ReportBuilder ;
import org.gcube.resourcemanagement.support.server.managers.report.ReportEntry ;
import org.gcube.resourcemanagement.support.server.managers.report.ReportOperation ;
import org.gcube.resourcemanagement.support.server.managers.scope.ScopeManager ;
import org.gcube.resourcemanagement.support.server.types.AllowedResourceTypes ;
import org.gcube.resourcemanagement.support.server.utils.Assertion ;
import org.gcube.resourcemanagement.support.server.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 ;
import org.gcube.resources.discovery.client.api.DiscoveryClient ;
import org.gcube. resources.discovery.client.queries.api.Query ;
import org.gcube. resources.discovery.client.queries.impl.QueryBox ;
import org.gcube.vremanagement.resourcemanager. client.RMBinderLibrary ;
import org.gcube.vremanagement.resourcemanager. client.RMReportingLibrary ;
import org.gcube.vremanagement.resourcemanager. client.fws.Types.AddResourcesParameters ;
import org.gcube.vremanagement.resourcemanager. client.fws.Types.RemoveResourcesParameters ;
import org.gcube.vremanagement.resourcemanager. client.fws.Types.ResourceItem ;
import org.gcube.vremanagement.resourcemanager. client.fws.Types.ResourceList ;
import org.gcube.vremanagement.resourcemanager.client.proxies.Proxies ;
/ * *
* The minimal interface all the resource managers must implement .
@ -68,16 +63,15 @@ public abstract class AbstractResourceManager {
private String name = null ;
private AllowedResourceTypes type = null ;
private String subType = null ;
private ISClient client = null ;
private GCUBESecurityManagerImpl managerSec = null ;
private IS Publisher publisher = null ;
private Registry Publisher 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 {
throws ResourceParameterException , ResourceAccessException {
Assertion < ResourceParameterException > checker = new Assertion < ResourceParameterException > ( ) ;
checker . validate ( type ! = null , new ResourceParameterException ( "Invalid Parameter type" ) ) ;
@ -93,13 +87,7 @@ public abstract class AbstractResourceManager {
} ;
try {
client = GHNContext . getImplementation ( ISClient . class ) ;
} catch ( Exception e ) {
throw new ResourceAccessException ( "Cannot instantiate the ISClient" ) ;
}
try {
this . publisher = GHNContext . getImplementation ( ISPublisher . class ) ;
this . publisher = RegistryPublisherFactory . create ( ) ;
} catch ( Exception e ) {
ServerConsole . error ( LOG_PREFIX , e ) ;
}
@ -114,10 +102,7 @@ public abstract class AbstractResourceManager {
* @throws ResourceParameterException
* @throws ResourceAccessException
* /
public AbstractResourceManager (
final String id ,
final AllowedResourceTypes type )
throws ResourceParameterException , ResourceAccessException {
public AbstractResourceManager ( String id , AllowedResourceTypes type ) throws ResourceParameterException , ResourceAccessException {
this ( type ) ;
Assertion < ResourceParameterException > checker = new Assertion < ResourceParameterException > ( ) ;
@ -126,45 +111,27 @@ public abstract class AbstractResourceManager {
this . id = id . trim ( ) ;
}
public AbstractResourceManager (
final String id ,
final String name ,
final AllowedResourceTypes type ) throws ResourceParameterException , ResourceAccessException {
public AbstractResourceManager ( String id , String name , AllowedResourceTypes type ) throws ResourceParameterException , ResourceAccessException {
this ( id , type ) ;
Assertion < ResourceParameterException > checker = new Assertion < ResourceParameterException > ( ) ;
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 {
public AbstractResourceManager ( String id , String name , AllowedResourceTypes type , 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 getIS Publisher( ) {
public final RegistryPublisher getRegistryPublisher ( ) {
return publisher ;
}
@ -226,134 +193,6 @@ public abstract class AbstractResourceManager {
return this . subType ;
}
/ * *
* Internally used by { @link AbstractResourceManager # getResourceManager ( GCUBEScope ) } .
* @param scope
* @return a raw list of resource manager descriptors .
* @throws Exception
* /
private List < GCUBERunningInstance > getResourceManagerFromScope ( final GCUBEScope scope ) throws Exception {
System . out . println ( "Looking for ResourceManager in scope: " + scope ) ;
GCUBERIQuery query = this . client . getQuery ( GCUBERIQuery . class ) ;
query . addAtomicConditions ( new AtomicCondition ( "//Profile/ServiceClass" , "VREManagement" ) ) ;
query . addAtomicConditions ( new AtomicCondition ( "//Profile/ServiceName" , "ResourceManager" ) ) ;
List < GCUBERunningInstance > result = client . execute ( query , scope ) ;
List < GCUBERunningInstance > toReturn = new ArrayList < GCUBERunningInstance > ( ) ;
for ( GCUBERunningInstance ri : result ) {
if ( ri . getScopes ( ) . containsValue ( scope ) ) {
toReturn . add ( ri ) ;
}
}
System . out . println ( "Found " + toReturn . size ( ) + " ResourceManager in scope: " + scope ) ;
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 < ResourceBinderPortType > getResourceManagers ( final GCUBEScope scope ) throws ResourceAccessException , ResourceParameterException {
Assertion < ResourceParameterException > checker = new Assertion < ResourceParameterException > ( ) ;
checker . validate ( scope ! = null , new ResourceParameterException ( "Invalid scope" ) ) ;
List < GCUBERunningInstance > resourceManagerList = null ;
try {
resourceManagerList = this . getResourceManagerFromScope ( scope ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
throw new ResourceAccessException ( "Cannot retrieve the managers for resource: " + this . getID ( ) ) ;
}
List < ResourceBinderPortType > retval = new Vector < ResourceBinderPortType > ( ) ;
if ( resourceManagerList . isEmpty ( ) ) {
System . out . println ( "\n\n\n\n\n****** Schianta qui size: " + resourceManagerList . size ( ) + " scope: " + scope ) ;
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 < ReportingPortType > getResourceReportManagers ( final GCUBEScope scope ) throws ResourceAccessException , ResourceParameterException {
Assertion < ResourceParameterException > checker = new Assertion < ResourceParameterException > ( ) ;
checker . validate ( scope ! = null , new ResourceParameterException ( "Invalid scope" ) ) ;
List < GCUBERunningInstance > resourceManagerList = null ;
try {
resourceManagerList = this . getResourceManagerFromScope ( scope ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
throw new ResourceAccessException ( "Cannot retrieve the managers for resource: " + this . getID ( ) ) ;
}
List < ReportingPortType > retval = new Vector < ReportingPortType > ( ) ;
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
@ -361,21 +200,22 @@ public abstract class AbstractResourceManager {
* @throws ResourceAccessException if no manager can be instantiated
* @throws ResourceParameterException if the parameters are invalid
* /
public final ResourceBinderPortType getResourceManager ( final GCUBEScope scope )
throws AbstractResourceException {
public final RMBinderLibrary getResourceManager ( String scope ) throws AbstractResourceException {
String currScope = ScopeProvider . instance . get ( ) ;
ScopeBean bscope = new ScopeBean ( scope ) ;
if ( bscope . is ( Type . VRE ) ) {
scope = bscope . enclosingScope ( ) . toString ( ) ;
}
ScopeProvider . instance . set ( scope ) ;
ServerConsole . info ( LOG_PREFIX , "Getting Resource Manager in scope [" + scope . toString ( ) + "]" ) ;
List < ResourceBinderPortType > 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 ;
RMBinderLibrary rml = Proxies . binderService ( ) . build ( ) ;
if ( rml = = null ) { // no managers found
throw new ResourceAccessException ( "Unable to find ResourceManagers for resource " + this . getType ( ) + " in scope: " + scope . toString ( ) ) ;
}
// no managers found
throw new ResourceAccessException ( "Unable to find ResourceManagers for resource " + this . getType ( ) + " in scope: " + scope . toString ( ) ) ;
ScopeProvider . instance . set ( currScope ) ;
return rml ;
}
/ * *
* The report resource manager needed to handle the resource in a given scope .
* @param scope the scope in which to operate
@ -383,19 +223,21 @@ public abstract class AbstractResourceManager {
* @throws ResourceAccessException if no manager can be instantiated
* @throws ResourceParameterException if the parameters are invalid
* /
public final ReportingPortType getReportResourceManager ( final GCUBEScope scope )
throws AbstractResourceException {
public final RMReportingLibrary getReportResourceManager ( String scope ) throws AbstractResourceException {
String currScope = ScopeProvider . instance . get ( ) ;
ScopeBean bscope = new ScopeBean ( scope ) ;
if ( bscope . is ( Type . VRE ) ) {
scope = bscope . enclosingScope ( ) . toString ( ) ;
}
ScopeProvider . instance . set ( scope ) ;
ServerConsole . info ( LOG_PREFIX , "Getting Resource Manager in scope [" + scope . toString ( ) + "]" ) ;
List < ReportingPortType > 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 ;
RMReportingLibrary rml = Proxies . reportingService ( ) . build ( ) ;
if ( rml = = null ) { // no managers found
throw new ResourceAccessException ( "Unable to find ResourceManagers for resource " + this . getType ( ) + " in scope: " + scope . toString ( ) ) ;
}
// no managers found
throw new ResourceAccessException ( "Unable to find ResourceManagers for resource " + this . getType ( ) + " in scope: " + scope . toString ( ) ) ;
ScopeProvider . instance . set ( currScope ) ;
return rml ;
}
/ * *
@ -408,7 +250,7 @@ public abstract class AbstractResourceManager {
* @return
* @throws ResourceOperationException
* /
private String bindToScope ( final GCUBEScope targetScope ) throws AbstractResourceException {
private String bindToScope ( final String targetScope ) throws AbstractResourceException {
Assertion < AbstractResourceException > checker = new Assertion < AbstractResourceException > ( ) ;
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." ) ) ;
@ -416,37 +258,47 @@ public abstract class AbstractResourceManager {
ServerConsole . trace (
LOG_PREFIX ,
"[BIND-SCOPE-ENTER] Adding " + this . getType ( ) + " " + this . getID ( ) + " to scope [" +
targetScope . toString ( ) + "]" ) ;
targetScope . toString ( ) + "]" ) ;
AddResourcesParameters addParam = new AddResourcesParameters ( ) ;
ResourceItem toAdd = new ResourceItem ( ) ;
toAdd . setI D ( this . getID ( ) ) ;
toAdd . setI d ( this . getID ( ) ) ;
toAdd . setType ( this . getType ( ) . name ( ) ) ;
ResourceList r = new ResourceList ( ) ;
r . setResource ( new ResourceItem [ ] { toAdd } ) ;
ArrayList < ResourceItem > temp = new ArrayList < ResourceItem > ( ) ;
temp . add ( toAdd ) ;
r . setResource ( temp ) ;
addParam . setResources ( r ) ;
addParam . setTargetScope ( targetScope . toString ( ) ) ;
R esourceBinderPortType manager = this . getResourceManager ( targetScope ) ;
R MBinderLibrary manager = this . getResourceManager ( targetScope ) ;
try {
String curr = ScopeProvider . instance . get ( ) ;
ScopeBean scope = new ScopeBean ( targetScope ) ;
if ( scope . is ( Type . VRE ) ) {
ScopeProvider . instance . set ( scope . enclosingScope ( ) . toString ( ) ) ;
} else
ScopeProvider . instance . set ( targetScope ) ;
String reportID = manager . addResources ( addParam ) ;
ServerConsole . trace (
LOG_PREFIX ,
"[BIND-SCOPE-EXIT] Applyed Adding " + this . getType ( ) + " " + this . getID ( ) + " to scope [" +
targetScope . toString ( ) + "]... reportID: " + reportID ) ;
targetScope . toString ( ) + "]... reportID: " + reportID ) ;
RMReportingLibrary pt = this . getReportResourceManager ( scope . toString ( ) ) ;
ReportingPortType pt = this . getReportResourceManager ( targetScope ) ;
return pt . getReport ( reportID ) ;
String toReturn = pt . getReport ( reportID ) ;
ScopeProvider . instance . set ( curr ) ;
return toReturn ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
ServerConsole . trace (
LOG_PREFIX ,
"[BIND-SCOPE-EXIT] [FAILURE]" ) ;
throw new ResourceOperationException ( "During resource::addToScope: " + e . getMessage ( ) ) ;
"[BIND-SCOPE-EXIT] [FAILURE]" ) ;
throw new ResourceOperationException ( "During resource::addToScope: " + targetScope + " Message: \n" + e . getMessage ( ) ) ;
}
}
@ -457,9 +309,7 @@ public abstract class AbstractResourceManager {
* @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 {
public final String addToExistingScope ( final ScopeBean sourceScope , final ScopeBean targetScope ) throws AbstractResourceException {
Assertion < AbstractResourceException > checker = new Assertion < AbstractResourceException > ( ) ;
checker . validate ( sourceScope ! = null , new ResourceParameterException ( "Invalid parameter sourceScope. null not allowed." ) ) ;
checker . validate ( targetScope ! = null , new ResourceParameterException ( "Invalid parameter targetScope. null not allowed." ) ) ;
@ -470,34 +320,36 @@ public abstract class AbstractResourceManager {
ServerConsole . trace (
LOG_PREFIX ,
"[ADD-ToExistingScope] Adding from scope [" +
sourceScope . toString ( ) +
"] to existing scope [" +
targetScope . toString ( ) +
"] " + this . getType ( ) + " " + this . getID ( ) ) ;
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 . ge tT ype( ) = = Type . VO & & targetScope . ge tT ype( ) = = Type . VO ) {
sourceScope . type( ) = = Type . VO & & targetScope . type( ) = = 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 . isE ncl osedI n( sourceScope ) ) {
if ( ! targetScope . toStr ing( ) . contains ( sourceScope . toString ( ) ) ) {
throw new ResourceOperationException (
"You are not allowed to apply to this scope. Target scope is not enclosed in the source one." ) ;
"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 ) ) ;
targetScope . toString ( ) + " the remote report ID is: " +
this . bindToScope ( targetScope . toString ( ) ) , true ) ) ;
return report . getXML ( ) ;
}
/ * *
* Similar to the { @link AbstractResourceManager # addToExistingScope } method but involves
* two scopes of type VO .
@ -510,17 +362,16 @@ public abstract class AbstractResourceManager {
* @return
* @throws AbstractResourceException
* /
public final String copyFromToVO ( final GCUBEScope sourceScope , final GCUBEScope targetScope )
throws AbstractResourceException {
public final String copyFromToVO ( final ScopeBean sourceScope , final ScopeBean targetScope ) throws AbstractResourceException {
Assertion < AbstractResourceException > checker = new Assertion < AbstractResourceException > ( ) ;
checker . validate (
sourceScope ! = null & & sourceScope . ge tT ype( ) = = Type . VO ,
sourceScope ! = null & & sourceScope . type( ) = = Type . VO ,
new ResourceParameterException ( "The sourceScope is invalid or not of type VO." ) ) ;
checker . validate (
targetScope ! = null & & targetScope . ge tT ype( ) = = Type . VO ,
targetScope ! = null & & targetScope . type( ) = = Type . VO ,
new ResourceParameterException ( "The targetScope is invalid or not of type VO." ) ) ;
checker . validate (
sourceScope . g etE nclosingScope( ) = = targetScope . g etE nclosingScope( ) ,
sourceScope . enclosingScope( ) = = targetScope . enclosingScope( ) ,
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." ) ) ;
@ -528,18 +379,24 @@ public abstract class AbstractResourceManager {
new ResourceAccessException ( "Operation not allowed on resources with no ID." ) ) ;
// Phase 1. retrieve the resource to copy
GCUBE Resource resStub = this . get GCUBE Resource( sourceScope ) ;
Resource resStub = this . get Resource( sourceScope ) ;
// Phase 2. Before to register the resource, the scope must be
// Phase 2. Before registering the resource, the scope must be
// bound to the local GCUBEResource
this . bindToScope ( targetScope );
this . bindToScope ( targetScope .toString ( ) );
// Phase 3. Register to the new VO through the ISPublisher
String currentScope = ScopeProvider . instance . get ( ) ;
ScopeProvider . instance . set ( targetScope . toString ( ) ) ;
Resource toReturn = null ;
try {
return this . getISPublisher ( ) . registerGCUBEResource ( resStub , targetScope , this . getSecurityManager ( ) ) ;
toReturn = this . getRegistryPublisher ( ) . create ( resStub ) ;
} catch ( Exception e ) {
throw new ResourceAccessException ( e . getMessage ( ) ) ;
}
ScopeProvider . instance . set ( currentScope ) ;
return toReturn . id ( ) ;
}
/ * *
@ -550,7 +407,7 @@ public abstract class AbstractResourceManager {
* @return
* @throws AbstractResourceException
* /
protected abstract GCUBE Resource build GCUBE Resource( final String xmlRepresentation ) throws AbstractResourceException ;
protected abstract Resource build Resource( final String xmlRepresentation ) throws AbstractResourceException ;
/ * *
* From a resource retrieves its GCUBEResource depending on the scope in which it is
@ -559,8 +416,8 @@ public abstract class AbstractResourceManager {
* @return
* @throws AbstractResourceException
* /
public final GCUBE Resource get GCUBE Resource( final GCUBE Scope scope ) throws AbstractResourceException {
return this . build GCUBE Resource( this . getXMLDescription ( scope ) ) ;
public final Resource get Resource( final ScopeBean scope ) throws AbstractResourceException {
return this . build Resource( this . getXMLDescription ( scope ) ) ;
}
/ * *
@ -570,44 +427,39 @@ public abstract class AbstractResourceManager {
* @return
* @throws AbstractResourceException
* /
protected final String getXMLDescription ( final GCUBE Scope scope ) throws AbstractResourceException {
protected final String getXMLDescription ( final ScopeBean scope ) throws AbstractResourceException {
Assertion < AbstractResourceException > checker = new Assertion < AbstractResourceException > ( ) ;
checker . validate ( this . getID ( ) ! = null , new ResourceAccessException ( "Cannot execute on resources with no ID." ) ) ;
String currScope = ScopeProvider . instance . get ( ) ;
ScopeProvider . instance . set ( scope . toString ( ) ) ;
// 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 ) ;
}
Query query = new QueryBox ( "for $resource in collection('/db/Profiles/" + this . getType ( ) . name ( ) + "')//Resource " +
"where ( $resource/ID/string() eq '" +
this . getID ( ) +
"') " +
"return $resource" ) ;
XMLResult resDescription = null ;
try {
resDescription = this . getISClient ( ) . execute ( query , scope ) . get ( 0 ) ;
return resDescription . toString ( ) ;
} catch ( Exception e ) {
DiscoveryClient < String > client = client ( ) ;
List < String > results = client . submit ( query ) ;
if ( results = = null | | results . isEmpty ( ) )
throw new ResourceAccessException ( "Cannot retrieve the IS profile for resource: " + this . getID ( ) +
" in scope: " + scope . toString ( ) ) ;
}
ScopeProvider . instance . set ( currScope ) ;
return results . get ( 0 ) . toString ( ) ;
}
/ * *
* The first phase of remove f o rm scope .
* The first phase of remove f ro m scope .
* This is common to all the resources ( RI and GHNs ) .
* @param scope
* @return
* @throws AbstractResourceException
* /
private String basicRemoveFromScope ( final GCUBEScope scope )
throws AbstractResourceException {
private String basicRemoveFromScope ( final ScopeBean scope ) throws AbstractResourceException {
Assertion < AbstractResourceException > checker = new Assertion < AbstractResourceException > ( ) ;
checker . validate ( scope ! = null , new ResourceParameterException ( "Invalid parameter scope. null not allowed." ) ) ;
checker . validate ( this . getID ( ) ! = null , new ResourceOperationException ( "Invalid ID. null not allowed." ) ) ;
@ -618,17 +470,22 @@ public abstract class AbstractResourceManager {
RemoveResourcesParameters params = new RemoveResourcesParameters ( ) ;
ResourceItem toRemove = new ResourceItem ( ) ;
toRemove . setI D ( this . getID ( ) ) ;
toRemove . setI d ( this . getID ( ) ) ;
toRemove . setType ( this . getType ( ) . name ( ) ) ;
ResourceList resourcesToRemove = new ResourceList ( ) ;
resourcesToRemove . setResource ( new ResourceItem [ ] { toRemove } ) ;
params . setResources ( resourcesToRemove ) ;
params . setTargetScope ( scope . toString ( ) ) ;
ArrayList < ResourceItem > temp = new ArrayList < ResourceItem > ( ) ;
temp . add ( toRemove ) ;
resourcesToRemove . setResource ( temp ) ;
params . resources = resourcesToRemove ;
params . targetScope = scope . toString ( ) ;
ServerConsole . trace ( LOG_PREFIX , "[REMOVE-FROM-SCOPE] Sending the Remove Resource request...." ) ;
try {
ResourceBinderPortType manager = this . getResourceManager ( scope ) ;
RMBinderLibrary manager = this . getResourceManager ( scope . toString ( ) ) ;
String currScope = ScopeProvider . instance . get ( ) ;
ScopeProvider . instance . set ( scope . toString ( ) ) ;
retval = manager . removeResources ( params ) ;
ScopeProvider . instance . set ( currScope ) ;
} catch ( Exception e ) {
throw new ResourceOperationException ( "During removeFrom scope of "
+ this . getType ( )
@ -641,59 +498,17 @@ public abstract class AbstractResourceManager {
* 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 {
public final String removeFromScope ( final ScopeBean scope ) throws AbstractResourceException {
Assertion < AbstractResourceException > checker = new Assertion < AbstractResourceException > ( ) ;
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 currScope = ScopeProvider . instance . get ( ) ;
ScopeProvider . instance . set ( scope . toString ( ) ) ;
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 < GCUBEScope > 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 ) ;
}
}
ScopeProvider . instance . set ( currScope ) ;
return retval ;
}
@ -704,14 +519,10 @@ public abstract class AbstractResourceManager {
* @param scopes
* @return
* /
protected List < GCUBEScope > validateScopes ( final GCUBEScope [ ] scopes ) {
List < GCUBEScope > retval = new Vector < GCUBEScope > ( ) ;
for ( GCUBEScope scope : scopes ) {
try {
retval . add ( ScopeManager . getScope ( scope . toString ( ) ) ) ;
} catch ( Exception e ) {
ServerConsole . error ( LOG_PREFIX , e ) ;
}
protected List < ScopeBean > validateScopes ( final String [ ] scopes ) {
List < ScopeBean > retval = new Vector < ScopeBean > ( ) ;
for ( int i = 0 ; i < scopes . length ; i + + ) {
retval . add ( new ScopeBean ( scopes [ i ] ) ) ;
}
return retval ;
}
@ -722,11 +533,11 @@ public abstract class AbstractResourceManager {
* @param scope where the resource is bound
* @throws AbstractResourceException
* /
public final void forceDelete ( final GCUBE Scope scope ) throws AbstractResourceException {
public final void forceDelete ( final ScopeBean scope ) throws AbstractResourceException {
ServerConsole . trace ( LOG_PREFIX , "[DELETE] [DELETE-BRANCH] deleting resource since no more scopes remained" ) ;
Resource resStub = this . getResource ( scope ) ;
try {
this . getISPublisher ( ) . removeGCUBEResource (
this . getID ( ) , this . getType ( ) . name ( ) , scope , this . getSecurityManager ( ) ) ;
this . getRegistryPublisher ( ) . remove ( resStub ) ;
} catch ( Exception e ) {
ServerConsole . error ( LOG_PREFIX , e ) ;
}
@ -737,64 +548,37 @@ public abstract class AbstractResourceManager {
* According to the
* < a href = "https://gcube.wiki.gcube-system.org/gcube/index.php/Programmatic_Administration_Interface" >
* official wiki < / a > the resource is deleted only if is not bound in other scopes . Otherwise this
* operation simply corresponds to the in deep remove from scope .
* operation simply corresponds to remove from scope .
* @param scope
* @throws ResourceOperationException
* /
public final void delete ( final GCUBE Scope scope ) throws AbstractResourceException {
public final void delete ( final ScopeBean scope ) throws AbstractResourceException {
Assertion < AbstractResourceException > checker = new Assertion < AbstractResourceException > ( ) ;
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 ( ) ) ;
String currScope = ScopeProvider . instance . get ( ) ;
ScopeProvider . instance . set ( scope . toString ( ) ) ;
GCUBEResource resStub = this . getGCUBEResource ( scope ) ;
List < GCUBEScope > boundedScopes = this . validateScopes ( resStub . getScopes ( ) . values ( ) . toArray ( new GCUBEScope [ ] { } ) ) ;
Resource resStub = this . getResource ( scope ) ;
List < ScopeBean > boundedScopes = this . validateScopes ( resStub . scopes ( ) . toArray ( new String [ 0 ] ) ) ;
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(<scope to remove>)
// 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 ) {
if ( boundedScopes . size ( ) > 1 )
ServerConsole . trace ( LOG_PREFIX , "[DELETE] [DELETE-BRANCH] deleting resource is a remove from scope since more than 1 scope is present " + resStub . id ( ) ) ;
else if ( boundedScopes . size ( ) = = 1 )
ServerConsole . trace ( LOG_PREFIX , "[DELETE] [DELETE-BRANCH] deleting resource since is bound to 1 scope only " + resStub . id ( ) ) ;
try {
this . getRegistryPublisher ( ) . remove ( resStub ) ;
} 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 ) ;
}
}
}
ScopeProvider . instance . set ( currScope ) ;
}
}