refs #200: Create accouting-lib library

https://support.d4science.org/issues/200
Defining Aggregation Facilities

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115489 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-06-23 15:47:38 +00:00
parent 670a882453
commit e4c8558c7f
17 changed files with 356 additions and 162 deletions

View File

@ -3,34 +3,28 @@
*/ */
package org.gcube.accounting.datamodel; package org.gcube.accounting.datamodel;
import java.util.List;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.accounting.exception.NotAggregatableRecorsExceptions;
/** /**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/ */
public interface AggregatedUsageRecord<T> extends UsageRecord { public interface AggregatedUsageRecord<T extends B, B extends SingleUsageRecord> extends UsageRecord {
public T getAggregatedUsageRecord(B b) throws InvalidValueException ;
/** /**
* Return the id of the usage record aggregating this, null if this record * Aggregate the Record provided as parameter with this record if Aggregatable
* has not been aggregated by any record. * @throws NotAggregatableRecorsExceptions
* @return Aggregated Id The ID of the aggregation Record
*/ */
public String getAggregatedUsageRecordId(); public void aggregate(T record) throws NotAggregatableRecorsExceptions;
/**
* Set the id of the usage record aggregating this
* @param aggregatedId The ID of the aggregation Record
* @throws InvalidValueException
*/
public void setAggregatedUsageRecordId(String aggregatedId) throws InvalidValueException;
/* * /* *
* *
* @param records * @param records
* @return * @return
* / * /
public List<T> aggregate(List<T> records); public Collection<T> aggregate(List<? extends B> records);
*/
} }

View File

@ -0,0 +1,80 @@
/**
*
*/
package org.gcube.accounting.datamodel;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.Set;
import org.gcube.accounting.exception.NotAggregatableRecorsExceptions;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public abstract class AggregationStrategy<T extends B, B extends SingleUsageRecord> {
protected T t;
protected Set<String> aggregationField;
public AggregationStrategy(T t){
this.t = t;
this.aggregationField = new HashSet<String>();
this.aggregationField.add(BasicUsageRecord.USAGE_RECORD_TYPE);
this.aggregationField.add(BasicUsageRecord.SCOPE);
}
protected String commonFieldHash(B record) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
String concatenation = "";
for(String field : aggregationField){
concatenation = concatenation + record.getResourceProperty(field).toString();
}
messageDigest.update(concatenation.getBytes());
return new String(messageDigest.digest());
}catch(NoSuchAlgorithmException e){
throw new UnsupportedOperationException(e.getCause());
}
}
protected boolean isAggregable(UsageRecord record) {
for(String field : aggregationField){
Serializable recordValue = record.getResourceProperty(field);
Serializable thisValue = t.getResourceProperty(field);
// TODO Check THIS
if(!recordValue.equals(thisValue)){
return false;
}
}
return true;
}
public void aggregate(B record) throws NotAggregatableRecorsExceptions {
try{
if(isAggregable(record)){
throw new NotAggregatableRecorsExceptions("The Record provided as argument has different values for field wich must be common to be aggragatable");
}
@SuppressWarnings("unchecked")
AggregatedUsageRecord<T, B> aggregatedUsageRecord = ((AggregatedUsageRecord<T, B>) t);
aggregatedUsageRecord.aggregate(aggregatedUsageRecord.getAggregatedUsageRecord(record));
throw new NotAggregatableRecorsExceptions("");
}catch(NotAggregatableRecorsExceptions e){
throw e;
}catch(Exception ex){
throw new NotAggregatableRecorsExceptions(ex.getCause());
}
}
}

View File

