Package eu.mosaic_cloud.sla

Source Code of eu.mosaic_cloud.sla.DecisorKV$DecisorKVContext

//###
//# #%L
//# mosaic-java-sla
//# %%
//# 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.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.tools.DefaultAmqpPublisherConnectorCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultAmqpQueueConsumerConnectorCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultKvStoreConnectorCallback;
//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.NullDataEncoder;
import eu.mosaic_cloud.platform.core.utils.PlainTextDataEncoder;
import eu.mosaic_cloud.platform.core.utils.SerializedDataEncoder;
import eu.mosaic_cloud.sla.SLAPolicy.KeyValueCallbackpolicystore;
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 DecisorKV {

  public static boolean Evaluate(String WSAG,int benchs,double charge) {
   
    //Get Target 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 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;

    //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<DecisorKVContext > {

    public CallbackCompletion<Void> initialize(final DecisorKVContext context, final CloudletCallbackArguments<DecisorKVContext> 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);
     
      final IConfiguration kvConfiguration = configuration
          .spliceConfiguration(ConfigurationIdentifier
              .resolveAbsolute("benchKV"));
     
      context.benchKV = context.cloudlet.getConnectorFactory
      (IKvStoreConnectorFactory.class).create (kvConfiguration,
          String.class, PlainTextDataEncoder.DEFAULT_INSTANCE,
          new KeyValueCallback(), context);

      return CallbackCompletion.createAndChained (context.consumer.initialize (),context.publisher.initialize (),context.benchKV.initialize());

    }
   
   

    public CallbackCompletion<Void>  initializeSucceeded(final DecisorKVContext context, final CloudletCallbackCompletionArguments<DecisorKVContext> 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();
     
      context.benchKV.set(context.benchKey, context.benchs,null);
      /*
      try {
        context.benchKV.set(context.benchKey, context.benchs.getBytes("UTF8"),null);
      } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      */
      return ICallback.SUCCESS;
    }
    public CallbackCompletion<Void>  destroy(final DecisorKVContext context, final CloudletCallbackArguments<DecisorKVContext> arguments) {
      this.logger
      .info("DecisorSimulato is being destroyed.");
      return CallbackCompletion.createAndChained (context.consumer.destroy (),context.publisher.destroy ());
    }
    public CallbackCompletion<Void>  destroySucceeded(final DecisorKVContext context, final CloudletCallbackCompletionArguments<DecisorKVContext> arguments) {
      this.logger.info(
          "DecisorSimulato was destroyed successfully.");
     
      return ICallback.SUCCESS;

    }
  }
 
  public static final class AmqpConsumerCallback extends
  DefaultAmqpQueueConsumerConnectorCallback<DecisorKVContext, String, Void>
  {

    public CallbackCompletion<Void> initializeSucceeded(final DecisorKVContext  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 DecisorKVContext 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 DecisorKVContext context, final AmqpQueueConsumeCallbackArguments<String> arguments) {

      //AmqpQueueConsumeMessage<String> message = arguments.getMessage();

      //String data = message.getData();
     
      final String data = arguments.getMessage ();
//      this.logger.info(
//          "DecisorKV Consumer received message from SLAstore "
//              + data);
      this.logger.info(
          "DecisorKV Consumer received message from SLAstore ");
      //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"))){
     
        //System.out.println("\n\nEntrato nel Ciclo\n\n");
       
        //QUi Evaluate
        boolean accept=Evaluate(context.object.getWSAG(),Integer.parseInt(context.benchs),context.Charge);
        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();
     
      //Before ending update the benchs
      context.benchKV.get(context.benchKey);
     
      return ICallback.SUCCESS;
     
    }
  }


  public static final class AmqpPublisherCallback
  extends
  DefaultAmqpPublisherConnectorCallback<DecisorKVContext , 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 DecisorKVContext  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 DecisorKVContext  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 DecisorKVContext 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 KeyValueCallback
  extends DefaultKvStoreConnectorCallback<DecisorKVContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final DecisorKVContext context, final CallbackArguments arguments)
{
  this.logger.info ("KeyValueResult destroyed successfully.");
  return ICallback.SUCCESS;
}

 

@Override
public CallbackCompletion<Void> initializeSucceeded (final DecisorKVContext context, final CallbackArguments arguments)
{
  this.logger.info ("KeyValueResult initialized successfully");
    return ICallback.SUCCESS;
}

public CallbackCompletion<Void> setSucceeded(final DecisorKVContext context, final KvStoreCallbackCompletionArguments<String,Void> arguments) {
  this.logger.info("\n\nBenchKV SET new data\n\n");
//  String k=arguments.getKey();
//  this.logger.info("\n\nKEY:"+k);
//  this.logger.info("\n\n");
//  byte[] valueb=(byte[])arguments.getValue();
//  String value=new String(valueb);
//  this.logger.info("\n\nResultati in byte:"+valueb);
//  this.logger.info("\n\nResultati in String:"+value);
//  this.logger.info("\n\n");
  return ICallback.SUCCESS;
}

public CallbackCompletion<Void> deleteSucceeded (final DecisorKVContext context, final CallbackArguments arguments)
{
  this.logger.info ("KeyValueResult store delete successfully.");
  return ICallback.SUCCESS;
}

public CallbackCompletion<Void> getSucceeded(final DecisorKVContext context,final KvStoreCallbackCompletionArguments<String,Void> arguments) {
 
  this.logger.info("\n\nBenchKV GET Succeeded\n\n");
  //String key =(String) arguments.getKey();
  //byte[] value=(byte[])arguments.getValue();
  String value=(String)arguments.getValue();
  //String StringVal=context.benchs;
  /*
  try {
    StringVal = new String(value,"UTF-8");
  } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  */
  context.benchs=value;
 
 
return ICallback.SUCCESS;
}

public CallbackCompletion<Void> ungetSucceeded (final DecisorKVContext context, final CallbackArguments arguments)
{
  this.logger.info ("KeyValueResult ungetSucceeded.");
  return ICallback.SUCCESS;
}

}

 
  public static final class DecisorKVContext {
   
    ICloudletController<DecisorKVContext> cloudlet;
   
    IAmqpQueueConsumerConnector<String, Void> consumer;
    IAmqpQueuePublisherConnector<String, Void> publisher;
    IKvStoreConnector<String, Void> benchKV;
   
    //qui oggetto contenente i risultati del benchmark
    String benchKey="bench";
    String benchs="23";
    //String benchs="{1.1:'1',1.2:'1',1.3:'1',2.1:'1',2.2:'1',2.3:'1',3.1:'1',3.2:'1',3.3:'1',}";
   
    //Non ancora suppotato
    String riskKey="risk";
    String risk="0.1";
   
    double Charge=0.5;
    Gson gson;
    JsonObjectfromStringDecisor object;
  }
}
TOP

Related Classes of eu.mosaic_cloud.sla.DecisorKV$DecisorKVContext

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.