//###
//# #%L
//# mosaic-java-benchmarks
//# %%
//# 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.benchmarks;
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.PlainTextDataEncoder;
import eu.mosaic_cloud.platform.core.utils.SerializedDataEncoder;
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 ControllerCloudlet
{
// Consumer dal client
public static final class AmqpConsumerCallbackClient
extends DefaultAmqpQueueConsumerConnectorCallback<ControllerCloudletContext, String, Void>
{
@Override
public CallbackCompletion<Void> consume (final ControllerCloudletContext context, final AmqpQueueConsumeCallbackArguments<String> arguments)
{
final String data = arguments.getMessage ();
this.logger.info ("ConsumerControllClient received message", data);
this.logger.info("\nDato ricevuto dal Client:"+data+"\n");
context.object=context.gson.fromJson(data,JsonObjectfromString.class);
context.Request.add(Integer.parseInt(context.object.getId()),data);
context.kvStore.get(context.object.getId(),null);
context.consumer.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded (final ControllerCloudletContext context, final CallbackArguments arguments)
{
this.logger.info ("ResultCloudlet destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ControllerCloudletContext context, final CallbackArguments arguments)
{
this.logger.info ("ControllerCloudlet ConsumerClient initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> acknowledgeSucceeded (final ControllerCloudletContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
// context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
// Consumer dal WG
public static final class AmqpConsumerCallbackState
extends DefaultAmqpQueueConsumerConnectorCallback<ControllerCloudletContext, String, Void>
{
@Override
public CallbackCompletion<Void> consume (final ControllerCloudletContext context, final AmqpQueueConsumeCallbackArguments<String> arguments)
{
final String datawg = arguments.getMessage ();
this.logger.info ("ConsumerControllWG received message dal WG:", datawg);
this.logger.info("\nDato ricevuto dal WG:"+datawg+"\n");
context.objectwg=context.gson.fromJson(datawg,JsonObjectfromString.class);
this.logger.info("TIPO:"+context.objectwg.getType());
if (context.objectwg.getType().equals("Stopping")){
context.kvStore.set(context.objectwg.getId(),"Stopped",null);
context.publisherev.publish(datawg, null);
//context.consumerev.acknowledge (arguments.getToken ());
}
if (context.objectwg.getType().equals("Starting")){
context.kvStore.set(context.objectwg.getId(),"Running",null);
//context.consumerev.acknowledge (arguments.getToken ());
}
context.consumerev.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded (final ControllerCloudletContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerControllWG was destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ControllerCloudletContext context, final CallbackArguments arguments)
{
this.logger.info ("ControllerCloudlet ConsumerWG initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> acknowledgeSucceeded (final ControllerCloudletContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
// context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
//Push verso il WG
public static final class AmqpPublisherCallbackWG
extends DefaultAmqpPublisherConnectorCallback<ControllerCloudletContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final ControllerCloudletContext context, final CallbackArguments arguments)
{
this.logger.info ("ControllerCloudlet publisher WG destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ControllerCloudletContext context, final CallbackArguments arguments)
{
this.logger.info ("ControllerCloudlet publisher WG initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> publishSucceeded (final ControllerCloudletContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
this.logger.info ("PublisherControllWG publish successfully");
//context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
// Push verso il Result
public static final class AmqpPublisherCallbackResult
extends DefaultAmqpPublisherConnectorCallback<ControllerCloudletContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final ControllerCloudletContext context, final CallbackArguments arguments)
{
this.logger.info ("ControllerCloudlet publisher Result destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ControllerCloudletContext context, final CallbackArguments arguments)
{
this.logger.info ("ControllerCloudlet publisher Result initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> publishSucceeded (final ControllerCloudletContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
this.logger.info ("PublisherControllResult publish successfully.");
//context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
//KV
public static final class KeyValueCallbackresult
extends DefaultKvStoreConnectorCallback<ControllerCloudletContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final ControllerCloudletContext context, final CallbackArguments arguments)
{
this.logger.info ("ControllerCloudlet store destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ControllerCloudletContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueControll accessor initialized successfully");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded(final ControllerCloudletContext context, final KvStoreCallbackCompletionArguments<String,Void> arguments) {
this.logger.info("\n\nSet nel KV Store Contenente lo Stato memorizzati Succeeded\n\n");
String k=arguments.getKey();
this.logger.info("\n\nKEY:"+k);
this.logger.info("\n\n");
String value=(String)arguments.getValue();
this.logger.info("\n\nResult:"+value);
this.logger.info("\n\n");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> deleteSucceeded (final ControllerCloudletContext context, final CallbackArguments arguments)
{
this.logger.info ("ControllerCloudlet Store Result delete successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded(final ControllerCloudletContext context,final KvStoreCallbackCompletionArguments<String,Void> arguments) {
this.logger.info("\n\nGet nel KV Store Contenente lo Stato memorizzati Succeeded\n\n");
String key =(String) arguments.getKey();
String value=(String) arguments.getValue();
this.logger.info("\n Key:"+key);
this.logger.info("\n Stato:"+value);
int pos = Integer.parseInt(key);
String vall=context.Request.get(pos);
this.logger.info("\n\nMessaggio Ricevuto nel Get:"+vall);
context.object=context.gson.fromJson(vall,JsonObjectfromString.class);
if (((context.object.getType()).equals("Start")) && (value.equals("Ready"))){
context.kvStore.set(key, "Starting", null);
/* try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
context.publisher.publish(vall, null); // sulla coda del WG
}
if (((context.object.getType()).equals("Ready")) && (value.equals("Stopped"))){
context.kvStore.set(key, "Ready", null);
}
if (((context.object.getType()).equals("Ready")) && (value.equals("SettingUp"))){
context.kvStore.set(key, "Ready", null);
}
if (((context.object.getType()).equals("Stop")) && (value.equals("Running"))){
context.kvStore.set(key, "Stopping", null);
context.publisher.publish(vall, null); // sulla coda del WG
}
if (((context.object.getType()).equals("Plot")) && (value.equals("Stopped"))){
context.publisherev.publish(vall, null); // sulla coda del Result
}
if ((context.object.getType()).equals("Check")){
this.logger.info("Stato del Benchmark:"+value);
}
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> ungetSucceeded (final ControllerCloudletContext context, final CallbackArguments arguments)
{
this.logger.info ("ControllerCloudlet ungetSucceeded.");
return ICallback.SUCCESS;
}
}
public static final class LifeCycleHandler
extends DefaultCloudletCallback<ControllerCloudletContext>
{
@Override
public CallbackCompletion<Void> destroy (final ControllerCloudletContext context, final CloudletCallbackArguments<ControllerCloudletContext> arguments)
{
this.logger.info ("ControllerCloudlet destroying...");
return CallbackCompletion.createAndChained (context.kvStore.destroy (), context.consumer.destroy (),context.consumerev.destroy(),context.publisher.destroy(),context.publisherev.destroy());
}
@Override
public CallbackCompletion<Void> destroySucceeded (final ControllerCloudletContext context, final CloudletCallbackCompletionArguments<ControllerCloudletContext> arguments)
{
this.logger.info ("ControllerCloudlet destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initialize (final ControllerCloudletContext context, final CloudletCallbackArguments<ControllerCloudletContext> arguments)
{
this.logger.info ("ControllerCloudlet initializing...");
context.cloudlet = arguments.getCloudlet ();
final IConfiguration configuration = context.cloudlet.getConfiguration ();
final IConfiguration kvConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("statostore"));
context.kvStore = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvConfiguration, String.class, JsonDataEncoder.create (String.class), new KeyValueCallbackresult (), context);
final IConfiguration queueConfiguration2 = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queueclient.consumer"));
context.consumer = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueConfiguration2, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpConsumerCallbackClient (), context);
final IConfiguration queueConfiguration4 = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queuestate.consumer"));
context.consumerev = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueConfiguration4, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpConsumerCallbackState (), context);
final IConfiguration queueConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queuerequest.publisher"));
context.publisher = context.cloudlet.getConnectorFactory (IAmqpQueuePublisherConnectorFactory.class).create (queueConfiguration, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpPublisherCallbackWG (), context);
final IConfiguration queueConfiguration3 = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queueresult.publisher"));
context.publisherev = context.cloudlet.getConnectorFactory (IAmqpQueuePublisherConnectorFactory.class).create (queueConfiguration3, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpPublisherCallbackResult (), context);
return CallbackCompletion.createAndChained (context.kvStore.initialize (), context.consumer.initialize (),context.consumerev.initialize (),context.publisher.initialize (),context.publisherev.initialize ());
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ControllerCloudletContext context, final CloudletCallbackCompletionArguments<ControllerCloudletContext> arguments)
{
this.logger.info ("CloudletCloudlet initialized successfully.");
context.Request.setSize(400);
context.object=new JsonObjectfromString();
context.objectwg=new JsonObjectfromString();
context.gson=new Gson();
context.kvStore.set(context.vectorListKey,"SettingUp", null);
return ICallback.SUCCESS;
}
}
public static final class ControllerCloudletContext
{
ICloudletController<ControllerCloudletContext> cloudlet;
IAmqpQueueConsumerConnector<String, Void> consumer;
IAmqpQueueConsumerConnector<String, Void> consumerev;
IAmqpQueuePublisherConnector<String, Void> publisher;
IAmqpQueuePublisherConnector<String, Void> publisherev;
IKvStoreConnector<String, Void> kvStore;
Gson gson;
JsonObjectfromString object;
JsonObjectfromString objectwg;
String vectorListKey="2";
Vector<String> Request=new Vector<String>();
}
}