refs #200: Create accouting-lib library

https://support.d4science.org/issues/200
Implementing library

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115267 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-06-09 13:53:21 +00:00
parent 4f52f0ce85
commit e43bfe1843
12 changed files with 151 additions and 11 deletions

View File

@ -5,6 +5,7 @@ package org.gcube.accounting.datamodel;
import java.io.Serializable; import java.io.Serializable;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -52,12 +53,20 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
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 RESOURCE_TYPE = "resourceType"; //@RequiredField @NotEmpty
//protected static final String RESOURCE_TYPE = "resourceType";
protected static final String USAGE_RECORD_TYPE = "resourceType";
@RequiredField @NotEmpty @RequiredField @NotEmpty
public static final String RESOURCE_SCOPE = "resourceScope"; public static final String RESOURCE_SCOPE = "resourceScope";
@DeprecatedWarning @NotEmpty @DeprecatedWarning @NotEmpty
protected static final String RESOURCE_OWNER = "resourceOwner"; protected static final String RESOURCE_OWNER = "resourceOwner";
@AggregatedField @NotEmptyIfNotNull
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";
@NotEmptyIfNotNull @MoveToAggregatedUsageRecordId @NotEmptyIfNotNull @MoveToAggregatedUsageRecordId
@ -116,16 +125,16 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
initializeValidation(); initializeValidation();
} }
protected RawUsageRecord(){ public RawUsageRecord(){
init(); init();
this.resourceProperties = new HashMap<String, Serializable>(); this.resourceProperties = new HashMap<String, Serializable>();
this.resourceProperties.put(ID, UUID.randomUUID().toString()); this.resourceProperties.put(ID, UUID.randomUUID().toString());
this.resourceProperties.put(RESOURCE_TYPE, this.getClass().getSimpleName()); this.resourceProperties.put(USAGE_RECORD_TYPE, this.getClass().getSimpleName());
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
this.resourceProperties.put(CREATION_TIME, calendar.getTimeInMillis()); this.resourceProperties.put(CREATION_TIME, calendar.getTimeInMillis());
} }
protected RawUsageRecord(Map<String, Serializable> properties) throws InvalidValueException { public RawUsageRecord(Map<String, Serializable> properties) throws InvalidValueException {
init(); init();
setResourceProperties(properties); setResourceProperties(properties);
} }
@ -547,6 +556,28 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
} }
@SuppressWarnings("unchecked")
protected static Class<? extends UsageRecord> getClass(String usageRecordName, boolean aggregated) throws ClassNotFoundException {
Class<? extends UsageRecord> clz = null;
Class<? extends UsageRecord> utilityClass = org.gcube.accounting.datamodel.implementations.JobUsageRecord.class;
if(aggregated){
utilityClass = org.gcube.accounting.datamodel.implementations.aggregated.JobUsageRecord.class;
}
String classCanonicalName = utilityClass.getCanonicalName();
classCanonicalName.replace(utilityClass.getSimpleName(), usageRecordName);
try {
clz = (Class<? extends UsageRecord>) Class.forName(classCanonicalName);
} catch (ClassNotFoundException e) {
logger.error("Unable to retrieve class {}", classCanonicalName);
throw e;
}
return clz;
}
/** /**
* This method use the resourceType value contained in the Map to instance * This method use the resourceType value contained in the Map to instance
* the right UsageRecord class and return it. If the type implementation * the right UsageRecord class and return it. If the type implementation
@ -557,8 +588,18 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
* @throws Exception if fails * @throws Exception if fails
*/ */
public static UsageRecord getUsageRecord(Map<String, Serializable> usageRecordMap) throws Exception { public static UsageRecord getUsageRecord(Map<String, Serializable> usageRecordMap) throws Exception {
// TODO implements String className = (String) usageRecordMap.get(USAGE_RECORD_TYPE);
throw new UnsupportedOperationException(); boolean aggregated = (Boolean) usageRecordMap.get(AGGREGATED);
Class<? extends UsageRecord> clz = getClass(className, aggregated);
logger.debug("Trying to instantiate {}", clz.getClass().getSimpleName());
@SuppressWarnings("rawtypes")
Class[] usageRecordArgTypes = { Map.class };
Constructor<? extends UsageRecord> usageRecordConstructor = clz.getDeclaredConstructor(usageRecordArgTypes);
Object[] usageRecordArguments = {usageRecordMap};
return usageRecordConstructor.newInstance(usageRecordArguments);
} }
} }

