From 7b74ab0d03c6e5179798cca11caaf0b8cad3ea2b Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 4 Jun 2015 16:52:29 +0000 Subject: [PATCH] refs #200: Create accouting-lib library https://support.d4science.org/issues/200 Fixing data model git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115228 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../accounting/datamodel/RawUsageRecord.java | 109 ++++++++++++------ .../accounting/datamodel/UsageRecord.java | 50 ++++++-- .../StorageStatusUsageRecord.java | 2 +- .../StorageUsageUsageRecord.java | 53 +++++++++ .../validators/ValidOperationResult.java | 15 +++ .../ValidOperationResultValidator.java | 31 +++++ .../persistence/CouchDBPersistence.java | 2 +- .../accounting/persistence/Persistence.java | 15 ++- .../datamodel/RawUsageRecordTest.java | 6 +- 9 files changed, 224 insertions(+), 59 deletions(-) rename src/main/java/org/gcube/accounting/datamodel/{implementations => aggregated}/StorageStatusUsageRecord.java (97%) create mode 100644 src/main/java/org/gcube/accounting/datamodel/validators/ValidOperationResult.java create mode 100644 src/main/java/org/gcube/accounting/datamodel/validators/ValidOperationResultValidator.java diff --git a/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java index 024a86f..a8997d7 100644 --- a/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/RawUsageRecord.java @@ -19,6 +19,7 @@ import java.util.UUID; import org.gcube.accounting.datamodel.validators.NotEmptyIfNotNull; import org.gcube.accounting.datamodel.validators.ValidLong; +import org.gcube.accounting.datamodel.validators.ValidOperationResult; import org.gcube.accounting.exception.InvalidValueException; import org.gcube.common.validator.annotations.FieldValidator; import org.gcube.common.validator.annotations.NotEmpty; @@ -30,7 +31,7 @@ import org.slf4j.LoggerFactory; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -public class RawUsageRecord implements UsageRecord, Serializable { +public abstract class RawUsageRecord implements UsageRecord, Serializable { private static Logger logger = LoggerFactory.getLogger(RawUsageRecord.class); @@ -43,9 +44,9 @@ public class RawUsageRecord implements UsageRecord, Serializable { @ValidLong public static final String CREATION_TIME = "creationTime"; @ValidLong - public static final String START_TIME = "startTime"; + protected static final String START_TIME = "startTime"; @ValidLong - public static final String END_TIME = "endTime"; + protected static final String END_TIME = "endTime"; @NotEmpty public static final String RESOURCE_TYPE = "resourceType"; @NotEmpty @@ -53,7 +54,9 @@ public class RawUsageRecord implements UsageRecord, Serializable { @NotEmpty public static final String RESOURCE_OWNER = "resourceOwner"; @NotEmptyIfNotNull - public static final String AGGREGATED_ID = "aggregatedId"; + protected static final String AGGREGATED_ID = "aggregatedId"; + @ValidOperationResult + public static final String OPERATION_RESULT = "operationResult"; /** * Generated Serial Version UID @@ -182,6 +185,13 @@ public class RawUsageRecord implements UsageRecord, Serializable { setResourceSpecificProperty(CREATION_TIME, creationTime.getTimeInMillis()); } + @Override + @Deprecated + public Date getCreateTime() { + long millis = (Long) this.resourceSpecificProperties.get(CREATION_TIME); + return timestampStringToCalendar(millis).getTime(); + } + /** * {@inheritDoc} */ @@ -203,17 +213,18 @@ public class RawUsageRecord implements UsageRecord, Serializable { } /** - * {@inheritDoc} + * Set the left end of the time interval covered by this {#UsageRecord} + * @param startTime Start Time + * @throws InvalidValueException */ - @Override - public void setStartTime(Calendar startTime) throws InvalidValueException { + protected void setStartTime(Calendar startTime) throws InvalidValueException { setResourceSpecificProperty(START_TIME, startTime.getTimeInMillis()); } - /** + /* * * {@inheritDoc} - */ + * / @Override @Deprecated public void setStartTime(Date startTime) throws InvalidValueException { @@ -221,6 +232,7 @@ public class RawUsageRecord implements UsageRecord, Serializable { calendar.setTime(startTime); setStartTime(calendar); } + */ /** * {@inheritDoc} @@ -232,16 +244,17 @@ public class RawUsageRecord implements UsageRecord, Serializable { } /** - * {@inheritDoc} + * Set the right end of the time interval covered by this {#UsageRecord} + * @param endTime End Time + * @throws InvalidValueException */ - @Override - public void setEndTime(Calendar endTime) throws InvalidValueException { + protected void setEndTime(Calendar endTime) throws InvalidValueException { setResourceSpecificProperty(END_TIME, endTime.getTimeInMillis()); } - /** + /* * * {@inheritDoc} - */ + * / @Override @Deprecated public void setEndTime(Date endTime) throws InvalidValueException { @@ -249,14 +262,17 @@ public class RawUsageRecord implements UsageRecord, Serializable { calendar.setTime(endTime); setEndTime(calendar); } + */ /** * {@inheritDoc} */ @Override + @Deprecated public String getUsageRecordType() { - return (String) this.resourceSpecificProperties.get(RESOURCE_TYPE); + //return (String) this.resourceSpecificProperties.get(RESOURCE_TYPE); + return this.getClass().getSimpleName(); } /** @@ -294,6 +310,7 @@ public class RawUsageRecord implements UsageRecord, Serializable { * {@inheritDoc} */ @Override + @Deprecated public String getResourceOwner() { return (String) this.resourceSpecificProperties.get(RESOURCE_OWNER); } @@ -302,6 +319,7 @@ public class RawUsageRecord implements UsageRecord, Serializable { * {@inheritDoc} */ @Override + @Deprecated public void setResourceOwner(String owner) throws InvalidValueException { setResourceSpecificProperty(RESOURCE_OWNER, owner); } @@ -310,6 +328,7 @@ public class RawUsageRecord implements UsageRecord, Serializable { * {@inheritDoc} */ @Override + @Deprecated public String getAggregatedId() { return (String) this.resourceSpecificProperties.get(AGGREGATED_ID); } @@ -317,7 +336,8 @@ public class RawUsageRecord implements UsageRecord, Serializable { /** * {@inheritDoc} */ - @Override + @Override@ + Deprecated public void setAggregatedId(String aggregatedId) throws InvalidValueException { setResourceSpecificProperty(AGGREGATED_ID, aggregatedId); } @@ -387,26 +407,6 @@ public class RawUsageRecord implements UsageRecord, Serializable { @Override public String toString(){ - /* - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("{"); - boolean first = true; - for(String key : this.resourceSpecificProperties.keySet()){ - if(!first){ - stringBuilder.append(", \""); - }else { - stringBuilder.append("\""); - first = false; - } - stringBuilder.append(key); - stringBuilder.append("\":\""); - stringBuilder.append(this.resourceSpecificProperties.get(key)); - stringBuilder.append("\""); - - } - stringBuilder.append("}"); - return stringBuilder.toString(); - */ return resourceSpecificProperties.toString(); } @@ -428,6 +428,23 @@ public class RawUsageRecord implements UsageRecord, Serializable { @Deprecated public void setFullyQualifiedConsumerId(String fqcid) { } + /** + * {@inheritDoc} + */ + @Override + public OperationResult getOperationResult(){ + return OperationResult.valueOf((String) this.resourceSpecificProperties.get(OPERATION_RESULT)); + } + + /** + * {@inheritDoc} + */ + @Override + public void setOperationResult(OperationResult operationResult) { + + } + + /** * Compare this UsageRecord instance with the one provided as argument * @param usageRecord the Usage Record to compare @@ -451,5 +468,25 @@ public class RawUsageRecord implements UsageRecord, Serializable { } return 1; } + + public static List aggregate(List records){ + // TODO implements + throw new UnsupportedOperationException(); + } + + + /** + * This method use the resourceType value contained in the Map to instance + * the right UsageRecord class and return it. If the type implementation + * does not exist or the validation of one or more field validation fails + * an exception is thrown + * @param usageRecordMap + * @return the instance of the UsageRecord class. + * @throws Exception if fails + */ + public static UsageRecord getUsageRecord(Map usageRecordMap) throws Exception { + // TODO Auto-generated method stub + return null; + } } diff --git a/src/main/java/org/gcube/accounting/datamodel/UsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/UsageRecord.java index 22e65e1..42219b6 100644 --- a/src/main/java/org/gcube/accounting/datamodel/UsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/UsageRecord.java @@ -9,6 +9,10 @@ import org.gcube.accounting.exception.InvalidValueException; public interface UsageRecord extends Comparable{ + public enum OperationResult { + SUCCESS, FAILED + } + /** * Return the unique id for this {#UsageRecord} * @return {#UsageRecord} Unique ID @@ -62,6 +66,13 @@ public interface UsageRecord extends Comparable{ */ public void setCreationTime(Calendar creationTime) throws InvalidValueException; + /** + * Return the creation time for this {#UsageRecord} + * @return the creation time for this {#UsageRecord} + */ + @Deprecated + public Date getCreateTime(); + /** * Use {{@link #setCreationTime(Calendar)}} instead * @param createTime @@ -75,21 +86,22 @@ public interface UsageRecord extends Comparable{ * @return Start Time */ public Calendar getStartTime(); - - /** + + /* * * Set the left end of the time interval covered by this {#UsageRecord} * @param startTime Start Time * @throws InvalidValueException - */ + * / public void setStartTime(Calendar startTime) throws InvalidValueException; - /** + /* * * Use {{@link #setStartTime(Calendar)}} instead * @param createTime * @throws InvalidValueException - */ + * / @Deprecated public void setStartTime(Date startTime) throws InvalidValueException; + */ /** * Return the right end of the time interval covered by this {#UsageRecord} @@ -97,26 +109,29 @@ public interface UsageRecord extends Comparable{ */ public Calendar getEndTime(); - /** + /* * * Set the right end of the time interval covered by this {#UsageRecord} * @param endTime End Time * @throws InvalidValueException - */ + * / public void setEndTime(Calendar endTime) throws InvalidValueException; - /** + /* * * Use {{@link #setEndTime(Calendar)}} instead * @param createTime * @throws InvalidValueException - */ + * / @Deprecated public void setEndTime(Date endTime) throws InvalidValueException; + */ /** * Return the type of the {#UsageRecord}. - * It is a alternative way of obj.getClass().getSimpleName() + * It is a alternative way of obj.getClass().getSimpleName() use this + * instead * @return Resource Type */ + @Deprecated public String getUsageRecordType(); /** @@ -153,6 +168,7 @@ public interface UsageRecord extends Comparable{ * Return the identity id of the accounting owner * @return The identity id of the accounting owner */ + @Deprecated public String getResourceOwner(); /** @@ -160,12 +176,14 @@ public interface UsageRecord extends Comparable{ * @param ownerID The identity id of the accounting owner * @throws InvalidValueException */ + @Deprecated public void setResourceOwner(String ownerID) throws InvalidValueException; /** * Return the id of the usage record aggregating this * @return Aggregated Id */ + @Deprecated public String getAggregatedId(); /** @@ -173,6 +191,7 @@ public interface UsageRecord extends Comparable{ * @param aggregatedId * @throws InvalidValueException */ + @Deprecated public void setAggregatedId(String aggregatedId) throws InvalidValueException; @@ -218,6 +237,17 @@ public interface UsageRecord extends Comparable{ @Deprecated public void setFullyQualifiedConsumerId(String fqcid); + /** + * @return the Operation Result related to the accounted Usage Record + */ + public OperationResult getOperationResult(); + + /** + * Set the Operation Result related to the accounted Usage Record + * @param operationResult the Operation Result to set + */ + public void setOperationResult(OperationResult operationResult); + /** * Validate the Resource Record diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/StorageStatusUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/aggregated/StorageStatusUsageRecord.java similarity index 97% rename from src/main/java/org/gcube/accounting/datamodel/implementations/StorageStatusUsageRecord.java rename to src/main/java/org/gcube/accounting/datamodel/aggregated/StorageStatusUsageRecord.java index ce95d51..723b7cb 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/StorageStatusUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/aggregated/StorageStatusUsageRecord.java @@ -1,7 +1,7 @@ /** * */ -package org.gcube.accounting.datamodel.implementations; +package org.gcube.accounting.datamodel.aggregated; import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.validators.ValidInteger; diff --git a/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageUsageRecord.java index 0007a6d..0cba896 100644 --- a/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/implementations/StorageUsageUsageRecord.java @@ -3,8 +3,11 @@ */ package org.gcube.accounting.datamodel.implementations; +import org.gcube.accounting.datamodel.aggregated.StorageStatusUsageRecord; import org.gcube.accounting.datamodel.validators.NotEmptyIfNotNull; import org.gcube.accounting.datamodel.validators.ValidIP; +import org.gcube.accounting.datamodel.validators.ValidInteger; +import org.gcube.accounting.datamodel.validators.ValidLong; import org.gcube.accounting.exception.InvalidValueException; import org.gcube.common.validator.annotations.NotEmpty; @@ -25,6 +28,16 @@ public class StorageUsageUsageRecord extends StorageStatusUsageRecord { public static final String OPERATION_TYPE = "operationType"; @ValidIP public static final String CALLER_IP = "callerIP"; + @NotEmpty + public static final String PROVIDER_ID = "providerId"; + @NotEmpty + public static final String QUALIFIER = "qualifier"; + @NotEmpty + public static final String DATA_TYPE = "dataType"; + @ValidLong + public static final String DATA_VOLUME = "dataVolume"; + @ValidInteger + public static final String DATA_COUNT = "dataCount"; public StorageUsageUsageRecord(){ @@ -54,5 +67,45 @@ public class StorageUsageUsageRecord extends StorageStatusUsageRecord { public void setCallerIP(String callerIP) throws InvalidValueException { setResourceSpecificProperty(CALLER_IP, callerIP); } + + public String getProviderId() { + return (String) this.resourceSpecificProperties.get(PROVIDER_ID); + } + + public void setProviderId(String providerId) throws InvalidValueException { + setResourceSpecificProperty(PROVIDER_ID, providerId); + } + + public String getQualifier() { + return (String) this.resourceSpecificProperties.get(QUALIFIER); + } + + public void setQualifier(String qualifier) throws InvalidValueException { + setResourceSpecificProperty(QUALIFIER, qualifier); + } + + public String getDataType() { + return (String) this.resourceSpecificProperties.get(DATA_TYPE); + } + + public void setDataType(String dataType) throws InvalidValueException { + setResourceSpecificProperty(DATA_TYPE, dataType); + } + + public long getDataVolume() { + return (Long) this.resourceSpecificProperties.get(DATA_VOLUME); + } + + public void setDataVolume(long dataVolume) throws InvalidValueException { + setResourceSpecificProperty(DATA_VOLUME, dataVolume); + } + + public int getDataCount() { + return (Integer) this.resourceSpecificProperties.get(DATA_COUNT); + } + + public void setDataCount(int dataCount) throws InvalidValueException { + setResourceSpecificProperty(DATA_COUNT, dataCount); + } } diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/ValidOperationResult.java b/src/main/java/org/gcube/accounting/datamodel/validators/ValidOperationResult.java new file mode 100644 index 0000000..0d43f23 --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/validators/ValidOperationResult.java @@ -0,0 +1,15 @@ +package org.gcube.accounting.datamodel.validators; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.gcube.common.validator.annotations.ValidityChecker; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@ValidityChecker(managed=ValidOperationResultValidator.class) +public @interface ValidOperationResult { + +} diff --git a/src/main/java/org/gcube/accounting/datamodel/validators/ValidOperationResultValidator.java b/src/main/java/org/gcube/accounting/datamodel/validators/ValidOperationResultValidator.java new file mode 100644 index 0000000..0e6db5d --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/validators/ValidOperationResultValidator.java @@ -0,0 +1,31 @@ +package org.gcube.accounting.datamodel.validators; + +import java.io.Serializable; + +import org.gcube.accounting.datamodel.UsageRecord.OperationResult; +import org.gcube.common.validator.annotations.FieldValidator; + +public class ValidOperationResultValidator implements FieldValidator{ + + public Class annotation() { + return ValidOperationResult.class; + } + + public boolean isValid(Object toValidate) { + if(toValidate instanceof OperationResult){ + return true; + } + if(toValidate instanceof String || toValidate instanceof Serializable){ + try { + OperationResult.valueOf((String) toValidate); + return true; + }catch(Exception e){} + } + return false; + } + + public String getErrorSuffix() { + return String.format("not instace of %s", OperationResult.class.getSimpleName()); + } + +} diff --git a/src/main/java/org/gcube/accounting/persistence/CouchDBPersistence.java b/src/main/java/org/gcube/accounting/persistence/CouchDBPersistence.java index cef4b77..391b2f5 100644 --- a/src/main/java/org/gcube/accounting/persistence/CouchDBPersistence.java +++ b/src/main/java/org/gcube/accounting/persistence/CouchDBPersistence.java @@ -110,7 +110,7 @@ public class CouchDBPersistence extends Persistence { ObjectMapper mapper = new ObjectMapper(); @SuppressWarnings("unchecked") Map result = mapper.convertValue(jsonNode, Map.class); - UsageRecord usageRecord = new RawUsageRecord(result); + UsageRecord usageRecord = RawUsageRecord.getUsageRecord(result); return usageRecord; } diff --git a/src/main/java/org/gcube/accounting/persistence/Persistence.java b/src/main/java/org/gcube/accounting/persistence/Persistence.java index 2438fb8..a3786b7 100644 --- a/src/main/java/org/gcube/accounting/persistence/Persistence.java +++ b/src/main/java/org/gcube/accounting/persistence/Persistence.java @@ -9,8 +9,8 @@ import java.util.GregorianCalendar; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.UsageRecord; +import org.gcube.accounting.datamodel.implementations.ServiceUsageRecord; import org.gcube.accounting.exception.InvalidValueException; import org.gcube.smartgears.context.container.ContainerContext; import org.gcube.smartgears.provider.ProviderFactory; @@ -71,22 +71,21 @@ public abstract class Persistence { } } - public static RawUsageRecord createTestRawUsageRecord(){ - RawUsageRecord usageRecord = new RawUsageRecord(); + public static UsageRecord createTestRawUsageRecord(){ + UsageRecord usageRecord = new ServiceUsageRecord(); try { usageRecord.setCreatorId("accounting"); usageRecord.setConsumerId("accounting"); - usageRecord.setResourceOwner("accounting.lib"); usageRecord.setResourceScope("/gcube/devsec"); Calendar creatingTime = new GregorianCalendar(); - Calendar startTime = new GregorianCalendar(); - Calendar endTime = new GregorianCalendar(); + //Calendar startTime = new GregorianCalendar(); + //Calendar endTime = new GregorianCalendar(); usageRecord.setCreationTime(creatingTime); - usageRecord.setStartTime(startTime); - usageRecord.setEndTime(endTime); + //usageRecord.setStartTime(startTime); + //usageRecord.setEndTime(endTime); usageRecord.setResourceSpecificProperty("ConnectionTest", "Test"); diff --git a/src/test/java/org/gcube/accounting/datamodel/RawUsageRecordTest.java b/src/test/java/org/gcube/accounting/datamodel/RawUsageRecordTest.java index 5829a2b..6909c28 100644 --- a/src/test/java/org/gcube/accounting/datamodel/RawUsageRecordTest.java +++ b/src/test/java/org/gcube/accounting/datamodel/RawUsageRecordTest.java @@ -24,7 +24,7 @@ public class RawUsageRecordTest { @Test public void testCompareToEqualsObject() throws Exception { UsageRecord usageRecord = Persistence.createTestRawUsageRecord(); - UsageRecord ur = new RawUsageRecord(usageRecord.getResourceSpecificProperties()); + UsageRecord ur = RawUsageRecord.getUsageRecord(usageRecord.getResourceSpecificProperties()); Assert.assertEquals(0, usageRecord.compareTo(ur)); Assert.assertEquals(0, ur.compareTo(usageRecord)); } @@ -32,7 +32,7 @@ public class RawUsageRecordTest { @Test public void testCompareToComparedAddedProperty() throws Exception { UsageRecord usageRecord = Persistence.createTestRawUsageRecord(); - UsageRecord ur = new RawUsageRecord(usageRecord.getResourceSpecificProperties()); + UsageRecord ur = RawUsageRecord.getUsageRecord(usageRecord.getResourceSpecificProperties()); for(int i=1; i<31; i++){ ur.setResourceSpecificProperty(Integer.toString(i), i); Assert.assertEquals(-i, usageRecord.compareTo(ur)); @@ -43,7 +43,7 @@ public class RawUsageRecordTest { @Test public void testCompareToDifferentForAddedProperties() throws Exception { UsageRecord usageRecord = Persistence.createTestRawUsageRecord(); - UsageRecord ur = new RawUsageRecord(usageRecord.getResourceSpecificProperties()); + UsageRecord ur = RawUsageRecord.getUsageRecord(usageRecord.getResourceSpecificProperties()); usageRecord.setResourceSpecificProperty(Integer.toString(1), 2); ur.setResourceSpecificProperty(Integer.toString(2), 2); Assert.assertEquals(1, usageRecord.compareTo(ur));