//###
//# #%L
//# mosaic-mjades
//# %%
//# Copyright (C) 2010 - 2012 Seconda Università di Napoli
//#Authors: Massimiliano Rak, Giuseppe Aversano
//# %%
//# Licensed under the Apache License, Version 2.0 (the "License");
//# you may not use this file except in compliance with the License.
//# You may obtain a copy of the License at
//#
//# http://www.apache.org/licenses/LICENSE-2.0
//#
//# Unless required by applicable law or agreed to in writing, software
//# distributed under the License is distributed on an "AS IS" BASIS,
//# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//# See the License for the specific language governing permissions and
//# limitations under the License.
//# #L%
//###
package eu.mosaic_cloud.sunutilities;
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.NullDataEncoder;
import eu.mosaic_cloud.platform.core.utils.PlainTextDataEncoder;
import eu.mosaic_cloud.platform.core.utils.SerializedDataEncoder;
import eu.mosaic_cloud.sunutilities.ConsumerCountcharacters.KeyValueCallback;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;
import eu.mosaic_cloud.platform.core.utils.JsonDataEncoder;
import java.util.Vector;
import com.google.gson.Gson;
public class XmlAnalyzerCloudletKv
{
// Consumer dal client
public static final class AmqpConsumerCallback
extends DefaultAmqpQueueConsumerConnectorCallback<XmlAnalyzerCloudletKvContext, String, Void>
{
@Override
public CallbackCompletion<Void> consume (final XmlAnalyzerCloudletKvContext context, final AmqpQueueConsumeCallbackArguments<String> arguments)
{
final String data = arguments.getMessage ();
context.object=context.gson.fromJson(data,JsonObjectfromString.class);
this.logger.info("\nDato ricevuto da XmlAnalyzerCloudletKv:"+context.object.getParameters(1)+"\n");
context.Count=CountElements.count(context.object.getParameters(1));
context.da=new Dato(data,context.Count,context.iter);
this.logger.info("\nSet nel Kv store oggetto Dato\n");
context.kvStore.set(context.keybase, context.da, null);
context.iter=Integer.toString((Integer.parseInt(context.iter))+1);
context.consumer.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded (final XmlAnalyzerCloudletKvContext context, final CallbackArguments arguments)
{
this.logger.info ("XmlAnalyzerCloudletKv destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final XmlAnalyzerCloudletKvContext context, final CallbackArguments arguments)
{
this.logger.info ("XmlAnalyzerCloudletKv ConsumerClient initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> acknowledgeSucceeded (final XmlAnalyzerCloudletKvContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
// context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
//Push verso il WG
public static final class AmqpPublisherCallback
extends DefaultAmqpPublisherConnectorCallback<XmlAnalyzerCloudletKvContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final XmlAnalyzerCloudletKvContext context, final CallbackArguments arguments)
{
this.logger.info ("XmlAnalyzerCloudletKv publisher destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final XmlAnalyzerCloudletKvContext context, final CallbackArguments arguments)
{
this.logger.info ("XmlAnalyzerCloudletKv publisher initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> publishSucceeded (final XmlAnalyzerCloudletKvContext context, final GenericCallbackCompletionArguments<String> arguments)
{
this.logger.info ("XmlAnalyzerCloudletKv publisher push successfully");
this.logger.info("\nPublisher Valutazione OK\n");
//context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
//KV Store
/*
* Il mio kvstore callback deve implementare l'interfaccia IKeyValueAccessorCallback o estendere la classe DefaultKeyValueAccessorCallback
*
*
* Default key-value storage callback.
* Il Contesto della cloudlet
* Il tipo dei valori memorizzato nel kv store usando questo connettore
* Il tipo dei dati aggiuntivi
*/
public static final class KeyValueCallback
extends DefaultKvStoreConnectorCallback<XmlAnalyzerCloudletKvContext, Dato, Void>
{
/*
* Si noti che i metodi di callback prendono un argomento di tipo KeyValueCallbackArguments. Questo estende CallbackArguments
* e contiene i dati restituiti dalle operazioni in funzione di accesso o dei messaggi di errore se le operazioni non sono state completate normalmente.
*/
@Override
public CallbackCompletion<Void> destroySucceeded (final XmlAnalyzerCloudletKvContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResult destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final XmlAnalyzerCloudletKvContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResult initialized successfully");
return ICallback.SUCCESS;
}
/*
* Gli argomenti dei metodi del cloudlet callback per la operazioni sui kvstore.
* Il tipo dei dati memorizzati o prelevati nel kv store.
* Il tipo dei dati aggiuntivi.
*/
public CallbackCompletion<Void> setSucceeded(final XmlAnalyzerCloudletKvContext context, final KvStoreCallbackCompletionArguments<Dato,Void> arguments) {
this.logger.info("\n\nSet nel KV Store Contenente i Risultati(Xml,Ntag) memorizzati Succeeded\n\n");
String k=arguments.getKey();
this.logger.info("\n\nKEY:"+k);
this.logger.info("\n\n");
Dato dat=(Dato)arguments.getValue();
this.logger.info("\n\nResultati Ntag:"+dat.getNtag());
this.logger.info("\n\nResultati nml:"+dat.getxml());
this.logger.info("\n\n");
//this.logger.info("\nPubblicazione Numero elementi contati:\n"+context.Count);
//context.publisher.publish(context.Count, null);
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> deleteSucceeded (final XmlAnalyzerCloudletKvContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResult store delete successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded(final XmlAnalyzerCloudletKvContext context,final KvStoreCallbackCompletionArguments<Dato,Void> arguments) {
this.logger.info("\n\nGet nel KV Store Contenente i Risultati(Xml,Ntag) memorizzati Succeeded\n\n");
String key =(String) arguments.getKey();
Dato dat1=(Dato)arguments.getValue();
this.logger.info("\n\nResultati Ntag:"+dat1.getNtag());
this.logger.info("\n\nResultati nml:"+dat1.getxml());
this.logger.info("\n\n");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> ungetSucceeded (final XmlAnalyzerCloudletKvContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResult ungetSucceeded.");
return ICallback.SUCCESS;
}
}
public static final class LifeCycleHandler
extends DefaultCloudletCallback<XmlAnalyzerCloudletKvContext>
{
@Override
public CallbackCompletion<Void> destroy (final XmlAnalyzerCloudletKvContext context, final CloudletCallbackArguments<XmlAnalyzerCloudletKvContext> arguments)
{
this.logger.info ("XmlAnalyzerCloudletKv destroying...");
return CallbackCompletion.createAndChained (context.consumer.destroy (),context.publisher.destroy(),context.kvStore.destroy());
}
@Override
public CallbackCompletion<Void> destroySucceeded (final XmlAnalyzerCloudletKvContext context, final CloudletCallbackCompletionArguments<XmlAnalyzerCloudletKvContext> arguments)
{
this.logger.info ("XmlAnalyzerCloudletKv destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initialize (final XmlAnalyzerCloudletKvContext context, final CloudletCallbackArguments<XmlAnalyzerCloudletKvContext> arguments)
{
this.logger.info ("XmlAnalyzerCloudletKv initializing...");
context.cloudlet = arguments.getCloudlet ();
final IConfiguration configuration = context.cloudlet.getConfiguration ();
final IConfiguration queueConfiguration2 = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue1.consumer"));
context.consumer = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueConfiguration2, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpConsumerCallback (), context);
final IConfiguration queueConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue2.publisher"));
context.publisher = context.cloudlet.getConnectorFactory (IAmqpQueuePublisherConnectorFactory.class).create (queueConfiguration, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpPublisherCallback (), context);
final IConfiguration kvConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("resultstore"));
context.kvStore = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvConfiguration, Dato.class, JsonDataEncoder.create(Dato.class) , new KeyValueCallback (), context);
return CallbackCompletion.createAndChained (context.consumer.initialize (),context.publisher.initialize (),context.kvStore.initialize());
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final XmlAnalyzerCloudletKvContext context, final CloudletCallbackCompletionArguments<XmlAnalyzerCloudletKvContext> arguments)
{
this.logger.info ("XmlAnalyzerCloudletKv initialized successfully.");
context.object=new JsonObjectfromString();
context.gson=new Gson();
return ICallback.SUCCESS;
}
}
public static final class XmlAnalyzerCloudletKvContext
{
ICloudletController<XmlAnalyzerCloudletKvContext> cloudlet;
IAmqpQueueConsumerConnector<String, Void> consumer;
IAmqpQueuePublisherConnector<String, Void> publisher;
IKvStoreConnector<Dato, Void> kvStore;
Dato da;
String Count="0";
String iter="1";
String keybase="2";
public Gson gson;
public JsonObjectfromString object;
}
}