//###
//# #%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;
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.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.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.tools.DefaultKvStoreConnectorCallback;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAPolicyCloudlet.Evaluate;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAPolicyCloudlet.KeyValueCallbackinfopoliciesstore;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAPolicyCloudlet.KeyValueCallbackstatestore;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAPolicyCloudlet.SLAPolicyCloudletContext;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAStoreInt.AmqpConsumerCallback;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAStoreInt.AmqpPublisherCallback;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAStoreInt.KeyValueCallbackinfow;
//import eu.mosaic_cloud.examples.cloudlets.simple.SLAStoreInt.SLAStoreIntContext;
import eu.mosaic_cloud.platform.core.configuration.ConfigUtils;
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.PlainTextDataEncoder;
import eu.mosaic_cloud.platform.core.utils.SerializedDataEncoder;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;
import java.lang.Object;
import com.google.gson.Gson;
import org.apache.commons.jexl.*;
public class SLAPolicy {
public static final class LifeCycleHandler extends
DefaultCloudletCallback<SLAPolicyContext > {
public CallbackCompletion<Void> initialize(final SLAPolicyContext context, final CloudletCallbackArguments<SLAPolicyContext> arguments) {
this.logger.info(
"Cloudlet is being initialized.");
context.cloudlet = arguments.getCloudlet ();
final IConfiguration configuration = context.cloudlet.getConfiguration ();
final IConfiguration configuration2 = context.cloudlet.getConfiguration();
final IConfiguration configuration3 = context.cloudlet.getConfiguration();
final IConfiguration queueConfiguration = configuration
.spliceConfiguration(ConfigurationIdentifier
.resolveAbsolute("queueupdate"));
context.consumer = context.cloudlet.getConnectorFactory
(IAmqpQueueConsumerConnectorFactory.class).create
(queueConfiguration, String.class, JsonDataEncoder.create (String.class),
new AmqpConsumerCallback (), context);
final IConfiguration queueConfigurationc = configuration3
.spliceConfiguration(ConfigurationIdentifier
.resolveAbsolute("queueevaluate"));
context.consumerev = context.cloudlet.getConnectorFactory
(IAmqpQueueConsumerConnectorFactory.class).create
(queueConfigurationc, String.class, JsonDataEncoder.create
(String.class), new AmqpConsumerCallbackEvaluate (), context);
final IConfiguration queueConfigurationb = configuration2
.spliceConfiguration(ConfigurationIdentifier
.resolveAbsolute("queueaction"));
context.publisher = context.cloudlet.getConnectorFactory
(IAmqpQueuePublisherConnectorFactory.class).create
(queueConfigurationb, String.class, JsonDataEncoder.create
(String.class), new AmqpPublisherCallback (), context);
final IConfiguration kvConfiguration = configuration
.spliceConfiguration(ConfigurationIdentifier
.resolveAbsolute("policystore"));
context.kvStorea = context.cloudlet.getConnectorFactory
(IKvStoreConnectorFactory.class).create (kvConfiguration,
String.class, JsonDataEncoder.create (String.class),
new KeyValueCallbackpolicystore (), context);
final IConfiguration kvConfigurationb = configuration
.spliceConfiguration(ConfigurationIdentifier
.resolveAbsolute("infopoliciesstore"));
context.kvStoreb = context.cloudlet.getConnectorFactory
(IKvStoreConnectorFactory.class).create (kvConfigurationb,
InfoPolicies.class, JsonDataEncoder.create (InfoPolicies.class),
new KeyValueCallbackinfopoliciesstore (), context);
final IConfiguration kvConfigurations = configuration
.spliceConfiguration(ConfigurationIdentifier
.resolveAbsolute("statestore"));
context.kvStorestate = context.cloudlet.getConnectorFactory
(IKvStoreConnectorFactory.class).create
(kvConfigurations, String.class,
JsonDataEncoder.create (String.class),
new KeyValueCallbackstatestore (), context);
return CallbackCompletion.createAndChained (context.kvStorea.initialize (), context.kvStoreb.initialize(), context.kvStorestate.initialize(), context.consumer.initialize (), context.consumerev.initialize(), context.publisher.initialize());
}
public CallbackCompletion<Void> initializeSucceeded(final SLAPolicyContext context, final CloudletCallbackCompletionArguments<SLAPolicyContext> arguments) {
this.logger.info(
"Cloudlet initialized successfully.");
context.info=new InfoPolicies();
context.object=new JsonObjectfromString();
context.gson=new Gson();
context.kvStoreb.set(context.vectorListKey,context.info, null);
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> destroy(final SLAPolicyContext context, final CloudletCallbackArguments<SLAPolicyContext> arguments) {
this.logger
.info("Cloudlet is being destroyed.");
return CallbackCompletion.createAndChained (context.consumer.destroy (), context.kvStorea.destroy());
}
public CallbackCompletion<Void> destroySucceeded(final SLAPolicyContext context, final CloudletCallbackCompletionArguments<SLAPolicyContext> arguments) {
this.logger.info(
"Cloudlet was destroyed successfully.");
return ICallback.SUCCESS;
}
}
// KV_A
public static final class KeyValueCallbackpolicystore extends
DefaultKvStoreConnectorCallback<SLAPolicyContext, String, Void> {
public CallbackCompletion<Void> initializeSucceeded(final SLAPolicyContext context,
final CallbackArguments arguments) {
this.logger
.info("KeyValue policystore accessor initialized successfully");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<String, Void> arguments) {
//context.cloudlet = arguments.getCloudlet ();
//this.logger.info("KEYVALUE1 SetSucceeded");
this.logger.info("KVpolicystore: SET SUCCEDEED");
////System.out.println("\n\nSetSucceeded KV_A\n\n");
String k=arguments.getKey();
this.logger.info("KEY: "+k);
////System.out.println("\nKEY:"+k);
////System.out.println("\n");
String p=(String)arguments.getValue();
this.logger.info("VALORE_POLICY:"+p);
////System.out.println("\nVALORE_POLICY:"+p);
////System.out.println("\n");
context.object=context.gson.fromJson(p,JsonObjectfromString.class);
if(k!=context.vectorListKey)
{context.tmpSetPolicyKey.add(k);
this.logger.info("ntmpSetPolicyKey=="+context.tmpSetPolicyKey);
////System.out.println("\ntmpSetPolicyKey=="+context.tmpSetPolicyKey);
////System.out.println("\n");
context.loaded++;
this.logger.info("loaded=="+context.loaded);
////System.out.println("\nloaded=="+context.loaded);
////System.out.println("\n");
context.kvStoreb.get(context.vectorListKey, null);
}
else
{
this.logger.info("Kvstore Update");
////System.out.println("\n\nKvstore Update\n\n ");
}
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setFailed(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<String, Void> arguments) {
String data=arguments.getValue();
String v=arguments.getError().toString();
this.logger.info("SETFAILED "+ v);
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> deleteSucceeded(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<String, Void> arguments) {
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<String, Void> arguments) {
this.logger.info("KVpolicystore: GET SUCCEDEED");
this.logger.info("KEYVALUE1 GetSucceeded");
////System.out.println("\nGetSucceeded KV_A\n");
String k=arguments.getKey();
////System.out.println("\nKEY:"+k);
////System.out.println("\n");
String p=(String)arguments.getValue();
////System.out.println("\nPOLICY:"+p);
////System.out.println("\n");
context.object=context.gson.fromJson(p,JsonObjectfromString.class);;
String parameter1= context.appstate;
String parameter2= context.appaction;
String WSAG=context.appwsag;
String identificativo=context.appkey;
String expression=context.object.getRules(2);
////System.out.println("\nparameter1:"+parameter1);
////System.out.println("\nparameter2:"+parameter2);
////System.out.println("\nexpression:"+expression);
boolean b = context.evalu.evaluetion(parameter1, parameter2, expression);
////System.out.println("\nresult of Evaluation:"+b);
//System.out.println("\n");
if (b==true){
////System.out.println("\nPubblicazione Action e NewState:"+context.object.getRules(4)+","+context.object.getRules(3));
////System.out.println("\n");
context.publisher.publish("{'id':'"+identificativo+"','action':'"+context.object.getRules(4)+"','newstate':'"+context.object.getRules(3)+"','wsag':'"+WSAG+"'}", null);
}
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> ungetSucceeded(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<String, Void> arguments) {
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> destroySucceeded(final SLAPolicyContext context,
final CallbackArguments arguments) {
return ICallback.SUCCESS;
}
}
//KV_B
public static final class KeyValueCallbackinfopoliciesstore extends
DefaultKvStoreConnectorCallback<SLAPolicyContext, InfoPolicies, Void> {
public CallbackCompletion<Void> initializeSucceeded(final SLAPolicyContext context,
final CallbackArguments arguments) {
this.logger.info("KeyValue2 accessor initialized successfully");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<InfoPolicies, Void> arguments) {
this.logger.info("KEYVALUEinfopolicies SetSucceeded");
String k=arguments.getKey();
InfoPolicies value=(InfoPolicies)arguments.getValue();
////System.out.println("\n\nSetSucceeded KV_B\n\n");
this.logger.info("KeY and Vectorlistkey and state:"+k +value.getId() +value.getState());
////System.out.println("\n\nKeY and Vectorlistkey and state:"+k +value.getId()+value.getState());
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> ungetSucceeded(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<String, Void> arguments) {
////System.out.println("ungetSucceeded2");
String k=arguments.getKey();
////System.out.println("KEY2=="+k);
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<InfoPolicies, Void> arguments) {
this.logger.info("KEYVALUEinfopolicies GetSucceeded");
////System.out.println("\nGetSucceeded KV_B\n\n");
String k=arguments.getKey();
this.logger.info("KEYVALUEinfopolicies KEY "+k);
this.logger.info("Loaded "+context.loaded);
////System.out.println("\nKEY KV_B:"+k);
////System.out.println("\nloaded:"+context.loaded);
////System.out.println("\n");
InfoPolicies value2 = (InfoPolicies)arguments.getValue();
this.logger.info("Dimensione tmpDletePolicy: "+context.tmpDeletePolicy.size());
// System.out.println("\n\nDimensione tmpDletePolicy:"+state.tmpDeletePolicy.size());
// System.out.println("\n\n");
if(context.tmpDeletePolicy.size()==0) {
if((k==context.vectorListKey)) {
this.logger.info("tmpSetPolicyKey: "+context.tmpSetPolicyKey);
// System.out.println("\n\ntmpSetPolicyKey:"+state.tmpSetPolicyKey);
// System.out.println("\n\n");
value2.getId().removeAllElements();
value2.getId().addAll(0,context.tmpSetPolicyKey);
this.logger.info("Vettore Valoue2: "+value2.getId());
this.logger.info("Dimensione tmpUpdatePolicy: "+context.tmpUpdatePolicy.size());
////System.out.println("\nVettore Valoue2:"+value2.getId());
// System.out.println("\n\nDimensione tmpUpdatePolicy:"+state.tmpUpdatePolicy.size());
if(context.tmpUpdatePolicy.size()!=0) {
value2.getId().remove(context.tmpUpdatePolicy.get(0));
context.tmpUpdatePolicy.removeAllElements();
}
this.logger.info("Key and VectorListKey and Loaded: "+k +value2.getId() +context.loaded);
this.logger.info("STATE "+context.State);
// System.out.println("\n\nKey and VectorListKey and Loaded:"+k +value2.getId() +state.loaded);
// System.out.println("\n\n");
// System.out.println("\n\nSTATE:"+state.State);
if(context.State.equals("Completing")) {
this.logger.info("Loaded: "+context.loaded);
this.logger.info("tmpTotalNumber: "+context.tmpTotalNumber);
////System.out.println("\n\nLoaded:"+context.loaded);
////System.out.println("\n\ntmpTotalNumber:"+context.tmpTotalNumber);
if(context.loaded==context.tmpTotalNumber) {
value2.setState("Complete");
context.State="Complete";
this.logger.info("STATE Value2: "+value2.getState());
this.logger.info("STATE: "+context.State);
////System.out.println("\n\nSTATE Value2:"+value2.getState());
////System.out.println("\n\nSTATE:"+context.State);
////System.out.println("\n");
context.kvStoreb.set(context.vectorListKey, value2, null);
} else {
value2.setState("Completing");
this.logger.info("STATE Value2:"+value2.getState());
this.logger.info("STATE:"+context.State);
////System.out.println("\n\nSTATE Value2:"+value2.getState());
////System.out.println("\n\nSTATE:"+context.State);
////System.out.println("\n\n");
context.kvStoreb.set(context.vectorListKey, value2, null);
}
}
if(value2.getState().equals("Complete")&&context.set==2) {
this.logger.info("Evaluation active with state: "+value2.getState());
////System.out.println("\n\nEvaluation active with state:"+value2.getState());
for(int i=0;i<value2.getId().size();i++) {
String idpolicy= value2.getId().get(i);
this.logger.info("Idpolicy: "+idpolicy);
// System.out.println("\n\nIdpolicy:"+idpolicy);
// System.out.println("\n");
context.kvStorea.get(idpolicy, null);
}
} else {
this.logger.info("Evaluation not active");
////System.out.println("\n\nEvaluation not active\n\n");
}
}
} else {
this.logger.info("Remove key");
this.logger.info("DIMENSIONE tmpSetPolicyKey: "+context.tmpSetPolicyKey.size());
////System.out.println("\n\nRemove key\n\n");
////System.out.println("\n\n DIMENSIONE tmpSetPolicyKey:"+context.tmpSetPolicyKey.size());
context.tmpSetPolicyKey.removeElement(context.tmpDeletePolicy.get(0));
this.logger.info("DIMENSIONE tmpSetPolicyKey dopo Rimozione: "+context.tmpSetPolicyKey.size());
////System.out.println("\n\n DIMENSIONE tmpSetPolicyKey dopo Rimozione:"+context.tmpSetPolicyKey.size());
boolean t=value2.getId().remove(context.tmpDeletePolicy.get(0));
this.logger.info("Remove key da Valoue2: "+t);
////System.out.println("\n\nRemove key da Valoue2:"+t);
context.tmpDeletePolicy.clear();
this.logger.info("DIMENSIONE tmpDeletePolicyKey dopo Rimozione: "+context.tmpDeletePolicy.size());
////System.out.println("\n\n DIMENSIONE tmpDeletePolicyKey dopo Rimozione:"+context.tmpDeletePolicy.size());
context.kvStoreb.set(context.vectorListKey, value2, null);
}
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> destroySucceeded(final SLAPolicyContext context,
final CallbackArguments arguments) {
arguments.getCloudlet().destroy();
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> deleteSucceeded(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<String, Void> arguments) {
////System.out.println("Delete succeeded2 ");
return ICallback.SUCCESS;
}
}
// KV_STATE
public static final class KeyValueCallbackstatestore extends
DefaultKvStoreConnectorCallback<SLAPolicyContext, String, Void> {
public CallbackCompletion<Void> initializeSucceeded(final SLAPolicyContext context,
final CallbackArguments arguments) {
this.logger
.info("KeyValue accessor initialized successfully");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<String, Void> arguments) {
////System.out.println("\n\nSetSucceeded KV_STATE\n");
String k=arguments.getKey();
////System.out.println("\n\nKEY:"+k);
////System.out.println("\n");
String s=(String)arguments.getValue();
////System.out.println("\n\nSTATO:"+s);
////System.out.println("\n");
context.object=context.gson.fromJson(s,JsonObjectfromString.class);
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> deleteSucceeded(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<String, Void> arguments) {
////System.out.println("\n\nDelete succeeded\n\n ");
context.kvStoreb.get(context.vectorListKey, null);
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<String, Void> arguments) {
////System.out.println("\n\nGetSucceeded KV_STATE\n");
String k=arguments.getKey();
////System.out.println("\n\nKEY:"+k);
////System.out.println("\n");
String s=(String)arguments.getValue();
////System.out.println("\n\nSTATO:"+s);
////System.out.println("\n");
context.appstate=s;
context.appkey=k;
context.kvStoreb.get(context.vectorListKey, null);
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> ungetSucceeded(final SLAPolicyContext context,
final KvStoreCallbackCompletionArguments<String, Void> arguments) {
////System.out.println("ungetSucceeded");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> destroySucceeded(final SLAPolicyContext context,
final CallbackArguments arguments) {
arguments.getCloudlet().destroy();
return ICallback.SUCCESS;
}
}
// Consumer
public static final class AmqpConsumerCallback extends
DefaultAmqpQueueConsumerConnectorCallback<SLAPolicyContext, String, Void>
{
public CallbackCompletion<Void> initializeSucceeded(final SLAPolicyContext context,
final CallbackArguments arguments) {
this.logger.info(
"ConsumerCloudlet initialized successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded(final SLAPolicyContext context,
final CallbackArguments arguments) {
this.logger.info(
"ConsumerCloudlet was destroyed successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> consume(final SLAPolicyContext context, final AmqpQueueConsumeCallbackArguments<String> arguments) {
final String data = arguments.getMessage ();
this.logger.info(
"ConsumerCloudlet received logging message for user "+ data);
context.object=context.gson.fromJson(data,JsonObjectfromString.class);
if(((context.object.getType())).equals("New"))
{
this.logger.info("Message== "+context.object.getType());
// System.out.println("\n\nTipo Message:"+state.object.getType());
// System.out.println("\n\n");
// System.out.println("\n\nID Message:"+state.object.getId());
// System.out.println("\n\n");
context.kvStorea.set(context.object.getId(),data, null);
}
if(((context.object.getType())).equals("Update"))
{this.logger.info(
"Message=="+context.object.getType());
////System.out.println("\n\nTipo Message:"+context.object.getType());
////System.out.println("\n\n");
////System.out.println("\n\nID Message:"+context.object.getId());
////System.out.println("\n\n");
context.loaded--;
////System.out.println("\n\nLoaded:"+context.loaded);
////System.out.println("\n\n");
context.tmpUpdatePolicy.add(context.object.getId());
context.kvStorea.set(context.object.getId(),data, null);
}
if(((context.object.getType())).equals("Delete"))
{ this.logger.info(
"Message=="+context.object.getType());
////System.out.println("\n\nTipo Message:"+context.object.getType());
////System.out.println("\n\n");
////System.out.println("\n\nID Message:"+context.object.getId());
////System.out.println("\n\n");
context.loaded--;
////System.out.println("\n\nLoaded:"+context.loaded);
////System.out.println("\n\n");
context.tmpDeletePolicy.add(context.object.getId());
context.kvStorea.delete(context.object.getId(), null);
}
if(((context.object.getType())).equals("Complete"))
{ this.logger.info(
"Message=="+context.object.getType());
//System.out.println("\n\nTipo Message:"+state.object.getType());
//System.out.println("\n\n");
//System.out.println("\n\nID Message:"+state.object.getId());
//System.out.println("\n\n");
//System.out.println("\n\nRules N_1:"+state.object.getRules(0));
//System.out.println("\n\n");
//System.out.println("\n\nRules N_2:"+state.object.getRules(1));
//System.out.println("\n\n");
this.logger.info(
"Rules:"+context.object.getRules(0));
this.logger.info(
"Rules:"+context.object.getRules(1));
context.State="Completing";
this.logger.info("STATE COMPLETING");
////System.out.println("\nSTATE COMPLETING\n");
context.tmpTotalNumber=context.object.getNumber();
this.logger.info("tmpTotalNumer:"+context.tmpTotalNumber);
////System.out.println("\ntmpTotalNumer:"+context.tmpTotalNumber);
////System.out.println("\n");
context.kvStoreb.get(context.vectorListKey, null);
}
context.consumer.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
}
//CONSUMER EVALUATION
public static final class AmqpConsumerCallbackEvaluate extends
DefaultAmqpQueueConsumerConnectorCallback<SLAPolicyContext, String, Void>{
public CallbackCompletion<Void> initializeSucceeded(final SLAPolicyContext context,
final CallbackArguments arguments) {
this.logger.info(
"ConsumerCloudlet initialized successfully.");
//state.consumerev.register();
return ICallback.SUCCESS;
}
/*@Override
public void registerSucceeded(SLAPolicyCloudletState state,
CallbackArguments<SLAPolicyCloudletState > arguments) {
this.logger.info(
"ConsumerCloudlet registered successfully.");
}*/
/* @Override
public void unregisterSucceeded(SLAPolicyCloudletState state,
CallbackArguments<SLAPolicyCloudletState > arguments) {
this.logger.info(
"ConsumerCloudlet unregistered successfully.");
ICloudletController<SLAPolicyCloudletState > cloudlet = arguments
.getCloudlet();
cloudlet.destroyResource(state.consumerev, this);
}*/
@Override
public CallbackCompletion<Void> destroySucceeded(final SLAPolicyContext context,
final CallbackArguments arguments) {
this.logger.info(
"ConsumerCloudlet was destroyed successfully.");
//state.consumerev = null;
//arguments.getCloudlet().destroy();
return ICallback.SUCCESS;
}
/* @Override
public void acknowledgeSucceeded(SLAPolicyCloudletState state,
CallbackArguments<SLAPolicyCloudletState > arguments) {
this.logger.info(
"acknowledgeSucceeded");
//state.consumerev.unregister();
}*/
public CallbackCompletion<Void> consume(final SLAPolicyContext context, final AmqpQueueConsumeCallbackArguments<String> arguments) {
/* AmqpQueueConsumeMessage<String> message = arguments.getMessage();
String dataev = message.getData();
*/
final String dataev = arguments.getMessage ();
this.logger.info(
"ConsumerCloudlet Evaluate received logging message for user "
+ dataev);
////System.out.println("\n\nMessage Ricevuto dalla Cloudlet Evaluate:"+dataev);
////System.out.println("\n");
context.object=context.gson.fromJson(dataev,JsonObjectfromString.class);
if(((context.object.getType())).equals("Evaluate"))
{
this.logger.info("Tipo Message:"+context.object.getType());
this.logger.info("ID Message:"+context.object.getId());
// System.out.println("\n\nTipo Message:"+state.object.getType());
// System.out.println("\n\n");
// System.out.println("\n\nID Message:"+state.object.getId());
// System.out.println("\n\n");
if(context.State=="Complete")
{context.set=2;
this.logger.info("SET: "+context.set);
////System.out.println("\nSET:"+context.set);
////System.out.println("\n");
context.appaction=context.object.getAction();
context.appwsag=context.object.getWSAG();
context.kvStorestate.get(context.object.getId(),null);
}
}
//message.acknowledge();
context.consumerev.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
}
//PUBLISHER
public static final class AmqpPublisherCallback
extends
DefaultAmqpPublisherConnectorCallback<SLAPolicyContext, String, Void> {
/*@Override
public void registerSucceeded(SLAPolicyCloudletState state,
CallbackArguments<SLAPolicyCloudletState > arguments) {
this.logger.info(
"PublisherCloudlet registered successfully.");
}*/
/* @Override
public void unregisterSucceeded(SLAPolicyCloudletState state,
CallbackArguments<SLAPolicyCloudletState > arguments) {
this.logger.info(
"PublisherCloudlet unregistered successfully.");
// if unregistered as publisher is successful then destroy resource
ICloudletController<SLAPolicyCloudletState > cloudlet = arguments
.getCloudlet();
//cloudlet.destroyResource(state.publisher, this);
}*/
@Override
public CallbackCompletion<Void> initializeSucceeded(final SLAPolicyContext context,
final CallbackArguments arguments) {
// if resource initialized successfully then just register as a
// publisher
//state.publisher.register();
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded(final SLAPolicyContext context,
final CallbackArguments arguments) {
this.logger.info(
"PublisherCloudlet was destroyed successfully.");
/*state.publisher = null;
arguments.getCloudlet().destroy();*/
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> publishSucceeded(final SLAPolicyContext context,
final GenericCallbackCompletionArguments<Void> arguments) {
this.logger.info(
"PublisherCloudlet publish successfully.");
////System.out.println("\n\nPublisher OK\n\n");
//state.publisher.unregister();
return ICallback.SUCCESS;
}
}
public static final class Evaluate{
public boolean evaluetion(String x,String y,String z)
{
JexlContext jc = JexlHelper.createContext();
jc.getVars().put("state", x);//associa la variabile y1 con la y
jc.getVars().put("request", y);
Expression e = null;
try {
e = ExpressionFactory.createExpression(z);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Object ris = null;
try {
ris = e.evaluate(jc);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
boolean r = (Boolean) ris;
return r;
}
}
public static final class SLAPolicyContext {
ICloudletController<SLAPolicyContext> cloudlet;
IKvStoreConnector<String, Void> kvStorea; // contiene le policy
IKvStoreConnector<InfoPolicies, Void> kvStoreb; // contiene la lista degli id delle policy memorizzate
IKvStoreConnector<String, Void> kvStorestate; // contine lo stato del FSM
IAmqpQueueConsumerConnector<String, Void> consumer; //Riceve le policy che vengono caricate inizialmente
IAmqpQueueConsumerConnector<String, Void> consumerev; // riceve i segnali di evaluate
IAmqpQueuePublisherConnector<String, Void> publisher; // pubblica l'azione e lo stato delle policy che danno esito positivo.
int set=0;
InfoPolicies info;
int tmpTotalNumber=0;
String vectorListKey="0";
String appaction;
String appstate;
String appkey;
String appwsag;
Vector <String> tmpSetPolicyKey=new Vector <String>();
Vector<String> tmpDeletePolicy=new Vector<String>();
Vector<String> tmpUpdatePolicy=new Vector<String>();
int loaded=0;
String State="UnComplete";
Evaluate evalu=new Evaluate();
Gson gson;
JsonObjectfromString object;
}
}