refs #1746: Separate Accounting Model and generalize solution

https://support.d4science.org/issues/1746

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib-couchdb@121988 82a268e6-3cf1-43bd-a215-b396298e98cf
master
Luca Frosini 8 years ago
commit f5a93f0850

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>document-store-lib-couchdb</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

@ -0,0 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8

@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

@ -0,0 +1 @@
Used as library in the gCube Framework

@ -0,0 +1,8 @@
gCube System - License
------------------------------------------------------------
The gCube/gCore software is licensed as Free Open Source software conveying to
the EUPL (http://ec.europa.eu/idabc/eupl).
The software and documentation is provided by its authors/distributors "as is"
and no expressed or implied warranty is given for its use, quality or fitness
for a particular case.

@ -0,0 +1,2 @@
Luca Frosini (luca.frosini@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".

@ -0,0 +1,28 @@
The gCube System - Accounting Common Library
------------------------------------------------------------
This work has been partially supported by the following European projects: DILIGENT (FP6-2003-IST-2),
D4Science (FP7-INFRA-2007-1.2.2), D4Science-II (FP7-INFRA-2008-1.2.2), iMarine (FP7-INFRASTRUCTURES-2011-2),
and EUBrazilOpenBio (FP7-ICT-2011-EU-Brazil), Parthenos (H2020-INFRADEV-1-2014-1), BlueBridge (H2020-EINFRA-2015-1).
Authors
-------
* Luca Frosini (luca.frosini@isti.cnr.it), CNR Pisa, Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".
Version and Release Date
------------------------
v 1.0, 30/09/2015
Description
-----------
${description}
Licensing
---------
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.

@ -0,0 +1,5 @@
<ReleaseNotes>
<Changeset component="org.gcube.accounting.accounting-lib-persistence-couchdb.1-0-0" date="2015-09-30">
<Change>First Release</Change>
</Changeset>
</ReleaseNotes>

@ -0,0 +1,42 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>servicearchive</id>
<formats>
<format>tar.gz</format>
</formats>
<baseDirectory>/</baseDirectory>
<fileSets>
<fileSet>
<directory>${distroDirectory}</directory>
<outputDirectory>/</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>README</include>
<include>LICENSE</include>
<include>INSTALL</include>
<include>MAINTAINERS</include>
<include>changelog.xml</include>
</includes>
<fileMode>755</fileMode>
<filtered>true</filtered>
</fileSet>
</fileSets>
<files>
<file>
<source>${distroDirectory}/profile.xml</source>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
</file>
<file>
<source>target/${build.finalName}.jar</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
<file>
<source>${distroDirectory}/svnpath.txt</source>
<outputDirectory>/${artifactId}</outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource>
<ID></ID>
<Type>Service</Type>
<Profile>
<Class>Accounting</Class>
<Name>${artifactId}</Name>
<Description>${description}</Description>
<Version>1.0.0</Version>
<Packages>
<Software>
<Description>${description}</Description>
<Name>${artifactId}</Name>
<Version>${version}</Version>
<MavenCoordinates>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</MavenCoordinates>
<Type>library</Type>
<Files>
<File>${build.finalName}.jar</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

@ -0,0 +1 @@
${scm.url}

@ -0,0 +1,98 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gcube.tools</groupId>
<artifactId>maven-parent</artifactId>
<version>1.0.0</version>
</parent>
<groupId>org.gcube.data.publishing</groupId>
<artifactId>document-store-lib-couchdb</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Document Store CouchDB</name>
<description>Document Store CouchDB Implementation</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<distroDirectory>${project.basedir}/distro</distroDirectory>
</properties>
<scm>
<connection>scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/${project.artifactId}</connection>
<developerConnection>scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/${project.artifactId}</developerConnection>
<url>https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/${project.artifactId}</url>
</scm>
<dependencies>
<dependency>
<groupId>org.gcube.data.publishing</groupId>
<artifactId>document-store-lib</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>couchdb-connector</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
<!-- Test Dependency -->
<dependency>
<groupId>org.gcube.accounting</groupId>
<artifactId>accounting-lib</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-encryption</artifactId>
<version>[1.0.2-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId>
<version>[1.0.0-SNAPSHOT,)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,82 @@
/**
*
*/
package org.gcube.documentstore.persistence;
import java.io.Serializable;
import java.util.Map;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.gcube.common.couchdb.connector.HttpCouchClient;
import org.gcube.documentstore.persistence.PersistenceBackend;
import org.gcube.documentstore.persistence.PersistenceBackendConfiguration;
import org.gcube.documentstore.records.Record;
import org.gcube.documentstore.records.RecordUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/
public class PersistenceCouchDB extends PersistenceBackend {
private static final Logger logger = LoggerFactory.getLogger(PersistenceCouchDB.class);
protected HttpCouchClient httpCouchClient;
public static final String URL_PROPERTY_KEY = "URL";
public static final String USERNAME_PROPERTY_KEY = "username";
public static final String PASSWORD_PROPERTY_KEY = "password";
public static final String DB_NAME = "dbName";
public PersistenceCouchDB() throws Exception {
super();
}
@Override
public void close() throws Exception {}
/**
* {@inheritDoc}
*/
@Override
protected void prepareConnection(PersistenceBackendConfiguration configuration) throws Exception {
logger.debug("Preparing Connection for {}", this.getClass().getSimpleName());
String url = configuration.getProperty(URL_PROPERTY_KEY);
String username = configuration.getProperty(USERNAME_PROPERTY_KEY);
String password = configuration.getProperty(PASSWORD_PROPERTY_KEY);
String dbName = configuration.getProperty(DB_NAME);
httpCouchClient = new HttpCouchClient(url, dbName, username, password);
}
protected void createItem(JsonNode node, String id) throws Exception {
httpCouchClient.put(node.toString(), id);
}
/**
* {@inheritDoc}
*/
@Override
protected void reallyAccount(Record record) throws Exception {
JsonNode node = usageRecordToJsonNode(record);
createItem(node, record.getId());
}
public static JsonNode usageRecordToJsonNode(Record record) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.valueToTree(record.getResourceProperties());
return node;
}
protected static Record jsonNodeToUsageRecord(JsonNode jsonNode) throws Exception {
ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String, Comparable<? extends Serializable>> result = mapper.convertValue(jsonNode, Map.class);
Record record =RecordUtility.getRecord(result);
return record;
}
}

@ -0,0 +1,124 @@
/**
*
*/
package org.gcube.accounting.persistence;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resources;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.documentstore.persistence.PersistenceBackend;
import org.gcube.documentstore.persistence.PersistenceBackendFactory;
import org.gcube.documentstore.persistence.PersistenceCouchDB;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.gcube.informationsystem.publisher.ScopedPublisher;
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class PersistenceCouchDBTest {
private static final Logger logger = LoggerFactory.getLogger(PersistenceCouchDBTest.class);
public static PersistenceBackend getPersistence(){
ScopeProvider.instance.set("/gcube/devNext");
PersistenceBackendFactory.setFallbackLocation(null);
return PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
}
@Test
public void persistenceIsCouchDB() {
PersistenceBackend persistence = getPersistence();
Assert.assertTrue(persistence instanceof PersistenceCouchDB);
}
private static void publishScopedResource(Resource resource, List<String> scopes) throws Exception {
StringWriter stringWriter = new StringWriter();
Resources.marshal(resource, stringWriter);
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
try {
logger.debug("Trying to publish to {}:\n{}", scopes, stringWriter);
scopedPublisher.create(resource, scopes);
} catch (Exception e) {
logger.error("The resource was not published", e);
throw e;
}
}
private static void unPublishScopedResource(Resource resource, List<String> scopes) throws RegistryNotFoundException, Exception {
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
String id = resource.id();
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, scopes);
scopedPublisher.remove(resource, scopes);
logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id);
}
public void testScopeRecheck() throws Exception {
final String vreScopeToUse = "/gcube/devsec/LucioVRE";
final String parentVREScopeToUse = "/gcube/devsec";
ScopeProvider.instance.set(vreScopeToUse);
ServiceEndpoint serviceEndpoint = null;
try {
AccountingPersistenceConfiguration persitenceConfiguration =
new AccountingPersistenceConfiguration(PersistenceCouchDB.class);
serviceEndpoint = persitenceConfiguration.getServiceEndpoint(
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY,
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME,
PersistenceCouchDB.class);
List<String> scopes = new ArrayList<String>();
scopes.add(ScopeProvider.instance.get());
unPublishScopedResource(serviceEndpoint, scopes);
} catch(IndexOutOfBoundsException e){
ScopeProvider.instance.set(parentVREScopeToUse);
AccountingPersistenceConfiguration persitenceConfiguration =
new AccountingPersistenceConfiguration(PersistenceCouchDB.class);
serviceEndpoint = persitenceConfiguration.getServiceEndpoint(
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY,
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME,
PersistenceCouchDB.class);
ScopeProvider.instance.set(vreScopeToUse);
}
long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime;
while(endTime <= (startTime + 10*1000)){ // 10 sec
endTime = Calendar.getInstance().getTimeInMillis();
}
logger.debug("Going to check First Time");
PersistenceBackend first = PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
logger.debug("First {} : {}", PersistenceBackend.class.getSimpleName(), first);
List<String> scopes = new ArrayList<String>();
scopes.add(ScopeProvider.instance.get());
publishScopedResource(serviceEndpoint, scopes);
startTime = Calendar.getInstance().getTimeInMillis();
endTime = startTime;
while(endTime <= (startTime + (PersistenceBackendFactory.FALLBACK_RETRY_TIME + 100))){
endTime = Calendar.getInstance().getTimeInMillis();
}
logger.debug("Going to check Second Time");
PersistenceBackend second = PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
logger.debug("Second {} : {}", PersistenceBackend.class.getSimpleName(), second);
Assert.assertNotEquals(first, second);
}
}

