package org.gcube.contentmanager.storageserver.accounting; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.messaging.ResourceAccounting; import org.gcube.accounting.messaging.ResourceAccountingFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ReportAccountingImpl implements Report { final Logger logger = LoggerFactory.getLogger(ReportAccountingImpl.class); public RawUsageRecord ur; public ResourceAccounting raFactory; @Override public void init(String consumerId, String resourceScope) { logger.info("set accounting properties: consumerId "+consumerId+" scope: "+resourceScope); raFactory = null; try { raFactory = ResourceAccountingFactory.getResourceAccountingInstance(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } this.ur = new RawUsageRecord(); //generic properties ur.setResourceType("storage-usage"); if(consumerId!=null) ur.setConsumerId(consumerId); // ur.setResourceOwner("paolo.fabriani"); if(resourceScope !=null) ur.setResourceScope(resourceScope); //set creation time Calendar createTime = new GregorianCalendar(); ur.setCreateTime(createTime.getTime()); } @Override public void timeUpdate() { // setIpAddress(); setStartTime(); } @Override public void ultimate(String owner, String uri, String operation, String size, String filePath, String id) { setEndTime(); logger.info("set accounting properties: owner "+owner+" uri: "+uri+" operation: "+operation+" size: "+size); //specific properties TODO if(owner != null) ur.setResourceOwner(owner); if(uri != null) ur.setResourceSpecificProperty("objectURI", uri); if (operation!=null) ur.setResourceSpecificProperty("operationType",operation); ur.setResourceSpecificProperty("dataType","STORAGE"); if(size!= null) ur.setResourceSpecificProperty("dataVolume", size); if(filePath != null) ur.setResourceSpecificProperty("remotePath", filePath); if(id!= null) ur.setResourceSpecificProperty("id", id); } @Override public void send() { logger.info("report sending "); raFactory.sendAccountingMessage(ur); } // private void setIpAddress() { // String address=null; // try { // address=InetAddress.getLocalHost().toString(); // } catch (UnknownHostException e) { // // } // logger.info("caller ip: "+address); // if (address!=null) ur.setResourceSpecificProperty("callerIP", address);; // } private void setStartTime() { //set start time Calendar startTime = new GregorianCalendar(); Date time=startTime.getTime(); logger.info("set start time: "+time); try { ur.setStartTime(time); } catch (InvalidValueException e) { e.printStackTrace(); } } private void setEndTime() { // set end time Calendar endTime = new GregorianCalendar(); Date time=endTime.getTime(); logger.info("set end time: "+time); try { ur.setEndTime(time); } catch (InvalidValueException e) { e.printStackTrace(); } } }