package eu.mosaic_cloud.sla;
import java.io.UnsupportedEncodingException;
import org.slf4j.Logger;
import java.io.*;
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.platform.core.utils.NullDataEncoder;
import eu.mosaic_cloud.platform.core.utils.PlainTextDataEncoder;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;
import eu.mosaic_cloud.tools.transcript.core.Transcript;
import com.google.gson.Gson;
public class SecLaEvaluation_dev10 {
public static final class KeyValueByteInterCallback
extends DefaultKvStoreConnectorCallback<SecLaEvaluationContext,String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final SecLaEvaluationContext context, final CallbackArguments arguments)
{
this.logger.info ("SecLaEvaluationCloudlet kvByteInterStore destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final SecLaEvaluationContext context, final CallbackArguments arguments)
{
this.logger.info ("SecLaEvaluationCloudlet kvByteInterStore initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded (final SecLaEvaluationContext context, final KvStoreCallbackCompletionArguments<String, Void> arguments)
{
this.logger.info("Set kvByteInterStore : OK");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded (final SecLaEvaluationContext context, final KvStoreCallbackCompletionArguments<String, Void> arguments)
{
this.logger.info("Get kvByteInterStore Succeded: OK");
return ICallback.SUCCESS;
}
}
public static final class KeyValueIdCallback
extends DefaultKvStoreConnectorCallback<SecLaEvaluationContext, InfoCSPs, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final SecLaEvaluationContext context, final CallbackArguments arguments)
{
this.logger.info ("SecLAEvaluationCloudlet IDstore destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final SecLaEvaluationContext context, final CallbackArguments arguments)
{
this.logger.info ("SecLAEvaluationCloudlet IDstore initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded (final SecLaEvaluationContext context, final KvStoreCallbackCompletionArguments<InfoCSPs, Void> arguments)
{
this.logger.info("Set : OK");
String kv=arguments.getKey();
this.logger.info("SetKey: "+kv);
context.kvStore.get(kv, null);
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded (final SecLaEvaluationContext context, final KvStoreCallbackCompletionArguments<InfoCSPs, Void> arguments)
{
this.logger.info("Get InfoCSP Succeded: OK");
String kv=arguments.getKey();
this.logger.info("GetKey(Provider): "+kv);
InfoCSPs vl=(InfoCSPs)arguments.getValue();
context.numel=vl.getSize();
context.list=new QPT[context.numel];
int i=0;
while(i<context.numel){
this.logger.info("Getting: "+vl.getCSP(i));
context.kvStore.get(vl.getCSP(i), null);
i++;
}
return ICallback.SUCCESS;
}
}
public static final class KeyValueCallback
extends DefaultKvStoreConnectorCallback<SecLaEvaluationContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final SecLaEvaluationContext context, final CallbackArguments arguments)
{
this.logger.info ("SecLAEvaluationCloudlet store destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final SecLaEvaluationContext context, final CallbackArguments arguments)
{
this.logger.info ("SecLAEvaluationCloudlet store initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded (final SecLaEvaluationContext context, final KvStoreCallbackCompletionArguments<String, Void> arguments)
{
this.logger.info("Set : OK");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded (final SecLaEvaluationContext context, final KvStoreCallbackCompletionArguments<String, Void> arguments)
{
this.logger.info("Get kvStar Succeded: OK");
String kv=arguments.getKey();
String vl=(String)arguments.getValue();
this.logger.info("GetKey: "+kv);
this.logger.info("GetValue: "+vl);
QPT local=new QPT();
local.setName(kv);
local.setSecLa(vl);
//local.setGSL(2+context.j);
context.list[context.j]=local;
this.logger.info("list element "+context.j+" : "+context.list[context.j].getName());
context.j++;
return ICallback.SUCCESS;
}
}
public static final class AmqpTradeStateCallback
extends DefaultAmqpQueueConsumerConnectorCallback<SecLaEvaluationContext, String, Void>
{
@Override
public CallbackCompletion<Void> acknowledgeSucceeded (final SecLaEvaluationContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> consume (final SecLaEvaluationContext context, final AmqpQueueConsumeCallbackArguments<String> arguments)
{
//prendo dalla coda Cagw il QPT user
this.logger.info("Entered consume");
String msg=arguments.getMessage();
this.logger.info ("SecLAEvaluationCloudlet received message: "+msg);
//StringQPT local=new StringQPT();
//local=context.gson.fromJson(msg, StringQPT.class);
StringWSAG objectwsag=context.gson.fromJson(msg,StringWSAG.class);//conversione json del messaggio in StringWSAG
String id=objectwsag.getId();//prelevo l'id del messaggio
String wsag=objectwsag.getWsag();//prelevo il wsag per far partire la valutazione
//FileOutputStream fout;
File file=new File("wsag.xml");
try {
DataOutputStream f = new DataOutputStream(new FileOutputStream(file));
String c=wsag;
f.write(c.getBytes());
BufferedReader r = new BufferedReader(new FileReader("wsag.xml"));
this.logger.info(r.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
QPT usermsg=context.hand.CreateQpt(file);//creo il QPT dal wsag
String resultb=usermsg.getSecLa();
/*
byte[] resultbyte=null;
try{ resultbyte=resultb.getBytes("ISO-8859-1");
}catch(UnsupportedEncodingException e){
//TODO Auto-generated catch block
e.printStackTrace();
}
*/
context.kvbyteinter.set(context.interfaceQPT, resultb, null);
QPT alist[]=new QPT[context.numel];//lista in cui ci saranno i risultati ordinati in seguito a OrderCSP
alist=context.hand.OrderCSP(context.list, usermsg,context.numel);
//QPT list2[]=new QPT[context.numel-1];//inizializzo una lista QPT[] per copiare i risultati e inviarli
String conclist="";
for(int i=0;i<alist.length;i++){
this.logger.info("QPT list saving element "+i+" : "+alist[i].getName());
conclist="\n"+conclist+alist[i].getName()+" Calculated GSL: "+alist[i].getGSL()+"\n";
}
/*
byte[] listbyte=null;
try{ listbyte=conclist.getBytes("ISO-8859-1");
}catch(UnsupportedEncodingException e){
//TODO Auto-generated catch block
e.printStackTrace();
}
*/
context.kvbyteinter.set(context.interfaceCSP, conclist, null);
StringQPT result=new StringQPT();//creo un oggetto StringQPT che contiene info sull'id e risultati relativi a quell'id
result.setId(id);
result.setlist(alist);
context.produttore.publish(result, null);
context.consumatore.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded (final SecLaEvaluationContext context, final CallbackArguments arguments)
{
this.logger.info ("SecLAEvaluationCloudlet consumer destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final SecLaEvaluationContext context, final CallbackArguments arguments)
{
this.logger.info ("SecLAEvaluationCloudlet consumer initialized successfully.");
return ICallback.SUCCESS;
}
}
public static final class AmqpResultCallback
extends DefaultAmqpPublisherConnectorCallback<SecLaEvaluationContext, StringQPT, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final SecLaEvaluationContext context, final CallbackArguments arguments)
{
this.logger.info ("SecLAEvaluationCloudlet produttore destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final SecLaEvaluationContext context, final CallbackArguments arguments)
{
this.logger.info ("SecLAEvaluationCloudlet produttore initialized successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> publishSucceeded (final SecLaEvaluationContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
this.logger.info("Push to TradeState: OK");
//context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
public static final class LifeCycleHandler
extends DefaultCloudletCallback<SecLaEvaluationContext>
{
@Override
public CallbackCompletion<Void> destroy (final SecLaEvaluationContext context, final CloudletCallbackArguments<SecLaEvaluationContext> arguments)
{
this.logger.info ("SecLAEvaluationCloudlet destroying...");
return context.produttore.destroy ();
}
@Override
public CallbackCompletion<Void> destroySucceeded (final SecLaEvaluationContext context, final CloudletCallbackCompletionArguments<SecLaEvaluationContext> arguments)
{
this.logger.info ("SecLAEvaluationCloudlet destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initialize (final SecLaEvaluationContext context, final CloudletCallbackArguments<SecLaEvaluationContext> arguments)
{
this.logger.info ("SecLAEvaluationCloudlet initializing...");
context.cloudlet = arguments.getCloudlet ();
final IConfiguration configuration = context.cloudlet.getConfiguration ();
final IConfiguration configuration2 = context.cloudlet.getConfiguration ();
final IConfiguration kvConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("repstar"));
context.kvStore = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvConfiguration, String.class, JsonDataEncoder.create(String.class), new KeyValueCallback (), context);
final IConfiguration kvIdConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("repidstore"));
context.kvIdStore = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvIdConfiguration, InfoCSPs.class, JsonDataEncoder.create(InfoCSPs.class), new KeyValueIdCallback (), context);
final IConfiguration kvByteInterConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("kvbyteinter"));
context.kvbyteinter = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvByteInterConfiguration, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new KeyValueByteInterCallback (), context);
final IConfiguration queueTradeStateConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue.tradestate"));
context.consumatore = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueTradeStateConfiguration, String.class, JsonDataEncoder.create(String.class), new AmqpTradeStateCallback (), context);
final IConfiguration queueResultConfiguration = configuration2.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue.result"));
context.produttore = context.cloudlet.getConnectorFactory (IAmqpQueuePublisherConnectorFactory.class).create (queueResultConfiguration, StringQPT.class, JsonDataEncoder.create(StringQPT.class), new AmqpResultCallback (), context);
return CallbackCompletion.createAndChained (context.kvbyteinter.initialize(),context.kvIdStore.initialize(),context.kvStore.initialize(),context.consumatore.initialize (),context.produttore.initialize());
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final SecLaEvaluationContext context, final CloudletCallbackCompletionArguments<SecLaEvaluationContext> arguments)
{
this.logger.info ("SecLAEvaluationCloudlet initialized successfully.");
context.kvIdStore.get(context.Id, null);
return ICallback.SUCCESS;
}
}
public static final class Handling {
int numel;
Transcript transcript;
Logger logger;
public Handling(){
this.transcript=Transcript.create(this,true);
this.logger=this.transcript.adaptAs(Logger.class);
}
//Create a QPT object from entire message received from TradeStateSec
public QPT CreateQpt (File UserSelect){
this.logger.info("Creating Qpt..");
ExtractionWithSAX object = new ExtractionWithSAX();
String QPTUser = "";
QPTUser = object.extractionQPTUser(UserSelect);
this.logger.info("Created QPTUser " + "\n");
this.logger.info(QPTUser);
QPT qptuser=new QPT();
qptuser.setName("QPTUser");
qptuser.setSecLa(QPTUser);
return qptuser;
}
public double Evaluate (QPT csp,QPT user){
//per ogni qpt csp dovrebbe essere generato un numero dato dal confronto con quello user, dato dalla media di tutti i valori interi della tabella dei valori numerici
this.logger.info("Evaluating..");
Evaluation obj = new Evaluation();
double value = obj.evaluatePolicy(csp.getSecLa(), user.getSecLa());
//this.logger.info("valore "+csp.getGSL());
return value;
}
public QPT[] OrderCSP (QPT list[],QPT user,int num){
this.numel=num;
this.logger.info("Number of elements="+num);
this.logger.info("GetName user : "+user.getName());
//this.logger.info("GetConf user: "+user.getGSL()); // Conf da cambiare con GSL e dichiararlo float
double res; //valore da cambiare in float
this.logger.info("Ordering..");
for(int i=0;i<num;i++){
this.logger.info("GetName provider: "+list[i].getName());
//this.logger.info("GetGSL provider: "+list.get(i).getGSL());
res=Evaluate(list[i],user);
list[i].setGSL(res);
this.logger.info("Calculated GSL value: "+res+" for: "+list[i].getName());
}
sort(list);
return list;
}
public void sort(QPT list[]){
boolean swapped = true;
int j = 0;
QPT tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < numel - j; i++) {
if (list[i].getGSL() > list[i+1].getGSL()) {
tmp = list[i];
list[i]=list[i+1];
list[i+1]=tmp;
swapped = true;
}
}
}
}
}
public static final class SecLaEvaluationContext
{
ICloudletController<SecLaEvaluationContext> cloudlet;
//Provider name-provider QPT Store
IKvStoreConnector<String, Void> kvStore;
//Provider name list Store
IKvStoreConnector<InfoCSPs, Void> kvIdStore;
IKvStoreConnector<String, Void> kvbyteinter;//kvstore a byte per interfaccia grafica
//Consumer queue from TradeStateSec
IAmqpQueueConsumerConnector<String, Void> consumatore;
//Publisher queue to TradeStateSec
IAmqpQueuePublisherConnector<StringQPT, Void> produttore;
//Usage data
String Id="Id";
Gson gson=new Gson();
QPT list[];
int j=0,numel;
//Evaluation class
Handling hand=new Handling();
String interfaceQPT="interfaceQpt";
String interfaceCSP="interfaceCsp";
}
}