first release
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib-accounting-service@148247 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
commit
e1d1fe5d66
|
@ -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 excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<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 excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<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 kind="output" path="target/classes"/>
|
||||
</classpath>
|
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>document-store-lib-accounting-service</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,4 @@
|
|||
gCube System - License
|
||||
------------------------------------------------------------
|
||||
|
||||
${gcube.license}
|
|
@ -0,0 +1,66 @@
|
|||
The gCube System - ${name}
|
||||
--------------------------------------------------
|
||||
|
||||
${description}
|
||||
|
||||
|
||||
${gcube.description}
|
||||
|
||||
${gcube.funding}
|
||||
|
||||
|
||||
Version
|
||||
--------------------------------------------------
|
||||
|
||||
${version} (${buildDate})
|
||||
|
||||
Please see the file named "changelog.xml" in this directory for the release notes.
|
||||
|
||||
|
||||
Authors
|
||||
--------------------------------------------------
|
||||
|
||||
* Alessandro Pieve (alessandro.pieve-AT-isti.cnr.it), Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" - CNR, Pisa (Italy).
|
||||
|
||||
|
||||
|
||||
Maintainers
|
||||
-----------
|
||||
|
||||
* Alessandro Pieve (alessandro.pieve-AT-isti.cnr.it), Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" - CNR, Pisa (Italy).
|
||||
|
||||
Download information
|
||||
--------------------------------------------------
|
||||
|
||||
Source code is available from SVN:
|
||||
${scm.url}
|
||||
|
||||
Binaries can be downloaded from the gCube website:
|
||||
${gcube.website}
|
||||
|
||||
|
||||
Installation
|
||||
--------------------------------------------------
|
||||
|
||||
Installation documentation is available on-line in the gCube Wiki:
|
||||
${gcube.wikiRoot}/Document_Store_Lib
|
||||
|
||||
|
||||
Documentation
|
||||
--------------------------------------------------
|
||||
|
||||
Documentation is available on-line in the gCube Wiki:
|
||||
${gcube.wikiRoot}/Document_Store_Lib
|
||||
|
||||
|
||||
Support
|
||||
--------------------------------------------------
|
||||
|
||||
Bugs and support requests can be reported in the gCube issue tracking tool:
|
||||
${gcube.issueTracking}
|
||||
|
||||
|
||||
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.document-store-lib.persistence.PersistenceAccountingService.1-0-0" date="2016-12-15">
|
||||
<Change>First Release</Change>
|
||||
</Changeset>
|
||||
</ReleaseNotes>
|
|
@ -0,0 +1,31 @@
|
|||
<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>changelog.xml</include>
|
||||
<include>profile.xml</include>
|
||||
</includes>
|
||||
<fileMode>755</fileMode>
|
||||
<filtered>true</filtered>
|
||||
</fileSet>
|
||||
</fileSets>
|
||||
<files>
|
||||
<file>
|
||||
<source>target/${build.finalName}.jar</source>
|
||||
<outputDirectory>/${artifactId}</outputDirectory>
|
||||
</file>
|
||||
</files>
|
||||
</assembly>
|
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Resource>
|
||||
<ID />
|
||||
<Type>Service</Type>
|
||||
<Profile>
|
||||
<Description>${description}</Description>
|
||||
<Class>${serviceClass}</Class>
|
||||
<Name>${artifactId}</Name>
|
||||
<Version>1.0.0</Version>
|
||||
<Packages>
|
||||
<Software>
|
||||
<Name>${artifactId}</Name>
|
||||
<Version>${version}</Version>
|
||||
<MavenCoordinates>
|
||||
<groupId>${groupId}</groupId>
|
||||
<artifactId>${artifactId}</artifactId>
|
||||
<version>${version}</version>
|
||||
</MavenCoordinates>
|
||||
<Files>
|
||||
<File>${build.finalName}.jar</File>
|
||||
</Files>
|
||||
</Software>
|
||||
</Packages>
|
||||
</Profile>
|
||||
</Resource>
|
||||
|
|
@ -0,0 +1,146 @@
|
|||
<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-accounting-service</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>Document Store Accounting Service Connector</name>
|
||||
<description>Document Store Connector for Accounting Service </description>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<distroDirectory>${project.basedir}/distro</distroDirectory>
|
||||
<serviceClass>DataPublishing</serviceClass>
|
||||
</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>
|
||||
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.gcube.distribution</groupId>
|
||||
<artifactId>gcube-bom</artifactId>
|
||||
<version>LATEST</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.gcube.data.publishing</groupId>
|
||||
<artifactId>document-store-lib</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.core</groupId>
|
||||
<artifactId>common-scope</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>authorization-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>common-authorization</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.core</groupId>
|
||||
<artifactId>common-encryption</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.resources.discovery</groupId>
|
||||
<artifactId>ic-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
<!--
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.3.4</version>
|
||||
</dependency>
|
||||
-->
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<!-- Test 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>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.accounting</groupId>
|
||||
<artifactId>accounting-lib</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<configuration>
|
||||
<descriptors>
|
||||
<descriptor>${distroDirectory}/descriptor.xml</descriptor>
|
||||
</descriptors>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<!--
|
||||
<executions>
|
||||
<execution>
|
||||
<id>servicearchive</id>
|
||||
<phase>install</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
-->
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,247 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.documentstore.persistence;
|
||||
|
||||
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
||||
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
|
||||
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
||||
import org.gcube.common.resources.gcore.GCoreEndpoint.Profile;
|
||||
import org.gcube.common.resources.gcore.GCoreEndpoint.Profile.Endpoint;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.documentstore.records.DSMapper;
|
||||
import org.gcube.documentstore.records.Record;
|
||||
import org.gcube.documentstore.records.SerializableList;
|
||||
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
/**
|
||||
* @author Alessandro Pieve (ISTI - CNR) alessandro.pieve@isti.cnr.it
|
||||
*
|
||||
*/
|
||||
public class PersistenceAccountingService extends PersistenceBackend {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PersistenceAccountingService.class);
|
||||
|
||||
public static final String URL_SERVICE_ACCOUNTING_KEY = "UrlAccountingService";
|
||||
|
||||
public static final String PATH_SERVICE_ACCOUNTING = "/accounting-service/gcube/service";
|
||||
|
||||
public static final String PATH_SERVICE_INSERT_ACCOUNTING = "/insert/record";
|
||||
public static final String PATH_SERVICE_INSERTS_ACCOUNTING = "/insert/records";
|
||||
public static final String PATH_SERVICE_STATUS_ACCOUNTING = "insert/getStatus";
|
||||
|
||||
public static final String GCORE_END_POINT_NAME="AccountService webapp";
|
||||
public static final String GCORE_END_RUNNING="RunningInstance";
|
||||
|
||||
public static final String RESOURCE_ACCOUNTING="org.gcube.data.publishing.accounting.service.AccountingResource";
|
||||
private static final String USER_AGENT = "Mozilla/5.0";
|
||||
private static final String CONTENT_TYPE_JSON = "application/json";
|
||||
private static final String CONTENT_TYPE_XML = "application/xml";
|
||||
|
||||
protected String urlService;
|
||||
protected String context;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected void prepareConnection(PersistenceBackendConfiguration configuration) throws Exception {
|
||||
|
||||
try{
|
||||
context = ScopeProvider.instance.get();
|
||||
logger.debug("prepareConnection context:{}",context);
|
||||
ScopeProvider.instance.set(context);
|
||||
SimpleQuery query = queryFor(GCoreEndpoint.class);
|
||||
query.addCondition("$resource/Profile/Description/text() eq '"+ GCORE_END_POINT_NAME +"'");
|
||||
query.addCondition("$resource/Type/text() eq '"+ GCORE_END_RUNNING +"'");
|
||||
|
||||
DiscoveryClient<GCoreEndpoint> client = clientFor(GCoreEndpoint.class);
|
||||
List<GCoreEndpoint> toReturn = client.submit(query);
|
||||
|
||||
GCoreEndpoint endpoint=toReturn.get(0);
|
||||
Profile profile =endpoint.profile();
|
||||
Endpoint url =profile.endpointMap().get(RESOURCE_ACCOUNTING);
|
||||
|
||||
urlService=url.uri().toString();
|
||||
logger.debug("urlService from GcoreEndPoint:{}",urlService);
|
||||
|
||||
|
||||
}catch(Exception e){
|
||||
try{
|
||||
urlService= configuration.getProperty(URL_SERVICE_ACCOUNTING_KEY)+PATH_SERVICE_ACCOUNTING+"/";
|
||||
logger.debug("urlService from Service End Point:{}",urlService);
|
||||
}catch(Exception ex){
|
||||
logger.error("Url service not found into configuration from service point");
|
||||
throw new IllegalStateException("Url service has a null property", ex);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected void reallyAccount(Record record) throws Exception {
|
||||
|
||||
logger.trace("init reallyAccount");
|
||||
String path=urlService+PATH_SERVICE_INSERT_ACCOUNTING;
|
||||
|
||||
URL obj = new URL(path);
|
||||
logger.trace("reallyAccount path:{}",path);
|
||||
String recordMarshal=DSMapper.marshal(record);
|
||||
int responseCode;
|
||||
if (path.indexOf("http") != -1){
|
||||
logger.trace("accountWithFallback http path:{}",path);
|
||||
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
||||
con.setRequestMethod("POST");
|
||||
con.setRequestProperty("User-Agent", USER_AGENT);
|
||||
con.setRequestProperty("Content-type", CONTENT_TYPE_JSON);
|
||||
con.setRequestProperty("gcube-scope", context);
|
||||
con.setDoOutput(true);
|
||||
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
|
||||
wr.writeBytes(recordMarshal);
|
||||
wr.flush();
|
||||
wr.close();
|
||||
responseCode = con.getResponseCode();
|
||||
}
|
||||
else{
|
||||
logger.trace("accountWithFallback https path:{}",path);
|
||||
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
|
||||
con.setRequestMethod("POST");
|
||||
con.setRequestProperty("User-Agent", USER_AGENT);
|
||||
con.setRequestProperty("Content-type", CONTENT_TYPE_JSON);
|
||||
con.setRequestProperty("gcube-scope", context);
|
||||
con.setDoOutput(true);
|
||||
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
|
||||
wr.writeBytes(recordMarshal);
|
||||
wr.flush();
|
||||
wr.close();
|
||||
responseCode = con.getResponseCode();
|
||||
}
|
||||
logger.debug("reallyAccount Post parameters : " + recordMarshal);
|
||||
logger.debug("reallyAccount Response Code : " + responseCode);
|
||||
switch (responseCode) {
|
||||
case HttpURLConnection.HTTP_OK:
|
||||
logger.trace("accountWithFallback - Send records to service:{}");
|
||||
break;
|
||||
case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
|
||||
throw new TimeoutException("Time out for call service accounting");
|
||||
|
||||
default:
|
||||
throw new Exception("Generic error for service accounting");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void accountWithFallback(Record... records) throws Exception {
|
||||
|
||||
logger.trace("init accountWithFallback");
|
||||
String path=urlService+PATH_SERVICE_INSERTS_ACCOUNTING;
|
||||
List<String> valuesList=new ArrayList<String>();
|
||||
for(Record record:records){
|
||||
logger.trace("add record:{}",record);
|
||||
valuesList.add(DSMapper.marshal(record));
|
||||
}
|
||||
SerializableList list=new SerializableList(valuesList);
|
||||
|
||||
JAXBContext contextRecord = JAXBContext.newInstance(SerializableList.class);
|
||||
StringWriter writer =new StringWriter();
|
||||
contextRecord.createMarshaller().marshal(list, writer);
|
||||
URL obj = new URL(path);
|
||||
//check if http or https
|
||||
int responseCode;
|
||||
if (path.indexOf("http") != -1){
|
||||
logger.trace("accountWithFallback http path:{}",path);
|
||||
HttpURLConnection con = (HttpURLConnection) obj.openConnection();//add request header
|
||||
con.setRequestMethod("POST");
|
||||
con.setRequestProperty("User-Agent", USER_AGENT);
|
||||
con.setRequestProperty("Content-type", CONTENT_TYPE_XML);
|
||||
con.setRequestProperty("gcube-scope", context);
|
||||
con.setDoOutput(true);
|
||||
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
|
||||
wr.writeBytes(writer.toString());
|
||||
wr.flush();
|
||||
wr.close();
|
||||
responseCode = con.getResponseCode();
|
||||
}
|
||||
else{
|
||||
logger.trace("accountWithFallback https path:{}",path);
|
||||
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
|
||||
con.setRequestMethod("POST");
|
||||
con.setRequestProperty("User-Agent", USER_AGENT);
|
||||
con.setRequestProperty("Content-type", CONTENT_TYPE_XML);
|
||||
con.setRequestProperty("gcube-scope", context);
|
||||
con.setDoOutput(true);
|
||||
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
|
||||
wr.writeBytes(writer.toString());
|
||||
wr.flush();
|
||||
wr.close();
|
||||
responseCode = con.getResponseCode();
|
||||
}
|
||||
logger.debug("accountWithFallback gcube-scope : " +context);
|
||||
logger.debug("accountWithFallback Post parameters : " + writer.toString());
|
||||
logger.debug("accountWithFallback Response Code : " + responseCode);
|
||||
logger.trace("accountWithFallback - Send records to service:{}");
|
||||
switch (responseCode) {
|
||||
case HttpURLConnection.HTTP_OK:
|
||||
logger.trace("accountWithFallback - Service respond ok :{}");
|
||||
break;
|
||||
case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
|
||||
throw new TimeoutException("Time out for call service accounting");
|
||||
|
||||
default:
|
||||
throw new Exception("Generic error for service accounting");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
//TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void openConnection() throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void closeConnection() throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void closeAndClean() throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
org.gcube.documentstore.persistence.PersistenceAccountingService
|
|
@ -0,0 +1,150 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.documentstore.persistence;
|
||||
|
||||
import org.gcube.documentstore.records.Record;
|
||||
import org.gcube.documentstore.utility.TestUsageRecord;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Alessandro Pieve (ISTI - CNR) alessandro.pieve@isti.cnr.it
|
||||
*
|
||||
*/
|
||||
public class PersistenceAccountingServiceTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PersistenceAccountingServiceTest.class);
|
||||
|
||||
|
||||
|
||||
@Before
|
||||
public void before() throws Exception{
|
||||
//SecurityTokenProvider.instance.set("36501a0d-a205-4bf1-87ad-4c7185faa0d6-98187548");
|
||||
//SecurityTokenProvider.instance.set("3acdde42-6883-4564-b3ba-69f6486f6fe0-98187548");
|
||||
|
||||
// SecurityTokenProvider.instance.set("2580f89f-d7a8-452d-a131-b3859bd771fd-98187548");
|
||||
|
||||
//ScopeProvider.instance.set("/gcube/devNext");
|
||||
//ScopeProvider.instance.set("/gcube");
|
||||
|
||||
|
||||
// ScopeProvider.instance.set("/gcube/devNext/NextNext");
|
||||
}
|
||||
|
||||
@After
|
||||
public void after(){
|
||||
//SecurityTokenProvider.instance.reset();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSingleInsertService() throws Exception{
|
||||
// Production-Preproduction Nodes
|
||||
PersistenceBackendConfiguration persitenceConfiguration = null;
|
||||
PersistenceAccountingService accountingService = new PersistenceAccountingService();
|
||||
accountingService.prepareConnection(persitenceConfiguration);
|
||||
|
||||
Record record = TestUsageRecord.createTestServiceUsageRecord();
|
||||
accountingService.reallyAccount(record);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMultipleInsertService() throws Exception{
|
||||
// Production-Preproduction Nodes
|
||||
PersistenceBackendConfiguration persitenceConfiguration = null;
|
||||
PersistenceAccountingService accountingService = new PersistenceAccountingService();
|
||||
accountingService.prepareConnection(persitenceConfiguration);
|
||||
Integer count=2;
|
||||
Record[] records = new Record[count];
|
||||
for(int i=0; i<count; i++){
|
||||
records[i] =TestUsageRecord.createTestServiceUsageRecord();
|
||||
}
|
||||
accountingService.accountWithFallback(records);
|
||||
logger.debug("send a :{} record",count);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleInsertStorage() throws Exception{
|
||||
// Production-Preproduction Nodes
|
||||
PersistenceBackendConfiguration persitenceConfiguration = null;
|
||||
PersistenceAccountingService accountingService = new PersistenceAccountingService();
|
||||
accountingService.prepareConnection(persitenceConfiguration);
|
||||
Integer count=10;
|
||||
Record[] records = new Record[count];
|
||||
for(int i=0; i<count; i++){
|
||||
records[i] =TestUsageRecord.createTestStorageUsageRecord();
|
||||
}
|
||||
accountingService.accountWithFallback(records);
|
||||
logger.debug("send a :{} record",count);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMultipleInsertTask() throws Exception{
|
||||
// Production-Preproduction Nodes
|
||||
PersistenceBackendConfiguration persitenceConfiguration = null;
|
||||
PersistenceAccountingService accountingService = new PersistenceAccountingService();
|
||||
accountingService.prepareConnection(persitenceConfiguration);
|
||||
Integer count=12;
|
||||
Record[] records = new Record[count];
|
||||
for(int i=0; i<count; i++){
|
||||
records[i] =TestUsageRecord.createTestTaskUsageRecord();
|
||||
}
|
||||
accountingService.accountWithFallback(records);
|
||||
logger.debug("send a :{} record",count);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleInsertJob() throws Exception{
|
||||
// Production-Preproduction Nodes
|
||||
PersistenceBackendConfiguration persitenceConfiguration = null;
|
||||
PersistenceAccountingService accountingService = new PersistenceAccountingService();
|
||||
accountingService.prepareConnection(persitenceConfiguration);
|
||||
Integer count=12;
|
||||
Record[] records = new Record[count];
|
||||
for(int i=0; i<count; i++){
|
||||
records[i] =TestUsageRecord.createTestJobUsageRecord();
|
||||
}
|
||||
accountingService.accountWithFallback(records);
|
||||
logger.debug("send a :{} record",count);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleInsertPortlet() throws Exception{
|
||||
// Production-Preproduction Nodes
|
||||
PersistenceBackendConfiguration persitenceConfiguration = null;
|
||||
PersistenceAccountingService accountingService = new PersistenceAccountingService();
|
||||
accountingService.prepareConnection(persitenceConfiguration);
|
||||
Integer count=12;
|
||||
Record[] records = new Record[count];
|
||||
for(int i=0; i<count; i++){
|
||||
records[i] =TestUsageRecord.createTestPortletUsageRecord();
|
||||
}
|
||||
accountingService.accountWithFallback(records);
|
||||
logger.debug("send a :{} record",count);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSearchConfiguration() throws Exception{
|
||||
// Production-Preproduction Nodes
|
||||
PersistenceBackendConfiguration persitenceConfiguration = null;
|
||||
PersistenceAccountingService accountingService = new PersistenceAccountingService();
|
||||
accountingService.prepareConnection(persitenceConfiguration);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.documentstore.persistence;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Alessandro Pieve (ISTI - CNR) alessandro.pieve@isti.cnr.it
|
||||
*
|
||||
*/
|
||||
public class PersistenceBackendFactoryTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PersistenceBackendFactoryTest.class);
|
||||
|
||||
@Test
|
||||
public void parsingTest() throws Exception {
|
||||
ScopeProvider.instance.set("/gcube/devNext/NextNext");
|
||||
|
||||
PersistenceBackend persistenceBackend = PersistenceBackendFactory.getPersistenceBackend("/gcube/devNext/NextNext");
|
||||
logger.debug("{}", persistenceBackend);
|
||||
|
||||
|
||||
PersistenceBackendFactory.discoverPersistenceBackend("/gcube/devNext/NextNext", (FallbackPersistenceBackend) persistenceBackend);
|
||||
|
||||
|
||||
|
||||
PersistenceBackendFactory.flushAll(100, TimeUnit.MILLISECONDS);
|
||||
PersistenceBackendFactory.flush(null, 100, TimeUnit.MILLISECONDS);
|
||||
persistenceBackend.flush(100, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,275 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.documentstore.utility;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
|
||||
import org.gcube.accounting.datamodel.basetypes.AbstractStorageUsageRecord;
|
||||
import org.gcube.accounting.datamodel.basetypes.AbstractStorageStatusRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.JobUsageRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.PortletUsageRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.StorageStatusRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.TaskUsageRecord;
|
||||
import org.gcube.accounting.persistence.AccountingPersistenceFactory;
|
||||
import org.gcube.documentstore.exception.InvalidValueException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*
|
||||
*/
|
||||
public class TestUsageRecord {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(TestUsageRecord.class);
|
||||
|
||||
//public final static String TEST_CONSUMER_ID = "stresstest.stresstest";
|
||||
public final static String TEST_CONSUMER_ID = "name.test.surname";
|
||||
public final static String TEST_SCOPE = "/infrastructure/vo";
|
||||
public final static String TEST_SCOPE_2 = "/infrastructure/vo/vre";
|
||||
public final static OperationResult TEST_OPERATION_RESULT = OperationResult.SUCCESS;
|
||||
|
||||
public final static String TEST_SERVICE_CLASS = "TestServiceClass";
|
||||
public final static String TEST_SERVICE_NAME = "TestServiceName";
|
||||
public final static String TEST_CALLED_METHOD = "TestCalledMethod";
|
||||
public final static String TEST_CALLER_QUALIFIER = "TestCallerQualifier";
|
||||
|
||||
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";
|
||||
|
||||
public final static String TEST_JOB_ID = UUID.randomUUID().toString();
|
||||
public final static String TEST_JOB_NAME = "TestJobName";
|
||||
public final static int TEST_VMS_USED = 2;
|
||||
public final static String TEST_JOB_QUALIFIER = "TestJobQualifier";
|
||||
public final static long HALF_DURATION = 10 * 60 * 1000; // 10 min
|
||||
|
||||
public final static String TEST_TASK_ID = UUID.randomUUID().toString();
|
||||
public final static String TEST_NESTED_MAP = "TestNestedMap";
|
||||
|
||||
public final static String TEST_PORTLET_ID = "TestPortlet";
|
||||
public final static String TEST_PORTLET_OPERATION_ID = "TestPortletOperationID";
|
||||
public final static String TEST_PORTLET_MESSAGE = "TestPortletMessage";
|
||||
|
||||
private final static long MIN_DURATION = 60; // millisec
|
||||
private final static long MAX_DURATION = 1000; // millisec
|
||||
|
||||
static {
|
||||
AccountingPersistenceFactory.initAccountingPackages();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate A Random long in a range between min and max.
|
||||
* This function is internally used to set random duration.
|
||||
* @return the generated random long
|
||||
*/
|
||||
public static long generateRandomLong(long min, long max){
|
||||
return min + (int)(Math.random() * ((max - min) + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a valid #ServiceUsageRecord with scope set automatically.
|
||||
* @return the created #ServiceUsageRecord
|
||||
*/
|
||||
public static ServiceUsageRecord createTestServiceUsageRecord() {
|
||||
ServiceUsageRecord usageRecord = new ServiceUsageRecord();
|
||||
try {
|
||||
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
||||
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
|
||||
|
||||
usageRecord.setCallerHost(TEST_CALLER_HOST);
|
||||
usageRecord.setHost(TEST_HOST);
|
||||
usageRecord.setCallerQualifier(TEST_CALLER_QUALIFIER);
|
||||
usageRecord.setServiceClass(TEST_SERVICE_CLASS);
|
||||
usageRecord.setServiceName(TEST_SERVICE_NAME);
|
||||
usageRecord.setCalledMethod(TEST_CALLED_METHOD);
|
||||
|
||||
usageRecord.setDuration(generateRandomLong(MIN_DURATION, MAX_DURATION));
|
||||
|
||||
} catch (InvalidValueException e) {
|
||||
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return usageRecord;
|
||||
|
||||
}
|
||||
public final static String TEST_RESOUCE_OWNER = "resource.owner";
|
||||
public final static String TEST_RESOUCE_SCOPE = TEST_SCOPE;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Create a valid #StorageUsageRecord with scope set automatically.
|
||||
* @return the created #StorageUsageRecord
|
||||
*/
|
||||
public static StorageUsageRecord createTestStorageUsageRecord() {
|
||||
StorageUsageRecord usageRecord = new StorageUsageRecord();
|
||||
try {
|
||||
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
||||
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
|
||||
|
||||
usageRecord.setResourceOwner(TEST_RESOUCE_OWNER);
|
||||
usageRecord.setResourceScope(TEST_RESOUCE_SCOPE);
|
||||
|
||||
usageRecord.setResourceURI(new URI(TEST_RESOURCE_URI));
|
||||
usageRecord.setProviderURI(new URI(TEST_PROVIDER_URI));
|
||||
|
||||
usageRecord.setOperationType(AbstractStorageUsageRecord.OperationType.READ);
|
||||
usageRecord.setDataType(AbstractStorageUsageRecord.DataType.STORAGE);
|
||||
|
||||
usageRecord.setDataVolume(generateRandomLong(MIN_DATA_VOLUME, MAX_DATA_VOLUME));
|
||||
|
||||
usageRecord.setQualifier("image/png");
|
||||
|
||||
|
||||
} catch (InvalidValueException | URISyntaxException e) {
|
||||
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return usageRecord;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a valid #StorageVolumeUsageRecord with scope set automatically.
|
||||
* @return the created #StorageVolumeUsageRecord
|
||||
*/
|
||||
public static StorageStatusRecord createTestStorageVolumeUsageRecord() {
|
||||
StorageStatusRecord usageRecord = new StorageStatusRecord();
|
||||
try {
|
||||
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
||||
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
|
||||
usageRecord.setDataVolume(generateRandomLong(MIN_DATA_VOLUME, MAX_DATA_VOLUME));
|
||||
usageRecord.setDataType(AbstractStorageStatusRecord.DataType.STORAGE);
|
||||
usageRecord.setDataCount(generateRandomLong(MIN_DATA_VOLUME, MAX_DATA_VOLUME));
|
||||
usageRecord.setDataServiceClass("dataServiceClass");
|
||||
usageRecord.setDataServiceName("dataServiceName");
|
||||
usageRecord.setDataServiceId("dataServiceId");
|
||||
usageRecord.setProviderId(new URI(TEST_PROVIDER_URI));
|
||||
|
||||
|
||||
|
||||
} catch (InvalidValueException | URISyntaxException e) {
|
||||
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return usageRecord;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public static JobUsageRecord createTestJobUsageRecord() {
|
||||
|
||||
JobUsageRecord usageRecord = new JobUsageRecord();
|
||||
try {
|
||||
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
||||
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
|
||||
|
||||
usageRecord.setJobId(TEST_JOB_ID);
|
||||
usageRecord.setJobName(TEST_JOB_NAME);
|
||||
Calendar startTime = Calendar.getInstance();
|
||||
Calendar endTime = Calendar.getInstance();
|
||||
endTime.setTimeInMillis(startTime.getTimeInMillis() + HALF_DURATION);
|
||||
startTime.setTimeInMillis(startTime.getTimeInMillis() - HALF_DURATION);
|
||||
|
||||
usageRecord.setJobStartTime(startTime);
|
||||
usageRecord.setJobEndTime(endTime);
|
||||
|
||||
} catch (InvalidValueException e) {
|
||||
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
||||
}
|
||||
|
||||
return usageRecord;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public static TaskUsageRecord createTestTaskUsageRecord() {
|
||||
|
||||
TaskUsageRecord usageRecord = new TaskUsageRecord();
|
||||
try {
|
||||
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
||||
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
|
||||
|
||||
usageRecord.setTaskId(TEST_TASK_ID);
|
||||
usageRecord.setTaskId(TEST_JOB_ID);
|
||||
usageRecord.setHost(TEST_HOST);
|
||||
|
||||
usageRecord.setRefHostingNodeId(UUID.randomUUID().toString());
|
||||
|
||||
Calendar startTime = Calendar.getInstance();
|
||||
Calendar endTime = Calendar.getInstance();
|
||||
endTime.setTimeInMillis(startTime.getTimeInMillis() + HALF_DURATION);
|
||||
startTime.setTimeInMillis(startTime.getTimeInMillis() - HALF_DURATION);
|
||||
|
||||
usageRecord.setTaskStartTime(startTime);
|
||||
usageRecord.setTaskEndTime(endTime);
|
||||
|
||||
HashMap<String, Serializable> inputParameters = new HashMap<>();
|
||||
inputParameters.put(TEST_PROPERTY_NAME, TEST_PROPERTY_VALUE);
|
||||
inputParameters.put(TEST_PROPERTY_VALUE, TEST_PROPERTY_NAME);
|
||||
|
||||
HashMap<String, Serializable> parameter = new HashMap<>();
|
||||
parameter.put(TEST_PROPERTY_NAME, TEST_PROPERTY_VALUE);
|
||||
parameter.put(TEST_PROPERTY_VALUE, TEST_PROPERTY_NAME);
|
||||
|
||||
inputParameters.put(TEST_NESTED_MAP, parameter);
|
||||
|
||||
usageRecord.setInputParameters(inputParameters);
|
||||
|
||||
} catch (InvalidValueException e) {
|
||||
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
||||
}
|
||||
|
||||
return usageRecord;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public static PortletUsageRecord createTestPortletUsageRecord() {
|
||||
|
||||
PortletUsageRecord usageRecord = new PortletUsageRecord();
|
||||
try {
|
||||
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
||||
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
|
||||
|
||||
Calendar startTime = Calendar.getInstance();
|
||||
Calendar endTime = Calendar.getInstance();
|
||||
endTime.setTimeInMillis(startTime.getTimeInMillis() + HALF_DURATION);
|
||||
startTime.setTimeInMillis(startTime.getTimeInMillis() - HALF_DURATION);
|
||||
|
||||
usageRecord.setPortletId(TEST_PORTLET_ID);
|
||||
usageRecord.setOperationId(TEST_PORTLET_OPERATION_ID);
|
||||
usageRecord.setMessage(TEST_PORTLET_MESSAGE);
|
||||
|
||||
} catch (InvalidValueException e) {
|
||||
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
||||
}
|
||||
|
||||
return usageRecord;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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>
|
Loading…
Reference in New Issue