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 ;
import org.gcube.common.core.faults.GCUBEFault ;
import org.gcube.common.core.resources.GCUBECollection ;
import org.gcube.common.core.resources.GCUBEMCollection ;
import org.gcube.common.core.resources.impl.kxml.KGCUBEResource ;
import org.gcube.common.core.scope.GCUBEScope ;
2009-01-13 00:29:55 +01:00
import org.gcube.common.core.utils.handlers.GCUBEServiceClientImpl ;
2008-12-18 20:08:37 +01:00
import org.gcube.common.core.utils.logging.GCUBELog ;
import org.gcube.vremanagement.vremanager.stubs.vremanager.AddResourcesParameters ;
import org.gcube.vremanagement.vremanager.stubs.vremanager.OptionsParameters ;
import org.gcube.vremanagement.vremanager.stubs.vremanager.ResourceItem ;
import org.gcube.vremanagement.vremanager.stubs.vremanager.ResourceList ;
import org.gcube.vremanagement.vremanager.stubs.vremanager.ScopeOption ;
import org.gcube.vremanagement.vremanager.stubs.vremanager.ServiceItem ;
import org.gcube.vremanagement.vremanager.stubs.vremanager.ServiceList ;
import org.gcube.vremanagement.vremanager.stubs.vremanager.VREManagerPortType ;
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 ;
2008-12-18 20:08:37 +01:00
import org.gcube.vremanagement.vremodeler.impl.util.Couple ;
import org.gcube.vremanagement.vremodeler.impl.util.VREManagerServiceHandler ;
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 ;
2009-01-13 01:07:51 +01:00
public class DeployVRE 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-01-14 12:11:12 +01:00
public DeployVRE ( 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 ;
}
2008-12-18 20:08:37 +01:00
public void run ( ) {
try {
2009-01-16 19:37:02 +01:00
ResultSet resRelatedGHN = DBInterface . queryDB ( " select VRERELATEDGHN.ghnid from VRERELATEDGHN where VRERELATEDGHN.vreid=' " + this . resourceId + " '; " ) ;
if ( ! resRelatedGHN . next ( ) ) {
DBInterface . ExecuteUpdate ( " UPDATE VRE SET STATUS='Failed' WHERE VRE.id=' " + this . resourceId + " '; " ) ;
}
String firstGHNId = resRelatedGHN . getString ( 1 ) ;
List < String > GHNList = new ArrayList < String > ( ) ;
while ( resRelatedGHN . next ( ) ) GHNList . add ( resRelatedGHN . getString ( 1 ) ) ;
deployManagerOnVRE ( firstGHNId ) ;
createVRE ( GHNList ) ;
logger . info ( " Deploying of the VRE with id " + this . resourceId + " FINISHED " ) ;
2008-12-18 20:08:37 +01:00
} catch ( Exception e ) {
2009-01-16 12:31:03 +01:00
logger . error ( " Error deploying the VRE with id " + this . resourceId + " " + e ) ;
2008-12-18 20:08:37 +01:00
e . printStackTrace ( ) ;
}
}
2009-01-16 19:37:02 +01:00
private void deployManagerOnVRE ( String firstGHNId ) throws GCUBEFault {
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-01-16 19:37:02 +01:00
2008-12-18 20:08:37 +01:00
} catch ( SQLException e ) {
logger . error ( " HSQLDB Error " + e ) ;
throw new GCUBEFault ( e ) ;
}
2009-01-14 12:11:12 +01:00
// Couple<GCUBEScope,Couple<String, String>>> is a triple with first GCUBEScope, second couple: first String is the VREName, second string is the GHNId
2008-12-18 20:08:37 +01:00
VREManagerServiceHandler < String , Couple < String , String > > vreManagerHandler = new VREManagerServiceHandler < String , Couple < String , String > > ( ) {
2009-01-14 12:11:12 +01:00
@SuppressWarnings ( " static-access " )
2008-12-18 20:08:37 +01:00
@Override
protected String makeCall ( VREManagerPortType mbportType )
throws Exception {
2009-01-14 19:55:26 +01:00
//System.out.println("makeCall called");
2009-01-12 18:35:50 +01:00
try {
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 " ) ;
service [ 0 ] . setServiceName ( " VREManager " ) ;
service [ 0 ] . setServiceVersion ( " 1.00.00 " ) ;
services . setService ( service ) ;
services . setGHN ( new String [ ] { this . getParameter ( ) . second } ) ;
arp . setServices ( services ) ;
2009-01-14 12:11:12 +01:00
arp . setTargetScope ( this . scope + " / " + this . getParameter ( ) . first ) ;
2009-01-12 18:35:50 +01:00
String reportId = mbportType . addResources ( arp ) ;
2009-01-14 18:30:25 +01:00
2009-01-12 18:35:50 +01:00
String report ;
int attempt = 0 ;
do {
2009-01-14 19:55:26 +01:00
Thread . currentThread ( ) . sleep ( 20000 ) ;
2009-01-12 18:35:50 +01:00
report = mbportType . getReport ( reportId ) ;
attempt + + ;
2009-01-14 18:30:25 +01:00
getResource ( ) . setReportFirstStep ( report ) ;
2009-01-14 20:29:04 +01:00
System . out . println ( " ---step 1------ " ) ;
2009-01-13 00:41:00 +01:00
System . out . println ( report ) ;
2009-01-14 20:29:04 +01:00
System . out . println ( " --------- " ) ;
2009-01-12 18:35:50 +01:00
} while ( ! isDeploymentStatusFinished ( report ) & & attempt < 10 ) ;
} catch ( Exception e ) { e . printStackTrace ( ) ; throw e ; }
2009-01-13 00:23:16 +01:00
return " " ;
2008-12-18 20:08:37 +01:00
}
} ;
2009-01-16 12:31:03 +01:00
vreManagerHandler . clearState ( ) ;
2009-01-13 00:29:55 +01:00
vreManagerHandler . setHandled ( new GCUBEServiceClientImpl ( ) ) ;
2008-12-18 20:08:37 +01:00
Couple < String , String > coupleParams = new Couple < String , String > ( vreName , firstGHNId ) ;
vreManagerHandler . setParameter ( coupleParams ) ;
2009-01-14 12:11:12 +01:00
vreManagerHandler . scope = this . startingScope ;
2008-12-18 20:08:37 +01:00
try {
vreManagerHandler . run ( ) ;
} catch ( Exception e ) {
2009-01-16 12:31:03 +01:00
logger . error ( " DeployingVRE step 1 -- failed " + e ) ;
2008-12-18 20:08:37 +01:00
throw new GCUBEFault ( e ) ;
}
this . vreName = vreName ;
}
2009-01-16 19:37:02 +01:00
private void createVRE ( List < String > ghnList ) throws GCUBEFault {
VREManagerServiceHandler < Boolean , Couple < List < String > , String > > vreManagerHandler = new VREManagerServiceHandler < Boolean , Couple < List < String > , String > > ( ) {
2008-12-18 20:08:37 +01:00
//the parameter is resourceID
2009-01-16 12:31:03 +01:00
@SuppressWarnings ( " static-access " )
2008-12-18 20:08:37 +01:00
@Override
2009-01-16 19:37:02 +01:00
protected Boolean makeCall ( VREManagerPortType vreManPortType )
2008-12-18 20:08:37 +01:00
throws Exception {
OptionsParameters ops ;
2009-01-14 20:29:04 +01:00
2009-01-16 19:37:02 +01:00
ResultSet resGenericInfo = DBInterface . queryDB ( " select VRE.name, VRE.vredesigner, VRE.vremanager, VRE.IntervalTo, VRE.IntervalFrom, VRE.description from VRE where VRE.id=' " + this . getParameter ( ) . second + " '; " ) ;
2008-12-18 20:08:37 +01:00
if ( ! resGenericInfo . next ( ) ) throw new GCUBEFault ( " The VRE with ID " + this . getParameter ( ) + " cannot be retrieved " ) ;
ops = 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 ;
ops . setScopeOptionList ( scopeOptionList ) ;
vreManPortType . setScopeOptions ( ops ) ;
//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 ;
2009-01-16 19:37:02 +01:00
ResultSet resRelatedCol = DBInterface . queryDB ( " select VRERELATEDCOLLECTION.collid from VRERELATEDCOLLECTION where VRERELATEDCOLLECTION.vreid=' " + this . getParameter ( ) . second + " '; " ) ;
2008-12-18 20:08:37 +01:00
while ( resRelatedCol . next ( ) ) {
resItem = new ResourceItem ( ) ;
resItem . setID ( resRelatedCol . getString ( 1 ) ) ;
resItem . setType ( GCUBECollection . TYPE ) ;
resItemList . add ( resItem ) ;
}
2009-01-15 19:43:05 +01:00
} catch ( SQLException sqle ) { logger . error ( " Error contacting HSQLDB " + sqle ) ; }
2008-12-18 20:08:37 +01:00
//retrieving MDCollection with native MDFormat
try {
ResourceItem resItem ;
2009-01-16 19:37:02 +01:00
ResultSet resMdColNative = DBInterface . queryDB ( " select s.mdcollid from SELECTEDNATIVEMDFORMAT AS s where s.vreid=' " + this . getParameter ( ) . second + " '; " ) ;
2008-12-18 20:08:37 +01:00
while ( resMdColNative . next ( ) ) {
resItem = new ResourceItem ( ) ;
resItem . setID ( resMdColNative . getString ( 1 ) ) ;
System . out . println ( resItem . getID ( ) ) ;
resItem . setType ( GCUBEMCollection . TYPE ) ;
resItemList . add ( resItem ) ;
}
2009-01-15 19:43:05 +01:00
} catch ( SQLException sqle ) { logger . error ( " Error contacting HSQLDB " + sqle ) ; }
2008-12-18 20:08:37 +01:00
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 > ( ) ;
2009-01-16 19:37:02 +01:00
ResultSet resMdColNative = DBInterface . queryDB ( " select s.name, s.class from VRERELATEDFUNCT AS vrf, SERVICES AS s where vrf.vreid=' " + this . getParameter ( ) . second + " ' AND s.id=vrf.funcid; " ) ;
2008-12-18 20:08:37 +01:00
while ( resMdColNative . next ( ) ) {
servItem = new ServiceItem ( ) ;
servItem . setServiceClass ( resMdColNative . getString ( " class " ) ) ;
servItem . setServiceName ( resMdColNative . getString ( " name " ) ) ;
2009-01-16 19:37:02 +01:00
servItem . setServiceVersion ( " 1.00.00 " ) ;
2008-12-18 20:08:37 +01:00
listService . add ( servItem ) ;
}
serviceList . setService ( listService . toArray ( new ServiceItem [ 0 ] ) ) ;
2009-01-15 19:43:05 +01:00
} catch ( SQLException sqle ) { logger . error ( " Error contacting HSQLDB " + sqle ) ; }
2008-12-18 20:08:37 +01:00
2009-01-16 19:37:02 +01:00
//sets the GHNs
serviceList . setGHN ( this . getParameter ( ) . first . toArray ( new String [ 0 ] ) ) ;
2008-12-18 20:08:37 +01:00
arp . setServices ( serviceList ) ;
String reportId = vreManPortType . addResources ( arp ) ;
String report ;
int attempt = 0 ;
do {
2009-01-15 19:43:05 +01:00
Thread . currentThread ( ) . sleep ( 20000 ) ;
2008-12-18 20:08:37 +01:00
report = vreManPortType . getReport ( reportId ) ;
2009-01-14 20:29:04 +01:00
System . out . println ( " ---step 2------ " ) ;
System . out . println ( report ) ;
System . out . println ( " --------- " ) ;
2008-12-18 20:08:37 +01:00
attempt + + ;
2009-01-14 18:30:25 +01:00
getResource ( ) . setReportSecondStep ( report ) ;
2009-01-14 20:29:04 +01:00
2009-01-12 18:35:50 +01:00
} while ( ! isDeploymentStatusFinished ( report ) & & attempt < 20 ) ;
2009-01-16 19:37:02 +01:00
return isSomethingFailed ( report ) ;
2008-12-18 20:08:37 +01:00
}
} ;
2009-01-15 22:30:56 +01:00
vreManagerHandler . clearState ( ) ;
2009-01-13 00:33:54 +01:00
vreManagerHandler . setHandled ( new GCUBEServiceClientImpl ( ) ) ;
2009-01-16 19:37:02 +01:00
vreManagerHandler . setParameter ( new Couple < List < String > , String > ( ghnList , this . resourceId ) ) ;
2009-01-15 19:43:05 +01:00
vreManagerHandler . scope = GCUBEScope . getScope ( this . startingScope + " / " + this . vreName ) ;
2008-12-18 20:08:37 +01:00
try {
vreManagerHandler . run ( ) ;
} catch ( Exception e ) {
2009-01-16 12:31:03 +01:00
logger . error ( " DeployingVRE step 2 -- failed " + e ) ;
2008-12-18 20:08:37 +01:00
throw new GCUBEFault ( e ) ;
}
}
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 ;
String xpath = " /ResourceReport/DeploymentActivity/GHN/LastReportReceived/Packages/Package/Status " ;
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 ( nl . item ( i ) . getFirstChild ( ) . getNodeValue ( ) . compareTo ( " FAILED " ) = = 0 ) {
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 ;
String xpath = " /ResourceReport/DeploymentActivity/GHN/LastReportReceived/Status " ;
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 ;
}
}