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.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
@ -52,12 +53,20 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
protected static final String START_TIME = "startTime";
@AggregatedField @ValidLong
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
public static final String RESOURCE_SCOPE = "resourceScope";
@DeprecatedWarning @NotEmpty
protected static final String RESOURCE_OWNER = "resourceOwner";
@AggregatedField @NotEmptyIfNotNull
protected static final String AGGREGATED = "aggregated";
@AggregatedField @NotEmptyIfNotNull
protected static final String AGGREGATED_USAGE_RECORD_ID = "aggregatedUsageRecordId";
@NotEmptyIfNotNull @MoveToAggregatedUsageRecordId
@ -116,16 +125,16 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
initializeValidation();
}
protected RawUsageRecord(){
public RawUsageRecord(){
init();
this.resourceProperties = new HashMap<String, Serializable>();
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();
this.resourceProperties.put(CREATION_TIME, calendar.getTimeInMillis());
}
protected RawUsageRecord(Map<String, Serializable> properties) throws InvalidValueException {
public RawUsageRecord(Map<String, Serializable> properties) throws InvalidValueException {
init();
setResourceProperties(properties);
}
@ -546,6 +555,28 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
return 1;
}
@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
@ -557,8 +588,18 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
* @throws Exception if fails
*/
public static UsageRecord getUsageRecord(Map<String, Serializable> usageRecordMap) throws Exception {
// TODO implements
throw new UnsupportedOperationException();
String className = (String) usageRecordMap.get(USAGE_RECORD_TYPE);
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;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Map;
import org.gcube.accounting.datamodel.RawUsageRecord;
import org.gcube.accounting.datamodel.SingleUsageRecord;
@ -54,6 +56,10 @@ public class JobUsageRecord extends RawUsageRecord implements SingleUsageRecord
super();
}
public JobUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
super(properties);
}
/**
* @return the Job Id
*/

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -3,9 +3,12 @@
*/
package org.gcube.accounting.datamodel.implementations.aggregated;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.exception.InvalidValueException;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
@ -20,6 +23,12 @@ public class StorageUsageUsageRecord extends org.gcube.accounting.datamodel.impl
public StorageUsageUsageRecord(){
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);
}
}