View File

@ -3,7 +3,9 @@
*/ */
package org.gcube.accounting.datamodel.implementations; package org.gcube.accounting.datamodel.implementations;
import java.io.Serializable;
import java.util.Calendar; import java.util.Calendar;
import java.util.Map;
import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.RawUsageRecord;
import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord;
@ -54,6 +56,10 @@ public class JobUsageRecord extends RawUsageRecord implements SingleUsageRecord
super(); super();
} }
public JobUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
super(properties);
}
/** /**
* @return the Job Id * @return the Job Id
*/ */

View File

@ -3,6 +3,9 @@
*/ */
package org.gcube.accounting.datamodel.implementations; package org.gcube.accounting.datamodel.implementations;
import java.io.Serializable;
import java.util.Map;
import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.RawUsageRecord;
import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord;
import org.gcube.accounting.datamodel.decorators.RequiredField; import org.gcube.accounting.datamodel.decorators.RequiredField;
@ -35,6 +38,10 @@ public class PortletUsageRecord extends RawUsageRecord implements SingleUsageRec
super(); super();
} }
public PortletUsageRecord(Map<String, Serializable> properties) throws InvalidValueException {
super(properties);
}
@Deprecated @Deprecated
public String getUserId() { public String getUserId() {
return (String) this.resourceProperties.get(USER_ID); return (String) this.resourceProperties.get(USER_ID);

View File

@ -3,6 +3,9 @@
*/ */
package org.gcube.accounting.datamodel.implementations; package org.gcube.accounting.datamodel.implementations;
import java.io.Serializable;
import java.util.Map;
import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.RawUsageRecord;
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.AggregatedField;
@ -47,6 +50,10 @@ public class ServiceUsageRecord extends RawUsageRecord implements SingleUsageRec
super(); super();
} }
public ServiceUsageRecord(Map<String, Serializable> properties) throws InvalidValueException {
super(properties);
}
public String getCallerIP() { public String getCallerIP() {
return (String) this.resourceProperties.get(CALLER_IP); return (String) this.resourceProperties.get(CALLER_IP);
} }

View File

@ -3,6 +3,9 @@
*/ */
package org.gcube.accounting.datamodel.implementations; package org.gcube.accounting.datamodel.implementations;
import java.io.Serializable;
import java.util.Map;
import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.RawUsageRecord;
import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord;
import org.gcube.accounting.datamodel.decorators.RequiredField; import org.gcube.accounting.datamodel.decorators.RequiredField;
@ -45,6 +48,10 @@ public class StorageUsageUsageRecord extends RawUsageRecord implements SingleUsa
super(); super();
} }
public StorageUsageUsageRecord(Map<String, Serializable> properties) throws InvalidValueException {
super(properties);
}
public String getObjectURI() { public String getObjectURI() {
return (String) this.resourceProperties.get(OBJECT_URI); return (String) this.resourceProperties.get(OBJECT_URI);
} }

View File

@ -3,7 +3,9 @@
*/ */
package org.gcube.accounting.datamodel.implementations; package org.gcube.accounting.datamodel.implementations;
import java.io.Serializable;
import java.util.Calendar; import java.util.Calendar;
import java.util.Map;
import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.RawUsageRecord;
import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord;
@ -61,11 +63,14 @@ public class TaskUsageRecord extends RawUsageRecord implements SingleUsageRecord
@ValidInteger @ValidInteger
public static final String PROCESSORS = "processors"; public static final String PROCESSORS = "processors";
public TaskUsageRecord(){ public TaskUsageRecord(){
super(); super();
} }
public TaskUsageRecord(Map<String, Serializable> properties) throws InvalidValueException {
super(properties);
}
/** /**
* @return the Job Id * @return the Job Id
*/ */

View File

