vre-removed-info/src/main/java/org/gcube/vremanagement/VREQueryService.java

375 lines
9.5 KiB
Java

package org.gcube.vremanagement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import com.google.common.collect.ArrayListMultimap;
import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.CSVWriter;
import static org.gcube.vremanagement.vremodel.cl.plugin.AbstractPlugin.factory;
import static org.gcube.vremanagement.vremodel.cl.plugin.AbstractPlugin.manager;
import javax.xml.ws.wsaddressing.W3CEndpointReference;
import org.gcube.vremanagement.vremodel.cl.stubs.types.VREDescription;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.asynchttpclient.AsyncCompletionHandler;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.Dsl;
import org.asynchttpclient.HttpResponseBodyPart;
import org.asynchttpclient.Response;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.vremanagement.vremodel.cl.stubs.types.Report;
import org.gcube.vremanagement.vremodel.cl.stubs.types.VREDescription;
import org.gcube.vremanagement.vremodeler.utils.reports.DeployReport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class VREQueryService {
private File fileCSV;
private File outFileCSV;
private File missFileCSV;
private CSVParser csvParser;
private CSVReader csvReader;
private CSVWriter csvWriter;
private CSVWriter csvMissWriter;
private SimpleDateFormat dateFormat;
private String OutFilePath;
private String MissFilePath;
private Map<String,List<List<Report>>> mapVres;
private List<String[]> allRows = null;
private String[] rowHeader = {"Ticket #", "Status", "Subject","VRE Names", "VO", "Gateways", "Ticket Closed", "Start Time" , "End Time", "Managers", "Description"};
private static final Logger logger = LoggerFactory.getLogger(VREQueryService.class);
public VREQueryService(String _InCsvPath, int _skipLines)
{
try {
String filePath = "src/test/resources";
String InFilePath = filePath+File.separator+ _InCsvPath;
OutFilePath = filePath+File.separator+"updated_"+_InCsvPath;
MissFilePath =filePath+File.separator+"missing_"+_InCsvPath;
fileCSV = new File(InFilePath);
if(!fileCSV.exists())
{
System.err.println("Input CSV File "+_InCsvPath+" does not exists in the path "+filePath+"!");
System.exit(0);
}
logger.debug("file url path:" + fileCSV.getPath());
csvParser = new CSVParserBuilder().withSeparator(',').withIgnoreQuotations(true).build();
csvReader = new CSVReaderBuilder(new FileReader(fileCSV)).withSkipLines(_skipLines).withCSVParser(csvParser).build();
allRows = csvReader.readAll();
dateFormat = new SimpleDateFormat("MMM dd yyyy hh:mm a");
mapVres = new HashMap<>();
} catch(Exception e)
{ e.printStackTrace();};
}
public VREQueryService() {}
public static VREQueryService getInstance() { return new VREQueryService();};
protected void finalize() throws Throwable
{
try {
csvReader.close();
}finally {
super.finalize();
}
}
public List<Report> getReportVREs(String _scopeVO)
{
try {
ScopeProvider.instance.set(_scopeVO);
logger.debug("Start getting vre info of VO: "+_scopeVO);
List<Report> reports = factory().build().getAllVREs();
return reports;
}catch(Exception e)
{
System.err.println("a map for " +_scopeVO+" is undefined");
e.printStackTrace();
return null;
}
}
public Map<String,List<List<Report>>> fillMapAllVREs(String[] _scopeList)
{
Map<String,List<List<Report>>> vreAllMap = new HashMap<>();
for(String _voItem : _scopeList)
{
try {
if(!vreAllMap.containsKey(_voItem))
{
vreAllMap.put(_voItem, new ArrayList<>());
}
logger.debug("Adding "+_voItem+" in Map of VREs");
vreAllMap.get(_voItem).add(this.getReportVREs(_voItem));
}catch(IllegalStateException ise)
{
System.err.println("a map for " +_voItem+" is undefined");
}
}
return vreAllMap;
}
public VREDescription findVREDescr(String _scope, String _vo, String _vreName)
{
String scopeVO = _scope.concat("/").concat(_vo);
List<Report> reportVREs = this.getReportVREs(scopeVO);
if (reportVREs !=null)
{
for (Report report: reportVREs)
{
W3CEndpointReference epr = factory().build().getEPRbyId(report.id());
VREDescription vre= manager().at(epr).build().getDescription();
if (report.state().compareTo("Disposed") ==0)
{
if (vre.name().compareToIgnoreCase(_vreName) == 0)
{
logger.debug("VRE "+_vreName+ " Found!");
return vre;
}
}
}
}
logger.debug("VRE " + _vreName+ " Not Found!");
return null;
}
public int isVREinCSVbyName(String _vreName)
{
for (String[] row: allRows)
{
if(!row[3].isEmpty())
{
if(row[3].toString().trim().compareToIgnoreCase(_vreName) == 0)
{
logger.debug("VRE Found in the CSV...skip");
return 1;
}
}
}
return 0;
}
/*
* CSV ROW FORMAT
* ROW[0] = ticket
* ROW[1] = Status
* ROW[2] = Subject
* ROW[3] = VREName
* ROW[4] = VO
* ROW[5] = Gateway
* ROW[6] = TicketClosed
* ROW[7] = Start Date
* ROW[8] = End Date
* ROW[9] = Managers
* ROM[10] = Description
*/
public void updateCSVformCSVList(String _scope)
{
try {
outFileCSV = new File (OutFilePath);
csvWriter = new CSVWriter (new FileWriter(outFileCSV));
csvWriter.writeNext(rowHeader);
for (String[] row: allRows)
{
if(!(row[3].isEmpty() || row[4].isEmpty()))
{
VREDescription vreDescr = this.findVREDescr(_scope, row[4], row[3]);
if(vreDescr != null)
{
row[6] = row[6].replace(",", ";")+row[7];
row[7] = dateFormat.format(vreDescr.startTime().getTime());
row[8] = dateFormat.format(vreDescr.endTime().getTime());
row[9] = vreDescr.manager();
row[10] =vreDescr.description();
csvWriter.writeNext(row);
}
else {
row[6] = row[6].replace(",", ";")+row[7];
csvWriter.writeNext(row);
}
}
}
csvWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* CSV ROW FORMAT
* ROW[0] = ticket
* ROW[1] = Status
* ROW[2] = Subject
* ROW[3] = VREName
* ROW[4] = VO
* ROW[5] = Gateway
* ROW[6] = TicketClosed
* ROW[7] = Start Date
* ROW[8] = End Date
* ROW[9] = Managers
* ROW[10] = Description
*/
public void missingVREinCSV(String _scope, String[] _voList)
{
try {
missFileCSV = new File (MissFilePath);
csvMissWriter = new CSVWriter (new FileWriter(missFileCSV));
csvMissWriter.writeNext(rowHeader);
for (String voItem : _voList)
{
String scopeVO = _scope.concat("/").concat(voItem);
List<Report> reportVREs = this.getReportVREs(scopeVO);
for (Report report: reportVREs)
{
W3CEndpointReference epr = factory().build().getEPRbyId(report.id());
VREDescription vre= manager().at(epr).build().getDescription();
if (report.state().compareTo("Disposed") ==0)
{
if(this.isVREinCSVbyName(vre.name()) < 1)
{
logger.debug("VRE "+vre.name()+" not found in the CSV...adding in the missing file");
String[] newRecord =
{" ",
report.state(),
voItem+" / "+vre.name() + " Removal",
vre.name(),
voItem,
" ",
" ",
dateFormat.format(vre.startTime().getTime()),
dateFormat.format(vre.endTime().getTime()),
vre.manager(),
vre.description()
};
csvMissWriter.writeNext(newRecord);
}
}
}
}
csvMissWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int getVREInfoFromClient(String _scope, String _vo, String _vreName)
{
try {
VREDescription vreFound = this.findVREDescr(_scope, _vo, _vreName);
if(vreFound !=null)
{
logger.debug("VRE Found: "
+ "name: " + vreFound.name()+"\n"
+ "vo: "+ _vo +"\n"
+ "manager: "+ vreFound.manager()+"\n"
+ "start time:"+ vreFound.startTime().getTime()+"\n"
+ "end time:"+ vreFound.endTime().getTime()+"\n"
);
return 1;
}
}catch(Exception ise)
{
System.err.println("a map for " +_scope+ "/"+_vo+" is undefined");
ise.getMessage();
return 0;
}
logger.debug("VRE Not Found!");
return 0;
}
public void updateEndTimeVRE(String _scope, String _vo, String _vreName, int _year, int _month, int _day )
{
try {
Calendar cal = Calendar.getInstance();
cal.set(_year,_month, _day);
String scopeVO = _scope.concat("/").concat(_vo);
List<Report> reportVREs = this.getReportVREs(scopeVO);
for (Report report: reportVREs)
{
W3CEndpointReference epr = factory().build().getEPRbyId(report.id());
VREDescription vre= manager().at(epr).build().getDescription();
if (vre.name().compareToIgnoreCase(_vreName) == 0)
{
logger.debug("VRE "+vre.name()+ " Found!");
logger.debug("Actual endTime: " +vre.endTime().getTime());
logger.debug("Updating time...");
manager().at(epr).build().setDescription(vre.name(), vre.description(), vre.designer(), vre.manager(), vre.startTime(), cal);
break;
}
}
this.getVREInfoFromClient(_scope, _vo, _vreName);
}catch(Exception e)
{
System.err.println("Error in updateTimeVRE ");
e.printStackTrace();
}
}
}