<commit_before>package org.fiteagle.core.reservation.dm;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.inject.Inject;
import javax.jms.JMSContext;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Topic;

import org.fiteagle.api.core.IMessageBus;
import org.fiteagle.api.core.MessageBusOntologyModel;
import org.fiteagle.api.core.MessageFilters;
import org.fiteagle.api.core.MessageUtil;
import org.fiteagle.core.tripletStoreAccessor.QueryExecuter;
import org.fiteagle.core.tripletStoreAccessor.TripletStoreAccessor;
import org.fiteagle.core.tripletStoreAccessor.TripletStoreAccessor.ResourceRepositoryException;

import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDF;

@MessageDriven(name = "ReservationMDBListener", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = IMessageBus.TOPIC_CORE),
    @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = MessageFilters.FILTER_RESERVATION),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
public class ReservationMDBListener implements MessageListener {
  
  private static Logger LOGGER = Logger.getLogger(ReservationMDBListener.class.toString());
  private static String OMN = "http://open-multinet.info/ontology/omn#";
  
  @Inject
  private JMSContext context;
  @javax.annotation.Resource(mappedName = IMessageBus.TOPIC_CORE_NAME)
  private Topic topic;
  
  public void onMessage(final Message message) {
    String messageType = MessageUtil.getMessageType(message);
    String serialization = MessageUtil.getMessageSerialization(message);
    String rdfString = MessageUtil.getStringBody(message);
    LOGGER.log(Level.INFO, "Received a " + messageType + " message");
    
    if (messageType != null && rdfString != null) {      
      Model messageModel = MessageUtil.parseSerializedModel(rdfString, serialization);
      
      if (messageType.equals(IMessageBus.TYPE_CREATE)) {        
        handleCreate(messageModel, serialization, MessageUtil.getJMSCorrelationID(message));
      }
      if (messageType.equals(IMessageBus.TYPE_GET)) {
        handleGet(messageModel, serialization, MessageUtil.getJMSCorrelationID(message));
      }
    }
  }

  private void handleGet(Model messageModel, String serialization, String jmsCorrelationID) {

  }

  private void handleCreate(Model requestModel, String serialization, String requestID) {
    LOGGER.log(Level.INFO, "handling reservation request ...");
    Message responseMessage = null;
    Model resultModel = ModelFactory.createDefaultModel();
    final Map<String, String> reservedSlivers = new HashMap<>();
    this.handleReservation(requestModel, reservedSlivers);
    this.reserve(requestModel);
    
    for (Map.Entry<String, String> sliver : reservedSlivers.entrySet()) {
      Resource sliversResource = resultModel.createResource(sliver.getKey());
      sliversResource.addProperty(resultModel.createProperty(OMN + "reservation_status"), sliver.getValue());
    }
    String serializedResponse = MessageUtil.serializeModel(resultModel, serialization);
    responseMessage = MessageUtil.createRDFMessage(serializedResponse, IMessageBus.TYPE_INFORM, null, serialization, requestID, context);
    LOGGER.log(Level.INFO, " a reply is sent to SFA ...");
    context.createProducer().send(topic, responseMessage);
  }
  
  private void handleReservation(Model requestModel, Map<String, String> reservedSlivers) {
    
    LOGGER.log(Level.INFO, "handle reservation ...");
    StmtIterator iterator = requestModel.listStatements(null, RDF.type, MessageBusOntologyModel.classReservation);
    while (iterator.hasNext()) {
      Resource sliver = iterator.next().getSubject();
      Statement st = sliver.getProperty(MessageBusOntologyModel.reserveInstanceFrom);
      String componentManagerId = st.getObject().toString();
      LOGGER.log(Level.INFO, "componentManagerId " + componentManagerId);
      if (this.isReservationAvailable(componentManagerId)) {
        LOGGER.log(Level.INFO, "reservation is available");
        LOGGER.log(Level.INFO, "sliver is " + sliver.getURI());
        reservedSlivers.put(sliver.getURI(), "geni_allocated");
      } else {
        requestModel.remove(sliver, null, null);
        reservedSlivers.put(sliver.getURI(), "geni_not_allocated");
      }
    }<commit_msg>changed allocation of names to slivers<commit_after>package org.fiteagle.core.reservation.dm;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.inject.Inject;
import javax.jms.JMSContext;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Topic;

import org.fiteagle.api.core.IMessageBus;
import org.fiteagle.api.core.MessageBusOntologyModel;
import org.fiteagle.api.core.MessageFilters;
import org.fiteagle.api.core.MessageUtil;
import org.fiteagle.core.tripletStoreAccessor.QueryExecuter;
import org.fiteagle.core.tripletStoreAccessor.TripletStoreAccessor;
import org.fiteagle.core.tripletStoreAccessor.TripletStoreAccessor.ResourceRepositoryException;

import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDF;

@MessageDriven(name = "ReservationMDBListener", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = IMessageBus.TOPIC_CORE),
    @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = MessageFilters.FILTER_RESERVATION),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
public class ReservationMDBListener implements MessageListener {
  