@ -3,9 +3,12 @@
*/ */
package org.gcube.accounting.datamodel.implementations.aggregated; package org.gcube.accounting.datamodel.implementations.aggregated;
import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord; import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.exception.InvalidValueException;
/** /**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
@ -20,6 +23,12 @@ public class JobUsageRecord extends org.gcube.accounting.datamodel.implementatio
public JobUsageRecord(){ public JobUsageRecord(){
super(); super();
this.resourceProperties.put(AGGREGATED, true);
}
public JobUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
super(properties);
this.resourceProperties.put(AGGREGATED, true);
} }
/** /**

View File

@ -3,9 +3,12 @@
*/ */
package org.gcube.accounting.datamodel.implementations.aggregated; package org.gcube.accounting.datamodel.implementations.aggregated;
import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord; import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.exception.InvalidValueException;
/** /**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
@ -21,6 +24,12 @@ public class PortletUsageRecord extends org.gcube.accounting.datamodel.implement
public PortletUsageRecord(){ public PortletUsageRecord(){
super(); super();
this.resourceProperties.put(AGGREGATED, true);
}
public PortletUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
super(properties);
this.resourceProperties.put(AGGREGATED, true);
} }
/** /**

View File

@ -3,7 +3,9 @@
*/ */
package org.gcube.accounting.datamodel.implementations.aggregated; package org.gcube.accounting.datamodel.implementations.aggregated;
import java.io.Serializable;
import java.util.List; import java.util.List;
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;
@ -28,8 +30,13 @@ public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implement
public ServiceUsageRecord(){ public ServiceUsageRecord(){
super(); super();
this.resourceProperties.put(AGGREGATED, true);
} }
public ServiceUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
super(properties);
this.resourceProperties.put(AGGREGATED, true);
}
public String getInvocationCount() { public String getInvocationCount() {
return (String) this.resourceProperties.get(INVOCATION_COUNT); return (String) this.resourceProperties.get(INVOCATION_COUNT);

View File

@ -4,10 +4,12 @@
package org.gcube.accounting.datamodel.implementations.aggregated; package org.gcube.accounting.datamodel.implementations.aggregated;
import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.RawUsageRecord;
/*
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty; import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
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.datamodel.validations.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
*/
/** /**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
@ -20,6 +22,7 @@ public class StorageStatusUsageRecord extends RawUsageRecord {
*/ */
private static final long serialVersionUID = 2456314684466092685L; private static final long serialVersionUID = 2456314684466092685L;
/*
@NotEmpty @NotEmpty
public static final String PROVIDER_ID = "providerId"; public static final String PROVIDER_ID = "providerId";
@NotEmpty @NotEmpty
@ -74,5 +77,5 @@ public class StorageStatusUsageRecord extends RawUsageRecord {
public void setDataCount(int dataCount) throws InvalidValueException { public void setDataCount(int dataCount) throws InvalidValueException {
setResourceProperty(DATA_COUNT, dataCount); setResourceProperty(DATA_COUNT, dataCount);
} }
*/
} }

View File

@ -3,9 +3,12 @@
*/ */
package org.gcube.accounting.datamodel.implementations.aggregated; package org.gcube.accounting.datamodel.implementations.aggregated;
import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord; import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.exception.InvalidValueException;
/** /**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
@ -20,6 +23,12 @@ public class StorageUsageUsageRecord extends org.gcube.accounting.datamodel.impl
public StorageUsageUsageRecord(){ public StorageUsageUsageRecord(){
super(); super();
this.resourceProperties.put(AGGREGATED, true);
}
public StorageUsageUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
super(properties);
this.resourceProperties.put(AGGREGATED, true);
} }
/** /**

View File

@ -0,0 +1,30 @@
/**
*
*/
package org.gcube.accounting.datamodel.validations.validators;
import java.io.Serializable;
import org.gcube.accounting.exception.InvalidValueException;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class NotEmptyIfNotNullValidatorTest {
@Test
public void testBoolean() throws InvalidValueException{
NotEmptyIfNotNullValidator notEmptyIfNotNullValidator = new NotEmptyIfNotNullValidator();
Serializable primitiveTrue = notEmptyIfNotNullValidator.validate(null, true, null);
Assert.assertTrue((Boolean) primitiveTrue);
Serializable primitiveFalse = notEmptyIfNotNullValidator.validate(null, false, null);
Assert.assertFalse((Boolean) primitiveFalse);
Serializable booleanClassTrue = notEmptyIfNotNullValidator.validate(null, Boolean.TRUE, null);
Assert.assertTrue((Boolean) booleanClassTrue);
Serializable booleanClassFalse = notEmptyIfNotNullValidator.validate(null, Boolean.FALSE, null);
Assert.assertFalse((Boolean) booleanClassFalse);
}
}