added voRemove method on RegistryPublisher to make only a remove for VO

This commit is contained in:
lucio 2020-04-07 10:57:15 +02:00
parent 1d01f50e29
commit a00b163f3f
11 changed files with 128 additions and 49 deletions

View File

@ -1,6 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -21,7 +21,8 @@
<groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId>
<version>1.3.3-SNAPSHOT</version>
<version>1.4.0-SNAPSHOT</version>
<dependencies>

View File

@ -19,8 +19,9 @@ public class ResourceMediator {
public static void setScope(Resource resource, String scope){
resource.scopes().add(scope);
}
public static void removeScope(Resource resource, String scope){
resource.scopes().remove(scope);
public static void removeScope(Resource resource, String ... scopes){
for (String scope : scopes)
resource.scopes().remove(scope);
}
public static Resource cleanAllScopes(Resource resource){

View File

@ -48,6 +48,11 @@ public class AdvancedPublisher extends AdvancedPublisherCommonUtils implements R
@Override
public <T extends Resource> T vosUpdate(T resource) {
return publisher.vosUpdate(resource);
}
@Override
public <T extends Resource> T vosRemove(T resource, List<String> scopes) {
return publisher.vosRemove(resource, scopes);
}

View File

@ -48,5 +48,16 @@ public interface RegistryPublisher {
* @return the resource without the current scope if the remove operation has been successfully completed
*/
<T extends Resource> T remove(T resource);
/**
* The resource will be removed from the list of scopes.
* if the scope is the last scope in the resource, the profile will be deleted from IS else:
* if it is a VRE scope then the profile will be updated without the VRE scope;
* if it is a VO scope but there is another VRE scope, belong to the VO, defined in the resource then throw IllegalArgumentException;
* if it is a INFRA scope but there is another VRE or VO scope , belong to the INFRA, defined in the resource then throw IllegalArgumentException.
* @throws IllegalArgumentException if the current scope is not defined in the resource or if there is another VRE scope defined in the resource
* @return the resource without the current scope if the remove operation has been successfully completed
*/
<T extends Resource> T vosRemove(T resource, List<String> scopes);
}

View File

@ -1,12 +1,12 @@
package org.gcube.informationsystem.publisher;
import java.io.StringWriter;
import java.util.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.xml.ws.soap.SOAPFaultException;
@ -107,13 +107,13 @@ public class RegistryPublisherImpl implements RegistryPublisher {
String currentVO = Utils.getCurrentVO(currentScope);
if (currentVO != null){
RegistryStub stub = getRegistryStub();
createResource(resource, currentVO, stub);
vosScopes.remove(currentVO);
//in this case it is a root-vo scope so we need to create the resource only at root-vo level
createResource(resource, currentVO, stub);
vosScopes.remove(currentVO);
//in this case it is a root-vo scope so we need to create the resource only at root-vo level
}else{
RegistryStub stub = getRegistryStub();
createResource(resource, currentVO, stub);
return resource;
createResource(resource, currentVO, stub);
return resource;
}
}
// update the resource for each VO
@ -232,17 +232,17 @@ public class RegistryPublisherImpl implements RegistryPublisher {
HashSet<String> vosScopes = Utils.getInternalVOScopes(resource);
try{
if(currentScope != null){
// checking the current scope: if the operation fail in the current VO it will give an exception, if it fails in another VO no exception will be given
// checking the current scope: if the operation fail in the current VO it will raise an exception, if it fails in another VO no exception will be raised
String currentVO = Utils.getCurrentVO(currentScope);
if (currentVO != null){
ScopeProvider.instance.set(currentVO);
registryUpdate(resource, 0);
vosScopes.remove(currentVO);
// in this case it is a root-vo scope so we need to update the resource only at root-vo level
// in this case it is a root-vo scope
}else{
ScopeProvider.instance.set(currentScope);
registryUpdate(resource, 0);
return resource;
vosScopes.remove(currentScope);
}
}
@ -274,6 +274,64 @@ public class RegistryPublisherImpl implements RegistryPublisher {
return resource;
}
/**
* The resource will be removed from all the scopes
* if the scope is the last scope in the resource, the profile will be deleted from IS else
* if it is a VRE scope then the profile will be updated without the VRE scope,
* if it is a VO scope but there is another VRE scope, belong to the VO, defined in the resource then throw IllegalArgumentException
*
* @throws IllegalArgumentException if no service endpoints can be discovered or if there is another VRE scope defined in the resource
*/
public <T extends Resource> T vosRemove(T resource, List<String> scopes){
String currentScope=ScopeProvider.instance.get();
log.info(" remove resource with id : {} from scope: {}",resource.id(),currentScope);
ValidationUtils.valid("resource", resource);// helper that throws an IllegalArgumentException if resource are null
ValidationUtils.valid("scopes", currentScope);// helper that throws an IllegalArgumentException if scopes are null
validateScope(resource);
// the returned voScopes list is not used yet. it should be used if the update/remove operation will be done at VO level
// HashSet<String> vosScopes = updateResourceScopes(resource);
updateResourceScopes(resource);
try {
Resources.validate(resource);
} catch (Exception e) {
log.error("the resource is not valid", e);
throw new IllegalArgumentException("the resource is not valid", e);
}
log.info(" remove {} scope from resource {}",scopes,resource.id());
Set<String> voScopesBeforeRemove =Utils.getInternalVOScopes(resource);
ResourceMediator.removeScope(resource, scopes.toArray(new String[scopes.size()]));
Set<String> voScopesAfterRemove =Utils.getInternalVOScopes(resource);
voScopesBeforeRemove.removeAll(voScopesAfterRemove);
for (String scope: voScopesBeforeRemove)
try {
ScopeProvider.instance.set(scope);
log.info("remove from IS scope {}",currentScope);
registry.getStubs().remove(resource.id(), resource.type().toString());
} catch (Exception e) {
log.error("the resource can't be removed ", e);
throw new IllegalArgumentException("the resource can't be removed from scope "+currentScope, e);
} finally {
ScopeProvider.instance.set(currentScope);
}
Utils.getInternalVOScopes(resource);
// retrieves the scopes on resource and update it
// updateResource(resource, currentScope);
try{
vosUpdate(resource);
}catch(Exception e){
log.error("exception message: "+e.getMessage());
throw new RuntimeException(e.getMessage());
}
return resource;
}
/**
* The resource will be removed from current scope
* if the scope is the last scope in the resource, the profile will be deleted from IS else
@ -284,12 +342,12 @@ public class RegistryPublisherImpl implements RegistryPublisher {
*/
public <T extends Resource> T remove(T resource){
String currentScope=ScopeProvider.instance.get();
log.info(" remove resource with id : "+resource.id()+" from scope: "+currentScope);
log.info(" remove resource with id : {} from scope: {}",resource.id(),currentScope);
ValidationUtils.valid("resource", resource);// helper that throws an IllegalArgumentException if resource are null
ValidationUtils.valid("scopes", currentScope);// helper that throws an IllegalArgumentException if scopes are null
validateScope(resource);
// the returned voScopes list is not used yet. it should be used if the update/remove operation will be done at VO level
// HashSet<String> vosScopes = updateResourceScopes(resource);
// the returned voScopes list is not used yet. it should be used if the update/remove operation will be done at VO level
// HashSet<String> vosScopes = updateResourceScopes(resource);
updateResourceScopes(resource);
try {
Resources.validate(resource);
@ -297,7 +355,7 @@ public class RegistryPublisherImpl implements RegistryPublisher {
log.error("the resource is not valid", e);
throw new IllegalArgumentException("the resource is not valid", e);
}
log.info(" remove "+currentScope+" scope from resource "+resource.id());
log.info(" remove {} scope from resource {}",currentScope,resource.id());
ResourceMediator.removeScope(resource, currentScope);
// retrieves the scopes on resource and update it
// updateResource(resource, currentScope);
@ -312,6 +370,7 @@ public class RegistryPublisherImpl implements RegistryPublisher {
}
private void registryUpdate(Resource resource, int tries){
log.trace("try to update resource with id: "+resource.id()+" times "+(tries+1)+" on scope: "+ScopeProvider.instance.get());
try{
@ -358,14 +417,14 @@ public class RegistryPublisherImpl implements RegistryPublisher {
*/
private <T extends Resource> void updateResourceRemoveOperation(T resource, String currentScope) {
if(!isRemoveNeeded(resource, currentScope)){
// updateResource(resource, currentScope);
// v 1.3 (20190902) try to update the resource just at vo level
// updateResource(resource, currentScope);
// v 1.3 (20190902) try to update the resource just at vo level
vosUpdate(resource);
}else{ // remove the profile from IC
log.info("the resource have only the "+currentScope+" scope defined. Remove the resource "+resource.id()+" from IC");
log.info("the resource have only the {} scope defined. Remove the resource {} from IC", currentScope,resource.id());
// if the resource hasn't any scope, the resource will be removed
try {
log.debug("remove from IS scope "+currentScope);
log.info("remove from IS scope {}",currentScope);
registry.getStubs().remove(resource.id(), resource.type().toString());
} catch (Exception e) {
log.error("the resource can't be removed ", e);
@ -429,8 +488,7 @@ public class RegistryPublisherImpl implements RegistryPublisher {
ScopeProvider.instance.set(currentScope);
}
}
/*
private <T extends Resource> void updateResourceVOLevel(T resource, List <String> voScopes, String currentScope) {
int tries=0;
try{
@ -443,17 +501,17 @@ public class RegistryPublisherImpl implements RegistryPublisher {
ScopeProvider.instance.set(currentScope);
}
}
*/
private <T extends Resource> HashSet<String> updateResourceScopes(T resource) {
HashSet<String> vosScopes = Utils.getInternalVOScopes(resource);
// if the resource type is a RunningInstance or a HostingNode the check on the most recent resource is bypassed
// if the resource type is a RunningInstance or a HostingNode the check on the most recent resource is bypassed
if((resource.type().toString().equalsIgnoreCase("RunningInstance")) || (resource.type().toString().equalsIgnoreCase("GHN")) || (resource.type().toString().equalsIgnoreCase("GCoreEndpoint")) || (resource.type().toString().equalsIgnoreCase("HostingNode")))
return vosScopes;
//extract the scopes from the more recent resource found at vo level
List <String> latestScopesFound= Utils.setLatestInternalScopes(resource, vosScopes);
log.debug("latest scope found are "+latestScopesFound);
if((latestScopesFound == null) || (latestScopesFound.isEmpty())){
// if it is a new resource the latestScopesFound should be null, in this case the more recent scopes are the new scopes
// if it is a new resource the latestScopesFound should be null, in this case the more recent scopes are the new scopes
latestScopesFound= new ArrayList<String>(vosScopes.size());
for (String scope: vosScopes){
latestScopesFound.add(scope);
@ -466,7 +524,7 @@ public class RegistryPublisherImpl implements RegistryPublisher {
log.debug("[VOCREATE] scope added {}",scope);
ResourceMediator.setScope(resource, scope);
ScopeBean scopeBean = new ScopeBean(scope);
// check if the scope was already present inside the resource, if not, it will be added to the voScopes
// check if the scope was already present inside the resource, if not, it will be added to the voScopes
if((!scopeBean.is(Type.VRE)) && (!vosScopes.contains(scope))){
vosScopes.add(scope);
}

View File

@ -47,13 +47,7 @@ public class ScopedPublisherImpl implements ScopedPublisher{
@Override
public <T extends Resource> T remove(T resource, List<String> scopes) throws RegistryNotFoundException{
String currentScope=ScopeProvider.instance.get();
for(String scope : scopes){
ScopeProvider.instance.set(scope);
registryPublisher.remove(resource);
}
ScopeProvider.instance.set(currentScope);
return resource;
return registryPublisher.vosRemove(resource, scopes);
}
}

View File

@ -73,7 +73,7 @@ public class CollectorStubs implements RegistryStub {
if (connection.getResponseCode()!=200) throw new Exception("error updating resource "+connection.getResponseCode());
}catch (Exception e) {
log.error("error on remove",e);
log.error("error on update",e);
throw new RemoveException(e.getMessage());
}
}
@ -105,10 +105,10 @@ public class CollectorStubs implements RegistryStub {
connection = (HttpsURLConnection)url.openConnection();
else connection = (HttpURLConnection)url.openConnection();
if (SecurityTokenProvider.instance.get()!=null)
connection.setRequestProperty(TOKEN_HEADER_ENTRY,SecurityTokenProvider.instance.get());
else if (ScopeProvider.instance.get()!=null)
if (ScopeProvider.instance.get()!=null)
connection.setRequestProperty(SCOPE_HEADER_ENTRY,ScopeProvider.instance.get());
else if (SecurityTokenProvider.instance.get()!=null)
connection.setRequestProperty(TOKEN_HEADER_ENTRY,SecurityTokenProvider.instance.get());
else throw new RuntimeException("Collector requires authorization (via token or scope)");
connection.setRequestMethod(method);

View File

@ -27,7 +27,7 @@ public class RegistryStubs {
private RegistryCache cache = new RegistryCache(10);
private List<URI> endpoints;
private static final Logger log = LoggerFactory.getLogger(RegistryStubs.class);
private static final String XMLSTOREACCESS_SERVICE ="XMLStoreService";
@ -51,7 +51,7 @@ public class RegistryStubs {
.setResult("$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint[string(@EntryName) eq '"+RegistryConstants.service_entrypoint+"']/string()");
endpoints = client.submit(query);
if (endpoints.size()==0){
throw new IllegalArgumentException("No registry endpoint founded");
throw new IllegalArgumentException("No registry endpoint found");
}
// able/disable cache
cache.put(scope, endpoints);
@ -59,22 +59,31 @@ public class RegistryStubs {
return endpoints;
}
public RegistryStub getStubs() throws RegistryNotFoundException{
ServiceMap serviceMap = ServiceMap.instance;
if (serviceMap!=null && serviceMap.version().equals("2.0.0")) {
String endpoint = serviceMap.endpoint(XMLSTOREACCESS_SERVICE);
return new CollectorStubs(endpoint);
try {
@SuppressWarnings("unchecked")
Class<? extends RegistryStub> localClientclass =(Class<? extends RegistryStub>) Class.forName("org.gcube.informationsystem.collector.client.LocalPublisherClient", true, Thread.currentThread().getContextClassLoader());
log.info("using LocalPublisherClient, information collector specific publisher");
return localClientclass.newInstance();
}catch (Exception e) {
String endpoint = serviceMap.endpoint(XMLSTOREACCESS_SERVICE);
log.info("using REST COLLECTOR");
return new CollectorStubs(endpoint);
}
} else {
//use another method to cache epr
URI endpoint = getEndPoints().get(0);
log.debug("get stubs from endpoint: "+ endpoint);
log.info("getting REGISTRY STUBS stubs from endpoint: {}",endpoint);
return stubFor(RegistryConstants.registry).at(endpoint);
}
}
public RegistryStub getStubs(URI endpoint) throws RegistryNotFoundException{
log.debug("get stubs from endpoint: "+ endpoint);
log.debug("get stubs from endpoint: {}", endpoint);
return stubFor(RegistryConstants.registry).at(endpoint);
}

View File

@ -42,13 +42,11 @@ public class Utils {
*/
public static String getCurrentVO(String currentScope) {
ScopeBean scopeBean = new ScopeBean(currentScope);
String currentVO=null;
if(scopeBean.is(Type.VRE))
currentVO=scopeBean.enclosingScope().toString();
return scopeBean.enclosingScope().toString();
else if (scopeBean.is(Type.VO))
currentVO= currentScope;
return currentScope;
else return null;
return currentVO;
}
/**

View File

@ -74,7 +74,7 @@ public class ValidationUtils {
}
public static <T extends Resource> boolean isCompatibleScopeForRemove(T resource, String scope){
log.info("scope: "+scope+" check if update is needed inr resource: "+resource.id());
log.info("scope: {} check if update is needed inr resource: {}", scope,resource.id());
if(resource.scopes().size() == 0)
return true;
if(new ScopeBean(scope).is(Type.VRE)){