This commit is contained in:
Lucio Lelii 2008-12-18 19:08:37 +00:00
parent c0a5daafb5
commit 19078ff416
1 changed files with 289 additions and 0 deletions

View File

@ -0,0 +1,289 @@
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;
import javax.xml.parsers.DocumentBuilder;
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;
import org.gcube.common.core.utils.logging.GCUBELog;
import org.gcube.common.vremanagement.ghnmanager.impl.contexts.ServiceContext;
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;
import org.gcube.vremanagement.vremodeler.impl.util.Couple;
import org.gcube.vremanagement.vremodeler.impl.util.VREManagerServiceHandler;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DeployVRE implements Runnable{
private static GCUBELog logger= new GCUBELog(DeployVRE.class);
private String resourceId;
private String vreName=null;
public DeployVRE(String resourceId){
this.resourceId=resourceId;
}
public void run() {
try {
deployManagerOnVRE();
ServiceContext.getContext().setScope(GCUBEScope.getScope(ServiceContext.getContext().getScope()+"/"+this.vreName));
createVRE();
logger.info("FINISHED");
}catch(Exception e){
e.printStackTrace();
}
}
private void deployManagerOnVRE() throws GCUBEFault{
String firstGHNId= null, vreName= null;
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");
}
vreName= resGenericInfo.getString("name");
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+"';");
}
firstGHNId= resRelatedGHN.getString("ghnid");
} catch (SQLException e) {
logger.error("HSQLDB Error "+e);
throw new GCUBEFault(e);
}
// couple: first String is the VREName, second string is the GHNId
VREManagerServiceHandler<String, Couple<String, String>> vreManagerHandler= new VREManagerServiceHandler<String,Couple<String, String>>(){
@Override
protected String makeCall(VREManagerPortType mbportType)
throws Exception {
AddResourcesParameters arp= new AddResourcesParameters();
ServiceList services= new ServiceList();
ServiceItem[] service= new ServiceItem[1];
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);
arp.setTargetScope(ServiceContext.getContext().getScope().toString()+"/"+this.getParameter().first);
String reportId=mbportType.addResources(arp);
String report;
int attempt=0;
do{
this.wait(30000);
report=mbportType.getReport(reportId);
attempt++;
}while (!isDeploymentStatusFinished(report) && attempt<10);
return null;
}
};
Couple<String, String> coupleParams= new Couple<String, String>(vreName, firstGHNId);
vreManagerHandler.setParameter(coupleParams);
try {
vreManagerHandler.run();
} catch (Exception e) {
throw new GCUBEFault(e);
}
this.vreName= vreName;
}
private void createVRE() throws GCUBEFault{
VREManagerServiceHandler<String, String> vreManagerHandler= new VREManagerServiceHandler<String,String>(){
//the parameter is resourceID
@Override
protected String makeCall(VREManagerPortType vreManPortType)
throws Exception {
OptionsParameters ops;
ResultSet resGenericInfo = DBInterface.queryDB("select VRE.name from VRE where VRE.id='"+this.getParameter()+"'; ");
if (!resGenericInfo.next()) {
throw new GCUBEFault("The VRE with ID "+this.getParameter()+" cannot be retrieved on the DB");
}
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;
ResultSet resRelatedCol =DBInterface.queryDB("select VRERELATEDCOLLECTION.collid from VRERELATEDCOLLECTION where VRERELATEDCOLLECTION.vreid='"+this.getParameter()+"';");
while (resRelatedCol.next()){
resItem= new ResourceItem();
resItem.setID(resRelatedCol.getString(1));
resItem.setType(GCUBECollection.TYPE);
resItemList.add(resItem);
}
}catch(SQLException sqle){logger.error("Error contacting HSQLDB");}
//retrieving MDCollection with native MDFormat
try{
ResourceItem resItem;
ResultSet resMdColNative =DBInterface.queryDB("select mdcollid from SELECTEDNATIVEMDFROMAT AS s where s.vreid='"+this.getParameter()+"';");
while (resMdColNative.next()){
resItem= new ResourceItem();
resItem.setID(resMdColNative.getString(1));
System.out.println(resItem.getID());
resItem.setType(GCUBEMCollection.TYPE);
resItemList.add(resItem);
}
}catch(SQLException sqle){logger.error("Error contacting HSQLDB");}
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 resMdColNative =DBInterface.queryDB("select s.name, s.class from VRERELATEDFUNCTIONALITY AS vrf, SERVICES AS s where vrf.vreid='"+this.getParameter()+"' AND s.id=vrf.funcid;");
while (resMdColNative.next()){
servItem= new ServiceItem();
servItem.setServiceClass(resMdColNative.getString("class"));
servItem.setServiceName(resMdColNative.getString("name"));
listService.add(servItem);
}
serviceList.setService(listService.toArray(new ServiceItem[0]));
}catch(SQLException sqle){logger.error("Error contacting HSQLDB");}
//retrieve the GHN
try{
List<String> listGHN= new ArrayList<String>();
ResultSet resGHN =DBInterface.queryDB("select ghnih, s.class from VRERELATEDGHN where vrf.vreid='"+this.getParameter()+"';");
//eliminating the First GHN used for VREManager Deployment
resGHN.next();
while (resGHN.next()){
listGHN.add(resGHN.getString(1));
}
serviceList.setGHN(listGHN.toArray(new String[0]));
}catch(SQLException sqle){logger.error("Error contacting HSQLDB");}
arp.setServices(serviceList);
String reportId=vreManPortType.addResources(arp);
String report;
int attempt=0;
do{
this.wait(30000);
report=vreManPortType.getReport(reportId);
attempt++;
}while (!isDeploymentStatusFinished(report) && attempt<10);
return "FINISHED";
}
};
vreManagerHandler.setParameter(this.resourceId);
try {
vreManagerHandler.run();
} catch (Exception e) {
throw new GCUBEFault(e);
}
}
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;
}
}