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
This commit is contained in:
Luca Frosini 2015-06-04 16:52:29 +00:00
parent d7b5dca960
commit 7b74ab0d03
9 changed files with 224 additions and 59 deletions

View File

@ -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<UsageRecord> aggregate(List<UsageRecord> 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<String, Serializable> usageRecordMap) throws Exception {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -9,6 +9,10 @@ import org.gcube.accounting.exception.InvalidValueException;
public interface UsageRecord extends Comparable<UsageRecord>{
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<UsageRecord>{
*/
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<UsageRecord>{
* @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<UsageRecord>{
*/
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<UsageRecord>{
* 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<UsageRecord>{
* @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<UsageRecord>{
* @param aggregatedId
* @throws InvalidValueException
*/
@Deprecated
public void setAggregatedId(String aggregatedId) throws InvalidValueException;
@ -218,6 +237,17 @@ public interface UsageRecord extends Comparable<UsageRecord>{
@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

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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 {
}

View File

@ -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<ValidOperationResult>{
public Class<ValidOperationResult> 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());
}
}

View File

@ -110,7 +110,7 @@ public class CouchDBPersistence extends Persistence {
ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String, Serializable> result = mapper.convertValue(jsonNode, Map.class);
UsageRecord usageRecord = new RawUsageRecord(result);
UsageRecord usageRecord = RawUsageRecord.getUsageRecord(result);
return usageRecord;
}

View File

@ -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");

View File

@ -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));