2014-03-13 15:22:33 +01:00
|
|
|
package org.gcube.contentmanager.storageserver.parse;
|
|
|
|
|
2014-03-19 18:08:15 +01:00
|
|
|
import java.text.ParseException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
|
2014-03-13 15:22:33 +01:00
|
|
|
import org.bson.types.ObjectId;
|
|
|
|
import org.gcube.contentmanager.storageserver.accounting.Report;
|
|
|
|
import org.gcube.contentmanager.storageserver.accounting.ReportConfig;
|
|
|
|
import org.gcube.contentmanager.storageserver.accounting.ReportException;
|
|
|
|
import org.gcube.contentmanager.storageserver.accounting.ReportFactory;
|
|
|
|
import org.gcube.contentmanager.storageserver.data.CubbyHole;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.mongodb.DBObject;
|
|
|
|
|
|
|
|
public class JsonParser extends Thread{
|
|
|
|
|
|
|
|
final static Logger logger=LoggerFactory.getLogger(JsonParser.class);
|
2014-03-21 16:50:43 +01:00
|
|
|
final static int MINUTE_DECREMENT=-2;
|
2014-03-13 15:22:33 +01:00
|
|
|
private CubbyHole c;
|
|
|
|
private int number;
|
|
|
|
private Report report;
|
2014-04-01 11:20:28 +02:00
|
|
|
// object fields
|
|
|
|
private String op;
|
|
|
|
private String filename;
|
|
|
|
private String type;
|
|
|
|
private String name;
|
|
|
|
private String owner;
|
|
|
|
private String creationTime;
|
|
|
|
private String lastAccess;
|
|
|
|
private String lastOperation;
|
|
|
|
private String lastUser;
|
|
|
|
private int linkCount;
|
|
|
|
private String delete;
|
|
|
|
private String id;
|
|
|
|
private String callerIp;
|
|
|
|
// private String previousInsert;
|
2014-03-13 15:22:33 +01:00
|
|
|
public JsonParser(CubbyHole c, int number){
|
|
|
|
this.c=c;
|
|
|
|
this.number=number;
|
2014-03-19 18:08:15 +01:00
|
|
|
// init the accounting report
|
2014-03-13 15:22:33 +01:00
|
|
|
try {
|
|
|
|
init();
|
|
|
|
} catch (ReportException e) {
|
|
|
|
throw new RuntimeException("Accounting report Exception initialization");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void init() throws ReportException{
|
|
|
|
report=new ReportFactory().getReport(ReportConfig.ACCOUNTING_TYPE);
|
2014-04-15 17:57:57 +02:00
|
|
|
report.init();
|
2014-03-13 15:22:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
while(true){
|
|
|
|
DBObject x=c.get();
|
|
|
|
logger.info("Consumer #" + this.number + " got: " + x);
|
2014-04-01 11:20:28 +02:00
|
|
|
op = (String) x.get("op");
|
2014-03-19 18:08:15 +01:00
|
|
|
// retrieve object fields
|
2014-03-13 15:22:33 +01:00
|
|
|
DBObject obj=(DBObject)x.get("o");
|
2014-04-01 11:20:28 +02:00
|
|
|
retrieveObjectFields(obj);
|
2014-03-14 17:09:55 +01:00
|
|
|
long length=-1;
|
|
|
|
if(obj.get("length")!=null) length=(long)obj.get("length");
|
2014-03-21 16:50:43 +01:00
|
|
|
logger.info("[recordCheck] operation: "+op+" name: "+name+" type: "+type+" path: "+filename+" length: "+length+" owner: "+owner+"\n\t cretionTime: "+creationTime+ " id: "+id+" lastOperation "+lastOperation+" lastUser: "+lastUser+" lastAccess: "+lastAccess);
|
2014-03-24 17:09:38 +01:00
|
|
|
if(((length >0) && ((filename!=null) || (linkCount > 0)))){
|
2014-04-01 11:20:28 +02:00
|
|
|
|
2014-03-24 17:09:38 +01:00
|
|
|
String scope=null;
|
|
|
|
if(filename!=null)
|
|
|
|
scope=retrieveScopeFromRemoteFilePath(filename);
|
|
|
|
else{
|
2014-04-01 11:20:28 +02:00
|
|
|
// field added on storage manager library for retrieve scope. Used only if it is a link delete
|
2014-03-24 17:09:38 +01:00
|
|
|
String pathString=(String)obj.get("onScope");
|
|
|
|
scope=retrieveScopeFromRemoteFilePath(pathString);
|
|
|
|
}
|
2014-04-15 17:57:57 +02:00
|
|
|
|
2014-03-21 16:50:43 +01:00
|
|
|
if(delete!=null){
|
2014-03-24 17:09:38 +01:00
|
|
|
lastOperation="DELETE";
|
2014-04-01 11:20:28 +02:00
|
|
|
}else if ((lastOperation != null) && (op != null) && (lastOperation.equalsIgnoreCase("LINK")) && (op.equalsIgnoreCase("u"))){
|
|
|
|
// it is an update on a link object this operation doesn't be accounted
|
|
|
|
logger.info("update on link object is not accounted. Skip next ");
|
|
|
|
continue;
|
2014-03-21 16:50:43 +01:00
|
|
|
}
|
|
|
|
// operation=mappingOperationField(op, id, delete, lastAccess);
|
2014-04-01 11:20:28 +02:00
|
|
|
//call to the accounting library
|
|
|
|
report( filename, owner, creationTime, id, length, scope, lastOperation, callerIp, lastAccess, lastUser);
|
2014-03-24 17:09:38 +01:00
|
|
|
logger.info(" operation accounted "+lastOperation);
|
2014-04-01 11:20:28 +02:00
|
|
|
logger.info("\n[accountingCall] operation: "+lastOperation+"\n\t name: "+name+"\n\t type: "+type+"\n\t path: "+filename+"\n\t length: "+length+"\n\t owner: "+owner+"\n\t cretionTime: "+creationTime+"\n\t id: "+id+"\n\t scope: "+scope+"\n\t lastOperation "+lastOperation+"\n\t lastUser: "+lastUser+"\n\t lastAccess: "+lastAccess+"\n\t callerIp: "+callerIp);
|
2014-03-13 15:22:33 +01:00
|
|
|
}else{
|
2014-03-14 17:09:55 +01:00
|
|
|
logger.info("operation is not accounted");
|
2014-03-13 15:22:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 11:20:28 +02:00
|
|
|
private void retrieveObjectFields(DBObject obj) {
|
|
|
|
filename = (String) obj.get("filename");
|
|
|
|
type = (String) obj.get("type");
|
|
|
|
name = (String) obj.get("name");
|
|
|
|
owner = (String) obj.get("owner");
|
|
|
|
creationTime = (String) obj.get("creationTime");
|
|
|
|
lastAccess = null;
|
|
|
|
if(obj.get("lastAccess") != null) lastAccess=(String)obj.get("lastAccess");
|
|
|
|
callerIp = null;
|
|
|
|
if(obj.get("callerIP") != null) callerIp=(String)obj.get("callerIP");
|
|
|
|
lastOperation = null;
|
|
|
|
if(obj.get("lastOperation") != null) lastOperation=(String)obj.get("lastOperation");
|
|
|
|
lastUser = null;
|
|
|
|
if(obj.get("lastUser") != null) lastUser=(String)obj.get("lastUser");
|
|
|
|
linkCount = 0;
|
|
|
|
if(obj.get("linkCount") != null) linkCount=(int)obj.get("linkCount");
|
|
|
|
delete = null;
|
|
|
|
if(obj.get("onDeleting") != null) delete=(String)obj.get("onDeleting");
|
|
|
|
ObjectId objectId=(ObjectId)obj.get("_id");
|
|
|
|
id = objectId.toString();
|
2014-03-19 18:08:15 +01:00
|
|
|
}
|
|
|
|
|
2014-04-01 11:20:28 +02:00
|
|
|
private void report(String filename, String owner,
|
|
|
|
String creationTime, String id, long length,
|
|
|
|
String scope, String operation, String callerIP, String lastAccess, String lastUser) {
|
2014-04-15 17:57:57 +02:00
|
|
|
report.start(lastUser, scope, creationTime);
|
2014-04-01 11:20:28 +02:00
|
|
|
// report.timeUpdate();
|
|
|
|
report.ultimate(owner, operation, length+"", filename, id, callerIP, lastAccess);
|
|
|
|
report.send();
|
2014-03-13 15:22:33 +01:00
|
|
|
}
|
|
|
|
|
2014-04-01 11:20:28 +02:00
|
|
|
// private String mappingOperationField(String op, String id, String onDeleting, String lastRead) throws ParseException {
|
|
|
|
// logger.info("delete field: "+onDeleting);
|
|
|
|
// if(op.equals("u")){
|
|
|
|
// if((onDeleting != null) && (onDeleting.equals("true"))){
|
|
|
|
// logger.info("found delete field");
|
|
|
|
// return "DELETE";
|
|
|
|
// }else if(lastRead !=null){
|
|
|
|
// SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy 'at' hh:mm:ss z");
|
|
|
|
// Date dateLastRead = sdf.parse(lastRead);
|
|
|
|
// Calendar now=Calendar.getInstance();
|
|
|
|
// logger.info("now: "+sdf.format(now.getTime()));
|
|
|
|
// now.add(Calendar.MINUTE, MINUTE_DECREMENT);
|
|
|
|
// logger.info("now decreased -5 min: "+sdf.format(now.getTime()));
|
|
|
|
// logger.info("lastRead is "+sdf.format(dateLastRead)+" now,5 min decreased is"+sdf.format(now.getTime()));
|
|
|
|
// if(now.getTime().compareTo(dateLastRead) < 0){
|
|
|
|
// logger.info("It is a read");
|
|
|
|
// return "READ";
|
|
|
|
// }else{
|
|
|
|
// logger.info("It isn't a read");
|
|
|
|
// return "UPDATE";
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// }else
|
|
|
|
// return "UPDATE";
|
|
|
|
// }else if(op.equals("i")){
|
|
|
|
// return "INSERT";
|
|
|
|
// }else if(op.equals("d")){
|
|
|
|
// return "DELETE";
|
|
|
|
// }
|
|
|
|
// return op;
|
|
|
|
// }
|
|
|
|
|
2014-03-19 18:08:15 +01:00
|
|
|
private String retrieveScopeFromRemoteFilePath(String filename) {
|
2014-03-13 15:22:33 +01:00
|
|
|
String[] split=filename.split("/");
|
|
|
|
if(split.length>0){
|
|
|
|
String scope=null;
|
|
|
|
int i=1;
|
|
|
|
if(split[1].equals("VOLATILE")){
|
|
|
|
i=2;
|
|
|
|
}
|
|
|
|
scope="/"+split[i];
|
|
|
|
i++;
|
|
|
|
while((!split[i].equals("home")) && (!split[i].equals("public"))){
|
|
|
|
scope=scope+"/"+split[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
logger.info("retieved scope: "+scope);
|
|
|
|
return scope;
|
|
|
|
}else logger.error("Scope bad format: scope not retrieved from string: "+filename);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|