/****Alberto&Loredana*****/
package eu.mosaic_cloud.sla;
import java.util.Vector;
import eu.mosaic_cloud.cloudlets.connectors.kvstore.IKvStoreConnector;
import eu.mosaic_cloud.cloudlets.connectors.kvstore.IKvStoreConnectorFactory;
import eu.mosaic_cloud.cloudlets.connectors.kvstore.KvStoreCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.AmqpQueueConsumeCallbackArguments;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueueConsumerConnector;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueueConsumerConnectorFactory;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueuePublisherConnector;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueuePublisherConnectorFactory;
import eu.mosaic_cloud.cloudlets.core.CallbackArguments;
import eu.mosaic_cloud.cloudlets.core.CloudletCallbackArguments;
import eu.mosaic_cloud.cloudlets.core.CloudletCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.core.GenericCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.core.ICallback;
import eu.mosaic_cloud.cloudlets.core.ICloudletController;
import eu.mosaic_cloud.cloudlets.tools.DefaultAmqpPublisherConnectorCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultAmqpQueueConsumerConnectorCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultCloudletCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultKvStoreConnectorCallback;
import eu.mosaic_cloud.platform.core.configuration.ConfigurationIdentifier;
import eu.mosaic_cloud.platform.core.configuration.IConfiguration;
import eu.mosaic_cloud.platform.core.utils.JsonDataEncoder;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;
import com.google.gson.Gson;
public class TradeStateSec {
public static final class KeyValueOptionAvailableCallback
extends DefaultKvStoreConnectorCallback<TradeStateSecContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet kvOptionAvailableStore destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet kvOptionAvailableStore initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded (final TradeStateSecContext context, final KvStoreCallbackCompletionArguments<String, Void> arguments)
{
this.logger.info("Set kvOptionAvailable : OK");
context.kvStatus.set(arguments.getKey(), "NegotiationClosed", null);
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded (final TradeStateSecContext context, final KvStoreCallbackCompletionArguments<String, Void> arguments)
{
this.logger.info("Get kvOptionAvailableStore Succeded: OK");
String kv=arguments.getKey();
String vl=(String)arguments.getValue();
this.logger.info("GetKey: "+kv);
this.logger.info("GetValue: "+vl);
return ICallback.SUCCESS;
}
}
public static final class KeyValueSlaCallback
extends DefaultKvStoreConnectorCallback<TradeStateSecContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet kvslastore destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet kvslastore initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded (final TradeStateSecContext context, final KvStoreCallbackCompletionArguments<String, Void> arguments)
{
this.logger.info("Set kvSlaStore : OK");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded (final TradeStateSecContext context, final KvStoreCallbackCompletionArguments<String, Void> arguments)
{
this.logger.info("Get kvSlaStore Succeded: OK");
String kv=arguments.getKey();
String vl=(String)arguments.getValue();
this.logger.info("GetKey: "+kv);
this.logger.info("GetValue: "+vl);
return ICallback.SUCCESS;
}
}
public static final class KeyValueStatusCallback
extends DefaultKvStoreConnectorCallback<TradeStateSecContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet kvStatusStore destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet kvStatusStore initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded (final TradeStateSecContext context, final KvStoreCallbackCompletionArguments<String, Void> arguments)
{
this.logger.info("Set KvStatus : OK");
this.logger.info("Current State: "+arguments.getValue()+"- Neg Id: "+arguments.getKey());
String id=arguments.getKey();
if(arguments.getValue().equals("Evaluating")){ //Case: State=Evaluating
String pub=context.messages.get(Integer.parseInt(id));//get message to publish to SecLAev, from Vector with appropriate id
context.seclaevpublisher.publish(pub, null);//push message to SecLAev
}
if(arguments.getValue().equals("OptionAvailable")){ //Case: State=OptionAvailable
context.kvStatus.get(arguments.getKey(), null);//get on kvStatus to verify Status
}
if(arguments.getValue().equals("NegotiationClosed")){ //Case: State=NegotiationClosed
this.logger.info("Negotiation id "+arguments.getKey()+" Ended.");
}
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded (final TradeStateSecContext context, final KvStoreCallbackCompletionArguments<String, Void> arguments)
{
this.logger.info("Get kvStatusStore Succeded: OK");
String kv=arguments.getKey();
String vl=null;
if (arguments.getValue()==null){
vl=new String("null");}
else {
vl=new String(arguments.getValue());}
this.logger.info("GetNegId: "+kv);
this.logger.info("GetState: "+vl);
//Check on State to do appropriate operation
if(vl.equals("null")){
this.logger.info("Case: Status=null");
context.kvStatus.set(kv, "Evaluating", null);//Change State
}
//Option are Available, setting results as a String in kvResult
if(vl.equals("OptionAvailable")){
this.logger.info("Case: OptionAvailable, ready to set Results in kvResult");
int i=context.result.length;
String result="";
String a="-----------------------------------PROPOSAL-----------------------------------\n";
for(int j=0;j<i;j++){
result= result+"\n"+a+context.result[j].getName()+"\n"+context.result[j].getSecLa()+"\n";
}//end "for"
this.logger.info("Results: "+result);
context.kvOption.set(kv, result);//Results ready for User
}//end "if"
return ICallback.SUCCESS;
}
}
public static final class AmqpUserCallback
extends DefaultAmqpQueueConsumerConnectorCallback<TradeStateSecContext, String, Void>
{
@Override
public CallbackCompletion<Void> acknowledgeSucceeded (final TradeStateSecContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> consume (final TradeStateSecContext context, final AmqpQueueConsumeCallbackArguments<String> arguments)
{
//User Request consume
this.logger.info("Entered TradeStateSec consume (User request)");
String data=new String(arguments.getMessage());
this.logger.info ("TradeStateSecCloudlet received message from SLAgw:"+data);
//push to SlaStore anycase
context.slastorepublisher.publish(data, null);
//json conversion in my StringWSAG class with setter and getter methods
context.objectwsag=context.gson.fromJson(data,StringWSAG.class);
this.logger.info("Conversion done: type: "+context.objectwsag.getType()+" Id: "+context.objectwsag.getId());
String type=context.objectwsag.getType();
//Message id
String ID=context.objectwsag.getId();
int id=Integer.parseInt(ID);
//Type Sumbit handling:evaluation process starts only with this type of messages
if(type.equals("Submit")){
this.logger.info("Type Sumbit message");
if(!context.keys.contains(ID)){
context.keys.add(ID);
context.messages.add(id, data);//Messages handling for multiple requests
this.logger.info("Got message, ready to check on State");
context.kvStatus.get(ID, null);//Check on State for this message
}else {this.logger.info("Bad request message");}//Same id messages are ignored
}else {this.logger.info("No Evaluation Process for this type");}//Different type messages don't need evaluating process
context.userconsumer.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet SLAgwConsumer destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet SLAgwConsumer initialized successfully.");
return ICallback.SUCCESS;
}
}
public static final class AmqpResultCallback
extends DefaultAmqpQueueConsumerConnectorCallback<TradeStateSecContext, StringQPT, Void>
{
@Override
public CallbackCompletion<Void> acknowledgeSucceeded (final TradeStateSecContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> consume (final TradeStateSecContext context, final AmqpQueueConsumeCallbackArguments<StringQPT> arguments)
{
//Message received from SecLAev with my evaluation results
this.logger.info("Entered Result queue consume from SecLaev");
context.msg=arguments.getMessage();
//message contains results list and the request id
String id=context.msg.getId();
context.result=context.msg.getlist();
this.logger.info ("TradeStateSecCloudlet received ProviderSortedList from SecLAevaluation:");
for(int i=0;i<context.result.length;i++){
this.logger.info("Numero "+(i+1)+": Nome Provider: "+context.result[i].nome);
}
context.kvStatus.set(id, "OptionAvailable", null); //Change State
context.seclaevconsumer.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet SecLaEvConsumer destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet SecLaEvConsumer initialized successfully.");
return ICallback.SUCCESS;
}
}
public static final class AmqpSecLAevCallback
extends DefaultAmqpPublisherConnectorCallback<TradeStateSecContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet publisher to SecLAev destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet publisher to SecLAev initialized successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> publishSucceeded (final TradeStateSecContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
this.logger.info("Push to SecLaev: OK");
return ICallback.SUCCESS;
}
}
public static final class AmqpSlaStoreCallback
extends DefaultAmqpPublisherConnectorCallback<TradeStateSecContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("SecLAEvaluationCloudlet produttore destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final TradeStateSecContext context, final CallbackArguments arguments)
{
this.logger.info ("TradeStateSecCloudlet produttore initialized successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> publishSucceeded (final TradeStateSecContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
this.logger.info("Push to SlaStore: OK");
return ICallback.SUCCESS;
}
}
public static final class LifeCycleHandler
extends DefaultCloudletCallback<TradeStateSecContext>
{
@Override
public CallbackCompletion<Void> destroy (final TradeStateSecContext context, final CloudletCallbackArguments<TradeStateSecContext> arguments)
{
this.logger.info ("TradeStateSecCloudlet destroying...");
return context.seclaevpublisher.destroy ();
}
@Override
public CallbackCompletion<Void> destroySucceeded (final TradeStateSecContext context, final CloudletCallbackCompletionArguments<TradeStateSecContext> arguments)
{
this.logger.info ("TradeStateSecCloudlet destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initialize (final TradeStateSecContext context, final CloudletCallbackArguments<TradeStateSecContext> arguments)
{
this.logger.info ("TradeStateSecCloudlet initializing...");
context.cloudlet = arguments.getCloudlet ();
context.messages.setSize(400);
final IConfiguration configuration = context.cloudlet.getConfiguration ();
final IConfiguration configuration2 = context.cloudlet.getConfiguration ();
final IConfiguration configuration3 = context.cloudlet.getConfiguration ();
final IConfiguration configuration4 = context.cloudlet.getConfiguration ();
final IConfiguration kvOptionAvailableConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("kvoption"));
context.kvOption = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvOptionAvailableConfiguration, String.class, JsonDataEncoder.create(String.class), new KeyValueOptionAvailableCallback (), context);
final IConfiguration kvSlaConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("kvsla"));
context.kvSla = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvSlaConfiguration, String.class, JsonDataEncoder.create(String.class), new KeyValueSlaCallback (), context);
final IConfiguration kvStatusConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("kvstatus"));
context.kvStatus = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvStatusConfiguration, String.class, JsonDataEncoder.create(String.class), new KeyValueStatusCallback (), context);
final IConfiguration queueUserConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue.user"));
context.userconsumer = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueUserConfiguration, String.class, JsonDataEncoder.create(String.class), new AmqpUserCallback (), context);
final IConfiguration queueResultConfiguration = configuration2.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue.result"));
context.seclaevconsumer = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueResultConfiguration, StringQPT.class, JsonDataEncoder.create(StringQPT.class), new AmqpResultCallback (), context);
final IConfiguration queueSecLAevConfiguration = configuration3.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue.seclaev"));
context.seclaevpublisher = context.cloudlet.getConnectorFactory (IAmqpQueuePublisherConnectorFactory.class).create (queueSecLAevConfiguration, String.class, JsonDataEncoder.create(String.class), new AmqpSecLAevCallback (), context);
final IConfiguration queueSlaStoreConfiguration = configuration4.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue.slastore"));
context.slastorepublisher = context.cloudlet.getConnectorFactory (IAmqpQueuePublisherConnectorFactory.class).create (queueSlaStoreConfiguration, String.class, JsonDataEncoder.create(String.class), new AmqpSlaStoreCallback (), context);
return CallbackCompletion.createAndChained (context.kvOption.initialize(),context.kvStatus.initialize(),context.kvSla.initialize(),context.userconsumer.initialize (),context.seclaevconsumer.initialize(),context.seclaevpublisher.initialize(),context.slastorepublisher.initialize());
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final TradeStateSecContext context, final CloudletCallbackCompletionArguments<TradeStateSecContext> arguments)
{
this.logger.info ("TradeStateSecCloudlet initialized successfully.");
return ICallback.SUCCESS;
}
}
public static final class TradeStateSecContext
{
ICloudletController<TradeStateSecContext> cloudlet;
//kvstores
IKvStoreConnector<String, Void> kvOption;//store in cui inserire il risultato per l'utente
IKvStoreConnector<String, Void> kvSla;//store riguardante anche slastore
IKvStoreConnector<String, Void> kvStatus;//stato della negoziazione
//queues
IAmqpQueueConsumerConnector<String, Void> userconsumer;//consumatore da slagw, da valutare che tipo di dato è scambiato
IAmqpQueuePublisherConnector<String, Void> seclaevpublisher;//produttore verso seclaev, da valutare tipo di dato da inviare
IAmqpQueueConsumerConnector<StringQPT, Void> seclaevconsumer;//consumatore coda result da seclaev ,arriva una lista QPT
IAmqpQueuePublisherConnector<String, Void> slastorepublisher;//produttore verso slastore, da valutare tipo di dato da inviare
//Usage data
StringQPT msg;
StringWSAG objectwsag=new StringWSAG();
Gson gson=new Gson();
QPT result[];
QPT userchoice;
Vector<String> keys=new Vector<String>();
Vector<String> messages=new Vector<String>();
}
}