refs #200: Create accouting-lib library

https://support.d4science.org/issues/200
Fixing model

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115587 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-06-26 13:50:39 +00:00
parent 10918da739
commit 21f740c0f7
14 changed files with 257 additions and 215 deletions

View File

@ -25,6 +25,7 @@ import org.gcube.accounting.datamodel.decorators.FieldDecorator;
import org.gcube.accounting.datamodel.decorators.RequiredField;
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
import org.gcube.accounting.datamodel.validations.annotations.NotEmptyIfNotNull;
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
import org.gcube.accounting.datamodel.validations.annotations.ValidOperationResult;
import org.gcube.accounting.exception.InvalidValueException;
@ -113,6 +114,11 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable {
*/
@AggregatedField @NotEmptyIfNotNull
protected static final String AGGREGATED = "aggregated";
/**
* KEY for : Indicate The Number of Aggregated Operation
*/
@AggregatedField @ValidInteger
protected static final String OPERATION_COUNT = "operationCount";
protected Set<String> aggregatedFields;
@ -159,7 +165,7 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable {
/**
* Initialize variable
*/
protected void init() {
private void init() {
this.validation = new HashMap<String, List<FieldAction>>();
this.requiredFields = new HashSet<String>();
this.aggregatedFields = new HashSet<String>();
@ -236,58 +242,6 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable {
setResourceProperty(CREATION_TIME, creationTime.getTimeInMillis());
}
/**
* Return the left end of the time interval covered by this {#UsageRecord}
* @return Start Time
*/
protected long getStartTimeInMillis() {
return (Long) this.resourceProperties.get(START_TIME);
}
/**
* Return the left end of the time interval covered by this {#UsageRecord}
* @return Start Time
*/
protected Calendar getStartTimeAsCalendar() {
long millis = getStartTimeInMillis();
return timestampStringToCalendar(millis);
}
/**
* Set the left end of the time interval covered by this {#UsageRecord}
* @param startTime Start Time
* @throws InvalidValueException
*/
protected void setStartTime(Calendar startTime) throws InvalidValueException {
setResourceProperty(START_TIME, startTime.getTimeInMillis());
}
/**
* Return the right end of the time interval covered by this {#UsageRecord}
* @return End Time
*/
protected long getEndTimeInMillis() {
return (Long) this.resourceProperties.get(END_TIME);
}
/**
* Return the right end of the time interval covered by this {#UsageRecord}
* @return End Time
*/
protected Calendar getEndTimeAsCalendar() {
long millis = getEndTimeInMillis();
return timestampStringToCalendar(millis);
}
/**
* Set the right end of the time interval covered by this {#UsageRecord}
* @param endTime End Time
* @throws InvalidValueException
*/
protected void setEndTime(Calendar endTime) throws InvalidValueException {
setResourceProperty(END_TIME, endTime.getTimeInMillis());
}
/**
* {@inheritDoc}
*/
@ -342,6 +296,72 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable {
}
}
// AGGREGATION
/* --------------------------------------- */
/**
* Return the left end of the time interval covered by this {#UsageRecord}
* @return Start Time
*/
protected long getStartTimeInMillis() {
return (Long) this.resourceProperties.get(START_TIME);
}
/**
* Return the left end of the time interval covered by this {#UsageRecord}
* @return Start Time
*/
protected Calendar getStartTimeAsCalendar() {
long millis = getStartTimeInMillis();
return timestampStringToCalendar(millis);
}
/**
* Set the left end of the time interval covered by this {#UsageRecord}
* @param startTime Start Time
* @throws InvalidValueException
*/
protected void setStartTime(Calendar startTime) throws InvalidValueException {
setResourceProperty(START_TIME, startTime.getTimeInMillis());
}
/**
* Return the right end of the time interval covered by this {#UsageRecord}
* @return End Time
*/
protected long getEndTimeInMillis() {
return (Long) this.resourceProperties.get(END_TIME);
}
/**
* Return the right end of the time interval covered by this {#UsageRecord}
* @return End Time
*/
protected Calendar getEndTimeAsCalendar() {
long millis = getEndTimeInMillis();
return timestampStringToCalendar(millis);
}
/**
* Set the right end of the time interval covered by this {#UsageRecord}
* @param endTime End Time
* @throws InvalidValueException
*/
protected void setEndTime(Calendar endTime) throws InvalidValueException {
setResourceProperty(END_TIME, endTime.getTimeInMillis());
}
protected int getOperationCount() {
return (Integer) this.resourceProperties.get(OPERATION_COUNT);
}
protected void setOperationCount(int operationCount) throws InvalidValueException {
setResourceProperty(OPERATION_COUNT, operationCount);
}
/* --------------------------------------- */
protected Serializable validateField(String key, Serializable serializable) throws InvalidValueException {
if(key == null){
throw new InvalidValueException("The key of property to set cannot be null");

View File

@ -9,6 +9,7 @@ import java.net.URISyntaxException;
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
import org.gcube.accounting.datamodel.implementations.ServiceUsageRecord;
import org.gcube.accounting.datamodel.implementations.StorageUsageRecord;
import org.gcube.accounting.datamodel.implementations.StorageUsageRecord.DataType;
import org.gcube.accounting.datamodel.implementations.StorageUsageRecord.OperationType;
import org.gcube.accounting.exception.InvalidValueException;
import org.slf4j.Logger;
@ -28,8 +29,8 @@ public class TestUsageRecord {
public final static String TEST_SERVICE_CLASS = "TestServiceClass";
public final static String TEST_SERVICE_NAME = "TestServiceName";
public final static String TEST_REF_HOST = "localhost";
public final static String TEST_REF_VM = "local";
public final static String TEST_CALLER_HOST = "remotehost";
public final static String TEST_HOST = "localhost";
public final static String TEST_PROPERTY_NAME = "TestPropertyName";
public final static String TEST_PROPERTY_VALUE = "TestPropertyValue";
@ -57,11 +58,11 @@ public class TestUsageRecord {
usageRecord.setScope(TEST_SCOPE);
usageRecord.setOperationResult(OperationResult.SUCCESS);
usageRecord.setServiceClass(TEST_SERVICE_CLASS);
usageRecord.setServiceName(TEST_SERVICE_NAME);
usageRecord.setCallerHost(TEST_CALLER_HOST);
usageRecord.setHost(TEST_HOST);
usageRecord.setRefHost(TEST_REF_HOST);
usageRecord.setRefVM(TEST_REF_VM);
usageRecord.setServiceClass(TEST_SERVICE_CLASS);
usageRecord.setServiceName(TEST_SERVICE_NAME);
usageRecord.setDuration(generateRandomLong(MIN_DURATION, MAX_DURATION));
@ -77,7 +78,8 @@ public class TestUsageRecord {
public final static String TEST_RESOUCE_OWNER = "resource.owner";
public final static String TEST_RESOUCE_SCOPE = TEST_SCOPE;
public final static String TEST_OBJECT_URI = "testprotocol://objectURI";
public final static String TEST_RESOURCE_URI = "testprotocol://objectURI";
public final static String TEST_PROVIDER_URI = "testprotocol://providerURI";
private final static long MIN_DATA_VOLUME = 1024;
private final static long MAX_DATA_VOLUME = 10240;
@ -95,14 +97,15 @@ public class TestUsageRecord {
usageRecord.setResourceOwner(TEST_RESOUCE_OWNER);
usageRecord.setResourceScope(TEST_RESOUCE_SCOPE);
usageRecord.setResourceURI(new URI(TEST_OBJECT_URI));
usageRecord.setOperationType(OperationType.READ);
usageRecord.setResourceURI(new URI(TEST_RESOURCE_URI));
usageRecord.setProviderURI(new URI(TEST_PROVIDER_URI));
usageRecord.setOperationType(OperationType.READ);
usageRecord.setDataType(DataType.STORAGE);
usageRecord.setDataType(StorageUsageRecord.STORAGE_DATA_TYPE);
usageRecord.setDataVolume(generateRandomLong(MIN_DATA_VOLUME, MAX_DATA_VOLUME));
usageRecord.setQualifier("image/png");
usageRecord.setCallerIP("192.168.0.1");
//usageRecord.setQualifier("image/png");
usageRecord.setResourceProperty(TEST_PROPERTY_NAME, TEST_PROPERTY_VALUE);

View File

@ -20,18 +20,18 @@ public class JobUsageRecord extends org.gcube.accounting.datamodel.implementatio
*/
private static final long serialVersionUID = -3376423316219914682L;
@Override
protected void init(){
super.init();
private void init(){
this.resourceProperties.put(AGGREGATED, true);
}
public JobUsageRecord(){
super();
init();
}
public JobUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
super(properties);
init();
}
/**

View File

@ -21,18 +21,18 @@ public class PortletUsageRecord extends org.gcube.accounting.datamodel.implement
*/
private static final long serialVersionUID = 7445526162102677455L;
@Override
protected void init(){
super.init();
private void init(){
this.resourceProperties.put(AGGREGATED, true);
}
public PortletUsageRecord(){
super();
init();
}
public PortletUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
super(properties);
init();
}
/**

View File

@ -10,7 +10,6 @@ import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
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.ValidLong;
import org.gcube.accounting.exception.InvalidValueException;
@ -29,45 +28,46 @@ public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implement
@RequiredField @ValidLong @AggregatedField
public static final String DURATION = "duration";
@RequiredField @ValidInteger @AggregatedField
protected static final String INVOCATION_COUNT = "invocationCount";
@RequiredField @ValidLong @AggregatedField
protected static final String MAX_INVOCATION_TIME = "maxInvocationTime";
@RequiredField @ValidLong @AggregatedField
protected static final String MIN_INVOCATION_TIME = "minInvocationTime";
@Override
protected void init(){
super.init();
private void init(){
this.resourceProperties.put(AGGREGATED, true);
}
public ServiceUsageRecord(){
super();
init();
}
protected ServiceUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
super(properties);
init();
}
public ServiceUsageRecord(org.gcube.accounting.datamodel.implementations.ServiceUsageRecord record) throws InvalidValueException{
super(record.getResourceProperties());
this.setInvocationCount(1);
this.setOperationCount(1);
long duration = record.getDuration();
this.setMinInvocationTime(duration);
this.setMaxInvocationTime(duration);
Calendar creationTime = record.getCreationTime();
this.setCreationTime(creationTime);
this.setCreationTime(Calendar.getInstance());
this.setStartTime(creationTime);
this.setEndTime(creationTime);
init();
}
public int getInvocationCount() {
return (Integer) this.resourceProperties.get(INVOCATION_COUNT);
@Override
public int getOperationCount() {
return super.getOperationCount();
}
public void setInvocationCount(int invocationCount) throws InvalidValueException {
setResourceProperty(INVOCATION_COUNT, invocationCount);
@Override
public void setOperationCount(int operationCount) throws InvalidValueException {
super.setOperationCount(operationCount);
}
public long getMaxInvocationTime() {

View File

@ -4,12 +4,12 @@
package org.gcube.accounting.datamodel.aggregation;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
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.ValidLong;
import org.gcube.accounting.exception.InvalidValueException;
@ -27,36 +27,40 @@ public class StorageUsageRecord extends org.gcube.accounting.datamodel.implement
// Redefining DATA_VOLUME to Set @AggregatedField
@RequiredField @ValidLong @AggregatedField
public static final String DATA_VOLUME = "dataVolume";
/**
* KEY for : Indicate The Number of Aggregated Operation
*/
@RequiredField @ValidInteger @AggregatedField
public static final String OPERATION_COUNT = "operationCount";
@Override
protected void init(){
super.init();
private void init(){
this.resourceProperties.put(AGGREGATED, true);
}
public StorageUsageRecord(){
public StorageUsageRecord() {
super();
init();
}
public StorageUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
super(properties);
init();
}
public StorageUsageRecord(org.gcube.accounting.datamodel.implementations.StorageUsageRecord usageRecord) throws InvalidValueException{
super(usageRecord.getResourceProperties());
public StorageUsageRecord(org.gcube.accounting.datamodel.implementations.StorageUsageRecord record) throws InvalidValueException{
super(record.getResourceProperties());
this.setOperationCount(1);
Calendar creationTime = record.getCreationTime();
this.setCreationTime(Calendar.getInstance());
this.setStartTime(creationTime);
this.setEndTime(creationTime);
init();
}
@Override
public int getOperationCount() {
return (Integer) this.resourceProperties.get(OPERATION_COUNT);
return super.getOperationCount();
}
@Override
public void setOperationCount(int operationCount) throws InvalidValueException {
setResourceProperty(OPERATION_COUNT, operationCount);
super.setOperationCount(operationCount);
}
/**

View File

@ -18,9 +18,8 @@ public class ServiceUsageRecordAggregationStrategy extends AggregationStrategy<S
*/
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.CALLER_HOST);
this.aggregationField.add(ServiceUsageRecord.HOST);
this.aggregationField.add(ServiceUsageRecord.SERVICE_CLASS);
this.aggregationField.add(ServiceUsageRecord.SERVICE_NAME);
}
@ -29,7 +28,7 @@ public class ServiceUsageRecordAggregationStrategy extends AggregationStrategy<S
throws NotAggregatableRecordsExceptions {
try {
t.setInvocationCount(t.getInvocationCount() + record.getInvocationCount());
t.setOperationCount(t.getOperationCount() + record.getOperationCount());
t.setDuration((t.getDuration() + record.getDuration()) / 2);
long max = record.getMaxInvocationTime();

View File

@ -21,6 +21,7 @@ public class StorageUsageRecordAggregationStrategy extends AggregationStrategy<S
this.aggregationField.add(StorageUsageRecord.RESOURCE_OWNER);
this.aggregationField.add(StorageUsageRecord.RESOURCE_SCOPE);
this.aggregationField.add(StorageUsageRecord.RESOURCE_URI);
this.aggregationField.add(StorageUsageRecord.PROVIDER_URI);
this.aggregationField.add(StorageUsageRecord.OPERATION_TYPE);
this.aggregationField.add(StorageUsageRecord.DATA_TYPE);
}

View File

@ -10,7 +10,6 @@ import org.gcube.accounting.datamodel.BasicUsageRecord;
import org.gcube.accounting.datamodel.SingleUsageRecord;
import org.gcube.accounting.datamodel.decorators.RequiredField;
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
import org.gcube.accounting.datamodel.validations.annotations.ValidIP;
import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException;
@ -26,22 +25,16 @@ public class ServiceUsageRecord extends BasicUsageRecord implements SingleUsageR
private static final long serialVersionUID = -4214891294699473587L;
/**
* KEY for : IP address that originated the service call
*/
@ValidIP
public static final String CALLER_IP = "callerIP";
/**
* KEY for : hostname:port of the Hosting Node
* KEY for : hostname:port of the Hosting Node made the service call
*/
@RequiredField @NotEmpty
public static final String REF_HOST = "refHost";
public static final String CALLER_HOST = "callerHOST";
/**
* KEY for : Hosting Node ID
* KEY for : hostname:port of the Hosting Node receiving the service call
*/
@RequiredField @NotEmpty
public static final String REF_VM = "refVM";
public static final String HOST = "host";
/**
* KEY for : Service Class
@ -61,7 +54,8 @@ public class ServiceUsageRecord extends BasicUsageRecord implements SingleUsageR
@RequiredField @ValidLong
public static final String DURATION = "duration";
public ServiceUsageRecord(){
public ServiceUsageRecord(){
super();
}
@ -69,28 +63,20 @@ public class ServiceUsageRecord extends BasicUsageRecord implements SingleUsageR
super(properties);
}
public String getCallerIP() {
return (String) this.resourceProperties.get(CALLER_IP);
public String getCallerHost() {
return (String) this.resourceProperties.get(CALLER_HOST);
}
public void setCallerIP(String callerIP) throws InvalidValueException {
setResourceProperty(CALLER_IP, callerIP);
public void setCallerHost(String callerHost) throws InvalidValueException {
setResourceProperty(CALLER_HOST, callerHost);
}
public String getRefHost() {
return (String) this.resourceProperties.get(REF_HOST);
public String getHost() {
return (String) this.resourceProperties.get(HOST);
}
public void setRefHost(String refHost) throws InvalidValueException {
setResourceProperty(REF_HOST, refHost);
}
public String getRefVM() {
return (String) this.resourceProperties.get(REF_VM);
}
public void setRefVM(String refVM) throws InvalidValueException {
setResourceProperty(REF_VM, refVM);
public void setHost(String host) throws InvalidValueException {
setResourceProperty(HOST, host);
}
public String getServiceClass() {

View File

@ -4,27 +4,17 @@
package org.gcube.accounting.datamodel.implementations;
import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.net.URI;
import java.util.Map;
import org.gcube.accounting.datamodel.BasicUsageRecord;
import org.gcube.accounting.datamodel.SingleUsageRecord;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.decorators.FieldAction;
import org.gcube.accounting.datamodel.decorators.FieldDecorator;
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.NotEmptyIfNotNull;
import org.gcube.accounting.datamodel.validations.annotations.ValidIP;
import org.gcube.accounting.datamodel.validations.annotations.ValidDataType;
import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
import org.gcube.accounting.datamodel.validations.annotations.ValidOperationType;
import org.gcube.accounting.datamodel.validations.annotations.ValidURI;
import org.gcube.accounting.datamodel.validations.validators.ValidURIValidator;
import org.gcube.accounting.exception.InvalidValueException;
/**
@ -39,26 +29,12 @@ public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageR
private static final long serialVersionUID = 1381025822586583326L;
public enum OperationType {
CREATE, READ, UPDATE, DELETE, COPY, MOVE
CREATE, READ, UPDATE, DELETE
}
/**
* Predefined DATA TYPE
*/
public static final String STORAGE_DATA_TYPE = "STORAGE";
/**
* Predefined DATA TYPE
*/
public static final String TREE_DATA_TYPE = "TREE";
/**
* Predefined DATA TYPE
*/
public static final String GEO_DATA_TYPE = "GEO";
/**
* Predefined DATA TYPE
*/
public static final String DATABASE_DATA_TYPE = "DATABASE";
public enum DataType {
STORAGE, TREE, GEO, DATABASE, OTHER
}
/**
* KEY for : The Owner of the stored Resource
@ -72,10 +48,6 @@ public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageR
public static final String RESOURCE_SCOPE = "resourceScope";
/*
@NotEmptyIfNotNull
public static final String PROVIDER_ID = "providerId";
*/
@DeprecatedWarning @MoveToResourceURI
protected static final String OBJECT_URI = "objectURI";
@Target(ElementType.FIELD)
@ -91,13 +63,17 @@ public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageR
return value;
}
}
*/
/**
* KEY for : The URI of the Stored Resource
*/
@RequiredField @ValidURI //@NotEmptyIfNotNull
@RequiredField @ValidURI
public static final String RESOURCE_URI = "resourceURI";
@RequiredField @ValidURI
public static final String PROVIDER_URI = "providerURI";
/**
* KEY for : The operation performed over the stored resource.
* The value is a controlled dictionary by
@ -107,13 +83,10 @@ public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageR
public static final String OPERATION_TYPE = "operationType";
/**
* KEY for : type of data accessed.
* Even this field is not a controlled dictionary some type are defined
* i.e. STORAGE, TREE, GEO, DATABASE.
* Check the constant on this class:
* {@link #STORAGE_DATA_TYPE}, {@link #TREE_DATA_TYPE},
* {@link #GEO_DATA_TYPE}, {@link #DATABASE_DATA_TYPE}
* The value is a controlled dictionary by
* {@link #StorageUsageRecord.DataType}
*/
@RequiredField @NotEmpty
@RequiredField @ValidDataType
public static final String DATA_TYPE = "dataType";
/**
@ -122,20 +95,16 @@ public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageR
@RequiredField @ValidLong
public static final String DATA_VOLUME = "dataVolume";
/**
/* *
* KEY for : Qualifies the data in terms of data (e.g. MIME type for the
* Storage, domain for a database)
*/
* /
@NotEmptyIfNotNull
public static final String QUALIFIER = "qualifier";
/**
* KEY for : IP address that originated the service call
*/
@ValidIP
public static final String CALLER_IP = "callerIP";
*/
public StorageUsageRecord(){
public StorageUsageRecord() {
super();
}
@ -153,10 +122,10 @@ public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageR
/**
* Set the identity id of the storage resource owner
* @param ownerID the identity id of the storage resource owner
* @param owner the identity id of the storage resource owner
* @throws InvalidValueException
*/
public void setResourceScope(String owner) throws InvalidValueException {
public void setResourceOwner(String owner) throws InvalidValueException {
setResourceProperty(RESOURCE_OWNER, owner);
}
/**
@ -172,19 +141,17 @@ public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageR
* @param scope the scope of the storage resource
* @throws InvalidValueException
*/
public void setResourceOwner(String scope) throws InvalidValueException {
public void setResourceScope(String scope) throws InvalidValueException {
setResourceProperty(RESOURCE_SCOPE, scope);
}
/*
public String getProviderId() {
return (String) this.resourceProperties.get(PROVIDER_ID);
public URI getProviderURI() {
return (URI) this.resourceProperties.get(PROVIDER_URI);
}
public void setProviderId(String providerId) throws InvalidValueException {
setResourceProperty(PROVIDER_ID, providerId);
public void setProviderURI(URI providerURI) throws InvalidValueException {
setResourceProperty(PROVIDER_URI, providerURI);
}
*/
public URI getResourceURI() {
return (URI) this.resourceProperties.get(RESOURCE_URI);
@ -202,11 +169,11 @@ public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageR
setResourceProperty(OPERATION_TYPE, operationType);
}
public String getDataType() {
return (String) this.resourceProperties.get(DATA_TYPE);
public DataType getDataType() {
return (DataType) this.resourceProperties.get(DATA_TYPE);
}
public void setDataType(String dataType) throws InvalidValueException {
public void setDataType(DataType dataType) throws InvalidValueException {
setResourceProperty(DATA_TYPE, dataType);
}
@ -218,6 +185,7 @@ public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageR
setResourceProperty(DATA_VOLUME, dataVolume);
}
/*
public String getQualifier() {
return (String) this.resourceProperties.get(QUALIFIER);
}
@ -225,15 +193,6 @@ public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageR
public void setQualifier(String qualifier) throws InvalidValueException {
setResourceProperty(QUALIFIER, qualifier);
}
public String getCallerIP() {
return (String) this.resourceProperties.get(CALLER_IP);
}
public void setCallerIP(String callerIP) throws InvalidValueException {
setResourceProperty(CALLER_IP, callerIP);
}
*/
}

View File

@ -0,0 +1,16 @@
package org.gcube.accounting.datamodel.validations.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.gcube.accounting.datamodel.decorators.FieldDecorator;
import org.gcube.accounting.datamodel.validations.validators.ValidDataTypeValidator;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@FieldDecorator(managed=ValidDataTypeValidator.class)
public @interface ValidDataType {
}

View File

@ -0,0 +1,61 @@
package org.gcube.accounting.datamodel.validations.validators;
import java.io.Serializable;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.decorators.FieldAction;
import org.gcube.accounting.datamodel.implementations.StorageUsageRecord.DataType;
import org.gcube.accounting.exception.InvalidValueException;
public class ValidDataTypeValidator implements FieldAction {
private static final String ERROR = String.format("Not Instance of %s", DataType.class.getSimpleName());
/**
* {@inheritDoc}
*/
@SuppressWarnings("rawtypes")
@Override
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException {
if(value instanceof DataType){
return value;
}
try {
if(value instanceof String){
try{
DataType dataType = DataType.valueOf((String) value);
if(dataType !=null){
return dataType;
}
} catch(Exception e){
// Trying another way
}
try{
Integer integer = Integer.getInteger((String) value);
if(integer!=null){
value = integer;
}
} catch(Exception e){
// Trying another way
}
}
if(value instanceof Integer){
return DataType.values()[(Integer) value];
}
if(value instanceof Enum){
return DataType.values()[((Enum) value).ordinal()];
}
}catch(Exception e){
throw new InvalidValueException(ERROR, e.getCause());
}
throw new InvalidValueException(ERROR);
}
}

View File

@ -47,7 +47,7 @@ public class ServiceUsageRecordAggregationStrategyTest {
aggregated.validate();
Assert.assertTrue(aggregated.getDuration() == ((firstDuration + secondDuration)/2));
Assert.assertTrue(aggregated.getInvocationCount() == 2);
Assert.assertTrue(aggregated.getOperationCount() == 2);
if(firstDuration >= secondDuration){
Assert.assertTrue(aggregated.getMaxInvocationTime() == firstDuration);
@ -90,7 +90,7 @@ public class ServiceUsageRecordAggregationStrategyTest {
aggregated.validate();
Assert.assertTrue(aggregated.getDuration() == ((firstduration + secondDuration)/2));
Assert.assertTrue(aggregated.getInvocationCount() == 2);
Assert.assertTrue(aggregated.getOperationCount() == 2);
if(firstduration >= secondDuration){
Assert.assertTrue(aggregated.getMaxInvocationTime() == firstduration);
@ -132,7 +132,7 @@ public class ServiceUsageRecordAggregationStrategyTest {
aggregated.validate();
Assert.assertTrue(aggregated.getDuration() == ((oldDuration + newDuration)/2));
Assert.assertTrue(aggregated.getInvocationCount() == i);
Assert.assertTrue(aggregated.getOperationCount() == i);
if(minInvocationTime >= newDuration){
Assert.assertTrue(aggregated.getMinInvocationTime() == newDuration);

View File

@ -3,6 +3,7 @@
*/
package org.gcube.accounting.datamodel.implementation;
import org.gcube.accounting.datamodel.TestUsageRecord;
import org.gcube.accounting.datamodel.implementations.ServiceUsageRecord;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.accounting.persistence.Persistence;
@ -19,15 +20,7 @@ public class ServiceUsageRecordTest {
Persistence.setFallbackLocation(System.getProperty("user.home"));
Persistence persistence = Persistence.getInstance();
ServiceUsageRecord serviceUsageRecord = new ServiceUsageRecord();
serviceUsageRecord.setConsumerId("luca.frosini");
serviceUsageRecord.setScope("/gcube/devsec");
serviceUsageRecord.setServiceClass("Accounting");
serviceUsageRecord.setServiceName("Accounting-Lib");
serviceUsageRecord.setRefHost("localhost");
serviceUsageRecord.setRefVM("local");
serviceUsageRecord.setResourceProperty("ConnectionTest", "Test");
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecord();
persistence.account(serviceUsageRecord);
}