diff --git a/.classpath b/.classpath index 1551d7b..3942bfb 100644 --- a/.classpath +++ b/.classpath @@ -1,18 +1,17 @@ - + - - + - + - + @@ -22,9 +21,17 @@ - + + + + + + + + + diff --git a/.settings/.gitignore b/.settings/.gitignore new file mode 100644 index 0000000..1de83a6 --- /dev/null +++ b/.settings/.gitignore @@ -0,0 +1,3 @@ +/org.eclipse.core.resources.prefs +/org.eclipse.jdt.core.prefs +/org.eclipse.m2e.core.prefs diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..1a75100 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,13 @@ +This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +# Changelog for Accounting Insert Storage Smart Executor Plugin + + +## [v2.0.0-SNAPSHOT] + +- The plugin has been completely rewrote + +## [v1.0.0] + +- First Release + diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..3af0507 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,312 @@ +# European Union Public Licence V. 1.1 + + +EUPL © the European Community 2007 + + +This European Union Public Licence (the “EUPL”) applies to the Work or Software +(as defined below) which is provided under the terms of this Licence. Any use of +the Work, other than as authorised under this Licence is prohibited (to the +extent such use is covered by a right of the copyright holder of the Work). + +The Original Work is provided under the terms of this Licence when the Licensor +(as defined below) has placed the following notice immediately following the +copyright notice for the Original Work: + +Licensed under the EUPL V.1.1 + +or has expressed by any other mean his willingness to license under the EUPL. + + + +## 1. Definitions + +In this Licence, the following terms have the following meaning: + +- The Licence: this Licence. + +- The Original Work or the Software: the software distributed and/or + communicated by the Licensor under this Licence, available as Source Code and + also as Executable Code as the case may be. + +- Derivative Works: the works or software that could be created by the Licensee, + based upon the Original Work or modifications thereof. This Licence does not + define the extent of modification or dependence on the Original Work required + in order to classify a work as a Derivative Work; this extent is determined by + copyright law applicable in the country mentioned in Article 15. + +- The Work: the Original Work and/or its Derivative Works. + +- The Source Code: the human-readable form of the Work which is the most + convenient for people to study and modify. + +- The Executable Code: any code which has generally been compiled and which is + meant to be interpreted by a computer as a program. + +- The Licensor: the natural or legal person that distributes and/or communicates + the Work under the Licence. + +- Contributor(s): any natural or legal person who modifies the Work under the + Licence, or otherwise contributes to the creation of a Derivative Work. + +- The Licensee or “You”: any natural or legal person who makes any usage of the + Software under the terms of the Licence. + +- Distribution and/or Communication: any act of selling, giving, lending, + renting, distributing, communicating, transmitting, or otherwise making + available, on-line or off-line, copies of the Work or providing access to its + essential functionalities at the disposal of any other natural or legal + person. + + + +## 2. Scope of the rights granted by the Licence + +The Licensor hereby grants You a world-wide, royalty-free, non-exclusive, +sub-licensable licence to do the following, for the duration of copyright vested +in the Original Work: + +- use the Work in any circumstance and for all usage, reproduce the Work, modify +- the Original Work, and make Derivative Works based upon the Work, communicate +- to the public, including the right to make available or display the Work or +- copies thereof to the public and perform publicly, as the case may be, the +- Work, distribute the Work or copies thereof, lend and rent the Work or copies +- thereof, sub-license rights in the Work or copies thereof. + +Those rights can be exercised on any media, supports and formats, whether now +known or later invented, as far as the applicable law permits so. + +In the countries where moral rights apply, the Licensor waives his right to +exercise his moral right to the extent allowed by law in order to make effective +the licence of the economic rights here above listed. + +The Licensor grants to the Licensee royalty-free, non exclusive usage rights to +any patents held by the Licensor, to the extent necessary to make use of the +rights granted on the Work under this Licence. + + + +## 3. Communication of the Source Code + +The Licensor may provide the Work either in its Source Code form, or as +Executable Code. If the Work is provided as Executable Code, the Licensor +provides in addition a machine-readable copy of the Source Code of the Work +along with each copy of the Work that the Licensor distributes or indicates, in +a notice following the copyright notice attached to the Work, a repository where +the Source Code is easily and freely accessible for as long as the Licensor +continues to distribute and/or communicate the Work. + + + +## 4. Limitations on copyright + +Nothing in this Licence is intended to deprive the Licensee of the benefits from +any exception or limitation to the exclusive rights of the rights owners in the +Original Work or Software, of the exhaustion of those rights or of other +applicable limitations thereto. + + + +## 5. Obligations of the Licensee + +The grant of the rights mentioned above is subject to some restrictions and +obligations imposed on the Licensee. Those obligations are the following: + +Attribution right: the Licensee shall keep intact all copyright, patent or +trademarks notices and all notices that refer to the Licence and to the +disclaimer of warranties. The Licensee must include a copy of such notices and a +copy of the Licence with every copy of the Work he/she distributes and/or +communicates. The Licensee must cause any Derivative Work to carry prominent +notices stating that the Work has been modified and the date of modification. + +Copyleft clause: If the Licensee distributes and/or communicates copies of the +Original Works or Derivative Works based upon the Original Work, this +Distribution and/or Communication will be done under the terms of this Licence +or of a later version of this Licence unless the Original Work is expressly +distributed only under this version of the Licence. The Licensee (becoming +Licensor) cannot offer or impose any additional terms or conditions on the Work +or Derivative Work that alter or restrict the terms of the Licence. + +Compatibility clause: If the Licensee Distributes and/or Communicates Derivative +Works or copies thereof based upon both the Original Work and another work +licensed under a Compatible Licence, this Distribution and/or Communication can +be done under the terms of this Compatible Licence. For the sake of this clause, +“Compatible Licence” refers to the licences listed in the appendix attached to +this Licence. Should the Licensee’s obligations under the Compatible Licence +conflict with his/her obligations under this Licence, the obligations of the +Compatible Licence shall prevail. + +Provision of Source Code: When distributing and/or communicating copies of the +Work, the Licensee will provide a machine-readable copy of the Source Code or +indicate a repository where this Source will be easily and freely available for +as long as the Licensee continues to distribute and/or communicate the Work. + +Legal Protection: This Licence does not grant permission to use the trade names, +trademarks, service marks, or names of the Licensor, except as required for +reasonable and customary use in describing the origin of the Work and +reproducing the content of the copyright notice. + + + +## 6. Chain of Authorship + +The original Licensor warrants that the copyright in the Original Work granted +hereunder is owned by him/her or licensed to him/her and that he/she has the +power and authority to grant the Licence. + +Each Contributor warrants that the copyright in the modifications he/she brings +to the Work are owned by him/her or licensed to him/her and that he/she has the +power and authority to grant the Licence. + +Each time You accept the Licence, the original Licensor and subsequent +Contributors grant You a licence to their contributions to the Work, under the +terms of this Licence. + + + +## 7. Disclaimer of Warranty + +The Work is a work in progress, which is continuously improved by numerous +contributors. It is not a finished work and may therefore contain defects or +“bugs” inherent to this type of software development. + +For the above reason, the Work is provided under the Licence on an “as is” basis +and without warranties of any kind concerning the Work, including without +limitation merchantability, fitness for a particular purpose, absence of defects +or errors, accuracy, non-infringement of intellectual property rights other than +copyright as stated in Article 6 of this Licence. + +This disclaimer of warranty is an essential part of the Licence and a condition +for the grant of any rights to the Work. + + + +## 8. Disclaimer of Liability + +Except in the cases of wilful misconduct or damages directly caused to natural +persons, the Licensor will in no event be liable for any direct or indirect, +material or moral, damages of any kind, arising out of the Licence or of the use +of the Work, including without limitation, damages for loss of goodwill, work +stoppage, computer failure or malfunction, loss of data or any commercial +damage, even if the Licensor has been advised of the possibility of such +damage. However, the Licensor will be liable under statutory product liability +laws as far such laws apply to the Work. + + + +## 9. Additional agreements + +While distributing the Original Work or Derivative Works, You may choose to +conclude an additional agreement to offer, and charge a fee for, acceptance of +support, warranty, indemnity, or other liability obligations and/or services +consistent with this Licence. However, in accepting such obligations, You may +act only on your own behalf and on your sole responsibility, not on behalf of +the original Licensor or any other Contributor, and only if You agree to +indemnify, defend, and hold each Contributor harmless for any liability incurred +by, or claims asserted against such Contributor by the fact You have accepted +any such warranty or additional liability. + + + +## 10. Acceptance of the Licence + +The provisions of this Licence can be accepted by clicking on an icon “I agree” +placed under the bottom of a window displaying the text of this Licence or by +affirming consent in any other similar way, in accordance with the rules of +applicable law. Clicking on that icon indicates your clear and irrevocable +acceptance of this Licence and all of its terms and conditions. + +Similarly, you irrevocably accept this Licence and all of its terms and +conditions by exercising any rights granted to You by Article 2 of this Licence, +such as the use of the Work, the creation by You of a Derivative Work or the +Distribution and/or Communication by You of the Work or copies thereof. + + + +## 11. Information to the public + +In case of any Distribution and/or Communication of the Work by means of +electronic communication by You (for example, by offering to download the Work +from a remote location) the distribution channel or media (for example, a +website) must at least provide to the public the information requested by the +applicable law regarding the Licensor, the Licence and the way it may be +accessible, concluded, stored and reproduced by the Licensee. + + + +## 12. Termination of the Licence + +The Licence and the rights granted hereunder will terminate automatically upon +any breach by the Licensee of the terms of the Licence. + +Such a termination will not terminate the licences of any person who has +received the Work from the Licensee under the Licence, provided such persons +remain in full compliance with the Licence. + + + +## 13. Miscellaneous + +Without prejudice of Article 9 above, the Licence represents the complete +agreement between the Parties as to the Work licensed hereunder. + +If any provision of the Licence is invalid or unenforceable under applicable +law, this will not affect the validity or enforceability of the Licence as a +whole. Such provision will be construed and/or reformed so as necessary to make +it valid and enforceable. + +The European Commission may publish other linguistic versions and/or new +versions of this Licence, so far this is required and reasonable, without +reducing the scope of the rights granted by the Licence. New versions of the +Licence will be published with a unique version number. + +All linguistic versions of this Licence, approved by the European Commission, +have identical value. Parties can take advantage of the linguistic version of +their choice. + + + +## 14. Jurisdiction + +Any litigation resulting from the interpretation of this License, arising +between the European Commission, as a Licensor, and any Licensee, will be +subject to the jurisdiction of the Court of Justice of the European Communities, +as laid down in article 238 of the Treaty establishing the European Community. + +Any litigation arising between Parties, other than the European Commission, and +resulting from the interpretation of this License, will be subject to the +exclusive jurisdiction of the competent court where the Licensor resides or +conducts its primary business. + + + +## 15. Applicable Law + +This Licence shall be governed by the law of the European Union country where +the Licensor resides or has his registered office. + +This licence shall be governed by the Belgian law if: + +- a litigation arises between the European Commission, as a Licensor, and any +- Licensee; the Licensor, other than the European Commission, has no residence +- or registered office inside a European Union country. + + + +## Appendix + + + +“Compatible Licences” according to article 5 EUPL are: + + +- GNU General Public License (GNU GPL) v. 2 + +- Open Software License (OSL) v. 2.1, v. 3.0 + +- Common Public License v. 1.0 + +- Eclipse Public License v. 1.0 + +- Cecill v. 2.0 + diff --git a/README.md b/README.md new file mode 100644 index 0000000..8a04412 --- /dev/null +++ b/README.md @@ -0,0 +1,71 @@ +# Accounting Insert Storage Smart Executor Plugin + +Accounting Insert Storage Smart Executor Plugin + +## Built With + +* [OpenJDK](https://openjdk.java.net/) - The JDK used +* [Maven](https://maven.apache.org/) - Dependency Management + +## Documentation + +[Accounting Insert Storage Smart Executor Plugin](https://wiki.gcube-system.org/gcube/Accounting) + +## Change log + +See [Releases](https://code-repo.d4science.org/gCubeSystem/accounting-insert-storage-se-plugin/releases). + +## Authors + +* **Luca Frosini** ([ORCID](https://orcid.org/0000-0003-3183-2291)) - [ISTI-CNR Infrascience Group](http://nemis.isti.cnr.it/groups/infrascience) + +## How to Cite this Software + +Tell people how to cite this software. +* Cite an associated paper? +* Use a specific BibTeX entry for the software? + + + @Manual{, + title = {Accounting Insert Storage Smart Executor Plugin}, + author = {{Frosini, Luca}}, + organization = {ISTI - CNR}, + address = {Pisa, Italy}, + year = 2021, + url = {http://www.gcube-system.org/} + } + +## License + +This project is licensed under the EUPL V.1.1 License - see the [LICENSE.md](LICENSE.md) file for details. + + +## About the gCube Framework +This software is part of the [gCubeFramework](https://www.gcube-system.org/ "gCubeFramework"): an +open-source software toolkit used for building and operating Hybrid Data +Infrastructures enabling the dynamic deployment of Virtual Research Environments +by favouring the realisation of reuse oriented policies. + +The projects leading to this software have received funding from a series of European Union programmes including: + +- the Sixth Framework Programme for Research and Technological Development + - DILIGENT (grant no. 004260). +- the Seventh Framework Programme for research, technological development and demonstration + - D4Science (grant no. 212488); + - D4Science-II (grant no.239019); + - ENVRI (grant no. 283465); + - iMarine(grant no. 283644); + - EUBrazilOpenBio (grant no. 288754). +- the H2020 research and innovation programme + - SoBigData (grant no. 654024); + - PARTHENOS (grant no. 654119); + - EGIEngage (grant no. 654142); + - ENVRIplus (grant no. 654182); + - BlueBRIDGE (grant no. 675680); + - PerformFish (grant no. 727610); + - AGINFRAplus (grant no. 731001); + - DESIRA (grant no. 818194); + - ARIADNEplus (grant no. 823914); + - RISIS2 (grant no. 824091); + + diff --git a/pom.xml b/pom.xml index 0449b1e..46af817 100644 --- a/pom.xml +++ b/pom.xml @@ -9,9 +9,9 @@ org.gcube.accounting accounting-insert-storage-se-plugin - 1.1.0-SNAPSHOT + 2.0.0-SNAPSHOT Accounting Insert Storage - Accounting Insert Storage + Accounting Insert Storage Smart Executor Plugin UTF-8 @@ -29,15 +29,15 @@ org.gcube.distribution - gcube-bom - LATEST + gcube-smartgears-bom + 1.1.0 pom import - org.gcube.distribution - gcube-smartgears-bom - LATEST + org.gcube.vremanagement + smart-executor-bom + 3.0.0-SNAPSHOT pom import @@ -48,61 +48,19 @@ org.gcube.vremanagement smart-executor-api - [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) - provided - - - org.gcube.common - common-authorization org.gcube.common - authorization-client - - - org.gcube.core - common-scope - provided - - - org.slf4j - slf4j-api - provided - - - - com.couchbase.client - java-client - 2.2.3 - - - com.couchbase.client - core-io - [1.2.3,2.0.0) - compile + gxHTTP - - - org.gcube.data.publishing - document-store-lib-couchbase - [1.0.1-SNAPSHOT, 2.0.0-SNAPSHOT) - - - document-store-lib - org.gcube.data.publishing - provided + javax.ws.rs + javax.ws.rs-api org.gcube.accounting accounting-lib - provided - - - - com.google.code.gson - gson - 2.7 + [4.0.0, 5.0.0-SNAPSHOT] org.gcube.contentmanagement @@ -114,6 +72,10 @@ storage-manager-core [2.2.0-SNAPSHOT, 3.0.0-SNAPSHOT) + + org.slf4j + slf4j-api + junit @@ -124,7 +86,6 @@ ch.qos.logback logback-classic - 1.0.13 test @@ -133,15 +94,43 @@ org.apache.maven.plugins - maven-assembly-plugin + maven-dependency-plugin + 3.1.1 - make-uberjar - install + copy-dependencies + compile + + copy-dependencies + + + runtime + ${basedir}${file.separator}target${file.separator}libs + false + false + true + war + false + + + + + org.apache.maven.plugins + maven-assembly-plugin + - make-servicearchive - install + uberjar + + true + + uberjar.xml + + + package + + single + diff --git a/src/main/java/org/gcube/accounting/insert/storage/plugin/AccountingInsertStoragePlugin.java b/src/main/java/org/gcube/accounting/insert/storage/plugin/AccountingInsertStoragePlugin.java index fa514fd..d6511f6 100644 --- a/src/main/java/org/gcube/accounting/insert/storage/plugin/AccountingInsertStoragePlugin.java +++ b/src/main/java/org/gcube/accounting/insert/storage/plugin/AccountingInsertStoragePlugin.java @@ -1,181 +1,119 @@ package org.gcube.accounting.insert.storage.plugin; +import java.net.HttpURLConnection; +import java.net.URI; +import java.util.HashSet; import java.util.Map; -import java.util.UUID; - -import org.gcube.accounting.insert.storage.persistence.AggregatorPersistenceBackendQueryConfiguration; -import org.gcube.accounting.insert.storage.utils.ConfigurationServiceEndpoint; -import org.gcube.accounting.insert.storage.utils.Constant; -import org.gcube.accounting.insert.storage.utils.DiscoveryListUser; -import org.gcube.common.scope.api.ScopeProvider; +import java.util.Set; + +import org.gcube.accounting.datamodel.UsageRecord.OperationResult; +import org.gcube.accounting.datamodel.basetypes.AbstractStorageUsageRecord.DataType; +import org.gcube.accounting.datamodel.usagerecords.StorageStatusRecord; +import org.gcube.accounting.insert.storage.utils.HTTPUtility; +import org.gcube.accounting.insert.storage.utils.SocialService; +import org.gcube.accounting.persistence.AccountingPersistence; +import org.gcube.accounting.persistence.AccountingPersistenceFactory; +import org.gcube.com.fasterxml.jackson.databind.JsonNode; +import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; +import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; +import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.contentmanagement.blobstorage.service.IClient; import org.gcube.contentmanager.storageclient.wrapper.AccessType; import org.gcube.contentmanager.storageclient.wrapper.MemoryType; import org.gcube.contentmanager.storageclient.wrapper.StorageClient; -import org.gcube.documentstore.persistence.PersistenceCouchBase; import org.gcube.vremanagement.executor.plugin.Plugin; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.couchbase.client.java.Bucket; -import com.couchbase.client.java.Cluster; -import com.couchbase.client.java.CouchbaseCluster; -import com.couchbase.client.java.document.JsonDocument; -import com.couchbase.client.java.document.json.JsonObject; -import com.couchbase.client.java.env.CouchbaseEnvironment; -import com.couchbase.client.java.env.DefaultCouchbaseEnvironment; - - - /** - * @author Alessandro Pieve (ISTI - CNR) - * + * @author Luca Frosini (ISTI-CNR) + * @author Alessandro Pieve (ISTI - CNR) */ -public class AccountingInsertStoragePlugin extends Plugin { +public class AccountingInsertStoragePlugin extends Plugin { private static Logger logger = LoggerFactory.getLogger(AccountingInsertStoragePlugin.class); - public AccountingInsertStoragePlugin(AccountingInsertStoragePluginDeclaration pluginDeclaration) { - super(pluginDeclaration); + protected static final String ALL_USERS_PATH = "2/users/get-all-usernames"; + protected static final String RESPONSE_SUCCESS_KEY = "success"; + protected static final String RESPONSE_MESSAGE_KEY = "message"; + protected static final String RESULT_KEY = "message"; + + protected ObjectMapper objectMapper; + + public AccountingInsertStoragePlugin() { + super(); + objectMapper = new ObjectMapper(); } - /* The environment configuration */ - protected static final CouchbaseEnvironment ENV = - DefaultCouchbaseEnvironment.builder() - .connectTimeout(Constant.CONNECTION_TIMEOUT * 1000) - .maxRequestLifetime(Constant.MAX_REQUEST_LIFE_TIME * 1000) - .queryTimeout(Constant.CONNECTION_TIMEOUT * 1000) //15 Seconds in milliseconds - .viewTimeout(Constant.VIEW_TIMEOUT_BUCKET * 1000)//120 Seconds in milliseconds - .keepAliveInterval(3600 * 1000) // 3600 Seconds in milliseconds - .kvTimeout(5000) //in ms - .build(); - - /**{@inheritDoc}*/ + /** {@inheritDoc} */ @Override public void launch(Map inputs) throws Exception { - String context=ScopeProvider.instance.get(); - logger.debug("AccountingInsertStoragePlugin: launch on context:{}",context); - String url=null; - String password =null; - String bucket=null; - AggregatorPersistenceBackendQueryConfiguration configuration; - try{ - configuration = new AggregatorPersistenceBackendQueryConfiguration(PersistenceCouchBase.class); - url = configuration.getProperty(ConfigurationServiceEndpoint.URL_PROPERTY_KEY); - password = configuration.getProperty(ConfigurationServiceEndpoint.PASSWORD_PROPERTY_KEY); - bucket=configuration.getProperty(ConfigurationServiceEndpoint.BUCKET_STORAGE_NAME_PROPERTY_KEY); - - } - catch (Exception e) { - logger.error("AccountingInsertStoragePlugin: launch",e.getLocalizedMessage()); - throw e; - } - Cluster cluster = CouchbaseCluster.create(ENV, url); - logger.debug("AccountingInsertStoragePlugin: create cluster"); - Bucket accountingBucket = cluster.openBucket(bucket,password); - - String urlService=null; - if (inputs.containsKey("urlService")) - urlService=(String) inputs.get("urlService"); - - String dataServiceClass="content-management"; - if (inputs.containsKey("dataServiceClass")) - dataServiceClass=(String) inputs.get("dataServiceClass"); - - String dataServiceName="storage-manager"; - if (inputs.containsKey("dataServiceName")) - dataServiceName=(String) inputs.get("dataServiceName"); - - String dataServiceId="identifier"; - if (inputs.containsKey("dataServiceId")) - dataServiceId=(String) inputs.get("dataServiceId"); - - String uri="MongoDb"; - if (inputs.containsKey("uri")) - uri=(String) inputs.get("uri"); - - String dataType="STORAGE"; - if (inputs.containsKey("dataType")) - dataType=(String) inputs.get("dataType"); - - Integer timeWait=1000; - if (inputs.containsKey("timeWait")) - timeWait=(Integer) inputs.get("timeWait"); - - DiscoveryListUser discoveryListUser= new DiscoveryListUser(context,urlService); - //list user - JsonDocument document=null; - JsonObject content=null; - for (String consumerId:discoveryListUser.getListUser()){ - //for each user call homelibrary and insert - IClient client=new StorageClient("", "", consumerId, AccessType.PUBLIC, MemoryType.PERSISTENT).getClient(); - try{ - String docId=UUID.randomUUID().toString(); - - Long dataVolume= Long.parseLong(client.getTotalUserVolume()); - Long dataCount=Long.parseLong(client.getUserTotalItems()); - Long timeStamp= System.currentTimeMillis(); - - content = JsonObject.empty().put("scope", context); - content.put("operationCount", 1); - content.put("dataCount", dataCount); - content.put("endTime", timeStamp); - content.put("consumerId", consumerId); - content.put("startTime", timeStamp); - content.put("id", docId); - content.put("dataVolume", dataVolume); - content.put("dataType", dataType); - content.put("operationResult", "SUCCESS"); - content.put("dataServiceClass", dataServiceClass); - content.put("dataServiceName", dataServiceName); - content.put("dataServiceId", dataServiceId); - content.put("aggregated", true); - content.put("providerId", uri); - content.put("creationTime", timeStamp); - content.put("recordType", "StorageStatusRecord"); - document = JsonDocument.create("docId", content); - - JsonDocument doc = JsonDocument.create(docId, content); - - JsonDocument response = accountingBucket.upsert(doc); - logger.debug("AccountingInsertStoragePlugin: upsert doc:{}. Response : {}",doc.toString(), response.toString()); - Thread.sleep(timeWait); - - + AccountingPersistence accountingPersistence = AccountingPersistenceFactory.getPersistence(); + + Set users = getAllUsers(); + + for (String consumerId : users) { + // for each user call storage manager and insert + IClient client = new StorageClient("", "", consumerId, AccessType.PUBLIC, MemoryType.PERSISTENT) + .getClient(); + StorageStatusRecord storageStatusRecord = new StorageStatusRecord(); + try { + + Long dataVolume = Long.parseLong(client.getTotalUserVolume()); + Long dataCount = Long.parseLong(client.getUserTotalItems()); + + storageStatusRecord.setConsumerId(consumerId); + storageStatusRecord.setDataVolume(dataVolume); + storageStatusRecord.setDataCount(dataCount); + storageStatusRecord.setDataType(DataType.STORAGE); + storageStatusRecord.setOperationResult(OperationResult.SUCCESS); + storageStatusRecord.setProviderURI(new URI("data.d4science.org")); + + accountingPersistence.account(storageStatusRecord); + + } catch (Exception e) { + logger.error("Error while accounting {} for {} for user {}", storageStatusRecord.getRecordType(), + DataType.STORAGE, consumerId); } - catch(Exception e){ - logger.error("AccountingInsertStoragePlugin: doc:{} not insert ({}), problem with exist bucket",document.id(),document.toString(),e); - logger.error("AccountingInsertStoragePlugin: force insert into list for insert"); - } - } - cluster.disconnect(); - logger.debug("AccountingInsertStoragePlugin: insert complete"); } + private Set getAllUsers() throws Exception { + String basePath = SocialService.getSocialService().getServiceBasePath(); + if (basePath == null) { + logger.error("Unable to get users because there is no social networking service available"); + throw new Exception("Unable to get users because there is no social networking service available"); + } + basePath = basePath.endsWith("/") ? basePath : basePath + "/"; + GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath); + gxhttpStringRequest.from(AccountingInsertStoragePlugin.class.getSimpleName()); + gxhttpStringRequest.path(ALL_USERS_PATH); + HttpURLConnection httpURLConnection = gxhttpStringRequest.get(); + String ret = HTTPUtility.getResultAsString(httpURLConnection); + JsonNode jsonNode = objectMapper.readTree(ret); + if (!jsonNode.get(RESPONSE_SUCCESS_KEY).asBoolean()) { + logger.info("Failed to get users. Reason {}", jsonNode.get(RESPONSE_MESSAGE_KEY).asText()); + } + ArrayNode arrayNode = (ArrayNode) jsonNode.get(RESULT_KEY); + Set users = new HashSet<>(arrayNode.size()); + for(int i=0; i getSupportedCapabilities() { - Map discoveredCapabilities = new HashMap(); - return discoveredCapabilities; - } - - /**{@inheritDoc}*/ - @Override - public Class> getPluginImplementation() { - return AccountingInsertStoragePlugin.class; - } - - @Override - public String toString() { - return "AccountingInsertStoragePluginDeclaration [getName()=" + getName() - + ", getDescription()=" + getDescription() + ", getVersion()=" - + getVersion() + ", getSupportedCapabilities()=" - + getSupportedCapabilities() + ", getPluginImplementation()=" - + getPluginImplementation() + "]"; - } - - -} diff --git a/src/main/java/org/gcube/accounting/insert/storage/plugin/Utility.java b/src/main/java/org/gcube/accounting/insert/storage/plugin/Utility.java deleted file mode 100644 index 345a91c..0000000 --- a/src/main/java/org/gcube/accounting/insert/storage/plugin/Utility.java +++ /dev/null @@ -1,44 +0,0 @@ - package org.gcube.accounting.insert.storage.plugin; - -import com.couchbase.client.java.document.json.JsonArray; -import com.couchbase.client.java.document.json.JsonObject; - -/** - * @author Alessandro Pieve (ISTI - CNR) - * - */ - -public class Utility { - /** - * Generate a key for map-reduce - * @param key - * @return JsonArray - */ - protected static JsonArray generateKey(String scope,String key){ - JsonArray generateKey = JsonArray.create(); - if (scope!=null){ - generateKey.add(scope); - } - for (String value: key.split(",")){ - if (!value.toString().isEmpty()) - generateKey.add(Integer.parseInt(value)); - } - return generateKey; - - } - /** - * Verify a record aggregated for insert into bucket - * @param item - * @return boolean - */ - public static boolean checkType(Object item) { - return item == null - || item instanceof String - || item instanceof Integer - || item instanceof Long - || item instanceof Double - || item instanceof Boolean - || item instanceof JsonObject - || item instanceof JsonArray; - } -} diff --git a/src/main/java/org/gcube/accounting/insert/storage/utils/ConfigurationServiceEndpoint.java b/src/main/java/org/gcube/accounting/insert/storage/utils/ConfigurationServiceEndpoint.java deleted file mode 100644 index 73bf236..0000000 --- a/src/main/java/org/gcube/accounting/insert/storage/utils/ConfigurationServiceEndpoint.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.gcube.accounting.insert.storage.utils; - -/** - * @author Alessandro Pieve (ISTI - CNR) - * - */ - -public class ConfigurationServiceEndpoint { - - //Static Key for Configuration from service end point - public static final String URL_PROPERTY_KEY = "URL"; - public static final String PASSWORD_PROPERTY_KEY = "password"; - public static final String BUCKET_NAME_PROPERTY_KEY = "bucketName"; - - public static final String BUCKET_STORAGE_NAME_PROPERTY_KEY="AggregatedStorageStatusRecord"; - public static final String BUCKET_STORAGE_TYPE="StorageStatusRecord"; - -} diff --git a/src/main/java/org/gcube/accounting/insert/storage/utils/Constant.java b/src/main/java/org/gcube/accounting/insert/storage/utils/Constant.java deleted file mode 100644 index 8f328da..0000000 --- a/src/main/java/org/gcube/accounting/insert/storage/utils/Constant.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.gcube.accounting.insert.storage.utils; -public class Constant { - - - - //CONSTANT for generate file and backup - public static String user=null; - public static String NAME_DIR_BACKUP=".aggregatorPlugin"; - public static String PATH_DIR_BACKUP="backup"; - public final static String HOME_SYSTEM_PROPERTY = "user.home"; - public static String PATH_DIR_BACKUP_INSERT="backup/insert"; - public static String PATH_DIR_BACKUP_DELETE="backup/delete"; - - //create a file for delete record before insert a new aggregate - public static final String FILE_RECORD_NO_AGGREGATE="no_aggregated"; - //create a temporany file for insert a new record aggregate - public static final String FILE_RECORD_AGGREGATE="aggregated"; - - - - public static final Integer CONNECTION_TIMEOUT=15; - public static final Integer NUM_RETRY=6; - public static final Integer CONNECTION_TIMEOUT_BUCKET=15; - public static final Integer VIEW_TIMEOUT_BUCKET=120; - public static final Integer MAX_REQUEST_LIFE_TIME=120; -} \ No newline at end of file diff --git a/src/main/java/org/gcube/accounting/insert/storage/utils/ContextUtility.java b/src/main/java/org/gcube/accounting/insert/storage/utils/ContextUtility.java new file mode 100644 index 0000000..2a7f48a --- /dev/null +++ b/src/main/java/org/gcube/accounting/insert/storage/utils/ContextUtility.java @@ -0,0 +1,81 @@ +package org.gcube.accounting.insert.storage.utils; + +import org.gcube.common.authorization.client.Constants; +import org.gcube.common.authorization.client.exceptions.ObjectNotFound; +import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.ClientType; +import org.gcube.common.authorization.library.provider.AuthorizationProvider; +import org.gcube.common.authorization.library.provider.ClientInfo; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.authorization.library.utils.Caller; +import org.gcube.common.scope.api.ScopeProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class ContextUtility { + + private static Logger logger = LoggerFactory.getLogger(ContextUtility.class); + + public static void setContext(String token) throws ObjectNotFound, Exception { + SecurityTokenProvider.instance.set(token); + AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); + ClientInfo clientInfo = authorizationEntry.getClientInfo(); + logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name()); + String qualifier = authorizationEntry.getQualifier(); + Caller caller = new Caller(clientInfo, qualifier); + AuthorizationProvider.instance.set(caller); + ScopeProvider.instance.set(getCurrentContext()); + } + + public static String getCurrentContext() { + try { + String token = SecurityTokenProvider.instance.get(); + return Constants.authorizationService().get(token).getContext(); + } catch(Exception e) { + String context = ScopeProvider.instance.get(); + if(context != null) { + return context; + } + throw new RuntimeException(e); + } + } + + public static ClientInfo getClientInfo() { + try { + Caller caller = AuthorizationProvider.instance.get(); + if(caller != null) { + return caller.getClient(); + } else { + String token = SecurityTokenProvider.instance.get(); + AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); + return authorizationEntry.getClientInfo(); + } + } catch(Exception e) { + throw new RuntimeException(e); + } + } + + private static final String GET_USERNAME_ERROR = "Unable to retrieve user"; + + public static String getUsername() { + try { + return getClientInfo().getId(); + } catch(Exception e) { + logger.error(GET_USERNAME_ERROR); + throw new RuntimeException(GET_USERNAME_ERROR, e); + } + } + + public static boolean isApplication() { + try { + ClientInfo clientInfo = getClientInfo(); + return clientInfo.getType() == ClientType.EXTERNALSERVICE; + } catch(Exception e) { + throw new RuntimeException(e); + } + } + +} diff --git a/src/main/java/org/gcube/accounting/insert/storage/utils/DiscoveryListUser.java b/src/main/java/org/gcube/accounting/insert/storage/utils/DiscoveryListUser.java deleted file mode 100644 index 97912b8..0000000 --- a/src/main/java/org/gcube/accounting/insert/storage/utils/DiscoveryListUser.java +++ /dev/null @@ -1,96 +0,0 @@ - package org.gcube.accounting.insert.storage.utils; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.List; - -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.gson.Gson; - -/** - * Retrieves the list user form the base url of the social-networking service in - * the scope provided - * - * @author Alessandro Pieve at ISTI-CNR (alessandro.pieve@isti.cnr.it) - */ -public class DiscoveryListUser { - - private static Logger log = LoggerFactory.getLogger(DiscoveryListUser.class); - - private List listUser = null; - - public DiscoveryListUser(String context, String urlService) { - - try { - String token = SecurityTokenProvider.instance.get(); - DiscoveryServiceListUser discoveryList = new DiscoveryServiceListUser(context); - if (urlService == null) { - urlService = discoveryList.getBasePath() + "=" + token; - } - log.debug("service DiscoveryServiceListUser:" + urlService); - log.debug("scope:{} ,tokend:{}", context, token); - String data = getJSON(urlService); - log.debug("data read:{}", data); - ListUser msg = new Gson().fromJson(data, ListUser.class); - // TODO for debug limit a list user: - listUser = msg.getResult(); - } catch (Exception ex) { - log.error("DiscoveryListUserException:{}", ex); - throw ex; - } - } - - public List getListUser() { - return listUser; - } - - public String getJSON(String url) { - HttpURLConnection c = null; - try { - URL u = new URL(url); - c = (HttpURLConnection) u.openConnection(); - c.setRequestMethod("GET"); - c.setRequestProperty("Content-length", "0"); - HttpURLConnection.setFollowRedirects(true); - c.setUseCaches(false); - c.setAllowUserInteraction(false); - c.connect(); - int status = c.getResponseCode(); - log.debug("status:{}", status); - switch (status) { - case 200: - case 201: - BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); - StringBuilder sb = new StringBuilder(); - String line; - while ((line = br.readLine()) != null) { - sb.append(line + "\n"); - } - br.close(); - return sb.toString(); - } - - } catch (MalformedURLException ex) { - log.error("MalformedURLException:{}", ex); - - } catch (IOException ex) { - log.error("IOException:{}", ex); - } finally { - if (c != null) { - try { - c.disconnect(); - } catch (Exception ex) { - log.error("Exception:{}", ex); - } - } - } - return null; - } -} diff --git a/src/main/java/org/gcube/accounting/insert/storage/utils/DiscoveryServiceListUser.java b/src/main/java/org/gcube/accounting/insert/storage/utils/DiscoveryServiceListUser.java deleted file mode 100644 index 9af1a96..0000000 --- a/src/main/java/org/gcube/accounting/insert/storage/utils/DiscoveryServiceListUser.java +++ /dev/null @@ -1,104 +0,0 @@ -package org.gcube.accounting.insert.storage.utils; - - -import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; -import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; - -import java.util.Iterator; -import java.util.List; - -import org.gcube.common.resources.gcore.ServiceEndpoint; -import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; -import org.gcube.common.scope.api.ScopeProvider; -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; - - -/** - * Retrieves the base url of the social-networking service in the scope provided - * @author Costantino Perciante at ISTI-CNR - * (costantino.perciante@isti.cnr.it) - */ -public class DiscoveryServiceListUser { - - private String basePath = null; - - private static Logger logger = LoggerFactory.getLogger(DiscoveryServiceListUser.class); - private final static String RUNTIME_RESOURCE_NAME = "SocialNetworking"; - private final static String CATEGORY = "Portal"; - private final static String NAME_SERVICE = "2/users/get-all-usernames?gcube-token"; - - public DiscoveryServiceListUser(String context){ - - if(context == null || context.isEmpty()) - throw new IllegalArgumentException("A valid context is needed to discover the service"); - - - String oldContext = ScopeProvider.instance.get(); - ScopeProvider.instance.set(context); - - try{ - - List resources = getConfigurationFromIS(); - if (resources.size() == 0){ - logger.error("There is no Runtime Resource having name " + RUNTIME_RESOURCE_NAME +" and Category " + CATEGORY + " in this scope."); - throw new Exception("There is no Runtime Resource having name " + RUNTIME_RESOURCE_NAME +" and Category " + CATEGORY + " in this scope."); - } - else { - - for (ServiceEndpoint res : resources) { - - Iterator accessPointIterator = res.profile().accessPoints().iterator(); - - while (accessPointIterator.hasNext()) { - ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator - .next(); - // get base path - basePath = accessPoint.address(); - break; - } - } - - } - - }catch(Exception e){ - - logger.error("Unable to retrieve such service endpoint information!", e); - - }finally{ - - if(oldContext != null && !oldContext.equals(context)) - ScopeProvider.instance.set(oldContext); - - } - - logger.info("Found base path " + basePath + " for the service"); - - } - - /** - * Retrieve endpoints information from IS for DB - * @return list of endpoints for ckan database - * @throws Exception - */ - private List getConfigurationFromIS() throws Exception{ - - SimpleQuery query = queryFor(ServiceEndpoint.class); - query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_RESOURCE_NAME +"'"); - query.addCondition("$resource/Profile/Category/text() eq '"+ CATEGORY +"'"); - DiscoveryClient client = clientFor(ServiceEndpoint.class); - List toReturn = client.submit(query); - return toReturn; - - } - - /** - * Get the base path of the social networking service - * @return the base path - */ - public String getBasePath() { - return basePath+NAME_SERVICE; - } -} \ No newline at end of file diff --git a/src/main/java/org/gcube/accounting/insert/storage/utils/HTTPUtility.java b/src/main/java/org/gcube/accounting/insert/storage/utils/HTTPUtility.java new file mode 100644 index 0000000..e2c424a --- /dev/null +++ b/src/main/java/org/gcube/accounting/insert/storage/utils/HTTPUtility.java @@ -0,0 +1,46 @@ +package org.gcube.accounting.insert.storage.utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response.Status; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HTTPUtility { + + private static final Logger logger = LoggerFactory.getLogger(HTTPUtility.class); + + public static StringBuilder getStringBuilder(InputStream inputStream) throws IOException { + StringBuilder result = new StringBuilder(); + try(BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { + String line; + while((line = reader.readLine()) != null) { + result.append(line); + } + } + + return result; + } + + public static String getResultAsString(HttpURLConnection httpURLConnection) throws IOException { + int responseCode = httpURLConnection.getResponseCode(); + if(responseCode >= Status.BAD_REQUEST.getStatusCode()) { + Status status = Status.fromStatusCode(responseCode); + InputStream inputStream = httpURLConnection.getErrorStream(); + StringBuilder result = getStringBuilder(inputStream); + logger.trace(result.toString()); + throw new WebApplicationException(result.toString(), status); + } + InputStream inputStream = httpURLConnection.getInputStream(); + String ret = getStringBuilder(inputStream).toString(); + logger.trace("Got Respose is {}", ret); + return ret; + } + +} diff --git a/src/main/java/org/gcube/accounting/insert/storage/utils/ListUser.java b/src/main/java/org/gcube/accounting/insert/storage/utils/ListUser.java deleted file mode 100644 index b8b9784..0000000 --- a/src/main/java/org/gcube/accounting/insert/storage/utils/ListUser.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.gcube.accounting.insert.storage.utils; - -import java.util.List; -/** - * - * @author pieve - * - */ -public class ListUser { - /* - "success" : false, - "message" : "String index out of range: 5", - "result" : null - */ - private Boolean success; - private String message; - private List result; - public Boolean getSuccess() { - return success; - } - public void setSuccess(Boolean success) { - this.success = success; - } - public String getMessage() { - return message; - } - public void setMessage(String message) { - this.message = message; - } - public List getResult() { - return result; - } - public void setResult(List result) { - this.result = result; - } - - -} \ No newline at end of file diff --git a/src/main/java/org/gcube/accounting/insert/storage/utils/SocialService.java b/src/main/java/org/gcube/accounting/insert/storage/utils/SocialService.java new file mode 100644 index 0000000..4b22c49 --- /dev/null +++ b/src/main/java/org/gcube/accounting/insert/storage/utils/SocialService.java @@ -0,0 +1,94 @@ +package org.gcube.accounting.insert.storage.utils; + +import static org.gcube.resources.discovery.icclient.ICFactory.client; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.gcube.common.resources.gcore.GCoreEndpoint; +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; + +/** + * Discover the Social Networking Service in the Infrastructure. + * @author Luca Frosini (ISTI - CNR) + */ +public class SocialService { + + private static final String RESOURCE = "jersey-servlet"; + private static final String SERVICE_NAME = "SocialNetworking"; + private static final String SERVICE_CLASSE = "Portal"; + + private static Logger logger = LoggerFactory.getLogger(SocialService.class); + private String serviceBasePath; + + // Map + private static Map socialServicePerContext; + + static { + socialServicePerContext = new HashMap<>(); + } + + public static SocialService getSocialService() throws Exception { + String contex = ContextUtility.getCurrentContext(); + SocialService socialService = socialServicePerContext.get(contex); + if(socialService == null) { + socialService = new SocialService(); + socialServicePerContext.put(contex, socialService); + } + return socialService; + } + + /** + * Discover the gcore endpoint for the social networking service. + * @throws Exception the exception + */ + private SocialService() throws Exception { + getServiceBasePathViaGCoreEndpoint(); + } + + protected void getServiceBasePathViaGCoreEndpoint() throws Exception { + try { + SimpleQuery query = queryFor(GCoreEndpoint.class); + query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'", SERVICE_CLASSE)); + query.addCondition("$resource/Profile/DeploymentData/Status/text() eq 'ready'"); + query.addCondition(String.format("$resource/Profile/ServiceName/text() eq '%s'", SERVICE_NAME)); + query.setResult( + "$resource/Profile/AccessPoint/RunningInstanceInterfaces//Endpoint[@EntryName/string() eq \"" + + RESOURCE + "\"]/text()"); + + DiscoveryClient client = client(); + List endpoints = client.submit(query); + if(endpoints == null || endpoints.isEmpty()) { + throw new Exception("Cannot retrieve the GCoreEndpoint SERVICE_NAME: " + SERVICE_NAME + + ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + ContextUtility.getCurrentContext()); + } + + this.serviceBasePath = endpoints.get(0); + + if(serviceBasePath == null) + throw new Exception("Endpoint:" + RESOURCE + ", is null for SERVICE_NAME: " + SERVICE_NAME + + ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + ContextUtility.getCurrentContext()); + + serviceBasePath = serviceBasePath.endsWith("/") ? serviceBasePath : serviceBasePath + "/"; + + } catch(Exception e) { + String error = "An error occurred during GCoreEndpoint discovery, SERVICE_NAME: " + SERVICE_NAME + + ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + ContextUtility.getCurrentContext() + "."; + logger.error(error, e); + throw new Exception(error); + } + } + + /** + * @return the base path of the service + */ + public String getServiceBasePath() { + return serviceBasePath; + } + +} diff --git a/src/main/resources/META-INF/services/org.gcube.vremanagement.executor.plugin.PluginDeclaration b/src/main/resources/META-INF/services/org.gcube.vremanagement.executor.plugin.Plugin similarity index 77% rename from src/main/resources/META-INF/services/org.gcube.vremanagement.executor.plugin.PluginDeclaration rename to src/main/resources/META-INF/services/org.gcube.vremanagement.executor.plugin.Plugin index eb646a9..29d93e8 100644 --- a/src/main/resources/META-INF/services/org.gcube.vremanagement.executor.plugin.PluginDeclaration +++ b/src/main/resources/META-INF/services/org.gcube.vremanagement.executor.plugin.Plugin @@ -1 +1 @@ -org.gcube.accounting.insert.storage.plugin.AccountingInsertStoragePluginDeclaration \ No newline at end of file +org.gcube.accounting.insert.storage.plugin.AccountingInsertStoragePlugin \ No newline at end of file diff --git a/src/test/java/org/gcube/accounting/insert/storage/plugin/ContextTest.java b/src/test/java/org/gcube/accounting/insert/storage/plugin/ContextTest.java new file mode 100644 index 0000000..f7fd938 --- /dev/null +++ b/src/test/java/org/gcube/accounting/insert/storage/plugin/ContextTest.java @@ -0,0 +1,103 @@ +/** + * + */ +package org.gcube.accounting.insert.storage.plugin; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import org.gcube.common.authorization.client.Constants; +import org.gcube.common.authorization.client.exceptions.ObjectNotFound; +import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.provider.AuthorizationProvider; +import org.gcube.common.authorization.library.provider.ClientInfo; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.authorization.library.utils.Caller; +import org.gcube.common.scope.api.ScopeProvider; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + * + */ +public class ContextTest { + + private static final Logger logger = LoggerFactory.getLogger(ContextTest.class); + + protected static Properties properties; + protected static final String PROPERTIES_FILENAME = "token.properties"; + + public static final String PARENT_DEFAULT_TEST_SCOPE; + public static final String DEFAULT_TEST_SCOPE; + public static final String ALTERNATIVE_TEST_SCOPE; + + public static final String DEFAULT_TEST_SCOPE_ANOTHER_USER; + + static { + properties = new Properties(); + InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME); + + try { + // load the properties file + properties.load(input); + } catch(IOException e) { + throw new RuntimeException(e); + } + + // PARENT_DEFAULT_TEST_SCOPE = "/pred4s" + // DEFAULT_TEST_SCOPE_NAME = PARENT_DEFAULT_TEST_SCOPE + "/preprod"; + // ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE_NAME + "/preVRE"; + + + PARENT_DEFAULT_TEST_SCOPE = "/gcube"; + DEFAULT_TEST_SCOPE = PARENT_DEFAULT_TEST_SCOPE + "/devNext"; + ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE + "/NextNext"; + + DEFAULT_TEST_SCOPE_ANOTHER_USER = "lucio.lelii_" + DEFAULT_TEST_SCOPE; + + try { + setContextByName(DEFAULT_TEST_SCOPE); + } catch(Exception e) { + throw new RuntimeException(e); + } + } + + public static String getCurrentScope(String token) throws ObjectNotFound, Exception { + AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); + String context = authorizationEntry.getContext(); + logger.info("Context of token {} is {}", token, context); + return context; + } + + public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception { + String token = ContextTest.properties.getProperty(fullContextName); + setContext(token); + } + + private static void setContext(String token) throws ObjectNotFound, Exception { + SecurityTokenProvider.instance.set(token); + AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); + ClientInfo clientInfo = authorizationEntry.getClientInfo(); + logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name()); + String qualifier = authorizationEntry.getQualifier(); + Caller caller = new Caller(clientInfo, qualifier); + AuthorizationProvider.instance.set(caller); + ScopeProvider.instance.set(getCurrentScope(token)); + } + + @BeforeClass + public static void beforeClass() throws Exception { + setContextByName(DEFAULT_TEST_SCOPE); + } + + @AfterClass + public static void afterClass() throws Exception { + SecurityTokenProvider.instance.reset(); + ScopeProvider.instance.reset(); + } + +} diff --git a/src/test/java/org/gcube/accounting/insert/storage/plugin/Tests.java b/src/test/java/org/gcube/accounting/insert/storage/plugin/Tests.java index 69b017e..f0a63e5 100644 --- a/src/test/java/org/gcube/accounting/insert/storage/plugin/Tests.java +++ b/src/test/java/org/gcube/accounting/insert/storage/plugin/Tests.java @@ -3,14 +3,16 @@ package org.gcube.accounting.insert.storage.plugin; import java.util.HashMap; import java.util.Map; -import org.gcube.accounting.insert.storage.plugin.AccountingInsertStoragePlugin; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; -import org.gcube.common.scope.api.ScopeProvider; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + + +/** + * @author Luca Frosini (ISTI - CNR) + */ public class Tests { /** @@ -19,40 +21,19 @@ public class Tests { private static Logger logger = LoggerFactory.getLogger(Tests.class); @Before - public void beforeTest(){ - + public void beforeTest() { + } @Test public void testLaunch() throws Exception { - - //SecurityTokenProvider.instance.set("36501a0d-a205-4bf1-87ad-4c7185faa0d6-98187548"); - SecurityTokenProvider.instance.set("3acdde42-6883-4564-b3ba-69f6486f6fe0-98187548"); - //FOR DEBUG - String scopeDebug="/gcube"; - ScopeProvider.instance.set(scopeDebug); - // END FOR DEBUG - - Map inputs = new HashMap(); - //type aggregation - - /*optional*/ - //inputs.put("urlService","http://socialnetworking-d-d4s.d4science.org/social-networking-library-ws/rest/"); - - inputs.put("dataServiceClass","content-management"); - inputs.put("dataServiceName","storage-manager"); - inputs.put("dataServiceId","identifier"); - inputs.put("uri","MongoDb"); - inputs.put("dataType","STORAGE"); - - AccountingInsertStoragePlugin plugin = new AccountingInsertStoragePlugin(null); + Map inputs = new HashMap(); + AccountingInsertStoragePlugin plugin = new AccountingInsertStoragePlugin(); plugin.launch(inputs); - logger.debug("-------------- launch test finished"); - } - + @After - public void after(){ + public void after() { } } diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore new file mode 100644 index 0000000..d3e27b4 --- /dev/null +++ b/src/test/resources/.gitignore @@ -0,0 +1,2 @@ +*.gcubekey +/token.properties diff --git a/src/test/resources/META-INF/plugin.properties b/src/test/resources/META-INF/plugin.properties new file mode 100644 index 0000000..fc4c61b --- /dev/null +++ b/src/test/resources/META-INF/plugin.properties @@ -0,0 +1,4 @@ +groupId=org.gcube.accounting +artifactId=accounting-insert-storage-se-plugin +version=2.0.0-SNAPSHOT +description=Accounting Insert Storage Smart Executor Plugin \ No newline at end of file diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml new file mode 100644 index 0000000..6c1d98a --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,22 @@ + + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n + + + + + + + + + + + + + + \ No newline at end of file diff --git a/uberjar.xml b/uberjar.xml new file mode 100644 index 0000000..5348cf9 --- /dev/null +++ b/uberjar.xml @@ -0,0 +1,24 @@ + + uberjar + + jar + + ${file.separator} + + + target${file.separator}libs + ${file.separator} + true + 755 + + + target${file.separator}classes + ${file.separator} + true + 755 + + + \ No newline at end of file