//###
//# #%L
//# mosaic-java-benchmarks
//# %%
//# Copyright (C) 2010 - 2012 Seconda Università di Napoli
//#Authors: Massimiliano Rak, Giuseppe Aversano, Loredana Liccardo
//# %%
//# 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.sla;
//package mosaic.cloudlet.SLA;
//import mosaic.cloudlet.core.CallbackArguments;
import java.util.*;
import eu.mosaic_cloud.cloudlets.core.CallbackArguments;
import eu.mosaic_cloud.cloudlets.core.CloudletCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.core.CloudletCallbackArguments;
import eu.mosaic_cloud.cloudlets.core.GenericCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.tools.DefaultCloudletCallback;
import eu.mosaic_cloud.cloudlets.core.ICallback;
import eu.mosaic_cloud.cloudlets.core.ICloudletController;
import eu.mosaic_cloud.platform.core.configuration.ConfigurationIdentifier;
import eu.mosaic_cloud.platform.core.configuration.IConfiguration;
//import mosaic.cloudlet.core.DefaultCloudletCallback;
//import mosaic.cloudlet.core.ICloudletController;
//import mosaic.cloudlet.resources.amqp.AmqpQueueConsumeCallbackArguments;
//import mosaic.cloudlet.resources.amqp.AmqpQueueConsumeMessage;
//import mosaic.cloudlet.resources.amqp.AmqpQueueConsumer;
//import mosaic.cloudlet.resources.amqp.AmqpQueuePublishCallbackArguments;
//import mosaic.cloudlet.resources.amqp.AmqpQueuePublisher;
//import mosaic.cloudlet.resources.amqp.DefaultAmqpConsumerCallback;
//import mosaic.cloudlet.resources.amqp.DefaultAmqpPublisherCallback;
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.tools.DefaultAmqpPublisherConnectorCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultAmqpQueueConsumerConnectorCallback;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAStoreCloudlet.AmqpConsumerCallback;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAStoreCloudlet.AmqpPublisherCallback;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAStoreCloudlet.SLAStoreCloudletContext;
import eu.mosaic_cloud.platform.core.utils.JsonDataEncoder;
import eu.mosaic_cloud.platform.core.utils.DataEncoder;
import eu.mosaic_cloud.platform.core.utils.SerializedDataEncoder;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;
//import mosaic.core.configuration.ConfigurationIdentifier;
//import mosaic.core.configuration.IConfiguration;
//import mosaic.core.log.MosaicLogger;
//import mosaic.core.utils.DataEncoder;
//import mosaic.core.utils.JsonDataEncoder;
//import mosaic.core.utils.PojoDataEncoder;
import java.lang.Object;
import com.google.gson.Gson;
public class CAgwEmulator {
public static final class LifeCycleHandler extends
DefaultCloudletCallback<CAgwEmulatorContext > {
public CallbackCompletion<Void> initialize(final CAgwEmulatorContext context, final CloudletCallbackArguments<CAgwEmulatorContext> arguments) {
this.logger.info("CloudletCAgwSimulato is being initialized.");
context.cloudlet = arguments.getCloudlet ();
final IConfiguration configuration = context.cloudlet.getConfiguration ();
final IConfiguration configuration2 = context.cloudlet.getConfiguration ();
/*ICloudletController<CAgwSimulatoState > cloudlet = arguments
.getCloudlet();
IConfiguration configuration = cloudlet.getConfiguration();
IConfiguration configuration2 = cloudlet.getConfiguration();*/
/*DataEncoder<String> encoder = new PojoDataEncoder<String>(
String.class); */
final IConfiguration queueConfiguration = configuration
.spliceConfiguration(ConfigurationIdentifier
.resolveAbsolute("queuecagw"));
context.consumer = context.cloudlet.getConnectorFactory
(IAmqpQueueConsumerConnectorFactory.class).create
(queueConfiguration, String.class, JsonDataEncoder.create
(String.class), new AmqpConsumerCallback (), context);
/*state.consumer = new AmqpQueueConsumer<CAgwSimulato.CAgwSimulatoState,String>(
queueConfiguration, cloudlet, String.class, encoder);*/
final IConfiguration queueConfigurationb = configuration2
.spliceConfiguration(ConfigurationIdentifier
.resolveAbsolute("queueresponce"));
context.publisher = context.cloudlet.getConnectorFactory
(IAmqpQueuePublisherConnectorFactory.class).create
(queueConfigurationb, String.class, JsonDataEncoder.create
(String.class), new AmqpPublisherCallback (), context);
/*state.publisher = new AmqpQueuePublisher<CAgwSimulato.CAgwSimulatoState,String>(
queueConfigurationb, cloudlet, String.class, encoder);*/
return CallbackCompletion.createAndChained (context.consumer.initialize (), context.publisher.initialize ());
}
public CallbackCompletion<Void> initializeSucceeded(final CAgwEmulatorContext context, final CloudletCallbackCompletionArguments<CAgwEmulatorContext> arguments) {
this.logger.info("CloudletCAgwSimulato initialized successfully.");
/*ICloudletController<CAgwSimulatoState > cloudlet = arguments
.getCloudlet();*/
//context.cloudlet = arguments.getCloudlet ();
/*cloudlet.initializeResource(state.consumer,
new AmqpConsumerCallback(), state);
cloudlet.initializeResource(state.publisher,
new AmqpPublisherCallback(), state);*/
context.object=new JsonObjectfromStringAction();
context.gson=new Gson();
context.ris.add(0, context.v1);
context.ris.add(1, context.v2);
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> destroy(final CAgwEmulatorContext context, final CloudletCallbackArguments<CAgwEmulatorContext> arguments) {
this.logger.info("CloudletCAgwSimulato is being destroyed.");
return CallbackCompletion.createAndChained (context.consumer.destroy (), context.publisher.destroy ());
}
public CallbackCompletion<Void> destroySucceeded(final CAgwEmulatorContext context, final CloudletCallbackCompletionArguments<CAgwEmulatorContext> arguments) {
this.logger.info(
"CloudletCAgwSimulato was destroyed successfully.");
return ICallback.SUCCESS;
}
}
public static final class AmqpConsumerCallback extends DefaultAmqpQueueConsumerConnectorCallback<CAgwEmulatorContext, String, Void>
{
public CallbackCompletion<Void> initializeSucceeded(final CAgwEmulatorContext context,
final CallbackArguments arguments) {
this.logger.info(
"CloudletCAgwSimulato Consumer initialized successfully.");
//state.consumer.register();
return ICallback.SUCCESS;
}
/*@Override
public void registerSucceeded(CAgwSimulatoState state,
CallbackArguments<CAgwSimulatoState > arguments) {
this.logger.info(
"CloudletCAgwSimulato Consumer registered successfully.");
}*/
/* @Override
public void unregisterSucceeded(CAgwSimulatoState state,
CallbackArguments<CAgwSimulatoState > arguments) {
this.logger.info(
"CloudletCAgwSimulato unregistered successfully.");
ICloudletController<CAgwSimulatoState > cloudlet = arguments
.getCloudlet();
// cloudlet.destroyResource(state.consumer, this);
}*/
@Override
public CallbackCompletion<Void> destroySucceeded(final CAgwEmulatorContext context,
final CallbackArguments arguments) {
this.logger.info(
"CloudletCAgwSimulato Consumer was destroyed successfully.");
// state.consumer = null;
// arguments.getCloudlet().destroy();
return ICallback.SUCCESS;
}
/*@Override
public void acknowledgeSucceeded(CAgwSimulatoState state,
CallbackArguments<CAgwSimulatoState > arguments) {
this.logger.info(
"acknowledgeSucceeded");
//state.consumer.unregister();
}*/
public CallbackCompletion<Void> consume(final CAgwEmulatorContext context, final AmqpQueueConsumeCallbackArguments<String> arguments){
/*AmqpQueueConsumeMessage<String> message = arguments.getMessage();
String data = message.getData();*/
final String data = arguments.getMessage ();
this.logger.info(
"CloudletCAgwSimulato Consumer received message from SLAstore "
+ data);
//System.out.println("\n\nMessage Ricevuto da TradeState:"+data);
this.logger.info("Message Ricevuto da TradeState: "+data);
//System.out.println("\n\n");
context.object=context.gson.fromJson(data,JsonObjectfromStringAction.class);
if(context.object.getAction().equals("CAcfps")){
context.step++;
if(context.step==2){
for(int i=0;i<context.ris.size();i++){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println("\nCAgw sta' trasmettendo:\n");
this.logger.info("CAgw sta' trasmettendo:");
//System.out.println("\n{'action':'CAresponse','id':'"+context.object.getId()+"','wsag':'"+context.ris.get(i)+"'}\n");
context.publisher.publish("{'action':'CAresponse','id':'"+context.object.getId()+"','wsag':'"+context.ris.get(i)+"'}", null);
}
}
}
if (context.object.getAction().equals("CAreserve")){
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println("\nCAgw stà trasmettendo:\n");
//System.out.println("\n{'action':'CAreserve','id':'"+context.object.getId()+"','wsag':'"+context.object.getWSAG()+"'}\n");
context.publisher.publish("{'action':'CAreserve','id':'"+context.object.getId()+"','wsag':'"+context.object.getWSAG()+"'}", null);
}
if (context.object.getAction().equals("CAsign")){
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println("\nCAgw stà trasmettendo:\n");
//System.out.println("\n{'action':'CAsigned','id':'"+context.object.getId()+"','wsag':'"+context.object.getWSAG()+"'}\n");
context.publisher.publish("{'action':'CAsigned','id':'"+context.object.getId()+"','wsag':'"+context.object.getWSAG()+"'}", null);
}
if (context.object.getAction().equals("CAterminate")){
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println("\nCAgw stà trasmettendo:\n");
//System.out.println("\n{'action':'CAterminated','id':'"+context.object.getId()+"','wsag':'"+context.object.getWSAG()+"'}\n");
context.publisher.publish("{'action':'CAterminated','id':'"+context.object.getId()+"','wsag':'"+context.object.getWSAG()+"'}", null);
}
//message.acknowledge();
context.consumer.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
}
public static final class AmqpPublisherCallback extends
DefaultAmqpPublisherConnectorCallback<CAgwEmulatorContext, String, Void> {
/*@Override
public void registerSucceeded(CAgwSimulatoState state,
CallbackArguments<CAgwSimulatoState > arguments) {
this.logger.info(
" CloudletCAgwSimulato Publisher registered successfully.");
}*/
/* @Override
public void unregisterSucceeded(CAgwSimulatoState state,
CallbackArguments<CAgwSimulatoState > arguments) {
this.logger.info(
"CloudletCAgwSimulato Publisher unregistered successfully.");
// if unregistered as publisher is successful then destroy resource
ICloudletController<CAgwSimulatoState > cloudlet = arguments
.getCloudlet();
//cloudlet.destroyResource(state.publisher, this);
}*/
@Override
public CallbackCompletion<Void> initializeSucceeded(final CAgwEmulatorContext context,
final CallbackArguments arguments) {
// if resource initialized successfully then just register as a
// publisher
//state.publisher.register();
this.logger.info(
"CloudletCAgwSimulato Publisher initialized successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded(final CAgwEmulatorContext context,
final CallbackArguments arguments) {
this.logger.info(
"CloudletCAgwSimulato Publisher was destroyed successfully.");
//state.publisher = null;
//arguments.getCloudlet().destroy();
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> publishSucceeded(final CAgwEmulatorContext context,
final GenericCallbackCompletionArguments<Void> arguments) {
this.logger.info(
"CloudletCAgwSimulato Publisher publish successfully.");
//System.out.println("\n\nCloudletCAgwSimulato Publisher publish successfully\n\n");
//state.publisher.unregister();
return ICallback.SUCCESS;
}
}
public static final class CAgwEmulatorContext {
ICloudletController<CAgwEmulatorContext> cloudlet;
IAmqpQueueConsumerConnector<String, Void> consumer;
IAmqpQueuePublisherConnector<String, Void> publisher;
/*AmqpQueuePublisher<CAgwSimulatoState , String> publisher;
AmqpQueueConsumer<CAgwSimulatoState , String> consumer;*/
Gson gson;
JsonObjectfromStringAction object;
public Vector<String> ris =new Vector <String>(40);
String v1="file_XML4";
String v2="file_XML5";
int step=0;
}
}