  private static Logger LOGGER = Logger.getLogger(ReservationMDBListener.class.toString());
  private static String OMN = "http://open-multinet.info/ontology/omn#";
  
  @Inject
  private JMSContext context;
  @javax.annotation.Resource(mappedName = IMessageBus.TOPIC_CORE_NAME)
  private Topic topic;
  
  public void onMessage(final Message message) {
    String messageType = MessageUtil.getMessageType(message);
    String serialization = MessageUtil.getMessageSerialization(message);
    String rdfString = MessageUtil.getStringBody(message);
    LOGGER.log(Level.INFO, "Received a " + messageType + " message");
    
    if (messageType != null && rdfString != null) {      
      Model messageModel = MessageUtil.parseSerializedModel(rdfString, serialization);
      
      if (messageType.equals(IMessageBus.TYPE_CREATE)) {        
        try {
			handleCreate(messageModel, serialization, MessageUtil.getJMSCorrelationID(message));
		} catch (ResourceRepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      if (messageType.equals(IMessageBus.TYPE_GET)) {
        handleGet(messageModel, serialization, MessageUtil.getJMSCorrelationID(message));
      }
    }
  }

  private void handleGet(Model messageModel, String serialization, String jmsCorrelationID) {

  }

  private void handleCreate(Model requestModel, String serialization, String requestID) throws ResourceRepositoryException {
    LOGGER.log(Level.INFO, "handling reservation request ...");
    Message responseMessage = null;
    Model resultModel = ModelFactory.createDefaultModel();
    final Map<String, String> reservedSlivers = new HashMap<>();
    this.handleReservation(requestModel, reservedSlivers);
    this.reserve(requestModel);
    
    for (Map.Entry<String, String> sliver : reservedSlivers.entrySet()) {
      Resource sliversResource = resultModel.createResource(sliver.getKey());
      sliversResource.addProperty(resultModel.createProperty(OMN + "reservation_status"), sliver.getValue());
    }
    String serializedResponse = MessageUtil.serializeModel(resultModel, serialization);
    responseMessage = MessageUtil.createRDFMessage(serializedResponse, IMessageBus.TYPE_INFORM, null, serialization, requestID, context);
    LOGGER.log(Level.INFO, " a reply is sent to SFA ...");
    context.createProducer().send(topic, responseMessage);
  }
  
  private void handleReservation(Model requestModel, Map<String, String> reservedSlivers) throws ResourceRepositoryException {
    
    LOGGER.log(Level.INFO, "handle reservation ...");
    StmtIterator iterator = requestModel.listStatements(null, RDF.type, MessageBusOntologyModel.classReservation);
    while (iterator.hasNext()) {
      Resource sliver = iterator.next().getSubject();
      Statement st = sliver.getProperty(MessageBusOntologyModel.reserveInstanceFrom);
      String componentManagerId = st.getObject().toString();
      LOGGER.log(Level.INFO, "componentManagerId " + componentManagerId);
      if (this.isReservationAvailable(componentManagerId)) {
        LOGGER.log(Level.INFO, "reservation is available");
        LOGGER.log(Level.INFO, "sliver is " + sliver.getURI());
        String sliverURN = setSliverURN(sliver.getURI());
        reservedSlivers.put(sliverURN, "geni_allocated");
        addSliverURNtoModel(requestModel);
      } else {
        requestModel.remove(sliver, null, null);
        reservedSlivers.put(sliver.getURI(), "geni_not_allocated");
      }
    }
  }
  
  private void addSliverURNtoModel(Model requestModel){
	  
  }
  
  private String setSliverURN(String sliver) throws ResourceRepositoryException{
	  String sliverURN = "";
	  Random random = new Random();
	  while(true){
		  sliverURN = sliver + random.nextInt();
		  if(!sliverURNallocated(sliverURN)){
			  break;
		  }
	  }
	  return sliverURN;
  }
  
  private boolean sliverURNallocated(String sliverURN) throws ResourceRepositoryException{
	String query = "PREFIX omn: <http://open-multinet.info/ontology/omn#> "
	  		+ "ASK WHERE { <" + sliverURN + "> a omn:Reservation }";
	return QueryExecuter.executeSparqlAskQuery(query);
{'fraction_non_alphanumeric': 0.1076652471764488, 'fraction_numerical': 0.0, 'mean_word_length': 4.842617631151974, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1797557', 'n_tokens_mistral': 3222, 'n_tokens_neox': 3206, 'n_words': 562}