@ -0,0 +1,61 @@
/**
*
*/
package org.gcube.documentstore.persistence;
import java.net.URL;
import org.codehaus.jackson.JsonNode;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.documentstore.records.Record;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class PersistenceCouchBaseTest {
private static final Logger logger = LoggerFactory.getLogger(PersistenceCouchBaseTest.class);
@Test
public void testJsonNodeUsageRecordConversions() throws Exception {
Record record = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
logger.debug("UsageRecord : {}", record.toString());
JsonNode node = PersistenceCouchDB.usageRecordToJsonNode(record);
logger.debug("Node : {}", node.toString());
Record r = PersistenceCouchDB.jsonNodeToUsageRecord(node);
Assert.assertEquals(0, record.compareTo(r));
Assert.assertEquals(0, r.compareTo(record));
}
public void testProxyWithTestConfiguration() throws Exception{
// Production-Preproduction Nodes
//URL url = new URL("http://accounting-d4s.d4science.org");
//URL url = new URL("http://couchdb02-d4s.d4science.org:5984");
//URL url = new URL("http://couchdb01-d4s.d4science.org:5984");
URL url = new URL("http://accounting-d-d4s.d4science.org/_utils/");
//URL url = new URL("http://couchdb02-d-d4s.d4science.org:5984");
//URL url = new URL("http://couchdb01-d-d4s.d4science.org:5984");
PersistenceBackendConfiguration persitenceConfiguration = PersistenceBackendConfiguration.getUnconfiguredInstance();
persitenceConfiguration.addProperty(PersistenceCouchDB.URL_PROPERTY_KEY, url.toString());
persitenceConfiguration.addProperty(PersistenceCouchDB.USERNAME_PROPERTY_KEY, "");
persitenceConfiguration.addProperty(PersistenceCouchDB.PASSWORD_PROPERTY_KEY, "");
persitenceConfiguration.addProperty(PersistenceCouchDB.DB_NAME,"");
PersistenceCouchDB couch = new PersistenceCouchDB();
couch.prepareConnection(persitenceConfiguration);
ScopeProvider.instance.set("/gcube/devNext");
Record record = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
couch.reallyAccount(record);
}
}

@ -0,0 +1,16 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n</pattern>
</encoder>
</appender>
<logger name="org.gcube" level="TRACE" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>