From 21f740c0f7b8210d2adb6989a91aa877a76d8fc8 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 26 Jun 2015 13:50:39 +0000 Subject: [PATCH] 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 --- .../datamodel/BasicUsageRecord.java | 126 ++++++++++-------- .../accounting/datamodel/TestUsageRecord.java | 27 ++-- .../datamodel/aggregation/JobUsageRecord.java | 6 +- .../aggregation/PortletUsageRecord.java | 6 +- .../aggregation/ServiceUsageRecord.java | 26 ++-- .../aggregation/StorageUsageRecord.java | 36 ++--- ...ServiceUsageRecordAggregationStrategy.java | 7 +- ...StorageUsageRecordAggregationStrategy.java | 1 + .../implementations/ServiceUsageRecord.java | 42 ++---- .../implementations/StorageUsageRecord.java | 101 +++++--------- .../annotations/ValidDataType.java | 16 +++ .../validators/ValidDataTypeValidator.java | 61 +++++++++ ...iceUsageRecordAggregationStrategyTest.java | 6 +- .../ServiceUsageRecordTest.java | 11 +- 14 files changed, 257 insertions(+), 215 deletions(-) create mode 100644 src/main/java/org/gcube/accounting/datamodel/validations/annotations/ValidDataType.java create mode 100644 src/main/java/org/gcube/accounting/datamodel/validations/validators/ValidDataTypeValidator.java diff --git a/src/main/java/org/gcube/accounting/datamodel/BasicUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/BasicUsageRecord.java index ae48d70..516d77e 100644 --- a/src/main/java/org/gcube/accounting/datamodel/BasicUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/BasicUsageRecord.java @@ -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 aggregatedFields; @@ -159,7 +165,7 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable { /** * Initialize variable */ - protected void init() { + private void init() { this.validation = new HashMap>(); this.requiredFields = new HashSet(); this.aggregatedFields = new HashSet(); @@ -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"); diff --git a/src/main/java/org/gcube/accounting/datamodel/TestUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/TestUsageRecord.java index 8e66ccf..e6072ff 100644 --- a/src/main/java/org/gcube/accounting/datamodel/TestUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/TestUsageRecord.java @@ -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); diff --git a/src/main/java/org/gcube/accounting/datamodel/aggregation/JobUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/aggregation/JobUsageRecord.java index 1da9688..58880bb 100644 --- a/src/main/java/org/gcube/accounting/datamodel/aggregation/JobUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/aggregation/JobUsageRecord.java @@ -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 properties) throws InvalidValueException{ super(properties); + init(); } /** diff --git a/src/main/java/org/gcube/accounting/datamodel/aggregation/PortletUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/aggregation/PortletUsageRecord.java index 7eb48e6..c7c281e 100644 --- a/src/main/java/org/gcube/accounting/datamodel/aggregation/PortletUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/aggregation/PortletUsageRecord.java @@ -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 properties) throws InvalidValueException{ super(properties); + init(); } /** diff --git a/src/main/java/org/gcube/accounting/datamodel/aggregation/ServiceUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/aggregation/ServiceUsageRecord.java index c77ce50..8a2dcd4 100644 --- a/src/main/java/org/gcube/accounting/datamodel/aggregation/ServiceUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/aggregation/ServiceUsageRecord.java @@ -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 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() { diff --git a/src/main/java/org/gcube/accounting/datamodel/aggregation/StorageUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/aggregation/StorageUsageRecord.java index d1b14e2..4f87963 100644 --- a/src/main/java/org/gcube/accounting/datamodel/aggregation/StorageUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/aggregation/StorageUsageRecord.java @@ -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 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); } /** diff --git a/src/main/java/org/gcube/accounting/datamodel/aggregation/aggregationstrategy/ServiceUsageRecordAggregationStrategy.java b/src/main/java/org/gcube/accounting/datamodel/aggregation/aggregationstrategy/ServiceUsageRecordAggregationStrategy.java index f193006..61c0424 100644 --- a/src/main/java/org/gcube/accounting/datamodel/aggregation/aggregationstrategy/ServiceUsageRecordAggregationStrategy.java +++ b/src/main/java/org/gcube/accounting/datamodel/aggregation/aggregationstrategy/ServiceUsageRecordAggregationStrategy.java @@ -18,9 +18,8 @@ public class ServiceUsageRecordAggregationStrategy extends AggregationStrategy= 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); diff --git a/src/test/java/org/gcube/accounting/datamodel/implementation/ServiceUsageRecordTest.java b/src/test/java/org/gcube/accounting/datamodel/implementation/ServiceUsageRecordTest.java index 84ceca6..91544e2 100644 --- a/src/test/java/org/gcube/accounting/datamodel/implementation/ServiceUsageRecordTest.java +++ b/src/test/java/org/gcube/accounting/datamodel/implementation/ServiceUsageRecordTest.java @@ -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); }