storage-manager-trigger/src/main/java/org/gcube/contentmanager/storageserver/parse/JsonParser.java

104 lines
3.2 KiB
Java

package org.gcube.contentmanager.storageserver.parse;
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);
private CubbyHole c;
private int number;
private Report report;
public JsonParser(CubbyHole c, int number){
this.c=c;
this.number=number;
// int the accounting report
try {
init();
} catch (ReportException e) {
throw new RuntimeException("Accounting report Exception initialization");
}
}
private void init() throws ReportException{
report=new ReportFactory().getReport(ReportConfig.ACCOUNTING_TYPE);
}
public void run() {
while(true){
DBObject x=c.get();
logger.info("Consumer #" + this.number + " got: " + x);
DBObject obj=(DBObject)x.get("o");
String op=(String) x.get("op");
String filename=(String) obj.get("filename");
String type=(String) obj.get("type");
String name=(String) obj.get("name");
String owner=(String) obj.get("owner");
ObjectId objectId=(ObjectId)obj.get("_id");
String id = objectId.toString();
long length=(long) obj.get("length");
logger.debug("[recordCheck] operation: "+op+" name: "+name+" type: "+type+" path: "+filename+" length: "+length+" owner: "+owner+ " id: "+id);
if((length >0) && (name!=null)){
//call to the accounting library
String scope=retrieveScopeFromFilename(filename);
report.init(owner, scope);
report.timeUpdate();
String operation=mappingOperationField(op);
report.ultimate(owner, null, operation, length+"", filename, id);
report.send();
logger.debug("[accountingCall] operation: "+op+" name: "+name+" type: "+type+" path: "+filename+" length: "+length+" owner: "+owner);
}else{
logger.debug("operation is not accounted");
}
}
}
private String mappingOperationField(String op) {
if(op.equals("u")){
return "UPDATE";
}else if(op.equals("i")){
return "INSERT";
}else if(op.equals("d")){
return "DELETE";
}
return op;
}
private String retrieveScopeFromFilename(String filename) {
String[] split=filename.split("/");
if(split.length>0){
String scope=null;
int i=1;
if(split[1].equals("VOLATILE")){
i=2;
// scope="/"+split[1];
// if(split[2] != null && (!split[2].equals("home")) && (!split[2].equals("public"))){
// scope=scope+"/"+split[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;
}
}