2008-12-18 20:08:37 +01:00
package org.gcube.vremanagement.vremodeler.impl.thread ;
import java.io.ByteArrayInputStream ;
import java.io.IOException ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.ArrayList ;
import java.util.List ;
2009-01-12 19:04:07 +01:00
import javax.xml.parsers.DocumentBuilder ;
2008-12-18 20:08:37 +01:00
import javax.xml.parsers.DocumentBuilderFactory ;
import javax.xml.parsers.ParserConfigurationException ;
import javax.xml.xpath.XPath ;
import javax.xml.xpath.XPathConstants ;
import javax.xml.xpath.XPathExpressionException ;
import javax.xml.xpath.XPathFactory ;
2010-06-04 17:07:28 +02:00
import org.apache.axis.message.addressing.EndpointReferenceType ;
import org.gcube.common.core.contexts.GCUBERemotePortTypeContext ;
import org.gcube.common.core.contexts.GHNContext ;
2008-12-18 20:08:37 +01:00
import org.gcube.common.core.faults.GCUBEFault ;
2010-06-04 17:07:28 +02:00
import org.gcube.common.core.informationsystem.client.ISClient ;
import org.gcube.common.core.informationsystem.client.queries.GCUBERIQuery ;
2008-12-18 20:08:37 +01:00
import org.gcube.common.core.resources.GCUBECollection ;
2009-02-09 19:43:07 +01:00
import org.gcube.common.core.resources.GCUBEGenericResource ;
2008-12-18 20:08:37 +01:00
import org.gcube.common.core.resources.GCUBEMCollection ;
2010-06-04 17:07:28 +02:00
import org.gcube.common.core.resources.GCUBERunningInstance ;
2008-12-18 20:08:37 +01:00
import org.gcube.common.core.scope.GCUBEScope ;
2009-01-13 00:29:55 +01:00
import org.gcube.common.core.utils.handlers.GCUBEServiceClientImpl ;
2010-06-04 17:07:28 +02:00
import org.gcube.common.core.utils.handlers.GCUBEServiceHandler.NoQueryResultException ;
2008-12-18 20:08:37 +01:00
import org.gcube.common.core.utils.logging.GCUBELog ;
2009-05-08 19:58:31 +02:00
import org.gcube.common.resources.kxml.KGCUBEResource ;
2010-05-13 18:20:41 +02:00
import org.gcube.vremanagement.resourcemanager.stubs.resourcemanager.AddResourcesParameters ;
import org.gcube.vremanagement.resourcemanager.stubs.resourcemanager.CreateScopeParameters ;
import org.gcube.vremanagement.resourcemanager.stubs.resourcemanager.OptionsParameters ;
import org.gcube.vremanagement.resourcemanager.stubs.resourcemanager.ResourceItem ;
import org.gcube.vremanagement.resourcemanager.stubs.resourcemanager.ResourceList ;
import org.gcube.vremanagement.resourcemanager.stubs.resourcemanager.ResourceManagerPortType ;
import org.gcube.vremanagement.resourcemanager.stubs.resourcemanager.ScopeOption ;
import org.gcube.vremanagement.resourcemanager.stubs.resourcemanager.ServiceItem ;
import org.gcube.vremanagement.resourcemanager.stubs.resourcemanager.ServiceList ;
2010-06-04 17:07:28 +02:00
import org.gcube.vremanagement.resourcemanager.stubs.resourcemanager.service.ResourceManagerServiceAddressingLocator ;
2008-12-18 20:08:37 +01:00
import org.gcube.vremanagement.vremodeler.db.DBInterface ;
2009-01-14 18:30:25 +01:00
import org.gcube.vremanagement.vremodeler.impl.ModelerContext ;
import org.gcube.vremanagement.vremodeler.impl.ModelerResource ;
2009-01-16 19:37:02 +01:00
import org.gcube.vremanagement.vremodeler.impl.ModelerService ;
2010-06-04 17:07:28 +02:00
import org.gcube.vremanagement.vremodeler.impl.ServiceContext ;
2010-06-04 17:03:10 +02:00
import org.gcube.vremanagement.vremodeler.impl.util.Pair ;
import org.gcube.vremanagement.vremodeler.impl.util.ResourceManagerServiceHandler ;
2009-01-22 20:22:51 +01:00
import org.gcube.vremanagement.vremodeler.portallayout.CollectionResourceCreation ;
2009-02-13 20:04:00 +01:00
import org.gcube.vremanagement.vremodeler.portallayout.GenericResourcePortlet ;
2009-02-23 16:08:27 +01:00
import org.gcube.vremanagement.vremodeler.portallayout.LayoutCreation ;
2010-05-13 18:20:41 +02:00
import org.gcube.vremanagement.vremodeler.stubs.VREState ;
2009-01-14 18:30:25 +01:00
import org.globus.wsrf.ResourceException ;
2008-12-18 20:08:37 +01:00
import org.w3c.dom.Document ;
import org.w3c.dom.NodeList ;
import org.xml.sax.SAXException ;
2010-10-29 19:36:10 +02:00
public class DeployVREonGHNs extends Thread {
2008-12-18 20:08:37 +01:00
2009-01-16 19:37:02 +01:00
private static GCUBELog logger = new GCUBELog ( ModelerService . class ) ;
2009-01-14 20:29:04 +01:00
2009-01-14 18:30:25 +01:00
private ModelerResource wsResource = null ;
2008-12-18 20:08:37 +01:00
private String resourceId ;
2009-01-14 12:11:12 +01:00
private GCUBEScope startingScope ;
2008-12-18 20:08:37 +01:00
private String vreName = null ;
2009-02-09 19:43:07 +01:00
private CollectionResourceCreation collectionResourceCreation ;
2008-12-18 20:08:37 +01:00
2010-10-29 19:36:10 +02:00
public DeployVREonGHNs ( String resourceId , GCUBEScope scope ) {
2008-12-18 20:08:37 +01:00
this . resourceId = resourceId ;
2009-01-14 12:11:12 +01:00
this . startingScope = scope ;
2008-12-18 20:08:37 +01:00
}
2009-01-14 18:30:25 +01:00
private ModelerResource getResource ( ) throws ResourceException {
if ( wsResource = = null ) wsResource = ( ModelerResource ) ModelerContext . getPortTypeContext ( ) . getWSHome ( ) . find ( ModelerContext . getPortTypeContext ( ) . makeKey ( resourceId ) ) ;
return wsResource ;
}
2010-06-04 17:03:10 +02:00
/ * *
*
* /
2008-12-18 20:08:37 +01:00
public void run ( ) {
try {
2010-02-22 14:17:57 +01:00
ResultSet resRelatedGHN = DBInterface . queryDB ( " select VRERELATEDGHN.ghnid, GHN.domain, VRERELATEDGHN.ISCANDIDATE from VRERELATEDGHN, GHN where GHN.id=VRERELATEDGHN.ghnid and VRERELATEDGHN.vreid=' " + this . resourceId + " ' ORDER BY GHN.host; " ) ;
2009-02-13 20:04:00 +01:00
2010-06-04 17:03:10 +02:00
List < Pair < String , String > > GHNList = new ArrayList < Pair < String , String > > ( ) ;
2010-02-22 14:17:57 +01:00
String candidateGHN = null ;
2010-06-04 17:03:10 +02:00
boolean isCandidatePresent = false ;
2010-02-22 14:17:57 +01:00
while ( resRelatedGHN . next ( ) ) {
2010-06-04 17:03:10 +02:00
if ( resRelatedGHN . getBoolean ( 3 ) ) {
2010-02-22 14:17:57 +01:00
candidateGHN = resRelatedGHN . getString ( 1 ) ;
2010-06-04 17:03:10 +02:00
isCandidatePresent = true ;
} else GHNList . add ( new Pair < String , String > ( resRelatedGHN . getString ( 1 ) , resRelatedGHN . getString ( 2 ) ) ) ;
2010-02-22 14:17:57 +01:00
}
2010-06-04 17:03:10 +02:00
if ( ! ( GHNList . size ( ) > = 2 | | ( GHNList . size ( ) = = 1 & & isCandidatePresent ) ) ) {
getResource ( ) . setFirstReport ( VREState . FAILED . getValue ( ) + " : wrong ghn definition " ) ;
getResource ( ) . store ( ) ;
logger . info ( " Deploying of the VRE with id " + this . resourceId + " FAILED for wrong ghn definition " ) ;
2010-05-13 18:20:41 +02:00
throw new Exception ( " No ghn Selected (at least 2 GHNs have to be selected) " ) ;
2009-02-16 19:31:32 +01:00
}
2009-01-19 16:20:22 +01:00
2010-02-22 14:17:57 +01:00
if ( ! deployManagerOnVRE ( GHNList , candidateGHN ) ) {
//transforming the Pair<String, String> to a ghnid List<String>
2009-02-16 19:31:32 +01:00
List < String > ghnEpuredList = new ArrayList < String > ( ) ;
2010-06-04 17:03:10 +02:00
for ( Pair < String , String > cpl : GHNList ) ghnEpuredList . add ( cpl . first ) ;
2009-02-16 19:31:32 +01:00
if ( ! createVRE ( ghnEpuredList ) ) {
2010-05-13 18:20:41 +02:00
DBInterface . ExecuteUpdate ( " UPDATE VRE SET STATUS=' " + VREState . DEPLOYED + " ' WHERE VRE.id=' " + this . resourceId + " '; " ) ;
2009-01-19 18:09:28 +01:00
logger . info ( " Deploying of the VRE with id " + this . resourceId + " FINISHED " ) ;
}
else {
logger . info ( " Deploying of the VRE with id " + this . resourceId + " FAILED on second step " ) ;
2010-06-04 17:03:10 +02:00
getResource ( ) . setSecondReport ( VREState . FAILED . getValue ( ) ) ;
2009-01-19 18:09:28 +01:00
throw new Exception ( " Error Deployng the VRE failed on second step " ) ;
}
2009-01-19 16:20:22 +01:00
} else {
2010-06-04 17:03:10 +02:00
getResource ( ) . setFirstReport ( VREState . FAILED . getValue ( ) ) ;
2009-02-13 12:00:20 +01:00
getResource ( ) . store ( ) ;
2009-01-19 18:09:28 +01:00
logger . info ( " Deploying of the VRE with id " + this . resourceId + " FAILED on first step " ) ;
throw new Exception ( " Error Deployng the VRE filed on first step " ) ;
2009-01-19 16:20:22 +01:00
}
2009-02-13 12:00:20 +01:00
2008-12-18 20:08:37 +01:00
} catch ( Exception e ) {
2009-01-19 16:20:22 +01:00
try {
2010-05-13 18:20:41 +02:00
DBInterface . ExecuteUpdate ( " UPDATE VRE SET STATUS=' " + VREState . FAILED + " ' WHERE VRE.id=' " + this . resourceId + " '; " ) ;
2009-01-19 18:09:28 +01:00
} catch ( Exception e1 ) {
2010-06-04 17:07:28 +02:00
logger . error ( " impossible to update the VRE Status " , e1 ) ;
2009-01-19 16:20:22 +01:00
}
2010-06-04 17:07:28 +02:00
logger . error ( " Error deploying the VRE with id " + this . resourceId + " " , e ) ;
2008-12-18 20:08:37 +01:00
}
}
2010-06-04 17:03:10 +02:00
private boolean deployManagerOnVRE ( List < Pair < String , String > > ghnList , String candidateGHN ) throws GCUBEFault {
2009-01-16 19:37:02 +01:00
String vreName = null ;
2008-12-18 20:08:37 +01:00
try {
DBInterface . ExecuteUpdate ( " UPDATE VRE SET STATUS='Deploying' WHERE VRE.id=' " + this . resourceId + " '; " ) ;
ResultSet resGenericInfo = DBInterface . queryDB ( " select VRE.name from VRE where VRE.id=' " + this . resourceId + " '; " ) ;
if ( ! resGenericInfo . next ( ) ) {
throw new GCUBEFault ( " The VRE with ID " + this . resourceId + " cannot be retrieved on the DB " ) ;
}
2009-01-12 23:58:33 +01:00
vreName = resGenericInfo . getString ( 1 ) ;
2009-10-01 16:59:26 +02:00
} catch ( Exception e ) {
2008-12-18 20:08:37 +01:00
logger . error ( " HSQLDB Error " + e ) ;
throw new GCUBEFault ( e ) ;
}
2010-06-04 17:03:10 +02:00
ResourceManagerServiceHandler < Boolean , Object [ ] > resourceManagerHandler = new ResourceManagerServiceHandler < Boolean , Object [ ] > ( ) {
2008-12-18 20:08:37 +01:00
2010-02-22 14:17:57 +01:00
@SuppressWarnings ( { " static-access " , " unchecked " } )
2008-12-18 20:08:37 +01:00
@Override
2010-05-13 18:20:41 +02:00
protected Boolean makeCall ( ResourceManagerPortType resourceManagerPT )
2008-12-18 20:08:37 +01:00
throws Exception {
2009-01-19 16:20:22 +01:00
String report ;
2009-01-12 18:35:50 +01:00
try {
2010-02-22 14:17:57 +01:00
String vreName = ( String ) this . getParameter ( ) [ 0 ] ;
2010-06-04 17:03:10 +02:00
List < Pair < String , String > > ghnList = ( List < Pair < String , String > > ) this . getParameter ( ) [ 1 ] ;
2010-02-22 14:17:57 +01:00
String candidateGHN = ( String ) this . getParameter ( ) [ 2 ] ;
2009-01-12 18:35:50 +01:00
AddResourcesParameters arp = new AddResourcesParameters ( ) ;
ServiceList services = new ServiceList ( ) ;
ServiceItem [ ] service = new ServiceItem [ 1 ] ;
2009-01-12 23:27:45 +01:00
service [ 0 ] = new ServiceItem ( ) ;
2009-01-12 18:35:50 +01:00
service [ 0 ] . setServiceClass ( " VREManagement " ) ;
2010-06-04 17:03:10 +02:00
service [ 0 ] . setServiceName ( " ResourceManager " ) ;
2009-01-12 18:35:50 +01:00
service [ 0 ] . setServiceVersion ( " 1.00.00 " ) ;
services . setService ( service ) ;
2009-02-16 19:31:32 +01:00
2010-02-22 14:17:57 +01:00
//preparing the GHN where will be deployed the VREManager
if ( candidateGHN = = null ) {
2010-06-04 17:03:10 +02:00
Pair < String , String > ghnId = null ;
for ( Pair < String , String > cpl : ghnList ) {
2010-02-22 14:17:57 +01:00
if ( this . usedhost . contains ( cpl . second ) ) {
ghnId = cpl ;
break ;
}
2009-02-16 19:31:32 +01:00
}
2010-02-22 14:17:57 +01:00
if ( ghnId = = null ) ghnId = ghnList . get ( 0 ) ;
2010-06-04 17:03:10 +02:00
logger . debug ( " deploying the ResourceManager for the vre on " + ghnId . second ) ;
2010-02-22 14:17:57 +01:00
services . setGHN ( new String [ ] { ghnId . first } ) ;
ghnList . remove ( ghnId ) ;
} else services . setGHN ( new String [ ] { candidateGHN } ) ;
2009-02-16 19:31:32 +01:00
2009-01-12 18:35:50 +01:00
arp . setServices ( services ) ;
2010-02-22 14:17:57 +01:00
arp . setTargetScope ( this . scope + " / " + vreName ) ;
2010-05-13 18:20:41 +02:00
String reportId = resourceManagerPT . addResources ( arp ) ;
2009-02-20 19:29:41 +01:00
2009-01-12 18:35:50 +01:00
int attempt = 0 ;
do {
2009-01-14 19:55:26 +01:00
Thread . currentThread ( ) . sleep ( 20000 ) ;
2010-05-13 18:20:41 +02:00
report = resourceManagerPT . getReport ( reportId ) ;
2009-01-12 18:35:50 +01:00
attempt + + ;
2009-02-13 12:00:20 +01:00
getResource ( ) . setFirstReport ( report ) ;
getResource ( ) . store ( ) ;
2009-01-12 18:35:50 +01:00
} while ( ! isDeploymentStatusFinished ( report ) & & attempt < 10 ) ;
2009-02-20 19:29:41 +01:00
2009-01-12 18:35:50 +01:00
} catch ( Exception e ) { e . printStackTrace ( ) ; throw e ; }
2009-01-19 19:33:36 +01:00
logger . trace ( " report step 1: " + report ) ;
logger . info ( " is something failed in the first step? " + isSomethingFailed ( report ) ) ;
2009-01-19 16:20:22 +01:00
return isSomethingFailed ( report ) ;
2008-12-18 20:08:37 +01:00
}
} ;
2009-01-16 12:31:03 +01:00
2010-06-04 17:03:10 +02:00
resourceManagerHandler . clearBlackboard ( ) ;
resourceManagerHandler . setHandled ( new GCUBEServiceClientImpl ( ) ) ;
resourceManagerHandler . setParameter ( new Object [ ] { vreName , ghnList , candidateGHN } ) ;
resourceManagerHandler . scope = this . startingScope ;
2009-01-14 12:11:12 +01:00
2008-12-18 20:08:37 +01:00
try {
2010-06-04 17:03:10 +02:00
resourceManagerHandler . run ( ) ;
2008-12-18 20:08:37 +01:00
} catch ( Exception e ) {
2009-01-16 12:31:03 +01:00
logger . error ( " DeployingVRE step 1 -- failed " + e ) ;
2009-01-19 16:20:22 +01:00
return false ;
2008-12-18 20:08:37 +01:00
}
this . vreName = vreName ;
2009-02-09 19:43:07 +01:00
collectionResourceCreation = new CollectionResourceCreation ( this . resourceId , this . vreName ) ;
2010-06-04 17:03:10 +02:00
return resourceManagerHandler . getReturnValue ( ) ;
2008-12-18 20:08:37 +01:00
}
2009-02-13 20:04:00 +01:00
2010-06-04 17:07:28 +02:00
@SuppressWarnings ( " static-access " )
private boolean createVRE ( List < String > ghnList ) throws Exception {
ResourceManagerPortType rmPortType = null ;
int attempt = 0 ;
do {
try {
2008-12-18 20:08:37 +01:00
try {
2010-06-04 17:07:28 +02:00
Thread . currentThread ( ) . sleep ( 30000 ) ;
} catch ( Exception et ) { }
2009-01-16 19:37:02 +01:00
2010-06-04 17:07:28 +02:00
ISClient client = GHNContext . getImplementation ( ISClient . class ) ;
GCUBERIQuery query = client . getQuery ( GCUBERIQuery . class ) ;
2008-12-18 20:08:37 +01:00
2010-06-04 17:07:28 +02:00
query . setExpression ( " declare namespace is = 'http://gcube-system.org/namespaces/informationsystem/registry'; " +
" for $outer in collection( \" /db/Profiles/RunningInstance \" )//Document/Data/is:Profile/Resource " +
" let $scope:= $outer/Scopes/Scope[string() eq ' " + GCUBEScope . getScope ( this . startingScope + " / " + this . vreName ) . toString ( ) + " '] " +
" where count($scope)>0 " +
" and $outer/Profile/ServiceName/string() eq 'ResourceManager' " +
" return $outer " ) ;
2010-06-04 17:03:10 +02:00
2010-06-04 17:07:28 +02:00
List < EndpointReferenceType > eprs = new ArrayList < EndpointReferenceType > ( ) ;
for ( GCUBERunningInstance instance : client . execute ( query , GCUBEScope . getScope ( this . startingScope + " / " + this . vreName ) ) )
eprs . add ( instance . getAccessPoint ( ) . getEndpoint ( " gcube/vremanagement/ResourceManager " ) ) ;
if ( eprs . size ( ) = = 0 ) throw new NoQueryResultException ( ) ;
ResourceManagerServiceAddressingLocator vmsal = new ResourceManagerServiceAddressingLocator ( ) ;
rmPortType = vmsal . getResourceManagerPortTypePort ( eprs . get ( 0 ) ) ;
rmPortType = GCUBERemotePortTypeContext . getProxy ( rmPortType , GCUBEScope . getScope ( this . startingScope + " / " + this . vreName ) , Integer . parseInt ( ( String ) ServiceContext . getContext ( ) . getProperty ( " resourceManagerTimeout " , true ) ) ) ;
} catch ( Exception e ) {
logger . warn ( e ) ;
logger . warn ( " the query for resourceManager returned no result, re-trying in 30 secs ( " + attempt + " ) " ) ;
} finally { attempt + + ; }
} while ( rmPortType = = null & & attempt < 10 ) ;
2008-12-18 20:08:37 +01:00
2010-06-04 17:07:28 +02:00
if ( attempt > = 10 ) throw new Exception ( " no ResourceMaanger can be retrieved for scope " + GCUBEScope . getScope ( this . startingScope + " / " + this . vreName ) . toString ( ) ) ;
ResultSet resGenericInfo = DBInterface . queryDB ( " select VRE.name, VRE.vredesigner, VRE.vremanager, VRE.IntervalTo, VRE.IntervalFrom, VRE.description from VRE where VRE.id=' " + this . resourceId + " '; " ) ;
if ( ! resGenericInfo . next ( ) ) throw new GCUBEFault ( " The VRE with ID " + this . resourceId + " cannot be retrieved " ) ;
CreateScopeParameters scopeParameter = new CreateScopeParameters ( ) ;
OptionsParameters optPar = new OptionsParameters ( ) ;
ScopeOption [ ] scopeOptionList = new ScopeOption [ 6 ] ;
ScopeOption soDesigner = new ScopeOption ( ) ;
soDesigner . setName ( " DESIGNER " ) ;
soDesigner . setValue ( resGenericInfo . getString ( " vredesigner " ) ) ;
scopeOptionList [ 0 ] = soDesigner ;
ScopeOption soCreator = new ScopeOption ( ) ;
soCreator . setName ( " CREATOR " ) ;
soCreator . setValue ( resGenericInfo . getString ( " vremanager " ) ) ;
scopeOptionList [ 1 ] = soCreator ;
ScopeOption soEndtime = new ScopeOption ( ) ;
soEndtime . setName ( " ENDTIME " ) ;
soEndtime . setValue ( KGCUBEResource . toXMLDateAndTime ( resGenericInfo . getDate ( " IntervalTo " ) ) ) ;
scopeOptionList [ 2 ] = soEndtime ;
ScopeOption soStarttime = new ScopeOption ( ) ;
soStarttime . setName ( " STARTTIME " ) ;
soStarttime . setValue ( KGCUBEResource . toXMLDateAndTime ( resGenericInfo . getDate ( " IntervalFrom " ) ) ) ;
scopeOptionList [ 3 ] = soStarttime ;
ScopeOption soDescription = new ScopeOption ( ) ;
soDescription . setName ( " DESCRIPTION " ) ;
soDescription . setValue ( resGenericInfo . getString ( " description " ) ) ;
scopeOptionList [ 4 ] = soDescription ;
ScopeOption soDisplayname = new ScopeOption ( ) ;
soDisplayname . setName ( " DISPLAYNAME " ) ;
soDisplayname . setValue ( resGenericInfo . getString ( " name " ) ) ;
scopeOptionList [ 5 ] = soDisplayname ;
optPar . setScopeOptionList ( scopeOptionList ) ;
scopeParameter . setOptionsParameters ( optPar ) ;
//creating the GenericResources for Portlets and Collection
GenericResourcePortlet . createResource ( getResource ( ) . getId ( ) , vreName ) ;
collectionResourceCreation . createAndPublish ( ) ;
LayoutCreation createdlayout = new LayoutCreation ( getResource ( ) . getId ( ) , vreName ) ;
createdlayout . createAndPublishLayout ( ) ;
//waiting few seconds to be sure that generic resources will be published
Thread . currentThread ( ) . sleep ( 120000 ) ;
//Adding the resources to the new VRE
AddResourcesParameters arp = new AddResourcesParameters ( ) ;
ResourceList rl = new ResourceList ( ) ;
List < ResourceItem > resItemList = new ArrayList < ResourceItem > ( ) ;
//retrieving Collection
try {
ResourceItem resItem ;
ResultSet resRelatedCol = DBInterface . queryDB ( " select VRERELATEDCOLLECTION.collid from VRERELATEDCOLLECTION where VRERELATEDCOLLECTION.vreid=' " + this . resourceId + " '; " ) ;
while ( resRelatedCol . next ( ) ) {
resItem = new ResourceItem ( ) ;
resItem . setID ( resRelatedCol . getString ( 1 ) ) ;
logger . debug ( " selected collection: " + resItem . getID ( ) ) ;
resItem . setType ( GCUBECollection . TYPE ) ;
resItemList . add ( resItem ) ;
}
} catch ( SQLException sqle ) { logger . error ( " Error contacting HSQLDB " + sqle ) ; }
//retrieving MDCollection with native MDFormat
try {
ResourceItem resItem ;
ResultSet resMdColNative = DBInterface . queryDB ( " select n.mdcollid from VRERELATEDMETADATAFORMAT AS s, NATIVEMDF as n where s.vreid=' " + this . resourceId + " ' and n.mdfid=s.mfid; " ) ;
while ( resMdColNative . next ( ) ) {
resItem = new ResourceItem ( ) ;
resItem . setID ( resMdColNative . getString ( 1 ) ) ;
logger . debug ( " selected MetadataCollection: " + resItem . getID ( ) ) ;
resItem . setType ( GCUBEMCollection . TYPE ) ;
resItemList . add ( resItem ) ;
}
} catch ( SQLException sqle ) {
logger . error ( " Error contacting HSQLDB " , sqle ) ;
2008-12-18 20:08:37 +01:00
}
2010-06-04 17:07:28 +02:00
//adding the CollectionResource genericResource
ResourceItem genResItem = new ResourceItem ( ) ;
genResItem . setID ( collectionResourceCreation . getCreatedResourceId ( ) ) ;
genResItem . setType ( GCUBEGenericResource . TYPE ) ;
resItemList . add ( genResItem ) ;
genResItem = new ResourceItem ( ) ;
genResItem . setID ( createdlayout . getCreatedResourceId ( ) ) ;
genResItem . setType ( GCUBEGenericResource . TYPE ) ;
resItemList . add ( genResItem ) ;
//adding the other needed resources
try {
ResourceItem resItem ;
ResultSet neededRes = DBInterface . queryDB ( " select n.id, n.type from NEEDEDRESOURCES AS n; " ) ;
while ( neededRes . next ( ) ) {
resItem = new ResourceItem ( ) ;
resItem . setID ( neededRes . getString ( 1 ) ) ;
resItem . setType ( neededRes . getString ( 2 ) ) ;
logger . debug ( " Needed GenericResources: " + resItem . getID ( ) ) ;
resItemList . add ( resItem ) ;
}
} catch ( SQLException sqle ) {
logger . error ( " Error contacting HSQLDB " + sqle ) ;
}
rl . setResource ( resItemList . toArray ( new ResourceItem [ 0 ] ) ) ;
arp . setResources ( rl ) ;
//retrieve services
ServiceList serviceList = new ServiceList ( ) ;
try {
ServiceItem servItem ;
List < ServiceItem > listService = new ArrayList < ServiceItem > ( ) ;
ResultSet resService = DBInterface . queryDB ( " select DISTINCT s.name, s.class, s.version from VRERELATEDFUNCT AS vrf, SERVICES AS s where vrf.vreid=' " + this . resourceId + " ' AND s.id=vrf.funcid; " ) ;
while ( resService . next ( ) ) {
servItem = new ServiceItem ( ) ;
servItem . setServiceClass ( resService . getString ( " class " ) ) ;
servItem . setServiceName ( resService . getString ( " name " ) ) ;
servItem . setServiceVersion ( resService . getString ( " version " ) ) ;
logger . debug ( " selected services: " + servItem . getServiceClass ( ) + " " + servItem . getServiceName ( ) + " " + servItem . getServiceVersion ( ) ) ;
listService . add ( servItem ) ;
}
serviceList . setService ( listService . toArray ( new ServiceItem [ 0 ] ) ) ;
} catch ( SQLException sqle ) { logger . error ( " Error contacting HSQLDB " + sqle ) ; }
//sets the GHNs
serviceList . setGHN ( ghnList . toArray ( new String [ 0 ] ) ) ;
arp . setServices ( serviceList ) ;
scopeParameter . setAddResourcesParameters ( arp ) ;
scopeParameter . setName ( GCUBEScope . getScope ( this . startingScope + " / " + this . vreName ) . toString ( ) ) ;
String report = " starting " ;
String reportId = rmPortType . createScope ( scopeParameter ) ;
int reportAttempt = 0 ;
do {
try {
Thread . currentThread ( ) . sleep ( 20000 ) ;
} catch ( Exception e ) { }
report = rmPortType . getReport ( reportId ) ;
reportAttempt + + ;
getResource ( ) . setSecondReport ( report ) ;
getResource ( ) . store ( ) ;
} while ( ! isDeploymentStatusFinished ( report ) & & reportAttempt < 40 ) ;
logger . info ( " is something failed in the second step? " + isSomethingFailed ( report ) ) ;
logger . trace ( " report step 2: " + report ) ;
return isSomethingFailed ( report ) ;
2008-12-18 20:08:37 +01:00
}
2009-01-16 19:37:02 +01:00
/ * *
* controls if something is failed deploying
* @param report the VRE deployment report
* @return true if Failed , else false
* /
private boolean isSomethingFailed ( String report ) {
boolean ret = false ;
2010-06-04 17:07:28 +02:00
String deploymentXpath = " /ResourceReport/DeploymentPlanCreation/Status " ;
String dependenciesResolutionXpath = " /ResourceReport/Services/Service/DependenciesResolutionStatus " ;
String serviceXpath = " /ResourceReport/Services/Service/DeploymentActivity/GHN/LastReportReceived/Packages/Package/Status " ;
2009-01-16 19:37:02 +01:00
DocumentBuilderFactory dbf = DocumentBuilderFactory . newInstance ( ) ;
DocumentBuilder db ;
try {
db = dbf . newDocumentBuilder ( ) ;
Document document = db . parse ( new ByteArrayInputStream ( report . getBytes ( ) ) ) ;
XPath engine = XPathFactory . newInstance ( ) . newXPath ( ) ;
2010-06-04 17:07:28 +02:00
NodeList nDeployment = ( NodeList ) engine . evaluate ( deploymentXpath , document , XPathConstants . NODESET ) ;
for ( int i = 0 ; i < nDeployment . getLength ( ) ; i + + ) {
if ( nDeployment . item ( i ) . getFirstChild ( ) . getNodeValue ( ) . compareTo ( " FAILED " ) = = 0 ) {
return true ;
}
}
NodeList nResoulution = ( NodeList ) engine . evaluate ( dependenciesResolutionXpath , document , XPathConstants . NODESET ) ;
for ( int i = 0 ; i < nResoulution . getLength ( ) ; i + + ) {
if ( nResoulution . item ( i ) . getFirstChild ( ) . getNodeValue ( ) . compareTo ( " FAILED " ) = = 0 ) {
return true ;
}
}
NodeList sResoulution = ( NodeList ) engine . evaluate ( serviceXpath , document , XPathConstants . NODESET ) ;
for ( int i = 0 ; i < sResoulution . getLength ( ) ; i + + ) {
if ( sResoulution . item ( i ) . getFirstChild ( ) . getNodeValue ( ) . compareTo ( " FAILED " ) = = 0 ) {
2009-01-16 19:37:02 +01:00
return true ;
}
}
} catch ( ParserConfigurationException e ) {
return true ;
} catch ( SAXException e ) {
return true ;
} catch ( IOException e ) {
return true ;
} catch ( XPathExpressionException e ) {
return true ;
}
return ret ;
}
2008-12-18 20:08:37 +01:00
private boolean isDeploymentStatusFinished ( String report ) {
boolean ret = false ;
2010-06-04 17:07:28 +02:00
String xpath = " /ResourceReport/Services/Service/DeploymentActivity/GHN/LastReportReceived/Status " ;
2008-12-18 20:08:37 +01:00
DocumentBuilderFactory dbf = DocumentBuilderFactory . newInstance ( ) ;
DocumentBuilder db ;
try {
db = dbf . newDocumentBuilder ( ) ;
Document document = db . parse ( new ByteArrayInputStream ( report . getBytes ( ) ) ) ;
XPath engine = XPathFactory . newInstance ( ) . newXPath ( ) ;
NodeList nl = ( NodeList ) engine . evaluate ( xpath , document , XPathConstants . NODESET ) ;
for ( int i = 0 ; i < nl . getLength ( ) ; i + + ) {
if ( i = = 0 ) {
ret = true ;
}
if ( nl . item ( i ) . getFirstChild ( ) . getNodeValue ( ) . compareTo ( " CLOSED " ) ! = 0 ) {
ret = false ;
break ;
}
}
} catch ( ParserConfigurationException e ) {
return false ;
} catch ( SAXException e ) {
return false ;
} catch ( IOException e ) {
return false ;
} catch ( XPathExpressionException e ) {
return false ;
}
return ret ;
}
}