//###
//# #%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 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 mosaic.cloudlet.core.CallbackArguments;
import java.util.*;
//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.SLAPolicyCloudlet.SLAPolicyCloudletContext;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAPolicyCloudlet.AmqpConsumerCallback;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAPolicyCloudlet.AmqpPublisherCallback;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAPolicyCloudlet.SLAPolicyCloudletContext;
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.SerializedDataEncoder;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;
import java.io.UnsupportedEncodingException;
//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 DecisorBench {
public static boolean Evaluate(String WSAGen) {
//Get Target from WSAG
String WSAG=null;;
try {
WSAG = java.net.URLDecoder.decode(WSAGen, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String KPIs=WSAG.substring(WSAG.indexOf("<wsag:KPITarget>"),WSAG.indexOf("</wsag:KPITarget>"));
String Level=KPIs.substring(KPIs.indexOf("<wsag:CustomServiceLevel>",KPIs.indexOf("</wsag:CustomServiceLevel>")));
String value=Level.substring(Level.indexOf(">"),Level.indexOf("</"));
int RT=Integer.parseInt(value);
int maxTasks=10;
//Get Offering from WSAG
//String KPIs=WSAG.substring(WSAG.indexOf("<wsag:KPITarget>"),WSAG.indexOf("</wsag:KPITarget>"));
//((String Level=KPIs.substring(KPIs.indexOf("<wsag:CustomServiceLevel>",KPIs.indexOf("</wsag:CustomServiceLevel>")));
//String value=Level.substring(Level.indexOf(">"),Level.indexOf("</"));
//int RT=Integer.parseInt(value);
int Offering=12;
int benchs=10;
double charge=0.5;
//confrotnare bench, risk e proposal
int predict=maxTasks*benchs;
boolean result=(charge*predict-Offering>0)&&(1.1*RT>predict);
return result;
}
public static final class LifeCycleHandler extends
DefaultCloudletCallback<DecisorBenchContext > {
public CallbackCompletion<Void> initialize(final DecisorBenchContext context, final CloudletCallbackArguments<DecisorBenchContext> arguments) {
this.logger.info("DecisorCloudlet is being initialized.");
context.cloudlet = arguments.getCloudlet ();
//ICloudletController<DecisorFasulloState > cloudlet = arguments.getCloudlet();
//IConfiguration configuration = cloudlet.getConfiguration();
//IConfiguration configuration2 = cloudlet.getConfiguration();
final IConfiguration configuration = context.cloudlet.getConfiguration ();
final IConfiguration configuration2 = context.cloudlet.getConfiguration();
//DataEncoder<String> encoder = new PojoDataEncoder<String>(String.class);
final IConfiguration queueConfiguration = configuration.spliceConfiguration
(ConfigurationIdentifier.resolveAbsolute("queuecons"));
context.consumer = context.cloudlet.getConnectorFactory
(IAmqpQueueConsumerConnectorFactory.class).create
(queueConfiguration, String.class, JsonDataEncoder.create
(String.class), new AmqpConsumerCallback (), context);
//state.consumer = new AmqpQueueConsumer<DecisorFasullo.DecisorFasulloState,String>(queueConfiguration, cloudlet, String.class, encoder);
final IConfiguration queueConfigurationb = configuration2.spliceConfiguration
(ConfigurationIdentifier.resolveAbsolute("queueslastore"));
//state.publisher = new AmqpQueuePublisher<DecisorFasullo.DecisorFasulloState,String>(queueConfigurationb, cloudlet, String.class, encoder);
context.publisher = context.cloudlet.getConnectorFactory
(IAmqpQueuePublisherConnectorFactory.class).create
(queueConfigurationb, String.class, JsonDataEncoder.create
(String.class), new AmqpPublisherCallback (), context);
return CallbackCompletion.createAndChained (context.consumer.initialize (),context.publisher.initialize ());
}
public CallbackCompletion<Void> initializeSucceeded(final DecisorBenchContext context, final CloudletCallbackCompletionArguments<DecisorBenchContext> arguments) {
this.logger.info(
"DecisorSimulato initialized successfully.");
context.cloudlet = arguments.getCloudlet ();
/*ICloudletController<DecisorFasulloState > cloudlet = arguments
.getCloudlet();
cloudlet.initializeResource(state.consumer,
new AmqpConsumerCallback(), state);
cloudlet.initializeResource(state.publisher,
new AmqpPublisherCallback(), state);*/
context.object=new JsonObjectfromStringDecisor();
context.gson=new Gson();
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> destroy(final DecisorBenchContext context, final CloudletCallbackArguments<DecisorBenchContext> arguments) {
this.logger
.info("DecisorSimulato is being destroyed.");
return CallbackCompletion.createAndChained (context.consumer.destroy (),context.publisher.destroy ());
}
public CallbackCompletion<Void> destroySucceeded(final DecisorBenchContext context, final CloudletCallbackCompletionArguments<DecisorBenchContext> arguments) {
this.logger.info(
"DecisorSimulato was destroyed successfully.");
return ICallback.SUCCESS;
}
}
public static final class AmqpConsumerCallback extends
DefaultAmqpQueueConsumerConnectorCallback<DecisorBenchContext, String, Void>
{
public CallbackCompletion<Void> initializeSucceeded(final DecisorBenchContext context,
final CallbackArguments arguments) {
this.logger.info(
"DecisorSimulato Consumer initialized successfully.");
//state.consumer.register();
return ICallback.SUCCESS;
}
/*@Override
public void registerSucceeded(DecisorFasulloState state,
CallbackArguments<DecisorFasulloState > arguments) {
this.logger().info(
"DecisorSimulato Consumer registered successfully.");
}*/
/*@Override
public void unregisterSucceeded(DecisorFasulloState state,
CallbackArguments<DecisorFasulloState > arguments) {
this.logger().info(
"DecisorCloudlet unregistered successfully.");
ICloudletController<DecisorFasulloState > cloudlet = arguments
.getCloudlet();
cloudlet.destroyResource(state.consumer, this);
}*/
@Override
public CallbackCompletion<Void> destroySucceeded(final DecisorBenchContext context,
final CallbackArguments arguments) {
this.logger.info(
"DecisorSimulato Consumer was destroyed successfully.");
//state.consumer = null;
//arguments.getCloudlet().destroy();
return ICallback.SUCCESS;
}
/*@Override
public void acknowledgeSucceeded(DecisorFasulloState state,
CallbackArguments<DecisorFasulloState > arguments) {
this.logger().info(
"acknowledgeSucceeded");
//state.consumer.unregister();
}*/
public CallbackCompletion<Void> consume(final DecisorBenchContext context, final AmqpQueueConsumeCallbackArguments<String> arguments) {
//AmqpQueueConsumeMessage<String> message = arguments.getMessage();
//String data = message.getData();
final String data = arguments.getMessage ();
this.logger.info(
"DecisorSimulato Consumer received message from SLAstore "
+ data);
//System.out.println("\n\nMessage Ricevuto dal DecisorSimulato:"+data);
//System.out.println("\n\n");
context.object=context.gson.fromJson(data,JsonObjectfromStringDecisor.class);
if (((context.object.getType()).equals("Submit")) && (((context.object.getStato())).equals("OfferReceiver"))){
//QUi Evaluate
boolean accept=Evaluate(context.object.getWSAG());
if (accept) {
context.publisher.publish("{'type':'ChangeState','newstate':'Pending','id':'"+context.object.getId()+"','wsag':'"+context.object.getWSAG()+"'}", null);
} else {
context.publisher.publish("{'type':'ChangeState','newstate':'Refused','id':'"+context.object.getId()+"','wsag':'"+context.object.getWSAG()+"'}", null);
}
}
if (((context.object.getType()).equals("Terminate")) && (((context.object.getStato())).equals("Observed&Terminating"))){
context.publisher.publish("{'type':'ChangeState','newstate':'Terminated','id':'"+context.object.getId()+"','wsag':'"+context.object.getWSAG()+"'}", null);
}
if (((context.object.getType()).equals("Terminate")) && (((context.object.getStato())).equals("Pending&Terminating"))){
context.publisher.publish("{'type':'ChangeState','newstate':'Terminated','id':'"+context.object.getId()+"','wsag':'"+context.object.getWSAG()+"'}", null);
}
context.consumer.acknowledge (arguments.getToken ());
//message.acknowledge();
return ICallback.SUCCESS;
}
}
public static final class AmqpPublisherCallback
extends
DefaultAmqpPublisherConnectorCallback<DecisorBenchContext , String, Void> {
/*@Override
public void registerSucceeded(DecisorFasulloState state,
CallbackArguments<DecisorFasulloState > arguments) {
this.logger().info(
" DecisorSimulato Publisher registered successfully.");
}*/
/*@Override
public void unregisterSucceeded(DecisorFasulloState state,
CallbackArguments<DecisorFasulloState > arguments) {
this.logger().info(
"DecisorSimulato Publisher unregistered successfully.");
// if unregistered as publisher is successful then destroy resource
ICloudletController<DecisorFasulloState > cloudlet = arguments
.getCloudlet();
//cloudlet.destroyResource(state.publisher, this);
}*/
@Override
public CallbackCompletion<Void> initializeSucceeded(final DecisorBenchContext context,
final CallbackArguments arguments) {
// if resource initialized successfully then just register as a
// publisher
//state.publisher.register();
this.logger.info(
"DecisorSimulato Publisher initialized successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded(final DecisorBenchContext context,
final CallbackArguments arguments) {
this.logger.info(
"DecisorSimulato Publisher was destroyed successfully.");
// state.publisher = null;
// arguments.getCloudlet().destroy();
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> publishSucceeded(final DecisorBenchContext context,
final GenericCallbackCompletionArguments<Void> arguments) {
this.logger.info(
"DecisorSimulato Publisher publish successfully.");
//System.out.println("\n\nPubblicazione ChangeState dal DecisorSimulato OK\n\n");
//state.publisher.unregister();
return ICallback.SUCCESS;
}
}
public static final class DecisorBenchContext {
ICloudletController<DecisorBenchContext> cloudlet;
IAmqpQueueConsumerConnector<String, Void> consumer;
IAmqpQueuePublisherConnector<String, Void> publisher;
//AmqpQueuePublisher<DecisorFasulloState , String> publisher;
//AmqpQueueConsumer<DecisorFasulloState , String> consumer;
Gson gson;
JsonObjectfromStringDecisor object;
}
}