@ -46,31 +46,29 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable {
@RequiredField @NotEmpty @RequiredField @NotEmpty
public static final String ID = "id"; public static final String ID = "id";
@RequiredField @NotEmpty @RequiredField @NotEmpty
public static final String CONSUMER_ID = "consumerId"; public static final String CONSUMER_ID = "consumerId";
@RequiredField @ValidLong @RequiredField @ValidLong
public static final String CREATION_TIME = "creationTime"; public static final String CREATION_TIME = "creationTime";
@RequiredField @NotEmpty
protected static final String USAGE_RECORD_TYPE = "usageRecordType";
@RequiredField @NotEmpty
public static final String SCOPE = "scope";
@RequiredField @ValidOperationResult
public static final String OPERATION_RESULT = "operationResult";
@AggregatedField @ValidLong @AggregatedField @ValidLong
protected static final String START_TIME = "startTime"; protected static final String START_TIME = "startTime";
@AggregatedField @ValidLong @AggregatedField @ValidLong
protected static final String END_TIME = "endTime"; protected static final String END_TIME = "endTime";
@RequiredField @NotEmpty
protected static final String USAGE_RECORD_TYPE = "usageRecordType";
@RequiredField @NotEmpty
public static final String RESOURCE_SCOPE = "resourceScope";
@AggregatedField @NotEmptyIfNotNull @AggregatedField @NotEmptyIfNotNull
protected static final String AGGREGATED = "aggregated"; protected static final String AGGREGATED = "aggregated";
/*
@AggregatedField @NotEmptyIfNotNull @AggregatedField @NotEmptyIfNotNull
protected static final String AGGREGATED_USAGE_RECORD_ID = "aggregatedUsageRecordId"; protected static final String AGGREGATED_USAGE_RECORD_ID = "aggregatedUsageRecordId";
*/
@RequiredField @ValidOperationResult
public static final String OPERATION_RESULT = "operationResult";
/** resource-specific properties */ /** resource-specific properties */
protected Map<String, Serializable> resourceProperties; protected Map<String, Serializable> resourceProperties;
@ -114,6 +112,8 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable {
} }
} }
} }
} }
/** /**
@ -124,7 +124,6 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable {
this.requiredFields = new HashSet<String>(); this.requiredFields = new HashSet<String>();
this.aggregatedFields = new HashSet<String>(); this.aggregatedFields = new HashSet<String>();
this.computedFields = new HashSet<String>(); this.computedFields = new HashSet<String>();
initializeValidation(); initializeValidation();
} }
@ -158,23 +157,6 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable {
setResourceProperty(ID, id); setResourceProperty(ID, id);
} }
/* *
* {@inheritDoc}
* /
@Override
public String getCreatorId() {
return (String) this.resourceProperties.get(CREATOR_ID);
}
/* *
* {@inheritDoc}
* /
@Override
public void setCreatorId(String creatorId) throws InvalidValueException {
setResourceProperty(CREATOR_ID, creatorId);
}
*/
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -262,16 +244,16 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public String getResourceScope() { public String getScope() {
return (String) this.resourceProperties.get(RESOURCE_SCOPE); return (String) this.resourceProperties.get(SCOPE);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void setResourceScope(String scope) throws InvalidValueException { public void setScope(String scope) throws InvalidValueException {
setResourceProperty(RESOURCE_SCOPE, scope); setResourceProperty(SCOPE, scope);
} }
/* * /* *
@ -289,6 +271,7 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable {
public void setAggregatedUsageRecordId(String aggregatedId) throws InvalidValueException { public void setAggregatedUsageRecordId(String aggregatedId) throws InvalidValueException {
setResourceProperty(AGGREGATED_USAGE_RECORD_ID, aggregatedId); setResourceProperty(AGGREGATED_USAGE_RECORD_ID, aggregatedId);
} }
*/
/** /**
* {@inheritDoc} * {@inheritDoc}

View File

@ -21,8 +21,8 @@ import org.gcube.accounting.datamodel.implementations.PortletUsageRecord;
import org.gcube.accounting.datamodel.implementations.ServiceUsageRecord; import org.gcube.accounting.datamodel.implementations.ServiceUsageRecord;
import org.gcube.accounting.datamodel.implementations.StorageUsageRecord; import org.gcube.accounting.datamodel.implementations.StorageUsageRecord;
import org.gcube.accounting.datamodel.implementations.TaskUsageRecord; import org.gcube.accounting.datamodel.implementations.TaskUsageRecord;
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
import org.gcube.accounting.datamodel.validations.annotations.NotEmptyIfNotNull; import org.gcube.accounting.datamodel.validations.annotations.NotEmptyIfNotNull;
import org.gcube.accounting.datamodel.validations.validators.NotEmptyIfNotNullValidator;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -32,7 +32,7 @@ import org.slf4j.LoggerFactory;
* *
*/ */
@Deprecated @Deprecated
public class RawUsageRecord extends BasicUsageRecord { public class RawUsageRecord extends BasicUsageRecord implements SingleUsageRecord {
/** /**
* Generated Serial Version UID * Generated Serial Version UID
@ -41,6 +41,25 @@ public class RawUsageRecord extends BasicUsageRecord {
private static Logger logger = LoggerFactory.getLogger(RawUsageRecord.class); private static Logger logger = LoggerFactory.getLogger(RawUsageRecord.class);
@DeprecatedWarning @MoveToScope @NotEmpty
public static final String RESOURCE_SCOPE = "resourceScope";
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@FieldDecorator(managed=MoveToScopeAction.class)
protected @interface MoveToScope { }
protected class MoveToScopeAction implements FieldAction {
@Override
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException {
if(value instanceof String){
usageRecord.setScope((String) value);
}else{
throw new InvalidValueException();
}
return value;
}
}
@DeprecatedWarning @NotEmptyIfNotNull @DeprecatedWarning @NotEmptyIfNotNull
public static final String CREATOR_ID = "creatorId"; public static final String CREATOR_ID = "creatorId";
@ -120,9 +139,10 @@ public class RawUsageRecord extends BasicUsageRecord {
} }
@DeprecatedWarning @MoveToAggregatedUsageRecordId @DeprecatedWarning //@MoveToAggregatedUsageRecordId
protected static final String AGGREGATED_ID = "aggregatedId"; protected static final String AGGREGATED_ID = "aggregatedId";
/*
@Target(ElementType.FIELD) @Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@FieldDecorator(managed=MoveToAggregatedUsageRecordIdAction.class) @FieldDecorator(managed=MoveToAggregatedUsageRecordIdAction.class)
@ -136,15 +156,18 @@ public class RawUsageRecord extends BasicUsageRecord {
return value; return value;
} }
} }
*/
@NotEmptyIfNotNull @DeprecatedWarning @NotEmptyIfNotNull @DeprecatedWarning
protected static final String FULLY_QUALIFIED_CONSUMER_ID = "fullyQualifiedConsumerId"; protected static final String FULLY_QUALIFIED_CONSUMER_ID = "fullyQualifiedConsumerId";
@Deprecated
public RawUsageRecord(){ public RawUsageRecord(){
super(); super();
this.resourceProperties.remove(USAGE_RECORD_TYPE); this.resourceProperties.remove(USAGE_RECORD_TYPE);
} }
@Deprecated
public RawUsageRecord(Map<String, Serializable> properties) throws InvalidValueException { public RawUsageRecord(Map<String, Serializable> properties) throws InvalidValueException {
super(properties); super(properties);
} }
@ -153,6 +176,7 @@ public class RawUsageRecord extends BasicUsageRecord {
* Return the identity of the entity creating this {#UsageRecord} * Return the identity of the entity creating this {#UsageRecord}
* @return Creator ID * @return Creator ID
*/ */
@Deprecated
public String getCreatorId() { public String getCreatorId() {
return (String) this.resourceProperties.get(CREATOR_ID); return (String) this.resourceProperties.get(CREATOR_ID);
} }
@ -162,6 +186,7 @@ public class RawUsageRecord extends BasicUsageRecord {
* @param creatorId Creator ID * @param creatorId Creator ID
* @throws InvalidValueException * @throws InvalidValueException
*/ */
@Deprecated
public void setCreatorId(String creatorId) throws InvalidValueException { public void setCreatorId(String creatorId) throws InvalidValueException {
setResourceProperty(CREATOR_ID, creatorId); setResourceProperty(CREATOR_ID, creatorId);
} }
@ -170,6 +195,7 @@ public class RawUsageRecord extends BasicUsageRecord {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
@Deprecated
public void setConsumerId(String consumerId) { public void setConsumerId(String consumerId) {
try{ try{
setConsumerId(consumerId); setConsumerId(consumerId);
@ -179,12 +205,13 @@ public class RawUsageRecord extends BasicUsageRecord {
} }
/** /**
* {@inheritDoc} * Set the accounting scope of the {#UsageRecord}
* @param scope The accounting scope of the {#UsageRecord}
*/ */
@Override @Deprecated
public void setResourceScope(String scope) { public void setResourceScope(String scope) {
try{ try{
setResourceScope(scope); setResourceProperty(RESOURCE_SCOPE, scope);
}catch(Exception e){ }catch(Exception e){
logger.error("Unable to Set {}", RESOURCE_SCOPE); logger.error("Unable to Set {}", RESOURCE_SCOPE);
} }

View File

@ -56,14 +56,14 @@ public interface UsageRecord extends Comparable<UsageRecord>{
* Return the accounting scope of the {#UsageRecord} * Return the accounting scope of the {#UsageRecord}
* @return The Accounting scope of the {#UsageRecord} * @return The Accounting scope of the {#UsageRecord}
*/ */
public String getResourceScope(); public String getScope();
/** /**
* Set the accounting scope of the {#UsageRecord} * Set the accounting scope of the {#UsageRecord}
* @param scope The accounting scope of the {#UsageRecord} * @param scope The accounting scope of the {#UsageRecord}
* @throws InvalidValueException * @throws InvalidValueException
*/ */
public void setResourceScope(String scope) throws InvalidValueException; public void setScope(String scope) throws InvalidValueException;
/* * /* *
* Return the id of the usage record aggregating this, null if this record * Return the id of the usage record aggregating this, null if this record
@ -78,6 +78,7 @@ public interface UsageRecord extends Comparable<UsageRecord>{
* @throws InvalidValueException * @throws InvalidValueException
* / * /
public void setAggregatedUsageRecordId(String aggregatedId) throws InvalidValueException; public void setAggregatedUsageRecordId(String aggregatedId) throws InvalidValueException;
*/
/** /**
* Return all resource-specific properties. The returned Map is a copy of * Return all resource-specific properties. The returned Map is a copy of

View File

@ -8,12 +8,9 @@ import java.util.Map;
import org.gcube.accounting.datamodel.BasicUsageRecord; import org.gcube.accounting.datamodel.BasicUsageRecord;
import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord;
import org.gcube.accounting.datamodel.decorators.AggregatedField;
import org.gcube.accounting.datamodel.decorators.RequiredField; import org.gcube.accounting.datamodel.decorators.RequiredField;
import org.gcube.accounting.datamodel.deprecationmanagement.annotations.DeprecatedWarning;
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty; import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
import org.gcube.accounting.datamodel.validations.annotations.ValidIP; import org.gcube.accounting.datamodel.validations.annotations.ValidIP;
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
import org.gcube.accounting.datamodel.validations.annotations.ValidLong; import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
@ -31,19 +28,9 @@ public class ServiceUsageRecord extends BasicUsageRecord implements SingleUsageR
@ValidIP @ValidIP
public static final String CALLER_IP = "callerIP"; public static final String CALLER_IP = "callerIP";
@RequiredField @NotEmpty @RequiredField @NotEmpty
public static final String CALLER_SCOPE = "callerScope";
@RequiredField @NotEmpty
public static final String REF_HOST = "refHost"; public static final String REF_HOST = "refHost";
@RequiredField @NotEmpty @RequiredField @NotEmpty
public static final String REF_VM = "refVM"; public static final String REF_VM = "refVM";
@NotEmpty @DeprecatedWarning
protected static final String DOMAIN = "domain";
@ValidInteger @AggregatedField
protected static final String INVOCATION_COUNT = "invocationCount";
@ValidInteger @AggregatedField
protected static final String AVERAGE_INVOCATION_COUNT = "averageInvocationTime";
@RequiredField @NotEmpty @RequiredField @NotEmpty
public static final String SERVICE_CLASS = "serviceClass"; public static final String SERVICE_CLASS = "serviceClass";
@RequiredField @NotEmpty @RequiredField @NotEmpty
@ -68,14 +55,6 @@ public class ServiceUsageRecord extends BasicUsageRecord implements SingleUsageR
setResourceProperty(CALLER_IP, callerIP); setResourceProperty(CALLER_IP, callerIP);
} }
public String getCallerScope() {
return (String) this.resourceProperties.get(CALLER_SCOPE);
}
public void setCallerScope(String callerScope) throws InvalidValueException {
setResourceProperty(CALLER_SCOPE, callerScope);
}
public String getRefHost() { public String getRefHost() {
return (String) this.resourceProperties.get(REF_HOST); return (String) this.resourceProperties.get(REF_HOST);
} }
@ -92,32 +71,6 @@ public class ServiceUsageRecord extends BasicUsageRecord implements SingleUsageR
setResourceProperty(REF_VM, refVM); setResourceProperty(REF_VM, refVM);
} }
/*
protected String getDomain() {
return (String) this.resourceProperties.get(DOMAIN);
}
protected void setDomain(String domain) throws InvalidValueException {
setResourceProperty(DOMAIN, domain);
}
public String getInvocationCount() {
return (String) this.resourceProperties.get(INVOCATION_COUNT);
}
public void setInvocationCount(String invocationCount) throws InvalidValueException {
setResourceProperty(INVOCATION_COUNT, invocationCount);
}
public String getAverageInvocationTime() {
return (String) this.resourceProperties.get(AVERAGE_INVOCATION_COUNT);
}
public void setAverageInvocationTime(String averageInvocationTime) throws InvalidValueException {
setResourceProperty(AVERAGE_INVOCATION_COUNT, averageInvocationTime);
}
*/
public String getServiceClass() { public String getServiceClass() {
return (String) this.resourceProperties.get(SERVICE_CLASS); return (String) this.resourceProperties.get(SERVICE_CLASS);
} }

View File

@ -4,17 +4,17 @@
package org.gcube.accounting.datamodel.implementations.aggregated; package org.gcube.accounting.datamodel.implementations.aggregated;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord; import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.accounting.exception.NotAggregatableRecorsExceptions;
/** /**
* This Class is for library internal use only
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/ */
public class JobUsageRecord extends org.gcube.accounting.datamodel.implementations.JobUsageRecord implements AggregatedUsageRecord<JobUsageRecord> { public class JobUsageRecord extends org.gcube.accounting.datamodel.implementations.JobUsageRecord implements AggregatedUsageRecord<JobUsageRecord, org.gcube.accounting.datamodel.implementations.JobUsageRecord> {
/** /**
* Generated Serial Version UID * Generated Serial Version UID
@ -35,10 +35,23 @@ public class JobUsageRecord extends org.gcube.accounting.datamodel.implementatio
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public List<JobUsageRecord> aggregate(List<JobUsageRecord> records) { public JobUsageRecord getAggregatedUsageRecord(
// TODO org.gcube.accounting.datamodel.implementations.JobUsageRecord b)
throw new UnsupportedOperationException(); throws InvalidValueException {
// TODO Auto-generated method stub
return null;
}
/**
* {@inheritDoc}
*/
@Override
public void aggregate(JobUsageRecord record)
throws NotAggregatableRecorsExceptions {
// TODO Auto-generated method stub
} }
} }

View File

@ -4,17 +4,17 @@
package org.gcube.accounting.datamodel.implementations.aggregated; package org.gcube.accounting.datamodel.implementations.aggregated;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord; import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.accounting.exception.NotAggregatableRecorsExceptions;
/** /**
* This Class is for library internal use only
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/ */
public class PortletUsageRecord extends org.gcube.accounting.datamodel.implementations.PortletUsageRecord implements AggregatedUsageRecord<PortletUsageRecord> { public class PortletUsageRecord extends org.gcube.accounting.datamodel.implementations.PortletUsageRecord implements AggregatedUsageRecord<PortletUsageRecord, org.gcube.accounting.datamodel.implementations.PortletUsageRecord> {
/** /**
@ -36,9 +36,23 @@ public class PortletUsageRecord extends org.gcube.accounting.datamodel.implement
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public List<PortletUsageRecord> aggregate(List<PortletUsageRecord> records) { public PortletUsageRecord getAggregatedUsageRecord(
// TODO org.gcube.accounting.datamodel.implementations.PortletUsageRecord b)
throw new UnsupportedOperationException(); throws InvalidValueException {
// TODO Auto-generated method stub
return null;
} }
/**
* {@inheritDoc}
*/
@Override
public void aggregate(PortletUsageRecord record)
throws NotAggregatableRecorsExceptions {
// TODO Auto-generated method stub
}
} }

View File

@ -4,29 +4,32 @@
package org.gcube.accounting.datamodel.implementations.aggregated; package org.gcube.accounting.datamodel.implementations.aggregated;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord; import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.datamodel.decorators.AggregatedField; import org.gcube.accounting.datamodel.decorators.AggregatedField;
import org.gcube.accounting.datamodel.decorators.RequiredField;
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger; import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
/** /**
* This Class is for library internal use only
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/ */
public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implementations.ServiceUsageRecord implements AggregatedUsageRecord<ServiceUsageRecord> { public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implementations.ServiceUsageRecord implements AggregatedUsageRecord<ServiceUsageRecord, org.gcube.accounting.datamodel.implementations.ServiceUsageRecord> {
/** /**
* Generated Serial Version UID * Generated Serial Version UID
*/ */
private static final long serialVersionUID = 6387584974618335623L; private static final long serialVersionUID = 6387584974618335623L;
@ValidInteger @AggregatedField @ValidInteger @AggregatedField @RequiredField
protected static final String INVOCATION_COUNT = "invocationCount"; protected static final String INVOCATION_COUNT = "invocationCount";
@ValidInteger @AggregatedField @ValidLong @AggregatedField @RequiredField
protected static final String AVERAGE_INVOCATION_COUNT = "averageInvocationTime"; protected static final String MAX_INVOCATION_TIME = "maxInvocationTime";
@ValidLong @AggregatedField @RequiredField
protected static final String MIN_INVOCATION_TIME = "minInvocationTime";
public ServiceUsageRecord(){ public ServiceUsageRecord(){
super(); super();
@ -38,28 +41,77 @@ public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implement
this.resourceProperties.put(AGGREGATED, true); this.resourceProperties.put(AGGREGATED, true);
} }
public String getInvocationCount() { protected ServiceUsageRecord(org.gcube.accounting.datamodel.implementations.ServiceUsageRecord record) throws InvalidValueException{
return (String) this.resourceProperties.get(INVOCATION_COUNT); super(record.getResourceProperties());
this.setInvocationCount(1);
long duration = record.getDuration();
this.setMinInvocationTime(duration);
this.setMaxInvocationTime(duration);
this.resourceProperties.put(AGGREGATED, true);
} }
public void setInvocationCount(String invocationCount) throws InvalidValueException {
public int getInvocationCount() {
return (Integer) this.resourceProperties.get(INVOCATION_COUNT);
}
public void setInvocationCount(int invocationCount) throws InvalidValueException {
setResourceProperty(INVOCATION_COUNT, invocationCount); setResourceProperty(INVOCATION_COUNT, invocationCount);
} }
public String getAverageInvocationTime() { public long getMaxInvocationTime() {
return (String) this.resourceProperties.get(AVERAGE_INVOCATION_COUNT); return (Long) this.resourceProperties.get(MAX_INVOCATION_TIME);
} }
public void setAverageInvocationTime(String averageInvocationTime) throws InvalidValueException { public void setMaxInvocationTime(long maxInvocationTime) throws InvalidValueException {
setResourceProperty(AVERAGE_INVOCATION_COUNT, averageInvocationTime); setResourceProperty(MAX_INVOCATION_TIME, maxInvocationTime);
}
public long getMinInvocationTime() {
return (Long) this.resourceProperties.get(MIN_INVOCATION_TIME);
}
public void setMinInvocationTime(long minInvocationTime) throws InvalidValueException {
setResourceProperty(MIN_INVOCATION_TIME, minInvocationTime);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public List<ServiceUsageRecord> aggregate(List<ServiceUsageRecord> records) { public void aggregate(ServiceUsageRecord record) {
// TODO try {
throw new UnsupportedOperationException(); this.setInvocationCount(this.getInvocationCount() + record.getInvocationCount());
this.setDuration((this.getDuration() + record.getDuration()) / 2);
long max = ((ServiceUsageRecord) record).getMaxInvocationTime();
if(max > this.getMaxInvocationTime()){
this.setMaxInvocationTime(max);
}
long min = ((ServiceUsageRecord) record).getMinInvocationTime();
if(min < this.getMinInvocationTime()){
this.setMinInvocationTime(min);
}
}catch(Exception e){
throw new UnsupportedOperationException(e.getCause());
} }
} }
/**
* {@inheritDoc}
*/
@Override
public ServiceUsageRecord getAggregatedUsageRecord(
org.gcube.accounting.datamodel.implementations.ServiceUsageRecord record)
throws InvalidValueException {
if(record instanceof ServiceUsageRecord){
return (ServiceUsageRecord) record;
}
return new ServiceUsageRecord(record);
}
}

View File

@ -10,8 +10,8 @@ import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
import org.gcube.accounting.datamodel.validations.annotations.ValidLong; import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
*/ */
/** /**
* This Class is for library internal use only
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/ */
@Deprecated @Deprecated

View File

@ -4,17 +4,17 @@
package org.gcube.accounting.datamodel.implementations.aggregated; package org.gcube.accounting.datamodel.implementations.aggregated;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord; import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.accounting.exception.NotAggregatableRecorsExceptions;
/** /**
* This Class is for library internal use only
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/ */
public class StorageUsageRecord extends org.gcube.accounting.datamodel.implementations.StorageUsageRecord implements AggregatedUsageRecord<StorageUsageRecord> { public class StorageUsageRecord extends org.gcube.accounting.datamodel.implementations.StorageUsageRecord implements AggregatedUsageRecord<StorageUsageRecord, org.gcube.accounting.datamodel.implementations.StorageUsageRecord> {
/** /**
* Generated Serial Version UID * Generated Serial Version UID
@ -35,8 +35,22 @@ public class StorageUsageRecord extends org.gcube.accounting.datamodel.implement
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public List<StorageUsageRecord> aggregate(List<StorageUsageRecord> records) { public StorageUsageRecord getAggregatedUsageRecord(
// TODO org.gcube.accounting.datamodel.implementations.StorageUsageRecord b)
throw new UnsupportedOperationException(); throws InvalidValueException {
// TODO Auto-generated method stub
return null;
} }
/**
* {@inheritDoc}
*/
@Override
public void aggregate(StorageUsageRecord record)
throws NotAggregatableRecorsExceptions {
// TODO Auto-generated method stub
}
} }

View File

@ -0,0 +1,27 @@
/**
*
*/
package org.gcube.accounting.datamodel.implementations.aggregationstrategy;
import org.gcube.accounting.datamodel.AggregationStrategy;
import org.gcube.accounting.datamodel.implementations.aggregated.ServiceUsageRecord;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class ServiceUsageRecordAggregationStrategy extends AggregationStrategy<ServiceUsageRecord, org.gcube.accounting.datamodel.implementations.ServiceUsageRecord>{
/**
* @param serviceUsageRecord
*/
public ServiceUsageRecordAggregationStrategy(ServiceUsageRecord serviceUsageRecord) {
super(serviceUsageRecord);
this.aggregationField.add(ServiceUsageRecord.CALLER_IP);
this.aggregationField.add(ServiceUsageRecord.REF_HOST);
this.aggregationField.add(ServiceUsageRecord.REF_VM);
this.aggregationField.add(ServiceUsageRecord.SERVICE_CLASS);
this.aggregationField.add(ServiceUsageRecord.SERVICE_NAME);
}
}

View File

@ -15,6 +15,10 @@ public class InvalidValueException extends Exception {
super(message); super(message);
} }
public InvalidValueException(Throwable cause) {
super(cause);
}
public InvalidValueException(String message, Throwable cause) { public InvalidValueException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }

View File

@ -0,0 +1,32 @@
/**
*
*/
package org.gcube.accounting.exception;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class NotAggregatableRecorsExceptions extends Exception {
/**
* Generated serial Version UID
*/
private static final long serialVersionUID = -1477792189431118048L;
public NotAggregatableRecorsExceptions() {
super();
}
public NotAggregatableRecorsExceptions(String message) {
super(message);
}
public NotAggregatableRecorsExceptions(Throwable cause) {
super(cause);
}
public NotAggregatableRecorsExceptions(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -8,6 +8,7 @@ import java.util.ServiceLoader;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.gcube.accounting.datamodel.SingleUsageRecord;
import org.gcube.accounting.datamodel.UsageRecord; import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.implementations.ServiceUsageRecord; import org.gcube.accounting.datamodel.implementations.ServiceUsageRecord;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
@ -80,12 +81,12 @@ public abstract class Persistence {
} }
} }
public static UsageRecord createTestUsageRecord(){ public static SingleUsageRecord createTestUsageRecord(){
ServiceUsageRecord serviceUsageRecord = new ServiceUsageRecord(); ServiceUsageRecord serviceUsageRecord = new ServiceUsageRecord();
try { try {
serviceUsageRecord.setConsumerId("accounting"); serviceUsageRecord.setConsumerId("accounting");
serviceUsageRecord.setResourceScope("/gcube/devsec"); serviceUsageRecord.setScope("/gcube/devsec");
//Calendar creationTime = new GregorianCalendar(); //Calendar creationTime = new GregorianCalendar();
//Calendar startTime = new GregorianCalendar(); //Calendar startTime = new GregorianCalendar();
@ -101,7 +102,7 @@ public abstract class Persistence {
serviceUsageRecord.setServiceName("Accounting-Lib"); serviceUsageRecord.setServiceName("Accounting-Lib");
serviceUsageRecord.setRefHost("localhost"); serviceUsageRecord.setRefHost("localhost");
serviceUsageRecord.setRefVM("local"); serviceUsageRecord.setRefVM("local");
serviceUsageRecord.setCallerScope("/gcube/devsec");
} catch (InvalidValueException e1) { } catch (InvalidValueException e1) {
@ -157,7 +158,7 @@ public abstract class Persistence {
*/ */
protected abstract void reallyAccount(UsageRecord usageRecord) throws Exception; protected abstract void reallyAccount(UsageRecord usageRecord) throws Exception;
private void accountWithFallback(UsageRecord usageRecord) throws Exception { private void accountWithFallback(SingleUsageRecord usageRecord) throws Exception {
String persistenceName = getInstance().getClass().getSimpleName(); String persistenceName = getInstance().getClass().getSimpleName();
try { try {
//logger.debug("Going to account {} using {}", usageRecord, persistenceName); //logger.debug("Going to account {} using {}", usageRecord, persistenceName);
@ -186,7 +187,7 @@ public abstract class Persistence {
* so that the {@link #UsageRecord} can be recorder later. * so that the {@link #UsageRecord} can be recorder later.
* @param usageRecord the {@link #UsageRecord} to persist * @param usageRecord the {@link #UsageRecord} to persist
*/ */
public void account(final UsageRecord usageRecord){ public void account(final SingleUsageRecord usageRecord){
Runnable runnable = new Runnable(){ Runnable runnable = new Runnable(){
@Override @Override
public void run(){ public void run(){

View File

@ -21,12 +21,11 @@ public class ServiceUsageRecordTest {
ServiceUsageRecord serviceUsageRecord = new ServiceUsageRecord(); ServiceUsageRecord serviceUsageRecord = new ServiceUsageRecord();
serviceUsageRecord.setConsumerId("luca.frosini"); serviceUsageRecord.setConsumerId("luca.frosini");
serviceUsageRecord.setResourceScope("/gcube/devsec"); serviceUsageRecord.setScope("/gcube/devsec");
serviceUsageRecord.setServiceClass("Accounting"); serviceUsageRecord.setServiceClass("Accounting");
serviceUsageRecord.setServiceName("Accounting-Lib"); serviceUsageRecord.setServiceName("Accounting-Lib");
serviceUsageRecord.setRefHost("localhost"); serviceUsageRecord.setRefHost("localhost");
serviceUsageRecord.setRefVM("local"); serviceUsageRecord.setRefVM("local");
serviceUsageRecord.setCallerScope("/gcube/devsec");
serviceUsageRecord.setResourceProperty("ConnectionTest", "Test"); serviceUsageRecord.setResourceProperty("ConnectionTest", "Test");

View File

@ -6,7 +6,7 @@ package org.gcube.accounting.datamodel.persistence;
import java.util.Calendar; import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import org.gcube.accounting.datamodel.UsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord;
import org.gcube.accounting.persistence.Persistence; import org.gcube.accounting.persistence.Persistence;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -36,7 +36,7 @@ public class PersistenceTest {
int quantity = 3000; int quantity = 3000;
Calendar startTestTime = new GregorianCalendar(); Calendar startTestTime = new GregorianCalendar();
for(int i=0; i< quantity; i++){ for(int i=0; i< quantity; i++){
UsageRecord usageRecord = Persistence.createTestUsageRecord(); SingleUsageRecord usageRecord = Persistence.createTestUsageRecord();
persistence.account(usageRecord); persistence.account(usageRecord);
} }
Calendar stopTestTime = new GregorianCalendar(); Calendar stopTestTime = new GregorianCalendar();