refs #2112: Port SmartExecutor to be compliant with new Authorization

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

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor@124010 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-02-09 16:33:06 +00:00
parent 74806f6c2d
commit c1d3e71f41
11 changed files with 74 additions and 98 deletions

View File

@ -46,7 +46,7 @@ public class SmartExecutorInitializator implements ApplicationManager {
public static final long JOIN_TIMEOUT = 1000;
public static String getScope(){
public static String getScopeFromToken(){
String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry;
try {
@ -65,14 +65,14 @@ public class SmartExecutorInitializator implements ApplicationManager {
* @throws RegistryNotFoundException if the Registry is not found so the
* resource has not be published
*/
private static void publishScopedResource(Resource resource) throws Exception {
private static void publishResource(Resource resource) throws Exception {
StringWriter stringWriter = new StringWriter();
Resources.marshal(resource, stringWriter);
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
try {
logger.debug("Trying to publish to {}:\n{}", getScope(), stringWriter);
logger.debug("Trying to publish to {}:\n{}", getScopeFromToken(), stringWriter);
registryPublisher.create(resource);
} catch (Exception e) {
logger.error("The resource was not published", e);
@ -86,14 +86,14 @@ public class SmartExecutorInitializator implements ApplicationManager {
* @throws RegistryNotFoundException if the Registry is not found so the
* resource has not be published
*/
private static void unPublishScopedResource(Resource resource) throws Exception {
private static void unPublishResource(Resource resource) throws Exception {
//StringWriter stringWriter = new StringWriter();
//Resources.marshal(resource, stringWriter);
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
String id = resource.id();
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, getScope());
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, getScopeFromToken());
registryPublisher.remove(resource);
@ -224,11 +224,11 @@ public class SmartExecutorInitializator implements ApplicationManager {
for (ServiceEndpoint serviceEndpoint : serviceEndpoints) {
try {
logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}",
serviceEndpoint.id(), getScope());
unPublishScopedResource(serviceEndpoint);
serviceEndpoint.id(), getScopeFromToken());
unPublishResource(serviceEndpoint);
} catch(Exception e){
logger.debug("Exception tryng to unpublish the old ServiceEndpoint with ID {} from scope {}",
serviceEndpoint.id(), getScope(), e);
serviceEndpoint.id(), getScopeFromToken(), e);
}
}
}catch(Exception e){
@ -275,7 +275,7 @@ public class SmartExecutorInitializator implements ApplicationManager {
// Persistence to clean previous situation of a failure of HostingNode
try {
publishScopedResource(serviceEndpoint);
publishResource(serviceEndpoint);
} catch (RegistryNotFoundException e) {
logger.error("Unable to Create ServiceEndpoint. the Service will be aborted", e);
return;
@ -306,7 +306,7 @@ public class SmartExecutorInitializator implements ApplicationManager {
"\n-------------------------------------------------------\n"
+ "Smart Executor is Stopping on scope {}\n"
+ "-------------------------------------------------------",
getScope());
getScopeFromToken());
SmartExecutorScheduler.getInstance().stopAll();
@ -317,14 +317,14 @@ public class SmartExecutorInitializator implements ApplicationManager {
} catch (Exception e) {
logger.error("Unable to correctly close {} for scope {}",
SmartExecutorPersistenceConnector.class.getSimpleName(),
getScope(), e);
getScopeFromToken(), e);
}
logger.trace(
"\n-------------------------------------------------------\n"
+ "Smart Executor Stopped Successfully on scope {}\n"
+ "-------------------------------------------------------",
getScope());
getScopeFromToken());
}
}

View File

@ -48,36 +48,36 @@ public class JSONLaunchParameter extends LaunchParameter {
public JSONLaunchParameter(String pluginName, Map<String, String> pluginCapabilities, Map<String, Object> inputs) {
super(pluginName, pluginCapabilities, inputs);
this.scope = SmartExecutorInitializator.getScope();
this.scope = SmartExecutorInitializator.getScopeFromToken();
}
public JSONLaunchParameter(String pluginName, Map<String, Object> inputs, Scheduling scheduling) throws ParseException {
super(pluginName, inputs, scheduling);
this.scope = SmartExecutorInitializator.getScope();
this.scope = SmartExecutorInitializator.getScopeFromToken();
}
public JSONLaunchParameter(String pluginName, Map<String, String> pluginCapabilities, Map<String, Object> inputs, Scheduling scheduling) throws ParseException {
super(pluginName, pluginCapabilities, inputs, scheduling);
this.scope = SmartExecutorInitializator.getScope();
this.scope = SmartExecutorInitializator.getScopeFromToken();
}
@Deprecated
public JSONLaunchParameter(String pluginName, Map<String, Object> inputs, Scheduling scheduling, boolean persist) throws ParseException {
super(pluginName, inputs, scheduling, persist);
this.scope = SmartExecutorInitializator.getScope();
this.scope = SmartExecutorInitializator.getScopeFromToken();
}
@Deprecated
public JSONLaunchParameter(String pluginName, Map<String, String> pluginCapabilities, Map<String, Object> inputs, Scheduling scheduling, boolean persist) throws ParseException {
super(pluginName, pluginCapabilities, inputs, scheduling, persist);
this.scheduling = new JSONScheduling(scheduling);
this.scope = SmartExecutorInitializator.getScope();
this.scope = SmartExecutorInitializator.getScopeFromToken();
}
public JSONLaunchParameter(LaunchParameter parameter) throws ParseException {
super(parameter.getPluginName(), parameter.getPluginCapabilities(), parameter.getInputs(), parameter.getScheduling());
this.scheduling = new JSONScheduling(parameter.getScheduling());
this.scope = SmartExecutorInitializator.getScope();
this.scope = SmartExecutorInitializator.getScopeFromToken();
}
public JSONLaunchParameter(JSONObject jsonObject) throws JSONException, ParseException, ScopeNotMatchException {
@ -113,7 +113,7 @@ public class JSONLaunchParameter extends LaunchParameter {
this.usedBy = jsonObject.getString(USED_BY);
}
this.scope = SmartExecutorInitializator.getScope();
this.scope = SmartExecutorInitializator.getScopeFromToken();
if(jsonObject.has(SCOPE)){
String jsonScope = jsonObject.getString(SCOPE);
if(jsonScope.compareTo(scope)!=0){

View File

@ -126,7 +126,7 @@ public class SmartExecutorPersistenceConfiguration {
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
if(serviceEndpoints.size()>1){
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Name/text() eq '%s'", TARGET_SCOPE));
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", SmartExecutorInitializator.getScope()));
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", SmartExecutorInitializator.getScopeFromToken()));
serviceEndpoints = client.submit(query);
}
return serviceEndpoints.get(0);

View File

@ -43,12 +43,12 @@ public abstract class SmartExecutorPersistenceFactory {
*/
public static synchronized SmartExecutorPersistenceConnector getPersistenceConnector() throws Exception {
SmartExecutorPersistenceConnector persistence =
getPersistenceConnector(SmartExecutorInitializator.getScope());
getPersistenceConnector(SmartExecutorInitializator.getScopeFromToken());
if(persistence==null){
logger.trace("Retrieving {} for scope {} not found on internal {}. Intializing it.",
SmartExecutorPersistenceConnector.class.getSimpleName(),
SmartExecutorInitializator.getScope(),
SmartExecutorInitializator.getScopeFromToken(),
Map.class.getSimpleName());
String className = CouchDBPersistenceConnector.class.getSimpleName();
@ -56,7 +56,7 @@ public abstract class SmartExecutorPersistenceFactory {
new SmartExecutorPersistenceConfiguration(className);
persistence = new CouchDBPersistenceConnector(configuration);
persistenceConnectors.put(SmartExecutorInitializator.getScope(),
persistenceConnectors.put(SmartExecutorInitializator.getScopeFromToken(),
persistence);
}
@ -65,10 +65,10 @@ public abstract class SmartExecutorPersistenceFactory {
public static synchronized void closePersistenceConnector() throws Exception {
SmartExecutorPersistenceConnector persistence =
getPersistenceConnector(SmartExecutorInitializator.getScope());
getPersistenceConnector(SmartExecutorInitializator.getScopeFromToken());
if(persistence!=null){
persistence.close();
persistenceConnectors.remove(SmartExecutorInitializator.getScope());
persistenceConnectors.remove(SmartExecutorInitializator.getScopeFromToken());
}
}

View File

@ -205,7 +205,7 @@ public class CouchDBPersistenceConnector extends SmartExecutorPersistenceConnect
ViewQuery query = new ViewQuery().designDocId(String.format("%s%s", MAP_REDUCE__DESIGN, PLUGIN_STATE_DOCUMENT));
String scope = SmartExecutorInitializator.getScope();
String scope = SmartExecutorInitializator.getScopeFromToken();
ArrayNode startKey = new ObjectMapper().createArrayNode();
startKey.add(scope);
ArrayNode endKey = new ObjectMapper().createArrayNode();
@ -273,7 +273,7 @@ public class CouchDBPersistenceConnector extends SmartExecutorPersistenceConnect
ViewQuery query = new ViewQuery().designDocId(String.format("%s%s", MAP_REDUCE__DESIGN, SCHEDULED_TASKS_DOCUMENT));
query = query.viewName(ORPHAN_VIEW);
String scope = SmartExecutorInitializator.getScope();
String scope = SmartExecutorInitializator.getScopeFromToken();
ArrayNode startKey = new ObjectMapper().createArrayNode();
startKey.add(scope);
ArrayNode endKey = new ObjectMapper().createArrayNode();
@ -316,7 +316,7 @@ public class CouchDBPersistenceConnector extends SmartExecutorPersistenceConnect
JSONObject obj = jlp.toJSON();
obj.append(TYPE_JSON_FIELD, SCHEDULED_TASK_TYPE);
obj.append(USED_BY_FIELD, consumerID);
obj.append(ScheduledTaskConfiguration.SCOPE, SmartExecutorInitializator.getScope());
obj.append(ScheduledTaskConfiguration.SCOPE, SmartExecutorInitializator.getScopeFromToken());
createItem(obj, uuid.toString());
} catch (Exception e) {
logger.error("Error Adding Scheduled Task UUID : {}, Consumer : {}, LaunchParameter : {}",

View File

@ -76,7 +76,7 @@ public class PluginStateEvolutionObjectNode {
}
public static void addScope(ObjectNode objectNode){
objectNode.put(SCOPE_FIELD, SmartExecutorInitializator.getScope());
objectNode.put(SCOPE_FIELD, SmartExecutorInitializator.getScopeFromToken());
}
public static ObjectNode getObjectMapper(PluginStateEvolution pluginStateEvolution){

View File

@ -20,33 +20,6 @@ import org.junit.Test;
*/
public class SmartExecutorImplTest {
/*
@Test
public void createServiceEndpointTest() {
TODO Redesign this test
ServiceEndpoint serviceEndpoint = SmartExecutorInitalizator.createServiceEndpoint();
Profile profile = serviceEndpoint.profile();
Assert.assertEquals(SmartExecutor.WEB_SERVICE_SERVICE_NAME, profile.category());
Assert.assertEquals(SmartExecutor.class.getSimpleName(), profile.name());
Group<AccessPoint> accessPoints = profile.accessPoints();
Assert.assertEquals(1, accessPoints.size());
HelloWorldPluginDeclaration hwpd = new HelloWorldPluginDeclaration();
Map<String, String> supportedCapabilities = hwpd.getSupportedCapabilities();
for(AccessPoint accessPoint : accessPoints){
Assert.assertEquals(hwpd.getName(),accessPoint.name());
Group<Property> properties = accessPoint.properties();
Assert.assertEquals(supportedCapabilities.size(), properties.size());
for(Property property : properties){
String propertyName = property.name();
Assert.assertTrue(supportedCapabilities.containsKey(propertyName));
Assert.assertEquals(supportedCapabilities.get(propertyName), property.value());
}
}
}
*/
@Test
public void helloWorldTest() throws Exception{
Map<String, Object> inputs = new HashMap<String, Object>();

View File

@ -16,6 +16,8 @@ import org.gcube.vremanagement.executor.configuration.ScheduledTaskConfiguration
import org.gcube.vremanagement.executor.persistence.couchdb.CouchDBPersistenceConnector;
import org.gcube.vremanagement.executor.plugin.PluginState;
import org.gcube.vremanagement.executor.plugin.PluginStateEvolution;
import org.gcube.vremanagement.executor.pluginmanager.TestUtility;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -31,10 +33,14 @@ public class SmartExecutorPersistenceConnectorTest {
private static Logger logger = LoggerFactory.getLogger(SmartExecutorPersistenceConnectorTest.class);
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set("82a84741-debe-4c90-a907-c429c8272071");
public void bfore(){
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
}
@After
public void after(){
SecurityTokenProvider.instance.reset();
}
@Test
public void getConnectionTest() throws Exception {
@ -78,7 +84,6 @@ public class SmartExecutorPersistenceConnectorTest {
List<LaunchParameter> lc = stc.getAvailableScheduledTasks();
logger.debug("Available Scheduled Tasks : {}", lc);
}

View File

@ -16,9 +16,9 @@ import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConn
import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFactory;
import org.gcube.vremanagement.executor.plugin.PluginState;
import org.gcube.vremanagement.executor.plugin.PluginStateNotification;
import org.junit.AfterClass;
import org.junit.After;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -28,22 +28,24 @@ import org.slf4j.LoggerFactory;
*
*/
public class RunnablePluginTest {
private static Logger logger = LoggerFactory.getLogger(SmartExecutorSchedulerTest.class);
@BeforeClass
public static void prepare() throws Exception{
try {
SecurityTokenProvider.instance.set("82a84741-debe-4c90-a907-c429c8272071");
SmartExecutorPersistenceFactory.getPersistenceConnector();
} catch (Exception e) {
logger.error("Unable to Create JDBCPersistenceConnector");
throw e;
}
@Before
public void before() throws Exception {
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
SmartExecutorPersistenceFactory.getPersistenceConnector();
}
@After
public void after() throws Exception {
SecurityTokenProvider.instance.reset();
SmartExecutorPersistenceFactory.getPersistenceConnector().close();
}
@Test
public void launchNullInputsTest() throws Exception {
System.out.println("Testing Null inputs");
logger.debug("Testing Null inputs");
UUID uuid = UUID.randomUUID();
HelloWorldPluginDeclaration hwpd = new HelloWorldPluginDeclaration();
SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory.getPersistenceConnector();
@ -60,7 +62,7 @@ public class RunnablePluginTest {
@Test
public void launchEmptyInputsTest() throws Exception {
System.out.println("Testing Empty inputs");
logger.debug("Testing Empty inputs");
Map<String, Object> inputs = new HashMap<String, Object>();
UUID uuid = UUID.randomUUID();
HelloWorldPluginDeclaration hwpd = new HelloWorldPluginDeclaration();
@ -80,7 +82,7 @@ public class RunnablePluginTest {
@Test
public void launchValidInputsTest() throws Exception {
System.out.println("Testing Some inputs");
logger.debug("Testing Some inputs");
Map<String, Object> inputs = new HashMap<String, Object>();
inputs.put("Test", "Test");
long sleepTime = 10000;
@ -111,9 +113,4 @@ public class RunnablePluginTest {
}
@AfterClass
public static void finish() throws Exception{
SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory.getPersistenceConnector();
persistenceConnector.close();
}
}

View File

@ -39,29 +39,16 @@ public class SmartExecutorSchedulerTest {
public static SmartExecutorPersistenceConnector pc;
@BeforeClass
public static void prepare() throws Exception{
try {
SecurityTokenProvider.instance.set("82a84741-debe-4c90-a907-c429c8272071");
/*
pc = new JDBCPersistenceConnector(".");
SmartExecutorPersistenceConnector.setPersistenceConnector(pc);
*/
pc = SmartExecutorPersistenceFactory.getPersistenceConnector();
} catch (Exception e) {
logger.error("Unable to Create SmartExecutorPersistenceConnector");
throw e;
}
@BeforeClass
public static void before() throws Exception {
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
pc = SmartExecutorPersistenceFactory.getPersistenceConnector();
}
@AfterClass
public static void close() throws Exception{
try {
SmartExecutorPersistenceFactory.closePersistenceConnector();
} catch (Exception e) {
logger.error("Unable to Close JDBCPersistenceConnector");
throw e;
}
public static void after() throws Exception {
pc.close();
SecurityTokenProvider.instance.reset();
}
public UUID scheduleTest(Scheduling scheduling, Long sleepTime) throws Exception {

View File

@ -0,0 +1,14 @@
/**
*
*/
package org.gcube.vremanagement.executor.pluginmanager;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class TestUtility {
public static final String TOKEN = "";
}